1 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.swt.graphics.Image; 19 import org.eclipse.swt.graphics.Point; 20 21 import org.eclipse.jface.text.BadLocationException; 22 import org.eclipse.jface.text.DocumentEvent; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.jface.text.IInformationControlCreator; 25 import org.eclipse.jface.text.ITextViewer; 26 import org.eclipse.jface.text.contentassist.ICompletionProposal; 27 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension; 28 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2; 29 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3; 30 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4; 31 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 32 import org.eclipse.jface.text.contentassist.IContextInformation; 33 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 34 35 import org.eclipse.ui.IEditorInput; 36 import org.eclipse.ui.IEditorPart; 37 import org.eclipse.ui.IEditorReference; 38 import org.eclipse.ui.IWorkbenchWindow; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.texteditor.HippieCompletionEngine; 41 42 50 public final class HippieProposalProcessor implements IContentAssistProcessor { 51 52 private static final ICompletionProposal[] NO_PROPOSALS= new ICompletionProposal[0]; 53 private static final IContextInformation[] NO_CONTEXTS= new IContextInformation[0]; 54 55 private static final class Proposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3, ICompletionProposalExtension4 { 56 57 private final String fString; 58 private final String fPrefix; 59 private final int fOffset; 60 61 public Proposal(String string, String prefix, int offset) { 62 fString= string; 63 fPrefix= prefix; 64 fOffset= offset; 65 } 66 67 public void apply(IDocument document) { 68 apply(null, '\0', 0, fOffset); 69 } 70 71 public Point getSelection(IDocument document) { 72 return new Point(fOffset + fString.length(), 0); 73 } 74 75 public String getAdditionalProposalInfo() { 76 return null; 77 } 78 79 public String getDisplayString() { 80 return fPrefix + fString; 81 } 82 83 public Image getImage() { 84 return null; 85 } 86 87 public IContextInformation getContextInformation() { 88 return null; 89 } 90 91 public void apply(IDocument document, char trigger, int offset) { 92 try { 93 String replacement= fString.substring(offset - fOffset); 94 document.replace(offset, 0, replacement); 95 } catch (BadLocationException x) { 96 x.printStackTrace(); 98 } 99 } 100 101 public boolean isValidFor(IDocument document, int offset) { 102 return validate(document, offset, null); 103 } 104 105 public char[] getTriggerCharacters() { 106 return null; 107 } 108 109 public int getContextInformationPosition() { 110 return 0; 111 } 112 113 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 114 apply(viewer.getDocument(), trigger, offset); 115 } 116 117 public void selected(ITextViewer viewer, boolean smartToggle) { 118 } 119 120 public void unselected(ITextViewer viewer) { 121 } 122 123 public boolean validate(IDocument document, int offset, DocumentEvent event) { 124 try { 125 int prefixStart= fOffset - fPrefix.length(); 126 return offset >= fOffset && offset < fOffset + fString.length() && document.get(prefixStart, offset - (prefixStart)).equals((fPrefix + fString).substring(0, offset - prefixStart)); 127 } catch (BadLocationException x) { 128 return false; 129 } 130 } 131 132 public IInformationControlCreator getInformationControlCreator() { 133 return null; 134 } 135 136 public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) { 137 return fPrefix + fString; 138 } 139 140 public int getPrefixCompletionStart(IDocument document, int completionOffset) { 141 return fOffset - fPrefix.length(); 142 } 143 144 public boolean isAutoInsertable() { 145 return true; 146 } 147 148 } 149 150 private final HippieCompletionEngine fEngine= new HippieCompletionEngine(); 151 152 155 public HippieProposalProcessor() { 156 } 157 158 161 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { 162 try { 163 String prefix= getPrefix(viewer, offset); 164 if (prefix == null || prefix.length() == 0) 165 return NO_PROPOSALS; 166 167 List suggestions= getSuggestions(viewer, offset, prefix); 168 169 List result= new ArrayList (); 170 for (Iterator it= suggestions.iterator(); it.hasNext();) { 171 String string= (String ) it.next(); 172 if (string.length() > 0) 173 result.add(createProposal(string, prefix, offset)); 174 } 175 176 return (ICompletionProposal[]) result.toArray(new ICompletionProposal[result.size()]); 177 178 } catch (BadLocationException x) { 179 return NO_PROPOSALS; 181 } 182 } 183 184 private String getPrefix(ITextViewer viewer, int offset) throws BadLocationException { 185 IDocument doc= viewer.getDocument(); 186 if (doc == null || offset > doc.getLength()) 187 return null; 188 189 int length= 0; 190 while (--offset >= 0 && Character.isJavaIdentifierPart(doc.getChar(offset))) 191 length++; 192 193 return doc.get(offset + 1, length); 194 } 195 196 private ICompletionProposal createProposal(String string, String prefix, int offset) { 197 return new Proposal(string, prefix, offset); 198 } 199 200 203 public IContextInformation[] computeContextInformation(ITextViewer viewer, int offset) { 204 return NO_CONTEXTS; 206 } 207 208 211 public char[] getCompletionProposalAutoActivationCharacters() { 212 return null; 213 } 214 215 218 public char[] getContextInformationAutoActivationCharacters() { 219 return null; 220 } 221 222 225 public IContextInformationValidator getContextInformationValidator() { 226 return null; 227 } 228 229 239 private ArrayList createSuggestionsFromOpenDocument(ITextViewer viewer, int offset, String prefix) throws BadLocationException { 240 IDocument document= viewer.getDocument(); 241 ArrayList completions= new ArrayList (); 242 completions.addAll(fEngine.getCompletionsBackwards(document, prefix, offset)); 243 completions.addAll(fEngine.getCompletionsForward(document, prefix, offset - prefix.length(), true)); 244 245 return completions; 246 } 247 248 260 private List getSuggestions(ITextViewer viewer, int offset, String prefix) throws BadLocationException { 261 262 ArrayList suggestions= createSuggestionsFromOpenDocument(viewer, offset, prefix); 263 IDocument currentDocument= viewer.getDocument(); 264 265 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 266 IEditorReference editorReferences[]= window.getActivePage().getEditorReferences(); 267 268 for (int i= 0; i < editorReferences.length; i++) { 269 IEditorPart editor= editorReferences[i].getEditor(false); if (editor instanceof ITextEditor) { 271 ITextEditor textEditor= (ITextEditor) editor; 272 IEditorInput input= textEditor.getEditorInput(); 273 IDocument doc= textEditor.getDocumentProvider().getDocument(input); 274 if (!currentDocument.equals(doc)) 275 suggestions.addAll(fEngine.getCompletionsForward(doc, prefix, 0, false)); 276 } 277 } 278 suggestions.add(""); 281 List uniqueSuggestions= fEngine.makeUnique(suggestions); 282 283 return uniqueSuggestions; 284 } 285 286 289 public String getErrorMessage() { 290 return null; } 292 } 293 | Popular Tags |