From 83589e4a3f4b04cff1472704a35dca80db5069ce Mon Sep 17 00:00:00 2001 From: anna Date: Wed, 26 Oct 2011 17:47:50 +0200 Subject: [PATCH] spell checker: do not extract misspelled word from problem description in onTheFly mode - store exact string instead --- .../inspections/SpellCheckingInspection.java | 26 +++++-------------- .../quickfixes/AcceptWordAsCorrect.java | 18 +++++-------- .../spellchecker/quickfixes/ChangeTo.java | 6 ++--- .../spellchecker/quickfixes/RenameTo.java | 4 +-- .../quickfixes/ShowSuggestions.java | 23 +++++----------- .../quickfixes/SpellCheckerQuickFix.java | 3 --- 6 files changed, 25 insertions(+), 55 deletions(-) diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java b/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java index f8a1c3955928..3f6f1a232b56 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java @@ -23,9 +23,6 @@ import com.intellij.codeInspection.ProblemsHolder; import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.lang.*; import com.intellij.lang.refactoring.NamesValidator; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.extensions.Extensions; -import com.intellij.openapi.fileTypes.PlainTextLanguage; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElementVisitor; @@ -41,7 +38,6 @@ import com.intellij.spellchecker.tokenizer.TokenConsumer; import com.intellij.spellchecker.tokenizer.Tokenizer; import com.intellij.spellchecker.util.SpellCheckerBundle; import com.intellij.util.Consumer; -import com.intellij.util.containers.hash.HashMap; import gnu.trove.THashSet; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; @@ -49,7 +45,6 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.*; -import java.util.Map; import java.util.Set; @@ -147,11 +142,11 @@ public class SpellCheckingInspection extends LocalInspectionTool { holder.registerProblem(problemDescriptor); } - private static void addRegularDescriptor(PsiElement element, int offset, @NotNull TextRange textRange, @NotNull ProblemsHolder holder, - boolean useRename) { + private static void addRegularDescriptor(PsiElement element, int offset, @NotNull TextRange textRange, @NotNull ProblemsHolder holder, + boolean useRename, String wordWithTypo) { SpellCheckerQuickFix[] fixes = new SpellCheckerQuickFix[]{ - (useRename ? new RenameTo() : new ChangeTo()), - new AcceptWordAsCorrect() + (useRename ? new RenameTo(wordWithTypo) : new ChangeTo(wordWithTypo)), + new AcceptWordAsCorrect(wordWithTypo) }; final ProblemDescriptor problemDescriptor = createProblemDescriptor(element, offset, textRange, holder, fixes, true); @@ -165,15 +160,8 @@ public class SpellCheckingInspection extends LocalInspectionTool { final TextRange highlightRange = TextRange.from(offset + textRange.getStartOffset(), textRange.getLength()); assert highlightRange.getStartOffset()>=0; - final ProblemDescriptor problemDescriptor = holder.getManager() - .createProblemDescriptor(element, highlightRange, description, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, holder.isOnTheFly(), - fixes); - if(onTheFly) { - for (SpellCheckerQuickFix fix : fixes) { - fix.setDescriptor(problemDescriptor); - } - } - return problemDescriptor; + return holder.getManager() + .createProblemDescriptor(element, highlightRange, description, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, holder.isOnTheFly(), fixes); } @SuppressWarnings({"PublicField"}) @@ -254,7 +242,7 @@ public class SpellCheckingInspection extends LocalInspectionTool { addBatchDescriptor(myElement, myOffset, textRange, myHolder); } else { - addRegularDescriptor(myElement, myOffset, textRange, myHolder, myUseRename); + addRegularDescriptor(myElement, myOffset, textRange, myHolder, myUseRename, word); } } } diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java index 8d95d92f22cc..95a56da175ca 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java @@ -16,7 +16,6 @@ package com.intellij.spellchecker.quickfixes; import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.codeInspection.ui.ProblemDescriptionNode; import com.intellij.openapi.actionSystem.Anchor; import com.intellij.openapi.project.Project; import com.intellij.spellchecker.SpellCheckerManager; @@ -27,16 +26,18 @@ import javax.swing.*; public class AcceptWordAsCorrect implements SpellCheckerQuickFix { - private ProblemDescriptor myProblemDescriptor; + private String myWord; + + public AcceptWordAsCorrect(String word) { + myWord = word; + } public AcceptWordAsCorrect() { } @NotNull public String getName() { - return myProblemDescriptor!=null ? SpellCheckerBundle.message("add.0.to.dictionary", ProblemDescriptionNode.extractHighlightedText(myProblemDescriptor, myProblemDescriptor.getPsiElement())) - : SpellCheckerBundle.message("add.to.dictionary") - ; + return myWord != null ? SpellCheckerBundle.message("add.0.to.dictionary", myWord) : SpellCheckerBundle.message("add.to.dictionary"); } @NotNull @@ -51,15 +52,10 @@ public class AcceptWordAsCorrect implements SpellCheckerQuickFix { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { SpellCheckerManager spellCheckerManager = SpellCheckerManager.getInstance(project); - final String w = ProblemDescriptionNode.extractHighlightedText(descriptor, descriptor.getPsiElement()); - spellCheckerManager.acceptWordAsCorrect(w, project); + spellCheckerManager.acceptWordAsCorrect(myWord, project); } public Icon getIcon(int flags) { return new ImageIcon(ShowSuggestions.class.getResource("spellcheck.png")); } - - public void setDescriptor(ProblemDescriptor problemDescriptor) { - myProblemDescriptor = problemDescriptor; - } } diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java index d72237ae785b..666e3a1c8751 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java @@ -35,8 +35,8 @@ import java.util.List; public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix { - public ChangeTo() { - super(); + public ChangeTo(String wordWithTypo) { + super(wordWithTypo); } @@ -75,7 +75,7 @@ public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix { } List lookupItems = new ArrayList(); - for (String variant : getSuggestions()) { + for (String variant : getSuggestions(project)) { lookupItems.add(LookupElementBuilder.create(variant)); } LookupElement[] items = new LookupElement[lookupItems.size()]; diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java index aaacb26ac540..e888ad5d421b 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java @@ -40,8 +40,8 @@ import javax.swing.*; public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix { - public RenameTo() { - super(); + public RenameTo(String wordWithTypo) { + super(wordWithTypo); } @NotNull diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java index 95d52335f30e..204502aa8a7c 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java @@ -16,11 +16,8 @@ package com.intellij.spellchecker.quickfixes; import com.intellij.codeInspection.LocalQuickFix; -import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.codeInspection.ui.ProblemDescriptionNode; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Iconable; -import com.intellij.openapi.util.TextRange; import com.intellij.spellchecker.SpellCheckerManager; import org.jetbrains.annotations.NotNull; @@ -32,31 +29,23 @@ public abstract class ShowSuggestions implements LocalQuickFix, Iconable { private List suggestions; private boolean processed; - protected ProblemDescriptor myProblemDescriptor; + private final String myWordWithTypo; - public ShowSuggestions() { + public ShowSuggestions(String wordWithTypo) { + myWordWithTypo = wordWithTypo; } @NotNull - public List getSuggestions(){ + public List getSuggestions(Project project){ if (!processed){ - calculateSuggestions(); - processed=true; + suggestions = SpellCheckerManager.getInstance(project).getSuggestions(myWordWithTypo); + processed = true; } return suggestions; } - private void calculateSuggestions(){ - SpellCheckerManager manager = SpellCheckerManager.getInstance(myProblemDescriptor.getPsiElement().getProject()); - suggestions = manager.getSuggestions(ProblemDescriptionNode.extractHighlightedText(myProblemDescriptor, myProblemDescriptor.getPsiElement())); - } - public Icon getIcon(int flags) { return new ImageIcon(ShowSuggestions.class.getResource("spellcheck.png")); } - - public void setDescriptor(ProblemDescriptor problemDescriptor) { - myProblemDescriptor = problemDescriptor; - } } diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java index c252da3b6f57..d9b4d9fc2536 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java @@ -16,7 +16,6 @@ package com.intellij.spellchecker.quickfixes; import com.intellij.codeInspection.LocalQuickFix; -import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.actionSystem.Anchor; import com.intellij.openapi.util.Iconable; import org.jetbrains.annotations.NotNull; @@ -25,6 +24,4 @@ public interface SpellCheckerQuickFix extends LocalQuickFix, Iconable { @NotNull Anchor getPopupActionAnchor(); - void setDescriptor(ProblemDescriptor problemDescriptor); - }