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/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() != 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