From a364d6c3837c36225dcd4ec9b15ef2c192feef0b Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 16 Jun 2021 00:50:42 +0800 Subject: [PATCH] Add latex --- .gitmodules | 5 + helix-syntax/languages/tree-sitter-latex | 1 + helix-syntax/src/lib.rs | 1 + languages.toml | 9 + runtime/queries/latex/folds.scm | 12 + runtime/queries/latex/highlights.scm | 410 +++++++++++++++++++++++ 6 files changed, 438 insertions(+) create mode 160000 helix-syntax/languages/tree-sitter-latex create mode 100644 runtime/queries/latex/folds.scm create mode 100644 runtime/queries/latex/highlights.scm diff --git a/.gitmodules b/.gitmodules index 4cf9e5022..87acea607 100644 --- a/.gitmodules +++ b/.gitmodules @@ -89,3 +89,8 @@ [submodule "helix-syntax/languages/tree-sitter-nix"] path = helix-syntax/languages/tree-sitter-nix url = https://github.com/cstrahan/tree-sitter-nix + shallow = true +[submodule "helix-syntax/languages/tree-sitter-latex"] + path = helix-syntax/languages/tree-sitter-latex + url = https://github.com/latex-lsp/tree-sitter-latex + shallow = true diff --git a/helix-syntax/languages/tree-sitter-latex b/helix-syntax/languages/tree-sitter-latex new file mode 160000 index 000000000..7f720661d --- /dev/null +++ b/helix-syntax/languages/tree-sitter-latex @@ -0,0 +1 @@ +Subproject commit 7f720661de5316c0f8fee956526d4002fa1086d8 diff --git a/helix-syntax/src/lib.rs b/helix-syntax/src/lib.rs index a4706d3b3..5e3bb3ea0 100644 --- a/helix-syntax/src/lib.rs +++ b/helix-syntax/src/lib.rs @@ -80,6 +80,7 @@ mk_langs!( (Java, tree_sitter_java), (Json, tree_sitter_json), (Julia, tree_sitter_julia), + (Latex, tree_sitter_latex), (Nix, tree_sitter_nix), (Php, tree_sitter_php), (Python, tree_sitter_python), diff --git a/languages.toml b/languages.toml index b93bcb8b6..9ae7a6586 100644 --- a/languages.toml +++ b/languages.toml @@ -156,6 +156,15 @@ roots = [] indent = { tab-width = 2, unit = " " } +[[language]] +name = "latex" +scope = "source.tex" +injection-regex = "tex" +file-types = ["tex"] +roots = [] + +indent = { tab-width = 4, unit = "\t" } + # [[language]] # name = "haskell" # scope = "source.haskell" diff --git a/runtime/queries/latex/folds.scm b/runtime/queries/latex/folds.scm new file mode 100644 index 000000000..fb5c71767 --- /dev/null +++ b/runtime/queries/latex/folds.scm @@ -0,0 +1,12 @@ +[ + (chapter) + (part) + (section) + (subsection) + (subsubsection) + (paragraph) + (subparagraph) + + (environment) + (displayed_equation) +] @fold diff --git a/runtime/queries/latex/highlights.scm b/runtime/queries/latex/highlights.scm new file mode 100644 index 000000000..cd04a62cb --- /dev/null +++ b/runtime/queries/latex/highlights.scm @@ -0,0 +1,410 @@ +;; Math +[ + (displayed_equation) + (inline_formula) +] @text.math + +;; This highlights the whole environment like vimtex does +((environment + (begin + name: (word) @_env)) @text.math + (#any-of? @_env + "displaymath" "displaymath*" + "equation" "equation*" + "multline" "multline*" + "eqnarray" "eqnarray*" + "align" "align*" + "array" "array*" + "split" "split*" + "alignat" "alignat*" + "gather" "gather*" + "flalign" "flalign*")) + +[ + (generic_command_name) + "\\newcommand" + "\\renewcommand" + "\\DeclareRobustCommand" + "\\DeclareMathOperator" + "\\newglossaryentry" + "\\caption" + "\\label" + "\\newlabel" + "\\color" + "\\colorbox" + "\\textcolor" + "\\pagecolor" + "\\definecolor" + "\\definecolorset" + "\\newtheorem" + "\\declaretheorem" + "\\newacronym" +] @function.macro + +[ + "\\ref" + "\\vref" + "\\Vref" + "\\autoref" + "\\pageref" + "\\cref" + "\\Cref" + "\\cref*" + "\\Cref*" + "\\namecref" + "\\nameCref" + "\\lcnamecref" + "\\namecrefs" + "\\nameCrefs" + "\\lcnamecrefs" + "\\labelcref" + "\\labelcpageref" + "\\crefrange" + "\\crefrange" + "\\Crefrange" + "\\Crefrange" + "\\crefrange*" + "\\crefrange*" + "\\Crefrange*" + "\\Crefrange*" +] @function.macro + +[ + "\\cite" + "\\cite*" + "\\Cite" + "\\nocite" + "\\citet" + "\\citep" + "\\citet*" + "\\citep*" + "\\citeauthor" + "\\citeauthor*" + "\\Citeauthor" + "\\Citeauthor*" + "\\citetitle" + "\\citetitle*" + "\\citeyear" + "\\citeyear*" + "\\citedate" + "\\citedate*" + "\\citeurl" + "\\fullcite" + "\\citeyearpar" + "\\citealt" + "\\citealp" + "\\citetext" + "\\parencite" + "\\parencite*" + "\\Parencite" + "\\footcite" + "\\footfullcite" + "\\footcitetext" + "\\textcite" + "\\Textcite" + "\\smartcite" + "\\Smartcite" + "\\supercite" + "\\autocite" + "\\Autocite" + "\\autocite*" + "\\Autocite*" + "\\volcite" + "\\Volcite" + "\\pvolcite" + "\\Pvolcite" + "\\fvolcite" + "\\ftvolcite" + "\\svolcite" + "\\Svolcite" + "\\tvolcite" + "\\Tvolcite" + "\\avolcite" + "\\Avolcite" + "\\notecite" + "\\notecite" + "\\pnotecite" + "\\Pnotecite" + "\\fnotecite" +] @function.macro + +[ + "\\ref" + "\\vref" + "\\Vref" + "\\autoref" + "\\pageref" + "\\cref" + "\\Cref" + "\\cref*" + "\\Cref*" + "\\namecref" + "\\nameCref" + "\\lcnamecref" + "\\namecrefs" + "\\nameCrefs" + "\\lcnamecrefs" + "\\labelcref" + "\\labelcpageref" +] @function.macro + + +[ + "\\crefrange" + "\\crefrange" + "\\Crefrange" + "\\Crefrange" + "\\crefrange*" + "\\crefrange*" + "\\Crefrange*" + "\\Crefrange*" +] @function.macro + + +[ + "\\gls" + "\\Gls" + "\\GLS" + "\\glspl" + "\\Glspl" + "\\GLSpl" + "\\glsdisp" + "\\glslink" + "\\glstext" + "\\Glstext" + "\\GLStext" + "\\glsfirst" + "\\Glsfirst" + "\\GLSfirst" + "\\glsplural" + "\\Glsplural" + "\\GLSplural" + "\\glsfirstplural" + "\\Glsfirstplural" + "\\GLSfirstplural" + "\\glsname" + "\\Glsname" + "\\GLSname" + "\\glssymbol" + "\\Glssymbol" + "\\glsdesc" + "\\Glsdesc" + "\\GLSdesc" + "\\glsuseri" + "\\Glsuseri" + "\\GLSuseri" + "\\glsuserii" + "\\Glsuserii" + "\\GLSuserii" + "\\glsuseriii" + "\\Glsuseriii" + "\\GLSuseriii" + "\\glsuseriv" + "\\Glsuseriv" + "\\GLSuseriv" + "\\glsuserv" + "\\Glsuserv" + "\\GLSuserv" + "\\glsuservi" + "\\Glsuservi" + "\\GLSuservi" +] @function.macro + + +[ + "\\acrshort" + "\\Acrshort" + "\\ACRshort" + "\\acrshortpl" + "\\Acrshortpl" + "\\ACRshortpl" + "\\acrlong" + "\\Acrlong" + "\\ACRlong" + "\\acrlongpl" + "\\Acrlongpl" + "\\ACRlongpl" + "\\acrfull" + "\\Acrfull" + "\\ACRfull" + "\\acrfullpl" + "\\Acrfullpl" + "\\ACRfullpl" + "\\acs" + "\\Acs" + "\\acsp" + "\\Acsp" + "\\acl" + "\\Acl" + "\\aclp" + "\\Aclp" + "\\acf" + "\\Acf" + "\\acfp" + "\\Acfp" + "\\ac" + "\\Ac" + "\\acp" + "\\glsentrylong" + "\\Glsentrylong" + "\\glsentrylongpl" + "\\Glsentrylongpl" + "\\glsentryshort" + "\\Glsentryshort" + "\\glsentryshortpl" + "\\Glsentryshortpl" + "\\glsentryfullpl" + "\\Glsentryfullpl" +] @function.macro + +(comment) @comment + +(bracket_group) @parameter + +[(math_operator) "="] @operator + +[ + "\\usepackage" + "\\documentclass" + "\\input" + "\\include" + "\\subfile" + "\\subfileinclude" + "\\subfileinclude" + "\\includegraphics" + "\\addbibresource" + "\\bibliography" + "\\includesvg" + "\\includeinkscape" + "\\usepgflibrary" + "\\usetikzlibrary" +] @include + +[ + "\\part" + "\\chapter" + "\\section" + "\\subsection" + "\\subsubsection" + "\\paragraph" + "\\subparagraph" +] @type + +"\\item" @punctuation.special + +((word) @punctuation.delimiter +(#eq? @punctuation.delimiter "&")) + +["$" "\\[" "\\]" "\\(" "\\)"] @punctuation.delimiter + +(label_definition + name: (_) @text.reference) +(label_reference + label: (_) @text.reference) +(equation_label_reference + label: (_) @text.reference) +(label_reference + label: (_) @text.reference) +(label_number + label: (_) @text.reference) + +(citation + key: (word) @text.reference) + +(key_val_pair + key: (_) @parameter + value: (_)) + +["[" "]" "{" "}"] @punctuation.bracket ;"(" ")" is has no special meaning in LaTeX + +(chapter + text: (brace_group) @text.title) + +(part + text: (brace_group) @text.title) + +(section + text: (brace_group) @text.title) + +(subsection + text: (brace_group) @text.title) + +(subsubsection + text: (brace_group) @text.title) + +(paragraph + text: (brace_group) @text.title) + +(subparagraph + text: (brace_group) @text.title) + +((environment + (begin + name: (word) @_frame) + (brace_group + child: (text) @text.title)) + (#eq? @_frame "frame")) + +((generic_command + name:(generic_command_name) @_name + arg: (brace_group + (text) @text.title)) + (#eq? @_name "\\frametitle")) + +;; Formatting + +((generic_command + name:(generic_command_name) @_name + arg: (_) @text.emphasis) + (#eq? @_name "\\emph")) + +((generic_command + name:(generic_command_name) @_name + arg: (_) @text.emphasis) + (#match? @_name "^(\\\\textit|\\\\mathit)$")) + +((generic_command + name:(generic_command_name) @_name + arg: (_) @text.strong) + (#match? @_name "^(\\\\textbf|\\\\mathbf)$")) + +((generic_command + name:(generic_command_name) @_name + . + arg: (_) @text.uri) + (#match? @_name "^(\\\\url|\\\\href)$")) + +(ERROR) @error + +[ + "\\begin" + "\\end" +] @text.environment + +(begin + name: (_) @text.environment.name + (#not-any-of? @text.environment.name + "displaymath" "displaymath*" + "equation" "equation*" + "multline" "multline*" + "eqnarray" "eqnarray*" + "align" "align*" + "array" "array*" + "split" "split*" + "alignat" "alignat*" + "gather" "gather*" + "flalign" "flalign*")) + +(end + name: (_) @text.environment.name + (#not-any-of? @text.environment.name + "displaymath" "displaymath*" + "equation" "equation*" + "multline" "multline*" + "eqnarray" "eqnarray*" + "align" "align*" + "array" "array*" + "split" "split*" + "alignat" "alignat*" + "gather" "gather*" + "flalign" "flalign*"))