Spellchecker: WI-39336 possibility to choose a dict in SaveTo quick-fix

This commit is contained in:
Olga Strizhenko
2017-12-22 15:16:09 +03:00
parent 5a6b9d6630
commit f6cbf42b30
7 changed files with 102 additions and 13 deletions

View File

@@ -15,8 +15,6 @@ import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.psi.PsiManager;
import com.intellij.psi.impl.PsiModificationTrackerImpl;
import com.intellij.spellchecker.dictionary.*;
import com.intellij.spellchecker.dictionary.Dictionary;
import com.intellij.spellchecker.engine.SpellCheckerEngine;
@@ -206,13 +204,15 @@ public class SpellCheckerManager implements Disposable {
}
public void acceptWordAsCorrect(@NotNull String word, Project project) {
acceptWordAsCorrect(word, project, PROJECT); // TODO: or default
}
public void acceptWordAsCorrect(@NotNull String word, @NotNull Project project, @NotNull String dictionaryName) {
final String transformed = spellChecker.getTransformation().transform(word);
final EditableDictionary dictionary = PROJECT.equals(dictionaryName) ? myProjectDictionary: myAppDictionary;
if (transformed != null) {
myProjectDictionary.addToDictionary(transformed);
myAppDictionary.addToDictionary(transformed);
final PsiModificationTrackerImpl modificationTracker =
(PsiModificationTrackerImpl)PsiManager.getInstance(project).getModificationTracker();
modificationTracker.incCounter();
dictionary.addToDictionary(word);
restartInspections();
}
}

View File

@@ -0,0 +1,78 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.spellchecker.quickfixes;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemDescriptorUtil;
import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.Anchor;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.AsyncResult;
import com.intellij.spellchecker.SpellCheckerManager;
import com.intellij.spellchecker.util.SpellCheckerBundle;
import com.intellij.ui.components.JBList;
import com.intellij.util.Consumer;
import icons.SpellcheckerIcons;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Arrays;
import java.util.List;
import static com.intellij.spellchecker.SpellCheckerManager.APP;
import static com.intellij.spellchecker.SpellCheckerManager.PROJECT;
public class SaveTo implements SpellCheckerQuickFix {
private String myWord;
public static final String FIX_NAME = SpellCheckerBundle.message("add.to");
public SaveTo(String word) {
myWord = word;
}
@NotNull
public String getName() {
return myWord != null ? SpellCheckerBundle.message("add.0.to", myWord) : SpellCheckerBundle.message("add.to");
}
@NotNull
public String getFamilyName() {
return SpellCheckerBundle.message("add.to");
}
@NotNull
public Anchor getPopupActionAnchor() {
return Anchor.LAST;
}
@Override
public boolean startInWriteAction() {
return false;
}
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final AsyncResult<DataContext> asyncResult = DataManager.getInstance().getDataContextFromFocus();
asyncResult.doWhenDone((Consumer<DataContext>)context -> {
final SpellCheckerManager spellCheckerManager = SpellCheckerManager.getInstance(project);
final List<String> dictionaryList = Arrays.asList(PROJECT, APP);
if (myWord == null) {
myWord = ProblemDescriptorUtil.extractHighlightedText(descriptor, descriptor.getPsiElement());
}
final JBList<String> dictList = new JBList<>(dictionaryList);
JBPopupFactory.getInstance()
.createListPopupBuilder(dictList)
.setTitle(SpellCheckerBundle.message("select.dictionary.title"))
.setItemChoosenCallback(() -> spellCheckerManager.acceptWordAsCorrect(myWord, project, dictList.getSelectedValue()))
.createPopup()
.showInBestPositionFor(context);
});
}
public Icon getIcon(int flags) {
return SpellcheckerIcons.Spellcheck;
}
}

View File

@@ -28,11 +28,7 @@ import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlToken;
import com.intellij.psi.xml.XmlTokenType;
import com.intellij.spellchecker.inspections.PlainTextSplitter;
import com.intellij.spellchecker.inspections.TextSplitter;
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.quickfixes.*;
import org.jetbrains.annotations.NotNull;
public class SpellcheckingStrategy {
@@ -97,6 +93,7 @@ public class SpellcheckingStrategy {
public static SpellCheckerQuickFix[] getDefaultRegularFixes(boolean useRename, String wordWithTypo) {
return new SpellCheckerQuickFix[]{
useRename ? new RenameTo(wordWithTypo) : new ChangeTo(wordWithTypo),
new SaveTo(wordWithTypo),
new AcceptWordAsCorrect(wordWithTypo)
};
}

View File

@@ -7,6 +7,8 @@ spellchecking.inspection.name=Typo
typo.in.word.ref=Typo: In word '#ref'
add.0.to.dictionary=Typo: Save ''{0}'' to dictionary
add.to.dictionary=Save to dictionary
add.0.to=Typo: Save ''{0}'' to...
add.to=Save to...
class.name.with.mistakes=Class name with mistakes
method.name.with.mistakes=Method name with mistakes
field.name.with.mistakes=Field name with mistakes
@@ -44,4 +46,5 @@ dictionary.filetype.name=Dictionary
dictionary.filetype.description=Dictionary file
user.dictionaries.title=User Dictionaries
project.dictionary=Project dictionary
app.dictionary=Application dictionary
app.dictionary=Application dictionary
select.dictionary.title=Select dictionary

View File

@@ -0,0 +1 @@
<caret>

View File

@@ -0,0 +1 @@
typo<caret>

View File

@@ -17,6 +17,7 @@ package com.intellij.spellchecker.inspection.quickfixes;
import com.intellij.spellchecker.quickfixes.ChangeTo;
import com.intellij.spellchecker.quickfixes.RenameTo;
import com.intellij.spellchecker.quickfixes.SaveTo;
public class PlainTextSpellCheckerFixesTest extends AbstractSpellCheckerFixesTest {
protected String getExtension() {
@@ -55,6 +56,14 @@ public class PlainTextSpellCheckerFixesTest extends AbstractSpellCheckerFixesTes
doNoQuickFixTest(RenameTo.FIX_NAME);
}
public void testEmptySaveTo() {
doNoQuickFixTest(SaveTo.FIX_NAME);
}
public void testNoTypoSaveTo() {
doNoQuickFixTest(SaveTo.FIX_NAME);
}
public void testNoTypoRenameTo() {
doNoQuickFixTest(RenameTo.FIX_NAME);
}