1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IFile; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IRegion; 21 import org.eclipse.jface.text.ITextViewer; 22 import org.eclipse.jface.text.contentassist.ICompletionProposal; 23 import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 24 import org.eclipse.jface.text.contentassist.IContextInformation; 25 import org.eclipse.jface.text.contentassist.IContextInformationValidator; 26 27 import org.eclipse.ui.IEditorInput; 28 import org.eclipse.ui.part.FileEditorInput; 29 30 import org.eclipse.jdt.core.ICompilationUnit; 31 32 import org.eclipse.jdt.ui.JavaUI; 33 import org.eclipse.jdt.ui.PreferenceConstants; 34 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 35 import org.eclipse.jdt.ui.text.java.IJavadocCompletionProcessor; 36 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; 37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 38 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal; 39 40 import org.eclipse.jdt.internal.ui.JavaPluginImages; 41 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; 42 43 48 public class WordCompletionProcessor implements IContentAssistProcessor, IJavadocCompletionProcessor { 49 50 51 protected static final int PREFIX_RANK_SHIFT= 4096; 52 53 54 private String fError= null; 55 56 59 public final IJavaCompletionProposal[] computeCompletionProposals(final ICompilationUnit cu, final int offset, final int length, final int flags) { 60 if (contributes()) { 61 IEditorInput editorInput= new FileEditorInput((IFile) cu.getResource()); 62 IDocument document= JavaUI.getDocumentProvider().getDocument(editorInput); 63 return computeCompletionProposals(document, offset); 64 } 65 return null; 66 } 67 68 71 public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offset) { 72 if (contributes()) 73 return computeCompletionProposals(viewer.getDocument(), offset); 74 return null; 75 } 76 77 private boolean contributes() { 78 return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_ENABLE_CONTENTASSIST); 79 } 80 81 private IJavaCompletionProposal[] computeCompletionProposals(final IDocument document, final int offset) { 82 IJavaCompletionProposal[] result= null; 83 try { 84 85 final IRegion region= document.getLineInformationOfOffset(offset); 86 final String content= document.get(region.getOffset(), region.getLength()); 87 88 int index= offset - region.getOffset() - 1; 89 while (index >= 0 && Character.isLetter(content.charAt(index))) 90 index--; 91 92 final int start= region.getOffset() + index + 1; 93 final String candidate= content.substring(index + 1, offset - region.getOffset()); 94 95 if (candidate.length() > 0) { 96 97 final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); 98 final ISpellChecker checker= engine.createSpellChecker(engine.getLocale(), PreferenceConstants.getPreferenceStore()); 99 100 if (checker != null) { 101 102 final ArrayList proposals= new ArrayList (checker.getProposals(candidate, Character.isUpperCase(candidate.charAt(0)))); 103 result= new IJavaCompletionProposal[proposals.size()]; 104 105 RankedWordProposal word= null; 106 for (int proposal= 0; proposal < result.length; proposal++) { 107 108 word= (RankedWordProposal)proposals.get(proposal); 109 if (word.getText().startsWith(candidate)) 110 word.setRank(word.getRank() + PREFIX_RANK_SHIFT); 111 } 112 113 115 String text= null; 116 for (int proposal= 0; proposal < result.length; proposal++) { 117 118 word= (RankedWordProposal)proposals.get(proposal); 119 text= word.getText(); 120 121 result[proposal]= new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, word.getRank()); 123 } 124 } 125 } 126 } catch (BadLocationException exception) { 127 fError= (result == null) ? exception.getLocalizedMessage() : null; 128 } 129 return result; 130 } 131 132 135 public final IContextInformation[] computeContextInformation(final ICompilationUnit cu, final int offset) { 136 return null; 137 } 138 139 142 public final IContextInformation[] computeContextInformation(final ITextViewer viewer, final int offset) { 143 return null; 144 } 145 146 149 public final char[] getCompletionProposalAutoActivationCharacters() { 150 return null; 151 } 152 153 156 public final char[] getContextInformationAutoActivationCharacters() { 157 return null; 158 } 159 160 163 public final IContextInformationValidator getContextInformationValidator() { 164 return null; 165 } 166 167 170 public final String getErrorMessage() { 171 return fError; 172 } 173 } 174 | Popular Tags |