From 8ecdea6d118317c79088807821b4923bea96c894 Mon Sep 17 00:00:00 2001 From: UNV Date: Wed, 5 Nov 2025 21:23:35 +0300 Subject: [PATCH] Refactoring spellchecker. --- python-spellchecker/pom.xml | 74 +++++++-------- .../PythonBundledDictionaryProvider.java | 17 ++-- ...PythonSpellcheckerDictionaryGenerator.java | 81 +++++++++-------- ...pellcheckerGenerateDictionariesAction.java | 89 +++++++++---------- .../src/main/java/module-info.java | 13 ++- 5 files changed, 136 insertions(+), 138 deletions(-) diff --git a/python-spellchecker/pom.xml b/python-spellchecker/pom.xml index 1b071ef6..c46eedc0 100644 --- a/python-spellchecker/pom.xml +++ b/python-spellchecker/pom.xml @@ -1,6 +1,6 @@ - 4.0.0 - - consulo - arch.ide-api-provided - 3-SNAPSHOT - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + consulo + arch.ide-api-provided + 3-SNAPSHOT + + - consulo.plugin - consulo.python.spellchecker - 3-SNAPSHOT - jar + consulo.plugin + consulo.python.spellchecker + 3-SNAPSHOT + jar - - - consulo - https://maven.consulo.io/repository/snapshots/ - - true - interval:60 - - - + + + consulo + https://maven.consulo.io/repository/snapshots/ + + true + interval:60 + + + - - - ${project.groupId} - consulo.python-python.impl - ${project.version} - provided - + + + ${project.groupId} + consulo.python-python.impl + ${project.version} + provided + - - ${project.groupId} - com.intellij.spellchecker - ${project.version} - provided - - + + ${project.groupId} + com.intellij.spellchecker + ${project.version} + provided + + \ No newline at end of file diff --git a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonBundledDictionaryProvider.java b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonBundledDictionaryProvider.java index 4a40fca7..6755019d 100644 --- a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonBundledDictionaryProvider.java +++ b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonBundledDictionaryProvider.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.python.spellchecker; import com.intellij.spellchecker.BundledDictionaryProvider; @@ -24,12 +23,12 @@ */ @ExtensionImpl public class PythonBundledDictionaryProvider implements BundledDictionaryProvider { - @Override - public String[] getBundledDictionaries() { - return new String[] { - "python.dic", // autogenerated from python stdlib - "pythonExtras.dic", // manually added - "django.dic" - }; - } + @Override + public String[] getBundledDictionaries() { + return new String[]{ + "python.dic", // autogenerated from python stdlib + "pythonExtras.dic", // manually added + "django.dic" + }; + } } diff --git a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerDictionaryGenerator.java b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerDictionaryGenerator.java index 24f17588..9a67c6f7 100644 --- a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerDictionaryGenerator.java +++ b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerDictionaryGenerator.java @@ -13,12 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.python.spellchecker; import com.intellij.spellchecker.generator.SpellCheckerDictionaryGenerator; import com.jetbrains.python.codeInsight.controlflow.ScopeOwner; import com.jetbrains.python.psi.*; +import consulo.annotation.access.RequiredReadAction; import consulo.document.util.TextRange; import consulo.language.Language; import consulo.language.psi.PsiFile; @@ -28,50 +28,57 @@ import consulo.project.Project; import consulo.virtualFileSystem.VirtualFile; -import java.util.HashSet; +import java.util.Set; /** * @author yole */ public class PythonSpellcheckerDictionaryGenerator extends SpellCheckerDictionaryGenerator { - public PythonSpellcheckerDictionaryGenerator(final Project project, final String dictOutputFolder) { - super(project, dictOutputFolder, "python"); - } + public PythonSpellcheckerDictionaryGenerator(Project project, String dictOutputFolder) { + super(project, dictOutputFolder, "python"); + } - @Override - protected void processFolder(final HashSet seenNames, PsiManager manager, VirtualFile folder) { - if (!myExcludedFolders.contains(folder)) { - final String name = folder.getName(); - IdentifierTokenSplitter.getInstance().split(name, TextRange.allOf(name), textRange -> { - final String word = textRange.substring(name); - addSeenWord(seenNames, word, Language.ANY); - }); + @Override + protected void processFolder(Set seenNames, PsiManager manager, VirtualFile folder) { + if (!myExcludedFolders.contains(folder)) { + String name = folder.getName(); + IdentifierTokenSplitter.getInstance().split( + name, + TextRange.allOf(name), + textRange -> { + String word = textRange.substring(name); + addSeenWord(seenNames, word, Language.ANY); + } + ); + } + super.processFolder(seenNames, manager, folder); } - super.processFolder(seenNames, manager, folder); - } - @Override - protected void processFile(PsiFile file, final HashSet seenNames) { - file.accept(new PyRecursiveElementVisitor() { - @Override - public void visitPyFunction(PyFunction node) { - super.visitPyFunction(node); - processLeafsNames(node, seenNames); - } + @Override + protected void processFile(PsiFile file, Set seenNames) { + file.accept(new PyRecursiveElementVisitor() { + @Override + @RequiredReadAction + public void visitPyFunction(PyFunction node) { + super.visitPyFunction(node); + processLeafsNames(node, seenNames); + } - @Override - public void visitPyClass(PyClass node) { - super.visitPyClass(node); - processLeafsNames(node, seenNames); - } + @Override + @RequiredReadAction + public void visitPyClass(PyClass node) { + super.visitPyClass(node); + processLeafsNames(node, seenNames); + } - @Override - public void visitPyTargetExpression(PyTargetExpression node) { - super.visitPyTargetExpression(node); - if (PsiTreeUtil.getParentOfType(node, ScopeOwner.class) instanceof PyFile) { - processLeafsNames(node, seenNames); - } - } - }); - } + @Override + @RequiredReadAction + public void visitPyTargetExpression(PyTargetExpression node) { + super.visitPyTargetExpression(node); + if (PsiTreeUtil.getParentOfType(node, ScopeOwner.class) instanceof PyFile) { + processLeafsNames(node, seenNames); + } + } + }); + } } diff --git a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerGenerateDictionariesAction.java b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerGenerateDictionariesAction.java index 69279187..8a2fe062 100644 --- a/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerGenerateDictionariesAction.java +++ b/python-spellchecker/src/main/java/consulo/python/spellchecker/PythonSpellcheckerGenerateDictionariesAction.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.python.spellchecker; import com.jetbrains.python.impl.sdk.PythonSdkType; @@ -21,9 +20,10 @@ import consulo.annotation.component.ActionRef; import consulo.content.base.BinariesOrderRootType; import consulo.content.bundle.Sdk; -import consulo.language.editor.LangDataKeys; +import consulo.localize.LocalizeValue; import consulo.module.Module; import consulo.module.content.ModuleRootManager; +import consulo.ui.annotation.RequiredUIAccess; import consulo.ui.ex.action.AnAction; import consulo.ui.ex.action.AnActionEvent; import consulo.virtualFileSystem.VirtualFile; @@ -32,54 +32,47 @@ * @author yole */ @ActionImpl(id = "PythonGenerateDictionaries", children = @ActionRef(id = "Internal")) -public class PythonSpellcheckerGenerateDictionariesAction extends AnAction -{ - public PythonSpellcheckerGenerateDictionariesAction() - { - super("Generate Python Spellchecker Dictionaries"); - } +public class PythonSpellcheckerGenerateDictionariesAction extends AnAction { + public PythonSpellcheckerGenerateDictionariesAction() { + super(LocalizeValue.localizeTODO("Generate Python Spellchecker Dictionaries")); + } - @Override - public void actionPerformed(AnActionEvent e) - { - Module module = e.getData(LangDataKeys.MODULE); - if(module == null) - { - return; - } - VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots(); - if(contentRoots.length == 0) - { - return; - } - Sdk sdk = PythonSdkType.findPythonSdk(module); - if(sdk == null) - { - return; - } + @Override + @RequiredUIAccess + public void actionPerformed(AnActionEvent e) { + Module module = e.getData(Module.KEY); + if (module == null) { + return; + } + VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots(); + if (contentRoots.length == 0) { + return; + } + Sdk sdk = PythonSdkType.findPythonSdk(module); + if (sdk == null) { + return; + } - final PythonSpellcheckerDictionaryGenerator generator = new PythonSpellcheckerDictionaryGenerator(module.getProject(), - contentRoots[0].getPath() + "/dicts"); + PythonSpellcheckerDictionaryGenerator generator = new PythonSpellcheckerDictionaryGenerator( + module.getProject(), + contentRoots[0].getPath() + "/dicts" + ); - VirtualFile[] roots = sdk.getRootProvider().getFiles(BinariesOrderRootType.getInstance()); - for(VirtualFile root : roots) - { - if(root.getName().equals("Lib")) - { - generator.addFolder("python", root); - generator.excludeFolder(root.findChild("test")); - generator.excludeFolder(root.findChild("site-packages")); - } - else if(root.getName().equals("site-packages")) - { - VirtualFile djangoRoot = root.findChild("django"); - if(djangoRoot != null) - { - generator.addFolder("django", djangoRoot); - } - } - } + VirtualFile[] roots = sdk.getRootProvider().getFiles(BinariesOrderRootType.getInstance()); + for (VirtualFile root : roots) { + if (root.getName().equals("Lib")) { + generator.addFolder("python", root); + generator.excludeFolder(root.findChild("test")); + generator.excludeFolder(root.findChild("site-packages")); + } + else if (root.getName().equals("site-packages")) { + VirtualFile djangoRoot = root.findChild("django"); + if (djangoRoot != null) { + generator.addFolder("django", djangoRoot); + } + } + } - generator.generate(); - } + generator.generate(); + } } diff --git a/python-spellchecker/src/main/java/module-info.java b/python-spellchecker/src/main/java/module-info.java index 8141617d..2ae3ef0c 100644 --- a/python-spellchecker/src/main/java/module-info.java +++ b/python-spellchecker/src/main/java/module-info.java @@ -1,11 +1,10 @@ /** * @author VISTALL - * @since 24/06/2023 + * @since 2023-06-24 */ -open module consulo.python.spellchecker -{ - requires consulo.ide.api; - requires com.intellij.spellchecker; - requires consulo.python.language.api; - requires consulo.python.impl; +open module consulo.python.spellchecker { + requires consulo.ide.api; + requires com.intellij.spellchecker; + requires consulo.python.language.api; + requires consulo.python.impl; } \ No newline at end of file