Skip to content

Commit

Permalink
Fix argument type of \gls, fixes #3508
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 6, 2024
1 parent 5b88bba commit e7f6887
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class LatexTextExtractor : TextExtractor() {
return null
}

return buildTextContent(root)
}

fun buildTextContent(root: LatexContent): TextContent? {
// Since Grazie works by first checking leaf elements, and if it gets null tries one level higher, we cannot return anything (e.g. literal for a command, comment for comments) other than LatexContent because then LatexContent itself will not be used as a root.
// However, we do need it as a root because we need to filter out certain things like inline math ourselves, so that we can make sure all the whitespace around ignored items is correct.
val domain = TextContent.TextDomain.PLAIN_TEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.intellij.psi.PsiElement
import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.psi.LatexParameterText
import nl.hannahsten.texifyidea.util.parser.firstChildOfType
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.parser.firstChildOfType
import nl.hannahsten.texifyidea.util.parser.requiredParameters

enum class LatexGlossariesCommand(
Expand Down Expand Up @@ -46,10 +46,10 @@ enum class LatexGlossariesCommand(
"long".asRequired(),
dependency = LatexPackage.GLOSSARIES
),
GLS("gls", "label".asRequired(), dependency = LatexPackage.GLOSSARIES),
GLSUPPER("Gls", "label".asRequired(), dependency = LatexPackage.GLOSSARIES),
GLSPLURAL("glspl", "label".asRequired(), dependency = LatexPackage.GLOSSARIES),
GLSPLURALUPPER("Glspl", "label".asRequired(), dependency = LatexPackage.GLOSSARIES),
GLS("gls", "label".asRequired(Argument.Type.TEXT), dependency = LatexPackage.GLOSSARIES),
GLSUPPER("Gls", "label".asRequired(Argument.Type.TEXT), dependency = LatexPackage.GLOSSARIES),
GLSPLURAL("glspl", "label".asRequired(Argument.Type.TEXT), dependency = LatexPackage.GLOSSARIES),
GLSPLURALUPPER("Glspl", "label".asRequired(Argument.Type.TEXT), dependency = LatexPackage.GLOSSARIES),

LOADGLSENTRIES(
"loadglsentries",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import com.intellij.grazie.ide.msg.GrazieStateLifecycle
import com.intellij.grazie.jlanguage.Lang
import com.intellij.grazie.remote.GrazieRemote
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.PsiFile
import com.intellij.spellchecker.inspections.SpellCheckingInspection
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl
import com.intellij.util.messages.Topic
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.psi.LatexPsiHelper
import nl.hannahsten.texifyidea.psi.LatexContent
import nl.hannahsten.texifyidea.util.parser.firstChildOfType

class GrazieInspectionTest : BasePlatformTestCase() {

Expand Down Expand Up @@ -123,6 +125,18 @@ class GrazieInspectionTest : BasePlatformTestCase() {
myFixture.checkHighlighting()
}

fun testGermanGlossaries() {
GrazieRemote.download(Lang.GERMANY_GERMAN)
GrazieConfig.update { it.copy(enabledLanguages = it.enabledLanguages + Lang.GERMANY_GERMAN) }
myFixture.configureByText(
LatexFileType,
"""
Der Hintergrund des Themas der Thesis ist der Umbruch beim Prozess des \gls{api}-Managements.
""".trimIndent()
)
myFixture.checkHighlighting()
}

fun testTabular() {
GrazieRemote.download(Lang.GERMANY_GERMAN)
GrazieConfig.update { it.copy(enabledLanguages = it.enabledLanguages + Lang.GERMANY_GERMAN) }
Expand Down Expand Up @@ -169,26 +183,20 @@ class GrazieInspectionTest : BasePlatformTestCase() {
/**
* Text as sent to Grazie.
*/
fun getSubmittedText(rootText: String, ranges: List<IntRange>): String {
return ranges.sortedBy { it.first }.flatMap { listOf(it.first, it.last) }.toMutableList().also { it.add(0, -1) }
.chunked(2) { if (it.size > 1) rootText.substring(it[0] + 1, it[1]) else null }.joinToString()
private fun getSubmittedText(file: PsiFile): String {
return LatexTextExtractor().buildTextContent(file.firstChildOfType(LatexContent::class)!!).toString()
}

fun testLongTextInCommand() {
fun testNewlinesShouldBeKept() {
val text = """
\section{This is the first section of my test document}
\section{The second section that is present in my testing document}
\section{A third section that I also put in my test document to showcase this issue}
\section{Here is a fourth section that I am putting in my document}
""".trimIndent()

myFixture.checkHighlighting()

val file = LatexPsiHelper(myFixture.project).createFromText(text)
val ranges = LatexTextExtractor().getStealthyRanges(file)
val submittedText = getSubmittedText(text, ranges)
assertEquals("A PID controller.", submittedText)
\section{First}
\section{Second}
""".trimIndent()
myFixture.configureByText(LatexFileType, text)
val submittedText = getSubmittedText(myFixture.file)
assertEquals("""
First
Second
""".trimIndent(), submittedText)
}

// todo that one german test
}

0 comments on commit e7f6887

Please sign in to comment.