mirror of
https://github.com/latex-lsp/texlab.git
synced 2025-12-23 09:19:21 +00:00
Refactor LatexUnit class
This commit is contained in:
parent
33586375e1
commit
08ad000673
1 changed files with 24 additions and 31 deletions
|
|
@ -8,9 +8,22 @@ data class LatexUnit(val file: File,
|
|||
val references: List<File>,
|
||||
val likelyPrimitives: Set<String>) {
|
||||
fun checkPrimitives(candidates: Iterable<String>): LatexPrimitives {
|
||||
val testCode = buildTestCode(candidates)
|
||||
val log = LatexCompiler.compile(testCode, format) ?: ""
|
||||
val testCode = buildString {
|
||||
appendln(buildCodeHeader(file.nameWithoutExtension, kind))
|
||||
appendln("\\usepackage{etoolbox}")
|
||||
appendln("\\begin{document}")
|
||||
|
||||
for (candidate in candidates) {
|
||||
appendln("\\ifcsundef{$candidate}{} {")
|
||||
appendln("\\ifcsundef{end$candidate}")
|
||||
appendln("{ \\wlog{cmd:$candidate} }")
|
||||
appendln("{ \\wlog{env:$candidate} } }")
|
||||
}
|
||||
|
||||
appendln("\\end{document}")
|
||||
}
|
||||
|
||||
val log = LatexCompiler.compile(testCode, format) ?: ""
|
||||
val commands = mutableListOf<String>()
|
||||
val environments = mutableListOf<String>()
|
||||
for (line in log.lines()) {
|
||||
|
|
@ -24,23 +37,6 @@ data class LatexUnit(val file: File,
|
|||
return LatexPrimitives(commands, environments)
|
||||
}
|
||||
|
||||
private fun buildTestCode(candidates: Iterable<String>): String {
|
||||
val code = buildCodeHeader(file.nameWithoutExtension, kind)
|
||||
.appendln("""\usepackage{etoolbox}""")
|
||||
.appendln("""\begin{document}""")
|
||||
|
||||
for (candidate in candidates) {
|
||||
code.appendln("""\ifcsundef{$candidate}{}{
|
||||
\ifcsundef{end$candidate}
|
||||
{ \wlog{cmd:$candidate} }
|
||||
{ \wlog{env:$candidate} }
|
||||
}""")
|
||||
}
|
||||
|
||||
code.appendln("""\end{document}""")
|
||||
return code.toString()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val fileRegex = Regex("""[a-zA-Z0-9_\-.]+\.(sty|tex|def|cls)""")
|
||||
private val primitiveRegex = Regex("""[a-zA-Z]+""")
|
||||
|
|
@ -57,10 +53,11 @@ data class LatexUnit(val file: File,
|
|||
else -> LatexFormat.LATEX
|
||||
}
|
||||
|
||||
val testCode = buildCodeHeader(file.nameWithoutExtension, kind)
|
||||
.appendln("""\listfiles""")
|
||||
.appendln("""\begin{document} \end{document}""")
|
||||
.toString()
|
||||
val testCode = buildString {
|
||||
appendln(buildCodeHeader(file.nameWithoutExtension, kind))
|
||||
appendln("\\listfiles")
|
||||
appendln("\\begin{document} \\end{document}")
|
||||
}
|
||||
val log = LatexCompiler.compile(testCode, format) ?: return null
|
||||
|
||||
val includes = extractIncludes(log, kind, resolver)
|
||||
|
|
@ -89,15 +86,11 @@ data class LatexUnit(val file: File,
|
|||
.toList()
|
||||
}
|
||||
|
||||
private fun buildCodeHeader(name: String, kind: LatexUnitKind): StringBuilder {
|
||||
val builder = StringBuilder()
|
||||
val line = when (kind) {
|
||||
LatexUnitKind.STY -> """\documentclass{article} \usepackage{$name}"""
|
||||
LatexUnitKind.CLS -> """\documentclass{$name}"""
|
||||
private fun buildCodeHeader(name: String, kind: LatexUnitKind): String = buildString {
|
||||
when (kind) {
|
||||
LatexUnitKind.STY -> appendln("\\documentclass{article} \\usepackage{$name}")
|
||||
LatexUnitKind.CLS -> appendln("\\documentclass{$name}")
|
||||
}
|
||||
|
||||
builder.appendln(line)
|
||||
return builder
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue