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.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 20 import org.eclipse.jface.text.BadLocationException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IRegion; 23 import org.eclipse.jface.text.contentassist.ICompletionProposal; 24 25 import org.eclipse.ui.texteditor.spelling.SpellingProblem; 26 27 import org.eclipse.jdt.internal.corext.util.Messages; 28 29 import org.eclipse.jdt.ui.PreferenceConstants; 30 import org.eclipse.jdt.ui.text.java.IInvocationContext; 31 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 32 33 import org.eclipse.jdt.internal.ui.JavaUIMessages; 34 import org.eclipse.jdt.internal.ui.text.correction.AssistContext; 35 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants; 36 import org.eclipse.jdt.internal.ui.text.javadoc.IJavaDocTagConstants; 37 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; 38 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 39 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellEvent; 40 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal; 41 42 48 public class JavaSpellingProblem extends SpellingProblem { 49 50 51 private ISpellEvent fSpellEvent; 52 53 58 private IDocument fDocument; 59 60 66 public JavaSpellingProblem(ISpellEvent spellEvent, IDocument document) { 67 Assert.isLegal(document != null); 68 Assert.isLegal(spellEvent != null); 69 fSpellEvent= spellEvent; 70 fDocument= document; 71 } 72 73 76 public int getOffset() { 77 return fSpellEvent.getBegin(); 78 } 79 80 83 public int getLength() { 84 return fSpellEvent.getEnd() - fSpellEvent.getBegin() + 1; 85 } 86 87 90 public String getMessage() { 91 if (isSentenceStart() && isDictionaryMatch()) 92 return Messages.format(JavaUIMessages.Spelling_error_case_label, new String [] { fSpellEvent.getWord() }); 93 94 return Messages.format(JavaUIMessages.Spelling_error_label, new String [] { fSpellEvent.getWord() }); 95 } 96 97 100 public ICompletionProposal[] getProposals() { 101 String [] arguments= getArguments(); 102 if (arguments == null) 103 return new ICompletionProposal[0]; 104 105 if (arguments[0].indexOf('&') != -1 && isIgnoringAmpersand()) 106 return new ICompletionProposal[0]; 108 final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD); 109 int size= 0; 110 List proposals= null; 111 112 RankedWordProposal proposal= null; 113 IJavaCompletionProposal[] result= null; 114 int index= 0; 115 116 boolean fixed= false; 117 boolean match= false; 118 boolean sentence= false; 119 120 final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); 121 final ISpellChecker checker= engine.getSpellChecker(); 122 123 if (checker != null) { 124 125 IInvocationContext context= new AssistContext(null, getOffset(), 126 getLength()); 127 128 fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX 130 || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX; 131 132 if ((sentence && match) && !fixed) 133 result= new IJavaCompletionProposal[] { new ChangeCaseProposal( 134 arguments, getOffset(), getLength(), context, engine 135 .getLocale()) }; 136 else { 137 138 proposals= new ArrayList (checker.getProposals(arguments[0], 139 sentence)); 140 size= proposals.size(); 141 142 if (threshold > 0 && size > threshold) { 143 144 Collections.sort(proposals); 145 proposals= proposals 146 .subList(size - threshold - 1, size - 1); 147 size= proposals.size(); 148 } 149 150 boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false; 151 result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)]; 152 153 for (index= 0; index < size; index++) { 154 155 proposal= (RankedWordProposal) proposals.get(index); 156 result[index]= new WordCorrectionProposal(proposal 157 .getText(), arguments, getOffset(), getLength(), 158 context, proposal.getRank()); 159 } 160 161 if (extendable) 162 result[index++]= new AddWordProposal(arguments[0], context); 163 164 result[index++]= new WordIgnoreProposal(arguments[0], context); 165 result[index++]= new DisableSpellCheckingProposal(context); 166 } 167 } 168 169 return result; 170 } 171 172 private boolean isIgnoringAmpersand() { 173 return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.SPELLING_IGNORE_AMPERSAND_IN_PROPERTIES); 174 } 175 176 public String [] getArguments() { 177 178 String prefix= ""; String postfix= ""; String word; 181 try { 182 word= fDocument.get(getOffset(), getLength()); 183 } catch (BadLocationException e) { 184 return null; 185 } 186 187 try { 188 189 IRegion line= fDocument.getLineInformationOfOffset(getOffset()); 190 int end= getOffset() + getLength(); 191 prefix= fDocument.get(line.getOffset(), getOffset() 192 - line.getOffset()); 193 postfix= fDocument.get(end + 1, line.getOffset() + line.getLength() 194 - end); 195 196 } catch (BadLocationException exception) { 197 } 199 return new String [] { 200 word, 201 prefix, 202 postfix, 203 isSentenceStart() ? Boolean.toString(true) : Boolean 204 .toString(false), 205 isDictionaryMatch() ? Boolean.toString(true) : Boolean 206 .toString(false) }; 207 } 208 209 217 public boolean isDictionaryMatch() { 218 return fSpellEvent.isMatch(); 219 } 220 221 229 public boolean isSentenceStart() { 230 return fSpellEvent.isStart(); 231 } 232 233 } 234 | Popular Tags |