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.CoreException; 19 20 import org.eclipse.jdt.core.ICompilationUnit; 21 22 import org.eclipse.jdt.ui.PreferenceConstants; 23 import org.eclipse.jdt.ui.text.java.IInvocationContext; 24 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 25 import org.eclipse.jdt.ui.text.java.IProblemLocation; 26 import org.eclipse.jdt.ui.text.java.IQuickFixProcessor; 27 28 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants; 29 import org.eclipse.jdt.internal.ui.text.javadoc.IJavaDocTagConstants; 30 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellCheckEngine; 31 import org.eclipse.jdt.internal.ui.text.spelling.engine.ISpellChecker; 32 import org.eclipse.jdt.internal.ui.text.spelling.engine.RankedWordProposal; 33 34 39 public class WordQuickFixProcessor implements IQuickFixProcessor { 40 41 44 public WordQuickFixProcessor() { 45 } 47 48 51 public IJavaCompletionProposal[] getCorrections(IInvocationContext context, IProblemLocation[] locations) throws CoreException { 52 53 final int threshold= PreferenceConstants.getPreferenceStore().getInt(PreferenceConstants.SPELLING_PROPOSAL_THRESHOLD); 54 55 int size= 0; 56 List proposals= null; 57 String [] arguments= null; 58 59 IProblemLocation location= null; 60 RankedWordProposal proposal= null; 61 IJavaCompletionProposal[] result= null; 62 63 boolean fixed= false; 64 boolean match= false; 65 boolean sentence= false; 66 67 final ISpellCheckEngine engine= SpellCheckEngine.getInstance(); 68 final ISpellChecker checker= engine.getSpellChecker(); 69 70 if (checker != null) { 71 72 for (int index= 0; index < locations.length; index++) { 73 74 location= locations[index]; 75 if (location.getProblemId() == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID) { 76 77 arguments= location.getProblemArguments(); 78 if (arguments != null && arguments.length > 4) { 79 80 sentence= Boolean.valueOf(arguments[3]).booleanValue(); 81 match= Boolean.valueOf(arguments[4]).booleanValue(); 82 fixed= arguments[0].charAt(0) == IHtmlTagConstants.HTML_TAG_PREFIX || arguments[0].charAt(0) == IJavaDocTagConstants.JAVADOC_TAG_PREFIX; 83 84 if ((sentence && match) && !fixed) 85 result= new IJavaCompletionProposal[] { new ChangeCaseProposal(arguments, location.getOffset(), location.getLength(), context, engine.getLocale())}; 86 else { 87 88 proposals= new ArrayList (checker.getProposals(arguments[0], sentence)); 89 size= proposals.size(); 90 91 if (threshold > 0 && size > threshold) { 92 93 Collections.sort(proposals); 94 proposals= proposals.subList(size - threshold - 1, size - 1); 95 size= proposals.size(); 96 } 97 98 boolean extendable= !fixed ? (checker.acceptsWords() || AddWordProposal.canAskToConfigure()) : false; 99 result= new IJavaCompletionProposal[size + (extendable ? 3 : 2)]; 100 101 for (index= 0; index < size; index++) { 102 103 proposal= (RankedWordProposal)proposals.get(index); 104 result[index]= new WordCorrectionProposal(proposal.getText(), arguments, location.getOffset(), location.getLength(), context, proposal.getRank()); 105 } 106 107 if (extendable) 108 result[index++]= new AddWordProposal(arguments[0], context); 109 110 result[index++]= new WordIgnoreProposal(arguments[0], context); 111 result[index++]= new DisableSpellCheckingProposal(context); 112 } 113 break; 114 } 115 } 116 } 117 } 118 return result; 119 } 120 121 124 public final boolean hasCorrections(ICompilationUnit unit, int id) { 125 return id == JavaSpellingReconcileStrategy.SPELLING_PROBLEM_ID; 126 } 127 } 128 | Popular Tags |