From c850b90f677eee731995765fb04532746b7d408e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thor=20=F0=9F=AA=81?=
 <7041313+thor314@users.noreply.github.com>
Date: Sat, 21 Sep 2024 18:13:02 +0100
Subject: [PATCH] add circom tree-sitter, syntax-highlighting, and lsp support
 (#11676)

* add circom tree-sitter and lsp support

* add circom syntax highlighting queries

* cargo xtask docgen

* updated highlights to reflect helix themes typing

* bugfix: ~= operator causing issues

* minor adjustment: add = and ; operator and delimiter
---
 book/src/generated/lang-support.md    |   1 +
 languages.toml                        |  17 +++
 runtime/queries/circom/highlights.scm | 142 ++++++++++++++++++++++++++
 runtime/queries/circom/locals.scm     |   9 ++
 4 files changed, 169 insertions(+)
 create mode 100644 runtime/queries/circom/highlights.scm
 create mode 100644 runtime/queries/circom/locals.scm

diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md
index f223c8b2..8a8c9fa8 100644
--- a/book/src/generated/lang-support.md
+++ b/book/src/generated/lang-support.md
@@ -19,6 +19,7 @@
 | cairo | ✓ | ✓ | ✓ | `cairo-language-server` |
 | capnp | ✓ |  | ✓ |  |
 | cel | ✓ |  |  |  |
+| circom | ✓ |  |  | `circom-lsp` |
 | clojure | ✓ |  |  | `clojure-lsp` |
 | cmake | ✓ | ✓ | ✓ | `cmake-language-server` |
 | comment | ✓ |  |  |  |
diff --git a/languages.toml b/languages.toml
index 9e1be0ac..cf1d5ae1 100644
--- a/languages.toml
+++ b/languages.toml
@@ -16,6 +16,7 @@ bicep-langserver = { command = "bicep-langserver" }
 bitbake-language-server = { command = "bitbake-language-server" }
 bufls = { command = "bufls", args = ["serve"] }
 cairo-language-server = { command = "cairo-language-server", args = [] }
+circom-lsp = { command = "circom-lsp" }
 cl-lsp = { command = "cl-lsp", args = [ "stdio" ] }
 clangd = { command = "clangd" }
 clojure-lsp = { command = "clojure-lsp" }
@@ -3788,3 +3789,19 @@ indent = { tab-width = 2, unit = "  " }
 [[grammar]]
 name = "thrift"
 source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-thrift" , rev = "68fd0d80943a828d9e6f49c58a74be1e9ca142cf" }
+
+[[language]]
+name             = "circom"
+scope            = "source.circom"
+injection-regex  = "circom"
+file-types       = ["circom"]
+roots            = ["package.json"]
+comment-tokens   = "//"
+indent           = { tab-width = 4, unit = "    " }
+auto-format      = false
+language-servers = ["circom-lsp"]
+
+[[grammar]]
+name   = "circom"
+source = { git = "https://github.com/Decurity/tree-sitter-circom", rev = "02150524228b1e6afef96949f2d6b7cc0aaf999e" }
+
diff --git a/runtime/queries/circom/highlights.scm b/runtime/queries/circom/highlights.scm
new file mode 100644
index 00000000..1d310bd8
--- /dev/null
+++ b/runtime/queries/circom/highlights.scm
@@ -0,0 +1,142 @@
+; identifiers
+; -----------
+(identifier) @variable
+
+; Pragma
+; -----------
+(pragma_directive) @keyword.directive
+
+; Include
+; -----------
+(include_directive) @keyword.directive
+
+; Literals
+; --------
+(string) @string
+(int_literal) @constant.numeric.integer
+(comment) @comment
+
+; Definitions
+; -----------
+(function_definition
+  name:  (identifier) @keyword.function)
+
+(template_definition
+  name:  (identifier) @keyword.function)
+
+; Use contructor coloring for special functions
+(main_component_definition) @constructor
+
+; Invocations
+(call_expression . (identifier) @function)
+
+; Function parameters
+(parameter name: (identifier) @variable.parameter)
+
+; Members
+(member_expression property: (property_identifier) @variable.other.member)
+
+; Tokens
+; -------
+
+; Keywords
+[
+ "signal"
+ "var"
+ "component"
+] @keyword.storage.type
+
+[  "include" ] @keyword.control.import
+
+[
+ "public"
+ "input"
+ "output"
+ ] @keyword.storage.modifier
+
+[
+ "for"
+ "while"
+] @keyword.control.repeat
+
+[
+ "if"
+ "else"
+] @keyword.control.conditional
+
+[
+ "return"
+] @keyword.control.return
+
+[
+  "function"
+  "template"
+] @keyword.function
+
+; Punctuation
+[
+  "("
+  ")"
+  "["
+  "]"
+  "{"
+  "}"
+] @punctuation.bracket
+
+[
+  "."
+  ","
+  ";"
+] @punctuation.delimiter
+
+; Operators
+; https://docs.circom.io/circom-language/basic-operators
+[
+  "="
+  "?"
+  "&&"
+  "||"
+  "!"
+  "<" 
+  ">" 
+  "<=" 
+  ">=" 
+  "==" 
+  "!=" 
+  "+"
+  "-"
+  "*"
+  "**"
+  "/"
+  "\\"
+  "%"
+  "+="
+  "-="
+  "*="
+  "**="
+  "/="
+  "\\="
+  "%="
+  "++"
+  "--"
+  "&"
+  "|"
+  "~"
+  "^"
+  ">>"
+  "<<"
+  "&="
+  "|="
+  ; "\~=" ; bug, uncomment and circom will not highlight
+  "^="
+  ">>="
+  "<<="
+] @operator
+
+[
+  "<=="
+  "==>"
+  "<--"
+  "-->"
+  "==="
+] @operator
diff --git a/runtime/queries/circom/locals.scm b/runtime/queries/circom/locals.scm
new file mode 100644
index 00000000..e0ea12de
--- /dev/null
+++ b/runtime/queries/circom/locals.scm
@@ -0,0 +1,9 @@
+(function_definition) @local.scope
+(template_definition) @local.scope
+(main_component_definition) @local.scope
+(block_statement) @local.scope
+
+(parameter name: (identifier) @local.definition) @local.definition
+
+
+(identifier) @local.reference
\ No newline at end of file