From c40b950b1e2b51876910823608588a15869d550e Mon Sep 17 00:00:00 2001 From: Alexey Gopachenko Date: Mon, 9 Nov 2009 18:45:34 +0300 Subject: [PATCH] Spellchecker to avoid duplicating info in fixes to save memory --- .../inspections/SpellCheckingInspection.java | 17 ++++++++++------- .../spellchecker/quickfixes/AcceptWordAsCorrect.java | 13 +++---------- .../com/intellij/spellchecker/quickfixes/ChangeTo.java | 6 ++++-- .../com/intellij/spellchecker/quickfixes/RenameTo.java | 4 ++-- .../spellchecker/quickfixes/ShowSuggestions.java | 18 +++++++++--------- .../spellchecker/quickfixes/SpellCheckerQuickFix.java | 3 +++ 6 files changed, 31 insertions(+), 30 deletions(-) diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java b/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java index f1b01613c9..5133c872ad 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/inspections/SpellCheckingInspection.java @@ -30,6 +30,7 @@ import com.intellij.spellchecker.SpellCheckerManager; import com.intellij.spellchecker.quickfixes.AcceptWordAsCorrect; import com.intellij.spellchecker.quickfixes.ChangeTo; import com.intellij.spellchecker.quickfixes.RenameTo; +import com.intellij.spellchecker.quickfixes.SpellCheckerQuickFix; import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy; import com.intellij.spellchecker.tokenizer.Token; import com.intellij.spellchecker.tokenizer.Tokenizer; @@ -176,13 +177,13 @@ public class SpellCheckingInspection extends LocalInspectionTool { } if (manager.hasProblem(word)) { - List fixes = new ArrayList(); + List fixes = new ArrayList(); if (isOnTheFly) { if (!token.isUseRename()) { - fixes.add(new ChangeTo(textRange, word, token.getElement().getProject())); + fixes.add(new ChangeTo()); } else { - fixes.add(new RenameTo(textRange, word, token.getElement().getProject())); + fixes.add(new RenameTo()); } } @@ -191,15 +192,13 @@ public class SpellCheckingInspection extends LocalInspectionTool { final ProblemDescriptor problemDescriptor = createProblemDescriptor(token, holder, textRange, fixes); holder.registerProblem(problemDescriptor); - - acceptWordAsCorrect.setDescriptor(problemDescriptor); } } private static ProblemDescriptor createProblemDescriptor(Token token, ProblemsHolder holder, - TextRange textRange, Collection fixes) { + TextRange textRange, Collection fixes) { //TODO: these descriptions eat LOTS of HEAP on batch run - need either to make them constant or evaluate template dynamically // ( add something like #text substitution) final String defaultDescription = SpellCheckerBundle.message("word.0.1.is.misspelled", token.getElement().getLanguage()); @@ -208,8 +207,12 @@ public class SpellCheckingInspection extends LocalInspectionTool { final TextRange highlightRange = TextRange.from(token.getOffset() + textRange.getStartOffset(), textRange.getLength()); final LocalQuickFix[] quickFixes = fixes.size() > 0 ? fixes.toArray(new LocalQuickFix[fixes.size()]) : null; - return holder.getManager() + final ProblemDescriptor problemDescriptor = holder.getManager() .createProblemDescriptor(token.getElement(), highlightRange, description, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, quickFixes); + for (SpellCheckerQuickFix fix : fixes) { + fix.setDescriptor(problemDescriptor); + } + return problemDescriptor; } @Nullable diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java index 7f29a089b9..72abfe39b5 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/AcceptWordAsCorrect.java @@ -17,6 +17,7 @@ package com.intellij.spellchecker.quickfixes; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ex.ProblemDescriptorImpl; +import com.intellij.codeInspection.ui.ProblemDescriptionNode; import com.intellij.openapi.actionSystem.Anchor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; @@ -35,7 +36,7 @@ public class AcceptWordAsCorrect implements SpellCheckerQuickFix { @NotNull public String getName() { - return SpellCheckerBundle.message("add.0.to.dictionary", getWord()); + return SpellCheckerBundle.message("add.0.to.dictionary", ProblemDescriptionNode.extractHighlightedText(myProblemDescriptor, myProblemDescriptor.getPsiElement())); } @NotNull @@ -50,18 +51,10 @@ public class AcceptWordAsCorrect implements SpellCheckerQuickFix { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { SpellCheckerManager spellCheckerManager = SpellCheckerManager.getInstance(project); - final String w = getWord(); + final String w = ProblemDescriptionNode.extractHighlightedText(descriptor, descriptor.getPsiElement()); spellCheckerManager.acceptWordAsCorrect(w); } - private String getWord() { - final ProblemDescriptorImpl d = (ProblemDescriptorImpl)myProblemDescriptor; - final TextRange r = d.getTextRange(); - final String t = d.getPsiElement().getContainingFile().getText(); - final String w = t.substring(r.getStartOffset(), r.getEndOffset()); - return w; - } - public Icon getIcon(int flags) { return new ImageIcon(ShowSuggestions.class.getResource("spellcheck.png")); } diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java index b970c217e3..40d1c55dcc 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ChangeTo.java @@ -19,6 +19,7 @@ import com.intellij.codeInsight.lookup.LookupElement; import com.intellij.codeInsight.lookup.LookupElementBuilder; import com.intellij.codeInsight.lookup.LookupManager; import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.codeInspection.ex.ProblemDescriptorImpl; import com.intellij.ide.DataManager; import com.intellij.openapi.actionSystem.Anchor; import com.intellij.openapi.actionSystem.PlatformDataKeys; @@ -34,8 +35,8 @@ import java.util.List; public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix { - public ChangeTo(@NotNull TextRange textRange, @NotNull String word, @NotNull Project project) { - super(textRange, word, project); + public ChangeTo() { + super(); } @@ -63,6 +64,7 @@ public class ChangeTo extends ShowSuggestions implements SpellCheckerQuickFix { } int psiElementOffset = descriptor.getPsiElement().getTextRange().getStartOffset(); + TextRange textRange = ((ProblemDescriptorImpl)descriptor).getTextRange(); editor.offsetToLogicalPosition(psiElementOffset + textRange.getStartOffset()); editor.getSelectionModel().setSelection(psiElementOffset + textRange.getStartOffset(), psiElementOffset + textRange.getEndOffset()); diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java index d9385e0fbc..bf4b4b5846 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/RenameTo.java @@ -33,8 +33,8 @@ import javax.swing.*; public class RenameTo extends ShowSuggestions implements SpellCheckerQuickFix { - public RenameTo(@NotNull TextRange textRange, @NotNull String word, @NotNull Project project) { - super(textRange, word, project); + public RenameTo() { + super(); } @NotNull diff --git a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java index 95ec166bf7..95d52335f3 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/ShowSuggestions.java @@ -16,6 +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; @@ -28,17 +30,12 @@ import java.util.List; public abstract class ShowSuggestions implements LocalQuickFix, Iconable { - protected TextRange textRange; - protected String word; - protected Project project; private List suggestions; private boolean processed; + protected ProblemDescriptor myProblemDescriptor; - public ShowSuggestions(@NotNull TextRange textRange, @NotNull String word, @NotNull Project project) { - this.textRange = textRange; - this.word = word; - this.project = project; + public ShowSuggestions() { } @NotNull @@ -51,12 +48,15 @@ public abstract class ShowSuggestions implements LocalQuickFix, Iconable { } private void calculateSuggestions(){ - SpellCheckerManager manager = SpellCheckerManager.getInstance(project); - suggestions = manager.getSuggestions(word); + 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 d9b4d9fc25..c252da3b6f 100644 --- a/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java +++ b/plugins/spellchecker/src/com/intellij/spellchecker/quickfixes/SpellCheckerQuickFix.java @@ -16,6 +16,7 @@ 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; @@ -24,4 +25,6 @@ public interface SpellCheckerQuickFix extends LocalQuickFix, Iconable { @NotNull Anchor getPopupActionAnchor(); + void setDescriptor(ProblemDescriptor problemDescriptor); + } -- 2.11.4.GIT