Skip to content

Commit

Permalink
Add support for ConTeXt
Browse files Browse the repository at this point in the history
Fixes #120.
  • Loading branch information
valentjn committed Nov 28, 2021
1 parent d1a1dee commit 5986c0b
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Find more information (how to install, how to use, etc.) at the [website of LT<s

## Features

- **Supported markup languages:** BibT<sub>E</sub>X, L<sup>A</sup>T<sub>E</sub>X, Markdown, Org, reStructuredText, R Sweave, XHTML
- **Supported markup languages:** BibT<sub>E</sub>X, ConT<sub>E</sub>Xt, L<sup>A</sup>T<sub>E</sub>X, Markdown, Org, reStructuredText, R Sweave, XHTML
- Comment checking in **many popular programming languages** (optional, opt-in)
- Comes with **everything included,** no need to install Java or LanguageTool
- **Offline checking:** Does not upload anything to the internet
Expand Down
3 changes: 3 additions & 0 deletions changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
</properties>
<body>
<release version="15.2.0" date="upcoming">
<action type="add" issue="#120">
Add support for ConTeXt
</action>
<action type="add" issue="#123">
Add support for strikethrough as in [GitHub Flavored Markdown](https://github.github.com/gfm/#strikethrough-extension-)
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@ abstract class CodeAnnotatedTextBuilder(
@Suppress("ComplexMethod")
fun create(codeLanguageId: String): CodeAnnotatedTextBuilder {
return when (codeLanguageId) {
"bib" -> LatexAnnotatedTextBuilder(codeLanguageId)
"bibtex" -> LatexAnnotatedTextBuilder(codeLanguageId)
"html" -> HtmlAnnotatedTextBuilder(codeLanguageId)
"latex" -> LatexAnnotatedTextBuilder(codeLanguageId)
"bib",
"bibtex",
-> LatexAnnotatedTextBuilder(codeLanguageId)
"html",
"xhtml",
-> HtmlAnnotatedTextBuilder(codeLanguageId)
"context",
"context.tex",
"latex",
"plaintex",
"rsweave",
"tex",
-> LatexAnnotatedTextBuilder(codeLanguageId)
"markdown" -> MarkdownAnnotatedTextBuilder(codeLanguageId)
"nop" -> NopAnnotatedTextBuilder(codeLanguageId)
"org" -> OrgAnnotatedTextBuilder(codeLanguageId)
"plaintex" -> LatexAnnotatedTextBuilder(codeLanguageId)
"plaintext" -> PlaintextAnnotatedTextBuilder(codeLanguageId)
"restructuredtext" -> RestructuredtextAnnotatedTextBuilder(codeLanguageId)
"rsweave" -> LatexAnnotatedTextBuilder(codeLanguageId)
"tex" -> LatexAnnotatedTextBuilder(codeLanguageId)
"xhtml" -> HtmlAnnotatedTextBuilder(codeLanguageId)
else -> {
if (ProgramCommentRegexs.isSupportedCodeLanguageId(codeLanguageId)) {
ProgramAnnotatedTextBuilder(codeLanguageId)
Expand Down
21 changes: 13 additions & 8 deletions src/main/kotlin/org/bsplines/ltexls/parsing/CodeFragmentizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,24 @@ abstract class CodeFragmentizer(
@Suppress("ComplexMethod")
fun create(codeLanguageId: String): CodeFragmentizer {
return when (codeLanguageId) {
"bib" -> BibtexFragmentizer(codeLanguageId)
"bibtex" -> BibtexFragmentizer(codeLanguageId)
"html" -> HtmlFragmentizer(codeLanguageId)
"latex" -> LatexFragmentizer(codeLanguageId)
"bib",
"bibtex",
-> BibtexFragmentizer(codeLanguageId)
"html",
"xhtml",
-> HtmlFragmentizer(codeLanguageId)
"context",
"context.tex",
"latex",
"plaintex",
"rsweave",
"tex",
-> LatexFragmentizer(codeLanguageId)
"markdown" -> MarkdownFragmentizer(codeLanguageId)
"nop" -> NopFragmentizer(codeLanguageId)
"org" -> OrgFragmentizer(codeLanguageId)
"plaintex" -> LatexFragmentizer(codeLanguageId)
"plaintext" -> PlaintextFragmentizer(codeLanguageId)
"restructuredtext" -> RestructuredtextFragmentizer(codeLanguageId)
"rsweave" -> LatexFragmentizer(codeLanguageId)
"tex" -> LatexFragmentizer(codeLanguageId)
"xhtml" -> HtmlFragmentizer(codeLanguageId)
else -> {
if (ProgramCommentRegexs.isSupportedCodeLanguageId(codeLanguageId)) {
ProgramFragmentizer(codeLanguageId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,35 @@ class LatexAnnotatedTextBuilder(
private fun processBackslash() {
var command = matchFromPositionAsString(COMMAND_REGEX)

if ((command == "\\begin") || (command == "\\end")) {
if (
(command == "\\begin")
|| (command == "\\end")
|| command.startsWith("\\start")
|| command.startsWith("\\stop")
) {
this.preserveDummyLast = true
val argument: String = matchFromPositionAsString(ARGUMENT_REGEX, this.pos + command.length)
val environmentName: String =
if (argument.length >= 2) argument.substring(1, argument.length - 1) else ""
val isBeginEnvironment: Boolean = ((command == "\\begin") || command.startsWith("\\start"))
val argument: String
val environmentName: String

if ((command == "\\begin") || (command == "\\end")) {
argument = matchFromPositionAsString(ARGUMENT_REGEX, this.pos + command.length)
environmentName =
if (argument.length >= 2) argument.substring(1, argument.length - 1) else ""
} else {
argument = ""
environmentName = command.substring(
if (isBeginEnvironment) LENGTH_OF_START_PREFIX else LENGTH_OF_STOP_PREFIX,
)
}

var argumentsProcessed = false
var interpretAs = ""

if (MATH_ENVIRONMENTS.contains(environmentName)) {
addMarkup(command)

if (command == "\\begin") {
if (isBeginEnvironment) {
if (environmentName == "math") {
enterInlineMath()
} else {
Expand All @@ -198,7 +215,7 @@ class LatexAnnotatedTextBuilder(
popMode()
interpretAs = generateDummy()
}
} else if (command == "\\begin") {
} else if (isBeginEnvironment) {
val possibleEnvironmentSignatures: List<LatexEnvironmentSignature> =
this.environmentSignatureMap[command + argument] ?: emptyList()

Expand All @@ -220,8 +237,11 @@ class LatexAnnotatedTextBuilder(
if (matchingEnvironmentSignature != null) {
if (matchingEnvironmentSignature.action == LatexCommandSignature.Action.Ignore) {
this.modeStack.add(Mode.IgnoreEnvironment)
this.ignoreEnvironmentEndRegex =
Regex("^\\\\end\\{" + Regex.escape(environmentName) + "}")
this.ignoreEnvironmentEndRegex = if (command == "\\begin") {
Regex("^\\\\end\\{" + Regex.escape(environmentName) + "}")
} else {
Regex("^\\\\stop" + Regex.escape(environmentName) + "(?![A-Za-z])")
}
}

if (matchingEnvironmentSignature.ignoreAllArguments) {
Expand All @@ -245,7 +265,7 @@ class LatexAnnotatedTextBuilder(

if (!argumentsProcessed) {
addMarkup(argument, interpretAs)
if (command == "\\begin") processEnvironmentArguments()
if (isBeginEnvironment) processEnvironmentArguments()
}
}
} else if ((command == "\\$") || (command == "\\%") || (command == "\\&")) {
Expand Down Expand Up @@ -893,13 +913,17 @@ class LatexAnnotatedTextBuilder(
"equation*",
"flalign",
"flalign*",
"formula",
"gather",
"gather*",
"math",
"multline",
"multline*",
)

private const val LENGTH_OF_START_PREFIX = 6
private const val LENGTH_OF_STOP_PREFIX = 5

private fun <T : LatexCommandSignature> createCommandSignatureMap(
commandSignatures: List<T>,
): Map<String, List<T>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\algnewcommand{}{}"),
LatexCommandSignature("\\algrenewcommand{}{}"),
LatexCommandSignature("\\arabic{}", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\at{}[]", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\AtBeginEnvironment{}{}"),
LatexCommandSignature("\\AtEndEnvironment{}{}"),
LatexCommandSignature("\\autocite{}", LatexCommandSignature.Action.Dummy),
Expand Down Expand Up @@ -402,6 +403,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\color[]{}"),
LatexCommandSignature("\\colorbox{}"),
LatexCommandSignature("\\colorlet{}{}"),
LatexCommandSignature("\\ConTeXt", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\counterwithin{}{}"),
LatexCommandSignature("\\counterwithin*{}{}"),
LatexCommandSignature("\\counterwithout{}{}"),
Expand Down Expand Up @@ -439,6 +441,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\defbibheading{}{}"),
LatexCommandSignature("\\defbibheading{}[]{}"),
LatexCommandSignature("\\defbibnote{}{}"),
LatexCommandSignature("\\definecolor[][]"),
LatexCommandSignature("\\definecolor{}{}{}"),
LatexCommandSignature("\\definespotcolor{}{}{}"),
LatexCommandSignature("\\directlua{}"),
Expand All @@ -451,6 +454,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\etocsetlevel{}{}"),
LatexCommandSignature("\\etocsetnexttocdepth{}"),
LatexCommandSignature("\\etocsettocstyle{}{}"),
LatexCommandSignature("\\externalfigure[]", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\fcolorbox{}"),
LatexCommandSignature("\\floatname{}{}"),
LatexCommandSignature("\\floatstyle{}"),
Expand All @@ -470,6 +474,8 @@ object LatexAnnotatedTextBuilderDefaults {
"\\foreignlanguage[]{}{}",
LatexCommandSignature.Action.Dummy,
),
LatexCommandSignature("\\framed[]"),
LatexCommandSignature("\\from[]", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\GenericWarning{}{}"),
LatexCommandSignature("\\geometry{}"),
LatexCommandSignature("\\gls{}", LatexCommandSignature.Action.Dummy),
Expand Down Expand Up @@ -543,11 +549,13 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\ifentrytype{}"),
LatexCommandSignature("\\@ifpackageloaded{}"),
LatexCommandSignature("\\iftoggle{}"),
LatexCommandSignature("\\in[]", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\include{}"),
LatexCommandSignature("\\includegraphics{}"),
LatexCommandSignature("\\includegraphics[]{}"),
LatexCommandSignature("\\includepdf{}"),
LatexCommandSignature("\\includepdf[]{}"),
LatexCommandSignature("\\index{}"),
LatexCommandSignature("\\input{}"),
LatexCommandSignature("\\inputminted{}{}"),
LatexCommandSignature("\\inputminted[]{}{}"),
Expand Down Expand Up @@ -622,6 +630,9 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\pgfmathsetmacro{}{}"),
LatexCommandSignature("\\pgfmathsetseed{}"),
LatexCommandSignature("\\phantom{}"),
LatexCommandSignature("\\placefigure[]{}"),
LatexCommandSignature("\\placefigure[][]"),
LatexCommandSignature("\\placeformula[]"),
LatexCommandSignature("\\printacronyms[]"),
LatexCommandSignature("\\printbibliography[]"),
LatexCommandSignature("\\printglossary[]"),
Expand Down Expand Up @@ -678,7 +689,19 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\@setplength{}{}"),
LatexCommandSignature("\\setplength{}{}"),
LatexCommandSignature("\\setstretch{}"),
LatexCommandSignature("\\setupbodyfont[]"),
LatexCommandSignature("\\setupcolors[]"),
LatexCommandSignature("\\setupfooter[]"),
LatexCommandSignature("\\setupfootertexts[]"),
LatexCommandSignature("\\setuphead[][]"),
LatexCommandSignature("\\setupindenting[]"),
LatexCommandSignature("\\setupinteraction[]"),
LatexCommandSignature("\\setupitemize[]"),
LatexCommandSignature("\\setuplayout[]"),
LatexCommandSignature("\\setuppagenumbering[]"),
LatexCommandSignature("\\setuppapersize[][]"),
LatexCommandSignature("\\setuptoc{}{}"),
LatexCommandSignature("\\setupwhitespace[]"),
LatexCommandSignature("\\sisetup{}"),
LatexCommandSignature("\\smartcite{}", LatexCommandSignature.Action.Dummy),
LatexCommandSignature("\\smartcite[]{}", LatexCommandSignature.Action.Dummy),
Expand Down Expand Up @@ -817,6 +840,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexCommandSignature("\\usepackage{}"),
LatexCommandSignature("\\usepackage[]{}"),
LatexCommandSignature("\\usetikzlibrary{}"),
LatexCommandSignature("\\useURL[][][][]"),
LatexCommandSignature("\\value{}"),
LatexCommandSignature("\\vphantom{}"),
LatexCommandSignature("\\vspace{}"),
Expand All @@ -841,6 +865,7 @@ object LatexAnnotatedTextBuilderDefaults {
LatexEnvironmentSignature("minted"),
LatexEnvironmentSignature("otherlanguage"),
LatexEnvironmentSignature("otherlanguage*"),
LatexEnvironmentSignature("\\starttikzpicture"),
LatexEnvironmentSignature("textpos"),
LatexEnvironmentSignature("textpos*"),
LatexEnvironmentSignature("tikzpicture"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ class LatexEnvironmentSignature(
val environmentName: String?

init {
val environmentName: String? = PREFIX_REGEX.find(environmentPrototype)?.groups?.get(1)?.value
val matchResult: MatchResult? = PREFIX_REGEX.find(environmentPrototype)
val environmentName: String = matchResult?.groups?.get(1)?.value.orEmpty().ifEmpty {
matchResult?.groups?.get(2)?.value.orEmpty()
}

if (environmentName != null) {
if (environmentName.isNotEmpty()) {
this.ignoreAllArguments = false
this.environmentName = environmentName
} else {
Expand All @@ -34,6 +37,6 @@ class LatexEnvironmentSignature(
}

companion object {
private val PREFIX_REGEX = Regex("^\\\\begin\\{([^}]+)}")
private val PREFIX_REGEX = Regex("^(?:\\\\begin\\{([^}]+)}|\\\\start([A-Za-z]+))")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ class LatexAnnotatedTextBuilderTest : CodeAnnotatedTextBuilderTest("latex") {
}

@Test
fun testRsweaveMode() {
fun testRsweave() {
assertPlainText(
"""
\SweaveOpts{prefix.string=figures}
Expand Down Expand Up @@ -493,6 +493,24 @@ class LatexAnnotatedTextBuilderTest : CodeAnnotatedTextBuilderTest("latex") {
)
}

@Test
fun testContext() {
assertPlainText(
"""
This is a first sentence.
\startformula
E = mc^2
\stopformula
This is a second sentence.
""".trimIndent(),
"This is a first sentence.\n\nDummy0 \n\nThis is a second sentence. ",
"context",
)
}

private fun assertOriginalTextPositions(
code: String,
plainTextStartPos: Int,
Expand Down

0 comments on commit 5986c0b

Please sign in to comment.