[spellchecker] SaveTo: extracted core part to static method

To reuse in custom `Save to dictionary` implementations in Rider and CLion. Temporary solution to cherry-pick in 241 stable branch.

GitOrigin-RevId: b34004ce230afeab164084c50d8ca71256e2047d
This commit is contained in:
Denis Mukhametianov
2024-05-05 12:11:03 +02:00
committed by intellij-monorepo-bot
parent 0b224e603b
commit f5235a2cf2
2 changed files with 19 additions and 20 deletions

View File

@@ -7,7 +7,8 @@ spellchecking.inspection.name=Typo
typo.in.word.ref=Typo: In word '#ref' typo.in.word.ref=Typo: In word '#ref'
add.0.to.dictionary=Typo: Save ''{0}'' to dictionary add.0.to.dictionary=Typo: Save ''{0}'' to dictionary
add.to.dictionary=Save to dictionary add.to.dictionary=Save to dictionary
save.0.to.1=Save {0} to dictionary save.0.to.dictionary.fix=Save {0} to dictionary
save.0.to.dictionary.action=Save {0} to Dictionary
0.in.quotes=''{0}'' 0.in.quotes=''{0}''
enter.simple.word=Enter word: enter.simple.word=Enter word:
add.new.word=Add New Word add.new.word=Add New Word

View File

@@ -17,7 +17,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory; import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Segment; import com.intellij.openapi.util.Segment;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import com.intellij.spellchecker.DictionaryLayer; import com.intellij.spellchecker.DictionaryLayer;
import com.intellij.spellchecker.DictionaryLayersProvider; import com.intellij.spellchecker.DictionaryLayersProvider;
@@ -34,8 +33,6 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.*; import javax.swing.*;
public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction { public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
private static final String DICTIONARY = " dictionary";
private static final String DOTS = "...";
@Nullable private DictionaryLayer myLayer = null; @Nullable private DictionaryLayer myLayer = null;
private String myWord; private String myWord;
@@ -54,13 +51,12 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
@Override @Override
public @NotNull String getName() { public @NotNull String getName() {
return SpellCheckerBundle.message("save.0.to.1", myWord != null ? SpellCheckerBundle.message("0.in.quotes", myWord) : ""); return SpellCheckerBundle.message("save.0.to.dictionary.fix", myWord != null ? SpellCheckerBundle.message("0.in.quotes", myWord) : "");
} }
@Override @Override
public @NotNull String getFamilyName() { public @NotNull String getFamilyName() {
final String dictionary = myLayer != null ? myLayer.getName() + DICTIONARY : DOTS; return SpellCheckerBundle.message("save.0.to.dictionary.fix", "");
return SpellCheckerBundle.message("save.0.to.1", "", dictionary);
} }
@Override @Override
@@ -70,11 +66,18 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
@Override @Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
var psiElement = descriptor.getPsiElement();
var psiFile = psiElement.getContainingFile();
var wordRange = descriptor.getTextRangeInElement().shiftRight(psiElement.getTextRange().getStartOffset());
var wordToSave = myWord != null ? myWord : ProblemDescriptorUtil.extractHighlightedText(descriptor, descriptor.getPsiElement());
applyFix(project, psiFile, wordRange, wordToSave, myLayer);
}
public static void applyFix(@NotNull Project project, PsiFile psiFile, TextRange wordRange, String wordToSave, @Nullable DictionaryLayer layer) {
DataManager.getInstance() DataManager.getInstance()
.getDataContextFromFocusAsync() .getDataContextFromFocusAsync()
.onSuccess(context -> { .onSuccess(context -> {
final String wordToSave = myWord != null ? myWord : ProblemDescriptorUtil.extractHighlightedText(descriptor, descriptor.getPsiElement()); if (layer == null) {
if (myLayer == null) {
final JBList<String> dictList = new JBList<>( final JBList<String> dictList = new JBList<>(
ContainerUtil.map(DictionaryLayersProvider.getAllLayers(project), it -> it.getName()) ContainerUtil.map(DictionaryLayersProvider.getAllLayers(project), it -> it.getName())
); );
@@ -86,8 +89,8 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
() -> () ->
CommandProcessor.getInstance().executeCommand( CommandProcessor.getInstance().executeCommand(
project, project,
() -> acceptWord(wordToSave, DictionaryLayersProvider.getLayer(project, dictList.getSelectedValue()), descriptor), () -> acceptWord(wordToSave, DictionaryLayersProvider.getLayer(project, dictList.getSelectedValue()), psiFile, wordRange),
getName(), SpellCheckerBundle.message("save.0.to.dictionary.action", wordToSave),
null null
) )
) )
@@ -95,21 +98,16 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
.showInBestPositionFor(context); .showInBestPositionFor(context);
} }
else { else {
acceptWord(wordToSave, myLayer, descriptor); acceptWord(wordToSave, layer, psiFile, wordRange);
} }
}); });
} }
private static void acceptWord(String word, @Nullable DictionaryLayer layer, ProblemDescriptor descriptor) { private static void acceptWord(String word, @Nullable DictionaryLayer layer, PsiFile file, TextRange wordRange) {
SideEffectGuard.checkSideEffectAllowed(SideEffectGuard.EffectType.SETTINGS); SideEffectGuard.checkSideEffectAllowed(SideEffectGuard.EffectType.SETTINGS);
PsiElement psi = descriptor.getPsiElement();
PsiFile file = psi.getContainingFile();
Project project = file.getProject(); Project project = file.getProject();
SpellCheckerManager.getInstance(project).acceptWordAsCorrect$intellij_spellchecker(word, file.getViewProvider().getVirtualFile(), project, layer); SpellCheckerManager.getInstance(project).acceptWordAsCorrect$intellij_spellchecker(word, file.getViewProvider().getVirtualFile(), project, layer);
removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, wordRange, SpellCheckingInspection.SPELL_CHECKING_INSPECTION_TOOL_NAME);
TextRange range = descriptor.getTextRangeInElement().shiftRight(psi.getTextRange().getStartOffset());
removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, range, SpellCheckingInspection.SPELL_CHECKING_INSPECTION_TOOL_NAME);
} }
/** /**
@@ -141,6 +139,6 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
} }
public static String getFixName() { public static String getFixName() {
return SpellCheckerBundle.message("save.0.to.1", "", DOTS); return SpellCheckerBundle.message("save.0.to.dictionary.fix", "");
} }
} }