From b929979057f805b1cae4880022f47ca271eaa873 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Mon, 19 Aug 2024 00:06:33 +0000 Subject: [PATCH 1/6] 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 12b0ad049c04..dcb29af9a383 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 000000000000..4589f135d8f4 --- /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 From 5d567dc0c6d7d754fd38bb919ae4e7caaa6efd86 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Mon, 19 Aug 2024 01:51:52 +0000 Subject: [PATCH 2/6] Add c3i file to support Libraries --- languages.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/languages.toml b/languages.toml index dcb29af9a383..c25d852b1b19 100644 --- a/languages.toml +++ b/languages.toml @@ -425,7 +425,7 @@ source = { git = "https://github.com/c3lang/tree-sitter-c3.git" , rev = "790a032 [[language]] name = "c3" scope = "source.c3" -file-types = [ "c3" ] +file-types = [ "c3" "c3i" ] roots = [ "project.json" ] comment-token = "//" block-comment-tokens = { start = "/*", end = "*/" } From ce99422f9995a2602b22a3f98d0d7f1be17567e8 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Mon, 19 Aug 2024 14:33:57 +0200 Subject: [PATCH 3/6] Fix missing comma --- languages.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/languages.toml b/languages.toml index c25d852b1b19..4f5272476020 100644 --- a/languages.toml +++ b/languages.toml @@ -425,7 +425,7 @@ source = { git = "https://github.com/c3lang/tree-sitter-c3.git" , rev = "790a032 [[language]] name = "c3" scope = "source.c3" -file-types = [ "c3" "c3i" ] +file-types = [ "c3", "c3i" ] roots = [ "project.json" ] comment-token = "//" block-comment-tokens = { start = "/*", end = "*/" } @@ -3801,4 +3801,4 @@ indent = { tab-width = 2, unit = " " } [[grammar]] name = "thrift" -source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-thrift" , rev = "68fd0d80943a828d9e6f49c58a74be1e9ca142cf" } \ No newline at end of file +source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-thrift" , rev = "68fd0d80943a828d9e6f49c58a74be1e9ca142cf" } From b39b3e5dd21bf2ebe817fcf1d4eab25762608231 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Wed, 21 Aug 2024 15:21:54 +0000 Subject: [PATCH 4/6] Fix Wrong Scopes --- runtime/queries/c3/highlights.scm | 80 +++++++++++++++---------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/runtime/queries/c3/highlights.scm b/runtime/queries/c3/highlights.scm index 4589f135d8f4..6e808ea46762 100644 --- a/runtime/queries/c3/highlights.scm +++ b/runtime/queries/c3/highlights.scm @@ -6,27 +6,27 @@ ;; Constant (const_ident) @constant -["true" "false"] @boolean +["true" "false"] @constant.builtin.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)))) +(field_expr field: (access_ident (ident) @variable.other.member)) +(struct_member_declaration (ident) @variable.other.member) +(struct_member_declaration (identifier_list (ident) @variable.other.member)) +(bitstruct_member_declaration (ident) @variable.other.member) +(initializer_list (arg (param_path (param_path_element (ident) @variable.other.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) +(global_declaration (ident) @variable) +(local_decl_after_type name: [(ident) (ct_ident)] @variable.other.member) +(var_decl name: [(ident) (ct_ident)] @variable) +(try_unwrap (ident) @variable) +(catch_unwrap (ident) @variable) ;; Keyword (from `c3c --list-keywords`) [ @@ -86,9 +86,9 @@ "fn" @keyword.function "macro" @keyword.function -"return" @keyword.return -"import" @keyword.import -"module" @keyword.module +"return" @keyword.control.return +"import" @keyword.control.import +"module" @keyword.storage.type [ "bitstruct" @@ -99,7 +99,7 @@ "interface" "struct" "union" -] @keyword.type +] @keyword.storage.type [ "case" @@ -108,7 +108,7 @@ "if" "nextcase" "switch" -] @keyword.conditional +] @keyword.control.conditional [ "break" @@ -118,7 +118,7 @@ "foreach" "foreach_r" "while" -] @keyword.repeat +] @keyword.control.repeat [ "const" @@ -126,7 +126,7 @@ "inline" "static" "tlocal" -] @keyword.modifier +] @keyword.storage.modifier ;; Operator (from `c3c --list-operators`) [ @@ -190,35 +190,35 @@ "..." "<<=" ">>=" -] @operator +] @keyword.operator -(range_expr ":" @operator) -(foreach_cond ":" @operator) +(range_expr ":" @keyword.operator) +(foreach_cond ":" @keyword.operator) (ternary_expr [ "?" ":" - ] @keyword.conditional.ternary) + ] @keyword.control.conditional) (elvis_orelse_expr [ "?:" "??" - ] @keyword.conditional.ternary) + ] @keyword.control.conditional) ;; Literal -(integer_literal) @number -(real_literal) @number.float -(char_literal) @character -(bytes_literal) @number +(integer_literal) @type.builtin +(real_literal) @type.builtin +(char_literal) @type.builtin +(bytes_literal) @type.builtin ;; String (string_literal) @string (raw_string_literal) @string ;; Escape Sequence -(escape_sequence) @string.escape +(escape_sequence) @string ;; Builtin (constants) ((builtin) @constant.builtin (#match? @constant.builtin "_*[A-Z][_A-Z0-9]*")) @@ -256,12 +256,12 @@ [ (label) (label_target) -] @label +] @keyword.storage.type ;; Module -(module_resolution (ident) @module) -(module (path_ident (ident) @module)) -(import_declaration (path_ident (ident) @module)) +(module_resolution (ident) @keyword.storage.type) +(module (path_ident (ident) @keyword.storage.type)) +(import_declaration (path_ident (ident) @keyword.storage.type)) ;; Attribute (attribute name: (_) @attribute) @@ -284,13 +284,13 @@ (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 +(call_expr function: [(ident) (at_ident)] @function) +(call_expr function: [(builtin)] @function.builtin) +(call_expr function: (module_ident_expr ident: (_) @function)) +(call_expr function: (trailing_generic_expr argument: (module_ident_expr ident: (_) @function))) +(call_expr function: (field_expr field: (access_ident [(ident) (at_ident)] @function.method))) ; 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))) +(call_expr function: (type_access_expr field: (access_ident [(ident) (at_ident)] @function.method))) ;; Assignment ;; (assignment_expr left: (ident) @variable.member) @@ -318,6 +318,6 @@ [ (line_comment) (block_comment) -] @comment @spell +] @comment -(doc_comment) @comment.documentation @spell +(doc_comment) @comment.block.documentation From 1873b02c637dd67e6580eb5ecb3d55a450f327c2 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Wed, 21 Aug 2024 23:05:54 +0000 Subject: [PATCH 5/6] Fixed for grammar not being able to fetch. Implementation of indent and textobjects querry for c3. --- languages.toml | 2 +- runtime/queries/c3/indents.scm | 13 +++++++++++++ runtime/queries/c3/textobjects.scm | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 runtime/queries/c3/indents.scm create mode 100644 runtime/queries/c3/textobjects.scm diff --git a/languages.toml b/languages.toml index 4f5272476020..0465a25221fa 100644 --- a/languages.toml +++ b/languages.toml @@ -420,7 +420,7 @@ source = { git = "https://github.com/sogaiu/tree-sitter-janet-simple", rev = "51 [[grammar]] name = "c3" -source = { git = "https://github.com/c3lang/tree-sitter-c3.git" , rev = "790a0326833cd647e00d8dec01268aa1ec2e3bdb" } +source = { git = "https://github.com/c3lang/tree-sitter-c3" , rev = "790a0326833cd647e00d8dec01268aa1ec2e3bdb" } [[language]] name = "c3" diff --git a/runtime/queries/c3/indents.scm b/runtime/queries/c3/indents.scm new file mode 100644 index 000000000000..901abf38de06 --- /dev/null +++ b/runtime/queries/c3/indents.scm @@ -0,0 +1,13 @@ +[ + (func_definition) + (if_stmt) + (enum_declaration) + (for_stmt) + (while_stmt) + (case_stmt) + (macro_declaration) +] @indent + +[ + "}" +] @outdent diff --git a/runtime/queries/c3/textobjects.scm b/runtime/queries/c3/textobjects.scm new file mode 100644 index 000000000000..8c0a3d514539 --- /dev/null +++ b/runtime/queries/c3/textobjects.scm @@ -0,0 +1,17 @@ +(func_definition + body: (_) @function.inside) @function.around + +(struct_declaration + body: (_) @class.inside) @class.around + +(enum_declaration + body: (_) @class.inside) @class.around + +(fn_parameter_list + ((_) @parameter.inside . ","? @parameter.around) @parameter.around) + +(line_comment) @comment.inside +(line_comment)+ @comment.around + +(doc_comment) @comment.inside +(doc_comment)+ @comment.outside From b7b8d9f67298b027d6e128f2c17b289eb600d8b2 Mon Sep 17 00:00:00 2001 From: TheZombie1999 Date: Sat, 24 Aug 2024 16:12:04 +0000 Subject: [PATCH 6/6] Add language support documentation --- book/src/generated/lang-support.md | 1 + 1 file changed, 1 insertion(+) diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index cb1c815f2150..e123f47caffe 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -15,6 +15,7 @@ | blueprint | ✓ | | | `blueprint-compiler` | | c | ✓ | ✓ | ✓ | `clangd` | | c-sharp | ✓ | ✓ | | `OmniSharp` | +| c3 | ✓ | ✓ | ✓ | `c3-lsp` | | cabal | | | | `haskell-language-server-wrapper` | | cairo | ✓ | ✓ | ✓ | `cairo-language-server` | | capnp | ✓ | | ✓ | |