Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add c3 language support to helix #11521

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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", "c3i" ]
roots = [ "project.json" ]
comment-token = "//"
block-comment-tokens = { start = "/*", end = "*/" }
indent = { tab-width = 4, unit=" "}
language-servers = [ "c3-lsp" ]
grammar = "c3"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grammar = "c3"

This key can be omitted since by default it matches the name of the language


[[language]]
name = "json"
scope = "source.json"
Expand Down Expand Up @@ -3785,4 +3801,4 @@ indent = { tab-width = 2, unit = " " }

[[grammar]]
name = "thrift"
source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-thrift" , rev = "68fd0d80943a828d9e6f49c58a74be1e9ca142cf" }
source = { git = "https://github.com/tree-sitter-grammars/tree-sitter-thrift" , rev = "68fd0d80943a828d9e6f49c58a74be1e9ca142cf" }
323 changes: 323 additions & 0 deletions runtime/queries/c3/highlights.scm
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the scopes in this file need fixing https://docs.helix-editor.com/themes.html#syntax-highlighting

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a good way to debug these scopes ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the :tree-sitter-highlight-name command to show the capture name under the cursor


;; 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
Loading