1 11 12 package org.eclipse.jdt.internal.ui.text.spelling; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 21 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.DocumentEvent; 24 import org.eclipse.jface.text.IDocument; 25 import org.eclipse.jface.text.IRegion; 26 27 import org.eclipse.jdt.ui.PreferenceConstants; 28 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer; 29 import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext; 30 31 import org.eclipse.jdt.internal.ui.JavaPlugin; 32 import org.eclipse.jdt.internal.ui.JavaPluginImages; 33 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal; 34 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; 35 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 36 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal; 37 38 50 public final class WordCompletionProposalComputer implements IJavaCompletionProposalComputer { 51 52 53 private static final int PREFIX_RANK_SHIFT= 500; 54 55 58 public List computeCompletionProposals(ContentAssistInvocationContext context, IProgressMonitor monitor) { 59 if (contributes()) { 60 try { 61 IDocument document= context.getDocument(); 62 final int offset= context.getInvocationOffset(); 63 64 final IRegion region= document.getLineInformationOfOffset(offset); 65 final String content= document.get(region.getOffset(), region.getLength()); 66 67 int index= offset - region.getOffset() - 1; 68 while (index >= 0 && Character.isLetter(content.charAt(index))) 69 index--; 70 71 final int start= region.getOffset() + index + 1; 72 final String candidate= content.substring(index + 1, offset - region.getOffset()); 73 74 if (candidate.length() > 0) { 75 76 final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); 77 final ISpellChecker checker= engine.getSpellChecker(); 78 79 if (checker != null) { 80 81 final List proposals= new ArrayList (checker.getProposals(candidate, Character.isUpperCase(candidate.charAt(0)))); 82 final List result= new ArrayList (proposals.size()); 83 84 for (Iterator it= proposals.iterator(); it.hasNext();) { 85 RankedWordProposal word= (RankedWordProposal) it.next(); 86 String text= word.getText(); 87 if (text.startsWith(candidate)) 88 word.setRank(word.getRank() + PREFIX_RANK_SHIFT); 89 90 result.add(new JavaCompletionProposal(text, start, candidate.length(), JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME), text, word.getRank()) { 91 94 public boolean validate(IDocument doc, int validate_offset, DocumentEvent event) { 95 return offset == validate_offset; 96 } 97 }); 98 } 99 100 return result; 101 } 102 } 103 } catch (BadLocationException exception) { 104 JavaPlugin.log(exception); 106 } 107 } 108 return Collections.EMPTY_LIST; 109 } 110 111 private boolean contributes() { 112 return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_ENABLE_CONTENTASSIST); 113 } 114 115 118 public List computeContextInformation(ContentAssistInvocationContext context, IProgressMonitor monitor) { 119 return Collections.EMPTY_LIST; 120 } 121 122 125 public String getErrorMessage() { 126 return null; } 128 129 132 public void sessionStarted() { 133 } 134 135 138 public void sessionEnded() { 139 } 140 } 141 | Popular Tags |