mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-12-23 09:19:21 +00:00
Parse LaTeX sections
This commit is contained in:
parent
828ea84aaa
commit
ae1c06bd22
3 changed files with 63 additions and 0 deletions
26
src/main/kotlin/texlab/latex/LatexSection.kt
Normal file
26
src/main/kotlin/texlab/latex/LatexSection.kt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package texlab.latex
|
||||
|
||||
data class LatexSection(val command: LatexCommandSyntax, val text: String, val level: Int) {
|
||||
companion object {
|
||||
private val COMMAND_NAMES = arrayOf(
|
||||
"\\chapter", "\\chapter*",
|
||||
"\\section", "\\section*",
|
||||
"\\subsection", "\\subsection*",
|
||||
"\\subsubsection", "\\subsubsection*",
|
||||
"\\paragraph", "\\paragraph*",
|
||||
"\\subparagraph", "\\subparagraph*")
|
||||
|
||||
fun analyze(root: LatexSyntaxNode): List<LatexSection> {
|
||||
return root.descendants()
|
||||
.filterIsInstance<LatexCommandSyntax>()
|
||||
.filter { COMMAND_NAMES.contains(it.name.text) }
|
||||
.mapNotNull { analyze(it) }
|
||||
}
|
||||
|
||||
private fun analyze(command: LatexCommandSyntax): LatexSection? {
|
||||
val text = command.extractText(0) ?: return null
|
||||
val level = COMMAND_NAMES.indexOf(command.name.text) / 2
|
||||
return LatexSection(command, text.words.joinToString(" ") { it.text }, level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,4 +7,6 @@ class LatexSyntaxTree(text: String) {
|
|||
val includes: List<LatexInclude> = LatexInclude.analyze(root)
|
||||
|
||||
val environments: List<LatexEnvironment> = LatexEnvironment.analyze(root)
|
||||
|
||||
val sections: List<LatexSection> = LatexSection.analyze(root)
|
||||
}
|
||||
|
|
|
|||
35
src/test/kotlin/texlab/latex/LatexSectionTests.kt
Normal file
35
src/test/kotlin/texlab/latex/LatexSectionTests.kt
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package texlab.latex
|
||||
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class LatexSectionTests {
|
||||
|
||||
@Test
|
||||
fun `it should find numbered sections`() {
|
||||
val text = "\\section{Foo Bar Baz}"
|
||||
val tree = LatexParser.parse(text)
|
||||
val expected = LatexSection(tree.children[0] as LatexCommandSyntax, "Foo Bar Baz", 1)
|
||||
val actual = LatexSection.analyze(tree)
|
||||
assertEquals(1, actual.size)
|
||||
assertEquals(expected, actual[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it should find unnumbered paragraphs`() {
|
||||
val text = "\\paragraph*{Foo}"
|
||||
val tree = LatexParser.parse(text)
|
||||
val expected = LatexSection(tree.children[0] as LatexCommandSyntax, "Foo", 4)
|
||||
val actual = LatexSection.analyze(tree)
|
||||
assertEquals(1, actual.size)
|
||||
assertEquals(expected, actual[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `it should ignore invalid chapters`() {
|
||||
val text = "\\chapter*"
|
||||
val tree = LatexParser.parse(text)
|
||||
val actual = LatexSection.analyze(tree)
|
||||
assertEquals(0, actual.size)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue