From a09ff055f731133d977a972cde1e1bce4fda5756 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Wed, 11 Dec 2024 10:05:50 +0100 Subject: [PATCH 1/3] Include optional parameters in spellcheck, if it contains text --- CHANGELOG.md | 1 + .../inspections/LatexSpellcheckingStrategy.kt | 40 ++++++++++--------- .../latex/LatexSpellCheckingStrategyTest.kt | 13 ++++++ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7975c0b53..d7b719f61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] ### Added +* Include optional parameters in spellcheck, if it contains text ### Fixed * Fix LaTeX files not showing up when choosing main file in run configuration diff --git a/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt b/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt index 0a0fdb8e5..8a2c3073e 100644 --- a/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt +++ b/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt @@ -2,14 +2,12 @@ package nl.hannahsten.texifyidea.inspections import com.intellij.psi.PsiElement import com.intellij.psi.impl.source.tree.LeafPsiElement -import com.intellij.psi.util.PsiTreeUtil +import com.intellij.psi.util.parentOfType import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy import com.intellij.spellchecker.tokenizer.Tokenizer import nl.hannahsten.texifyidea.grammar.LatexLanguage -import nl.hannahsten.texifyidea.lang.commands.Argument +import nl.hannahsten.texifyidea.lang.commands.* import nl.hannahsten.texifyidea.lang.commands.Argument.Type -import nl.hannahsten.texifyidea.lang.commands.LatexMathCommand -import nl.hannahsten.texifyidea.lang.commands.LatexRegularCommand import nl.hannahsten.texifyidea.psi.* import nl.hannahsten.texifyidea.util.parser.firstParentOfType import nl.hannahsten.texifyidea.util.parser.hasParent @@ -33,8 +31,7 @@ class LatexSpellcheckingStrategy : SpellcheckingStrategy() { psiElement.elementType == LatexTypes.COMMENT_TOKEN || psiElement.elementType == LatexTypes.LEFT || psiElement.elementType == LatexTypes.RIGHT || - isBeginEnd(psiElement) || - psiElement.hasParent(LatexOptionalParam::class) + isBeginEnd(psiElement) ) { return EMPTY_TOKENIZER } @@ -57,30 +54,35 @@ class LatexSpellcheckingStrategy : SpellcheckingStrategy() { } private fun isBeginEnd(element: PsiElement): Boolean { - var elt: PsiElement? = PsiTreeUtil.getParentOfType(element, LatexBeginCommand::class.java) - if (elt != null) { - return true - } - - elt = PsiTreeUtil.getParentOfType(element, LatexEndCommand::class.java) - return elt != null + return element.parentOfType() != null || element.parentOfType() != null } /** * Get the argument (with type) from TeXiFy knowledge that corresponds with the current psi element. */ private fun getArgument(leaf: LeafPsiElement): Argument? { - val parent = PsiTreeUtil.getParentOfType(leaf, LatexCommands::class.java) ?: return null + val parent = leaf.parentOfType() ?: return null - val arguments = getArguments(parent.commandToken.text.substring(1)) ?: return null + val arguments = getArguments(parent.name?.substring(1) ?: return null) ?: return null + val requiredArguments = arguments.filterIsInstance() + val optionalArguments = arguments.filterIsInstance() - val realParams = parent.getRequiredParameters() + val requiredParams = parent.getRequiredParameters() // Note that a leaf may be only part of a parameter - val parameterIndex = realParams.indexOf(leaf.firstParentOfType(LatexParameterText::class)?.text) - return if (parameterIndex < 0 || parameterIndex >= arguments.size) { + val parameterText = leaf.firstParentOfType(LatexParameterText::class)?.text + val parameterIndex = requiredParams.indexOf(parameterText) + if (parameterIndex >= 0 && parameterIndex < requiredArguments.size) { + return arguments[parameterIndex] + } + + // Also check optional arguments, if not key-value pairs it may contain text + val optionalParamIndex = parent.getOptionalParameterMap().map { it.key.text }.indexOf(parameterText) + return if (optionalParamIndex >= 0 && optionalParamIndex < optionalArguments.size) { + optionalArguments[optionalParamIndex] + } + else { null } - else arguments[parameterIndex] } private fun getArguments(commandName: String): Array? { diff --git a/test/nl/hannahsten/texifyidea/inspections/latex/LatexSpellCheckingStrategyTest.kt b/test/nl/hannahsten/texifyidea/inspections/latex/LatexSpellCheckingStrategyTest.kt index af4a3e571..7f0654f5a 100644 --- a/test/nl/hannahsten/texifyidea/inspections/latex/LatexSpellCheckingStrategyTest.kt +++ b/test/nl/hannahsten/texifyidea/inspections/latex/LatexSpellCheckingStrategyTest.kt @@ -17,4 +17,17 @@ class LatexSpellCheckingStrategyTest : TexifyInspectionTestBase(SpellCheckingIns ) myFixture.checkHighlighting() } + + fun testOptionalParameterTypo() { + myFixture.configureByText( + LatexFileType, + """ + \begin{description} + \item[Tpyo] + Tpyo + \end{description} + """.trimIndent() + ) + myFixture.checkHighlighting() + } } \ No newline at end of file From 7f4d1dd363cb77cd002562098f2e782aee7f402a Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Wed, 11 Dec 2024 10:46:53 +0100 Subject: [PATCH 2/3] Fix #3802, maybe --- .../completion/LatexBibliographyReferenceProvider.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/completion/LatexBibliographyReferenceProvider.kt b/src/nl/hannahsten/texifyidea/completion/LatexBibliographyReferenceProvider.kt index 6a4155b5c..3f67e1a02 100644 --- a/src/nl/hannahsten/texifyidea/completion/LatexBibliographyReferenceProvider.kt +++ b/src/nl/hannahsten/texifyidea/completion/LatexBibliographyReferenceProvider.kt @@ -51,9 +51,11 @@ object LatexBibliographyReferenceProvider : CompletionProvider Date: Wed, 11 Dec 2024 10:48:09 +0100 Subject: [PATCH 3/3] Formatting --- .../texifyidea/inspections/LatexSpellcheckingStrategy.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt b/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt index 8a2c3073e..925db3d12 100644 --- a/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt +++ b/src/nl/hannahsten/texifyidea/inspections/LatexSpellcheckingStrategy.kt @@ -63,7 +63,7 @@ class LatexSpellcheckingStrategy : SpellcheckingStrategy() { private fun getArgument(leaf: LeafPsiElement): Argument? { val parent = leaf.parentOfType() ?: return null - val arguments = getArguments(parent.name?.substring(1) ?: return null) ?: return null + val arguments = getArguments(parent.name?.substring(1) ?: return null) ?: return null val requiredArguments = arguments.filterIsInstance() val optionalArguments = arguments.filterIsInstance()