mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-02-04 15:06:56 +07:00
[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:
committed by
intellij-monorepo-bot
parent
0b224e603b
commit
f5235a2cf2
@@ -7,7 +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
|
||||
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}''
|
||||
enter.simple.word=Enter word:
|
||||
add.new.word=Add New Word
|
||||
|
||||
@@ -17,7 +17,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.util.Segment;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.spellchecker.DictionaryLayer;
|
||||
import com.intellij.spellchecker.DictionaryLayersProvider;
|
||||
@@ -34,8 +33,6 @@ import org.jetbrains.annotations.Nullable;
|
||||
import javax.swing.*;
|
||||
|
||||
public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
private static final String DICTIONARY = " dictionary";
|
||||
private static final String DOTS = "...";
|
||||
@Nullable private DictionaryLayer myLayer = null;
|
||||
private String myWord;
|
||||
|
||||
@@ -54,13 +51,12 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
|
||||
@Override
|
||||
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
|
||||
public @NotNull String getFamilyName() {
|
||||
final String dictionary = myLayer != null ? myLayer.getName() + DICTIONARY : DOTS;
|
||||
return SpellCheckerBundle.message("save.0.to.1", "", dictionary);
|
||||
return SpellCheckerBundle.message("save.0.to.dictionary.fix", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +66,18 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
|
||||
@Override
|
||||
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()
|
||||
.getDataContextFromFocusAsync()
|
||||
.onSuccess(context -> {
|
||||
final String wordToSave = myWord != null ? myWord : ProblemDescriptorUtil.extractHighlightedText(descriptor, descriptor.getPsiElement());
|
||||
if (myLayer == null) {
|
||||
if (layer == null) {
|
||||
final JBList<String> dictList = new JBList<>(
|
||||
ContainerUtil.map(DictionaryLayersProvider.getAllLayers(project), it -> it.getName())
|
||||
);
|
||||
@@ -86,8 +89,8 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
() ->
|
||||
CommandProcessor.getInstance().executeCommand(
|
||||
project,
|
||||
() -> acceptWord(wordToSave, DictionaryLayersProvider.getLayer(project, dictList.getSelectedValue()), descriptor),
|
||||
getName(),
|
||||
() -> acceptWord(wordToSave, DictionaryLayersProvider.getLayer(project, dictList.getSelectedValue()), psiFile, wordRange),
|
||||
SpellCheckerBundle.message("save.0.to.dictionary.action", wordToSave),
|
||||
null
|
||||
)
|
||||
)
|
||||
@@ -95,21 +98,16 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
.showInBestPositionFor(context);
|
||||
}
|
||||
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);
|
||||
|
||||
PsiElement psi = descriptor.getPsiElement();
|
||||
PsiFile file = psi.getContainingFile();
|
||||
Project project = file.getProject();
|
||||
SpellCheckerManager.getInstance(project).acceptWordAsCorrect$intellij_spellchecker(word, file.getViewProvider().getVirtualFile(), project, layer);
|
||||
|
||||
TextRange range = descriptor.getTextRangeInElement().shiftRight(psi.getTextRange().getStartOffset());
|
||||
removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, range, SpellCheckingInspection.SPELL_CHECKING_INSPECTION_TOOL_NAME);
|
||||
removeHighlightersWithExactRange(file.getViewProvider().getDocument(), project, wordRange, SpellCheckingInspection.SPELL_CHECKING_INSPECTION_TOOL_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,6 +139,6 @@ public final class SaveTo implements SpellCheckerQuickFix, LowPriorityAction {
|
||||
}
|
||||
|
||||
public static String getFixName() {
|
||||
return SpellCheckerBundle.message("save.0.to.1", "", DOTS);
|
||||
return SpellCheckerBundle.message("save.0.to.dictionary.fix", "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user