Skip to content

Commit

Permalink
Ignore text in tables as these are normally not sentences
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 9, 2024
1 parent f6beb6e commit 9fa6661
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<LatexBeginCommand>() != null || text.firstParentOfType<LatexEndCommand>() != 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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,34 @@ class GrazieInspectionTest : BasePlatformTestCase() {
}

fun testCommentInText() {
myFixture.configureByText(LatexFileType, """
myFixture.configureByText(
LatexFileType,
"""
\begin{document}
All <GRAMMAR_ERROR descr="The verb 'is' is singular. Did you mean: this is or those are?">those is</GRAMMAR_ERROR> problems in the middle of a sentence.
% <GRAMMAR_ERROR descr="The verb 'is' is singular. Did you mean: this is or Those are?">Those is</GRAMMAR_ERROR> a problem in a comment
<GRAMMAR_ERROR descr="The verb 'is' is singular. Did you mean: this is or Those are?">Those is</GRAMMAR_ERROR> 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}
<GRAMMAR_ERROR descr="Use An instead of 'A' if the following word starts with a vowel sound, e.g. 'an article', 'an hour'.">A</GRAMMAR_ERROR> 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()
}

Expand All @@ -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()
}

Expand Down Expand Up @@ -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.""")
Expand All @@ -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
)
}
}

0 comments on commit 9fa6661

Please sign in to comment.