diff --git a/src/nl/hannahsten/texifyidea/inspections/grazie/LatexTextExtractor.kt b/src/nl/hannahsten/texifyidea/inspections/grazie/LatexTextExtractor.kt index d9527a066..e11bff279 100644 --- a/src/nl/hannahsten/texifyidea/inspections/grazie/LatexTextExtractor.kt +++ b/src/nl/hannahsten/texifyidea/inspections/grazie/LatexTextExtractor.kt @@ -3,6 +3,7 @@ package nl.hannahsten.texifyidea.inspections.grazie import com.intellij.grazie.grammar.strategy.StrategyUtils import com.intellij.grazie.text.TextContent import com.intellij.grazie.text.TextExtractor +import com.intellij.lang.tree.util.children import com.intellij.psi.PsiElement import com.intellij.psi.PsiWhiteSpace import com.intellij.psi.util.startOffset @@ -57,20 +58,17 @@ class LatexTextExtractor : TextExtractor() { .filter { !it.inMathContext() && it.isNotInSquareBrackets() } // Ordering is relevant for whitespace .sortedBy { it.startOffset } - // Always keep newlines, as they may be the only whitespace splitting consecutive commands .filter { text -> text !is PsiWhiteSpace || text.text.contains("\n") } - // Skip arguments of non-text commands, but keep arguments of unknown commands, in particular if they are in the middle of a sentence // Even commends which have no text as argument, for example certain reference commands like auteref, may need to be kept in to get correct punctuation .filterNot { text -> text is LatexParameterText && LatexCommand.lookup(text.firstParentOfType(LatexCommands::class)?.name)?.firstOrNull()?.arguments?.any { it.type != Argument.Type.TEXT && it.type != Argument.Type.LABEL } == true } - // Environment names are never part of a sentence .filterNot { text -> text.firstParentOfType() != null || text.firstParentOfType() != null } - + // If we encounter an unescaped &, we are in some language construct like a tabular, so we ignore this because ofter a tabular does not contain full sentences + .filter { text -> text.node.children().none { it.elementType == LatexTypes.AMPERSAND } } // NOTE: it is not allowed to start the text we send to Grazie with a newline! If we do, then Grazie will just not do anything. So we exclude whitespace at the start .dropWhile { it is PsiWhiteSpace } - // Ranges that we need to keep // Note that textRangeInParent will not be correct because that's the text range in the direct parent, not in the root .flatMap { text -> diff --git a/test/nl/hannahsten/texifyidea/inspections/grazie/GrazieInspectionTest.kt b/test/nl/hannahsten/texifyidea/inspections/grazie/GrazieInspectionTest.kt index 1dad2e4ad..4b2214a19 100644 --- a/test/nl/hannahsten/texifyidea/inspections/grazie/GrazieInspectionTest.kt +++ b/test/nl/hannahsten/texifyidea/inspections/grazie/GrazieInspectionTest.kt @@ -37,30 +37,34 @@ class GrazieInspectionTest : BasePlatformTestCase() { } fun testCommentInText() { - myFixture.configureByText(LatexFileType, """ + myFixture.configureByText( + LatexFileType, + """ \begin{document} All those is problems in the middle of a sentence. % Those is a problem in a comment Those is a problem at the beginning of a sentence. \end{document} - """.trimIndent()) + """.trimIndent() + ) myFixture.checkHighlighting(true, false, false, true) } fun testSentenceAtEnvironmentStart() { - myFixture.configureByText(LatexFileType, """ + myFixture.configureByText( + LatexFileType, + """ \begin{document} A apple a day keeps the doctor away. Some other sentence. \end{document} - """.trimIndent()) + """.trimIndent() + ) myFixture.checkHighlighting(true, false, false, true) } fun testInlineMath() { - myFixture.configureByText( - LatexFileType, """Does Grazie detect ${'$'}m$ as a sentence?""" - ) + myFixture.configureByText(LatexFileType, """Does Grazie detect ${'$'}m$ as a sentence?""") myFixture.checkHighlighting() } @@ -76,9 +80,7 @@ class GrazieInspectionTest : BasePlatformTestCase() { } fun testMatchingParens() { - myFixture.configureByText( - LatexFileType, """A sentence (in this case). More sentence.""" - ) + myFixture.configureByText(LatexFileType, """A sentence (in this case). More sentence.""") myFixture.checkHighlighting() } @@ -171,14 +173,12 @@ class GrazieInspectionTest : BasePlatformTestCase() { * To find a rule id, search for the name in https://community.languagetool.org/rule/list and use the id together with the prefex from LangTool.globalIdPrefix */ - fun testCommaInSentence() { GrazieConfig.update { it.copy(userEnabledRules = setOf("LanguageTool.EN.COMMA_PARENTHESIS_WHITESPACE")) } myFixture.configureByText(LatexFileType, """\label{fig} Similar to the structure presented in \autoref{fig}, it is.""") myFixture.checkHighlighting() } - fun testCommandsInSentence() { GrazieConfig.update { it.copy(userEnabledRules = setOf("LanguageTool.EN.CONSECUTIVE_SPACES")) } myFixture.configureByText(LatexFileType, """The principles of a generic \ac{PID} controller.""") @@ -200,15 +200,18 @@ class GrazieInspectionTest : BasePlatformTestCase() { } fun testNewlinesShouldBeKept() { - val text = """ + val text = """ \section{First} \section{Second} """.trimIndent() myFixture.configureByText(LatexFileType, text) val submittedText = getSubmittedText(myFixture.file) - assertEquals(""" + assertEquals( + """ First Second - """.trimIndent(), submittedText) + """.trimIndent(), + submittedText + ) } } \ No newline at end of file