diff --git a/src/main/kotlin/texlab/completion/latex/data/LatexUnit.kt b/src/main/kotlin/texlab/completion/latex/data/LatexUnit.kt index 96a5c652..32af730e 100644 --- a/src/main/kotlin/texlab/completion/latex/data/LatexUnit.kt +++ b/src/main/kotlin/texlab/completion/latex/data/LatexUnit.kt @@ -8,9 +8,22 @@ data class LatexUnit(val file: File, val references: List, val likelyPrimitives: Set) { fun checkPrimitives(candidates: Iterable): 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() val environments = mutableListOf() 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 { - 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 } } }