Skip to content

Commit

Permalink
tree-sitter-solidity: Use upstream, update queries
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer authored and jdrst committed Sep 13, 2022
1 parent ed1de6f commit c567114
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 53 deletions.
2 changes: 1 addition & 1 deletion languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ language-server = { command = "solc", args = ["--lsp"] }

[[grammar]]
name = "solidity"
source = { git = "https://github.com/slinlee/tree-sitter-solidity", rev = "f3a002274744e859bf64cf3524985f8c31ea84fd" }
source = { git = "https://github.com/JoranHonig/tree-sitter-solidity", rev = "9004b86531cb424bd379424cf7266a4585f2af7d" }

[[language]]
name = "gleam"
Expand Down
134 changes: 82 additions & 52 deletions runtime/queries/solidity/highlights.scm
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
(comment) @comment
; identifiers
; -----------
(identifier) @variable
(yul_identifier) @variable

; Pragma
(pragma_directive) @tag
(solidity_version_comparison_operator ">=" @tag)
(solidity_version_comparison_operator "<=" @tag)
(solidity_version_comparison_operator "=" @tag)
(solidity_version_comparison_operator "~" @tag)
(solidity_version_comparison_operator "^" @tag)
(solidity_version_comparison_operator _ @tag)


; Literals
; --------

[
(string)
(hex_string_literal)
Expand All @@ -26,26 +27,38 @@
(false)
] @constant.builtin

(comment) @comment


; Definitions and references
; -----------

; Type
(type_name) @type
(primitive_type) @type
(struct_declaration struct_name: (identifier) @type)
(enum_declaration enum_type_name: (identifier) @type)
(user_defined_type (identifier) @type)

; Color payable in payable address conversion as type and not as keyword
(payable_conversion_expression "payable" @type)
(emit_statement . (identifier) @type)
; Handles ContractA, ContractB in function foo() override(ContractA, contractB) {}
(override_specifier (identifier) @type)
; Ensures that delimiters in mapping( ... => .. ) are not colored like types
(type_name "(" @punctuation.bracket "=>" @punctuation.delimiter ")" @punctuation.bracket)



; Functions and parameters
; Definitions
(struct_declaration
name: (identifier) @type)
(enum_declaration
name: (identifier) @type)
(contract_declaration
name: (identifier) @type)
(library_declaration
name: (identifier) @type)
(interface_declaration
name: (identifier) @type)
(event_definition
name: (identifier) @type)

(function_definition
function_name: (identifier) @function)
name: (identifier) @function)

(modifier_definition
name: (identifier) @function)
(yul_evm_builtin) @function.builtin
Expand All @@ -55,37 +68,38 @@
(fallback_receive_definition "receive" @constructor)
(fallback_receive_definition "fallback" @constructor)

(modifier_invocation (identifier) @function)
(struct_member name: (identifier) @variable.other.member)
(enum_value) @constant

; Handles expressions like structVariable.g();
(call_expression . (member_expression (property_identifier) @function.method))
; Invocations
(emit_statement . (identifier) @type)
(modifier_invocation (identifier) @function)

; Handles expressions like g();
(call_expression . (member_expression property: (identifier) @function.method))
(call_expression . (identifier) @function)

; Function parameters
(event_paramater name: (identifier) @variable.parameter) ; TODO fix spelling once fixed upstream
(function_definition
function_name: (identifier) @variable.parameter)
(call_struct_argument name: (identifier) @field)
(event_paramater name: (identifier) @variable.parameter)
(parameter name: (identifier) @variable.parameter)

; Yul functions
(yul_function_call function: (yul_identifier) @function)

; Yul function parameters
(yul_function_definition . (yul_identifier) @function (yul_identifier) @variable.parameter)

(meta_type_expression "type" @keyword)

(member_expression (property_identifier) @variable.other.member)
(property_identifier) @variable.other.member
(struct_expression ((identifier) @variable.other.member . ":"))
(enum_value) @variable.other.member
; Structs and members
(member_expression property: (identifier) @variable.other.member)
(struct_expression type: ((identifier) @type .))
(struct_field_assignment name: (identifier) @variable.other.member)

; Tokens
; -------

; Keywords
(meta_type_expression "type" @keyword)
[
"pragma"
"import"
"contract"
"interface"
"library"
Expand All @@ -95,19 +109,6 @@
"event"
"using"
"assembly"
"switch"
"case"
"default"
"break"
"continue"
"if"
"else"
"for"
"while"
"do"
"try"
"catch"
"return"
"emit"
"public"
"internal"
Expand All @@ -117,20 +118,47 @@
"view"
"payable"
"modifier"
"returns"
"memory"
"storage"
"calldata"
"function"
"var"
(constant)
"constant"
(virtual)
(override_specifier)
(yul_leave)
] @keyword

(import_directive "as" @keyword)
(import_directive "from" @keyword)
[
"for"
"while"
"do"
] @keyword.control.repeat

[
"break"
"continue"
"if"
"else"
"switch"
"case"
"default"
] @keyword.control.conditional

[
"try"
"catch"
] @keyword.control.exception

[
"return"
"returns"
] @keyword.control.return

"function" @keyword.function

"import" @keyword.control.import
(import_directive "as" @keyword.control.import)
(import_directive "from" @keyword.control.import)
(event_paramater "indexed" @keyword) ; TODO fix spelling once fixed upstream

; Punctuation
Expand All @@ -142,7 +170,7 @@
"]"
"{"
"}"
] @punctuation.bracket
] @punctuation.bracket


[
Expand Down Expand Up @@ -185,5 +213,7 @@
"--"
] @operator

(identifier) @variable
(yul_identifier) @variable
[
"delete"
"new"
] @keyword.operator
9 changes: 9 additions & 0 deletions runtime/queries/solidity/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function_definition) @local.scope
(block_statement) @local.scope

(function_definition (parameter name: (identifier) @local.definition))

; still have to support tuple assignments
(assignment_expression left: (identifier) @local.definition)

(identifier) @local.reference

0 comments on commit c567114

Please sign in to comment.