1 11 package org.eclipse.jdt.ui.text.java; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.text.BadLocationException; 16 import org.eclipse.jface.text.IDocument; 17 import org.eclipse.jface.text.ITextViewer; 18 19 33 public class ContentAssistInvocationContext { 34 35 36 private final ITextViewer fViewer; 37 private final IDocument fDocument; 38 private final int fOffset; 39 40 41 private CharSequence fPrefix; 42 43 49 public ContentAssistInvocationContext(ITextViewer viewer) { 50 this(viewer, viewer.getSelectedRange().x); 51 } 52 53 59 public ContentAssistInvocationContext(ITextViewer viewer, int offset) { 60 Assert.isNotNull(viewer); 61 fViewer= viewer; 62 fDocument= null; 63 fOffset= offset; 64 } 65 66 69 protected ContentAssistInvocationContext() { 70 fDocument= null; 71 fViewer= null; 72 fOffset= -1; 73 } 74 75 81 public ContentAssistInvocationContext(IDocument document, int offset) { 82 Assert.isNotNull(document); 83 Assert.isTrue(offset >= 0); 84 fViewer= null; 85 fDocument= document; 86 fOffset= offset; 87 } 88 89 94 public final int getInvocationOffset() { 95 return fOffset; 96 } 97 98 103 public final ITextViewer getViewer() { 104 return fViewer; 105 } 106 107 112 public IDocument getDocument() { 113 if (fDocument == null) { 114 if (fViewer == null) 115 return null; 116 return fViewer.getDocument(); 117 } 118 return fDocument; 119 } 120 121 129 public CharSequence computeIdentifierPrefix() throws BadLocationException { 130 if (fPrefix == null) { 131 IDocument document= getDocument(); 132 if (document == null) 133 return null; 134 int end= getInvocationOffset(); 135 int start= end; 136 while (--start >= 0) { 137 if (!Character.isJavaIdentifierPart(document.getChar(start))) 138 break; 139 } 140 start++; 141 fPrefix= document.get(start, end - start); 142 } 143 144 return fPrefix; 145 } 146 147 179 public boolean equals(Object obj) { 180 if (obj == null) 181 return false; 182 if (!getClass().equals(obj.getClass())) 183 return false; 184 ContentAssistInvocationContext other= (ContentAssistInvocationContext) obj; 185 return (fViewer == null && other.fViewer == null || fViewer != null && fViewer.equals(other.fViewer)) && fOffset == other.fOffset && (fDocument == null && other.fDocument == null || fDocument != null && fDocument.equals(other.fDocument)); 186 } 187 188 191 public int hashCode() { 192 return 23459213 << 5 | (fViewer == null ? 0 : fViewer.hashCode() << 3) | fOffset; 193 } 194 } 195 | Popular Tags |