From b929979057f805b1cae4880022f47ca271eaa873 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Mon, 19 Aug 2024 00:06:33 +0000 Subject: [PATCH] Add c3 language support to helix --- languages.toml | 16 ++ runtime/queries/c3/highlights.scm | 323 ++++++++++++++++++++++++++++++ 2 files changed, 339 insertions(+) create mode 100644 runtime/queries/c3/highlights.scm diff --git a/languages.toml b/languages.toml index 12b0ad049..dcb29af9a 100644 --- a/languages.toml +++ b/languages.toml @@ -24,6 +24,7 @@ crystalline = { command = "crystalline", args = ["--stdio"] } cs = { command = "cs", args = ["launch", "--contrib", "smithy-language-server", "--", "0"] } csharp-ls = { command = "csharp-ls" } cuelsp = { command = "cuelsp" } +c3-lsp = { command = "c3-lsp" } dart = { command = "dart", args = ["language-server", "--client-id=helix"] } dhall-lsp-server = { command = "dhall-lsp-server" } docker-langserver = { command = "docker-langserver", args = ["--stdio"] } @@ -417,6 +418,21 @@ grammar = "janet-simple" name = "janet-simple" source = { git = "https://github.com/sogaiu/tree-sitter-janet-simple", rev = "51271e260346878e1a1aa6c506ce6a797b7c25e2" } +[[grammar]] +name = "c3" +source = { git = "https://github.com/c3lang/tree-sitter-c3.git" , rev = "790a0326833cd647e00d8dec01268aa1ec2e3bdb" } + +[[language]] +name = "c3" +scope = "source.c3" +file-types = [ "c3" ] +roots = [ "project.json" ] +comment-token = "//" +block-comment-tokens = { start = "/*", end = "*/" } +indent = { tab-width = 4, unit=" "} +language-servers = [ "c3-lsp" ] +grammar = "c3" + [[language]] name = "json" scope = "source.json" diff --git a/runtime/queries/c3/highlights.scm b/runtime/queries/c3/highlights.scm new file mode 100644 index 000000000..4589f135d --- /dev/null +++ b/runtime/queries/c3/highlights.scm @@ -0,0 +1,323 @@ +;; NOTE In this file later patterns are assumed to have priority! + +;; Punctuation +["(" ")" "[" "]" "{" "}" "(<" ">)" "[<" ">]" "{|" "|}"] @punctuation.bracket +[";" "," ":" "::"] @punctuation.delimiter + +;; Constant +(const_ident) @constant +["true" "false"] @boolean +["null"] @constant.builtin + +;; Variable +[(ident) (ct_ident)] @variable +;; 1) Member +(field_expr field: (access_ident (ident) @variable.member)) +(struct_member_declaration (ident) @variable.member) +(struct_member_declaration (identifier_list (ident) @variable.member)) +(bitstruct_member_declaration (ident) @variable.member) +(initializer_list (arg (param_path (param_path_element (ident) @variable.member)))) +;; 2) Parameter +(parameter name: (_) @variable.parameter) +(call_invocation (arg (param_path (param_path_element [(ident) (ct_ident)] @variable.parameter)))) +(enum_param_declaration (ident) @variable.parameter) +;; 3) Declaration +(global_declaration (ident) @variable.declaration) +(local_decl_after_type name: [(ident) (ct_ident)] @variable.declaration) +(var_decl name: [(ident) (ct_ident)] @variable.declaration) +(try_unwrap (ident) @variable.declaration) +(catch_unwrap (ident) @variable.declaration) + +;; Keyword (from `c3c --list-keywords`) +[ + "assert" + "asm" + "catch" + "defer" + "try" + "var" +] @keyword + +[ + "$alignof" + "$and" + "$append" + "$assert" + "$assignable" + "$case" + "$concat" + "$default" + "$defined" + "$echo" + "$else" + "$embed" + "$endfor" + "$endforeach" + "$endif" + "$endswitch" + "$eval" + "$evaltype" + "$error" + "$exec" + "$extnameof" + "$feature" + "$for" + "$foreach" + "$if" + "$include" + "$is_const" + "$nameof" + "$offsetof" + "$or" + "$qnameof" + "$sizeof" + "$stringify" + "$switch" + "$typefrom" + "$typeof" + "$vacount" + "$vatype" + "$vaconst" + "$varef" + "$vaarg" + "$vaexpr" + "$vasplat" +] @keyword.directive + +"fn" @keyword.function +"macro" @keyword.function +"return" @keyword.return +"import" @keyword.import +"module" @keyword.module + +[ + "bitstruct" + "def" + "distinct" + "enum" + "fault" + "interface" + "struct" + "union" +] @keyword.type + +[ + "case" + "default" + "else" + "if" + "nextcase" + "switch" +] @keyword.conditional + +[ + "break" + "continue" + "do" + "for" + "foreach" + "foreach_r" + "while" +] @keyword.repeat + +[ + "const" + "extern" + "inline" + "static" + "tlocal" +] @keyword.modifier + +;; Operator (from `c3c --list-operators`) +[ + "&" + "!" + "~" + "|" + "^" + ;; ":" + ;; "," + ;; ";" + "=" + ">" + "/" + "." + ;; "#" + "<" + ;; "{" + ;; "[" + ;; "(" + "-" + "%" + "+" + "?" + ;; "}" + ;; "]" + ;; ")" + "*" + ;; "_" + "&&" + ;; "->" + "!!" + "&=" + "|=" + "^=" + "/=" + ".." + "?:" + "==" + ">=" + "=>" + "<=" + ;; "{|" + ;; "(<" + ;; "[<" + "-=" + "--" + "%=" + "*=" + "!=" + "||" + "+=" + "++" + ;; "|}" + ;; ">)" + ;; ">]" + "??" + ;; "::" + "<<" + ">>" + "..." + "<<=" + ">>=" +] @operator + +(range_expr ":" @operator) +(foreach_cond ":" @operator) + +(ternary_expr + [ + "?" + ":" + ] @keyword.conditional.ternary) + +(elvis_orelse_expr + [ + "?:" + "??" + ] @keyword.conditional.ternary) + +;; Literal +(integer_literal) @number +(real_literal) @number.float +(char_literal) @character +(bytes_literal) @number + +;; String +(string_literal) @string +(raw_string_literal) @string + +;; Escape Sequence +(escape_sequence) @string.escape + +;; Builtin (constants) +((builtin) @constant.builtin (#match? @constant.builtin "_*[A-Z][_A-Z0-9]*")) + +;; Type Property (from `c3c --list-type-properties`) +(type_access_expr (access_ident [(ident) "typeid"] @variable.builtin + (#any-of? @variable.builtin + "alignof" + "associated" + "elements" + "extnameof" + "inf" + "is_eq" + "is_ordered" + "is_substruct" + "len" + "max" + "membersof" + "min" + "nan" + "inner" + "kindof" + "names" + "nameof" + "params" + "parentof" + "qnameof" + "returns" + "sizeof" + "values" + ;; Extra token + "typeid"))) + +;; Label +[ + (label) + (label_target) +] @label + +;; Module +(module_resolution (ident) @module) +(module (path_ident (ident) @module)) +(import_declaration (path_ident (ident) @module)) + +;; Attribute +(attribute name: (_) @attribute) +(define_attribute name: (_) @attribute) +(call_inline_attributes (at_ident) @attribute) +(asm_block_stmt (at_ident) @attribute) + +;; Type +[ + (type_ident) + (ct_type_ident) +] @type +(base_type_name) @type.builtin + +;; Function Definition +(func_header name: (_) @function) +(func_header method_type: (_) name: (_) @function.method) +;; NOTE macro_declaration can also have a func_header +(macro_header name: (_) @function) +(macro_header method_type: (_) name: (_) @function.method) + +;; Function Call +(call_expr function: [(ident) (at_ident)] @function.call) +(call_expr function: [(builtin)] @function.builtin.call) +(call_expr function: (module_ident_expr ident: (_) @function.call)) +(call_expr function: (trailing_generic_expr argument: (module_ident_expr ident: (_) @function.call))) +(call_expr function: (field_expr field: (access_ident [(ident) (at_ident)] @function.method.call))) ; NOTE Ambiguous, could be calling a method or function pointer +;; Method on type +(call_expr function: (type_access_expr field: (access_ident [(ident) (at_ident)] @function.method.call))) + +;; Assignment +;; (assignment_expr left: (ident) @variable.member) +;; (assignment_expr left: (module_ident_expr (ident) @variable.member)) +;; (assignment_expr left: (field_expr field: (_) @variable.member)) +;; (assignment_expr left: (unary_expr operator: "*" @variable.member)) +;; (assignment_expr left: (subscript_expr ["[" "]"] @variable.member)) + +;; (update_expr argument: (ident) @variable.member) +;; (update_expr argument: (module_ident_expr ident: (ident) @variable.member)) +;; (update_expr argument: (field_expr field: (_) @variable.member)) +;; (update_expr argument: (unary_expr operator: "*" @variable.member)) +;; (update_expr argument: (subscript_expr ["[" "]"] @variable.member)) + +;; (unary_expr operator: ["--" "++"] argument: (ident) @variable.member) +;; (unary_expr operator: ["--" "++"] argument: (module_ident_expr (ident) @variable.member)) +;; (unary_expr operator: ["--" "++"] argument: (field_expr field: (access_ident (ident)) @variable.member)) +;; (unary_expr operator: ["--" "++"] argument: (subscript_expr ["[" "]"] @variable.member)) + +;; Asm +(asm_instr [(ident) "int"] @function.builtin) +(asm_expr [(ct_ident) (ct_const_ident)] @variable.builtin) + +;; Comment +[ + (line_comment) + (block_comment) +] @comment @spell + +(doc_comment) @comment.documentation @spell