1 11 package org.eclipse.ui.texteditor.spelling; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.jface.text.Position; 19 import org.eclipse.jface.text.contentassist.ICompletionProposal; 20 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; 21 import org.eclipse.jface.text.quickassist.IQuickAssistProcessor; 22 import org.eclipse.jface.text.source.Annotation; 23 import org.eclipse.jface.text.source.IAnnotationModel; 24 import org.eclipse.jface.text.source.ISourceViewer; 25 import org.eclipse.jface.text.source.TextInvocationContext; 26 27 import org.eclipse.ui.internal.texteditor.spelling.NoCompletionsProposal; 28 29 30 36 public final class SpellingCorrectionProcessor implements IQuickAssistProcessor { 37 38 39 private static final ICompletionProposal[] fgNoSuggestionsProposal= new ICompletionProposal[] { new NoCompletionsProposal() }; 40 41 42 45 public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) { 46 ISourceViewer viewer= quickAssistContext.getSourceViewer(); 47 int documentOffset= quickAssistContext.getOffset(); 48 49 int length= viewer != null ? viewer.getSelectedRange().y : 0; 50 TextInvocationContext context= new TextInvocationContext(viewer, documentOffset, length); 51 52 53 IAnnotationModel model= viewer.getAnnotationModel(); 54 if (model == null) 55 return fgNoSuggestionsProposal; 56 57 List proposals= computeProposals(context, model); 58 if (proposals.isEmpty()) 59 return fgNoSuggestionsProposal; 60 61 return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); 62 } 63 64 private boolean isAtPosition(int offset, Position pos) { 65 return (pos != null) && (offset >= pos.getOffset() && offset <= (pos.getOffset() + pos.getLength())); 66 } 67 68 private List computeProposals(IQuickAssistInvocationContext context, IAnnotationModel model) { 69 int offset= context.getOffset(); 70 ArrayList annotationList= new ArrayList (); 71 Iterator iter= model.getAnnotationIterator(); 72 while (iter.hasNext()) { 73 Annotation annotation= (Annotation)iter.next(); 74 if (canFix(annotation)) { 75 Position pos= model.getPosition(annotation); 76 if (isAtPosition(offset, pos)) { 77 collectSpellingProblems(annotation, pos, annotationList); 78 } 79 } 80 } 81 SpellingProblem[] spellingProblems= (SpellingProblem[]) annotationList.toArray(new SpellingProblem[annotationList.size()]); 82 return computeProposals(context, spellingProblems); 83 } 84 85 private void collectSpellingProblems(Annotation annotation, Position pos, List problems) { 86 if (annotation instanceof SpellingAnnotation) 87 problems.add(((SpellingAnnotation)annotation).getSpellingProblem()); 88 } 89 90 private List computeProposals(IQuickAssistInvocationContext context, SpellingProblem[] spellingProblems) { 91 List proposals= new ArrayList (); 92 for (int i= 0; i < spellingProblems.length; i++) 93 proposals.addAll(Arrays.asList(spellingProblems[i].getProposals())); 94 95 return proposals; 96 } 97 98 101 public String getErrorMessage() { 102 return null; 103 } 104 105 108 public boolean canFix(Annotation annotation) { 109 return annotation instanceof SpellingAnnotation && !annotation.isMarkedDeleted(); 110 } 111 112 115 public boolean canAssist(IQuickAssistInvocationContext invocationContext) { 116 return false; 117 } 118 119 } 120 | Popular Tags |