From f4a49f45da5ca1ee93fafa136a50da56df4d3dd6 Mon Sep 17 00:00:00 2001 From: Christoph Horn Date: Mon, 22 Aug 2022 14:22:48 +0200 Subject: [PATCH 1/5] Julia queries: update grammar, fix breaking changes --- languages.toml | 2 +- runtime/queries/julia/highlights.scm | 23 ++++++++++++++--------- runtime/queries/julia/injections.scm | 11 +++++++---- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/languages.toml b/languages.toml index 5772be479ca3..acc87772b71c 100644 --- a/languages.toml +++ b/languages.toml @@ -571,7 +571,7 @@ indent = { tab-width = 4, unit = " " } [[grammar]] name = "julia" -source = { git = "https://github.com/tree-sitter/tree-sitter-julia", rev = "12ea597262125fc22fd2e91aa953ac69b19c26ca" } +source = { git = "https://github.com/tree-sitter/tree-sitter-julia", rev = "fc60b7cce87da7a1b7f8cb0f9371c3dc8b684500" } [[language]] name = "java" diff --git a/runtime/queries/julia/highlights.scm b/runtime/queries/julia/highlights.scm index 7c4479853474..318488888a63 100644 --- a/runtime/queries/julia/highlights.scm +++ b/runtime/queries/julia/highlights.scm @@ -1,10 +1,7 @@ -[ - (triple_string) - (string) -] @string +(string_literal) @string -(string +(prefixed_string_literal prefix: (identifier) @constant.builtin) (macro_identifier) @function.macro @@ -80,7 +77,9 @@ (struct_definition name: (identifier) @type) -(number) @constant.numeric.integer +(integer_literal) @constant.numeric.integer +(float_literal) @constant.numeric.float + (range_expression (identifier) @constant.numeric.integer (eq? @constant.numeric.integer "end")) @@ -89,7 +88,10 @@ (identifier) @constant.numeric.integer (eq? @constant.numeric.integer "end"))) (coefficient_expression - (number) + [ + (integer_literal) + (float_literal) + ] (identifier) @constant.builtin) ;; TODO: operators. @@ -117,7 +119,10 @@ (function_definition ["function" "end"] @keyword.function) -(comment) @comment +[ + (line_comment) + (block_comment) +] @comment [ "const" @@ -171,7 +176,7 @@ (((identifier) @constant.builtin.boolean) (#eq? @constant.builtin.boolean "false")) -["::" ":" "." "," "..." "!"] @punctuation.delimiter +["::" ":" "." "," "..."] @punctuation.delimiter ["[" "]" "(" ")" "{" "}"] @punctuation.bracket ["="] @operator diff --git a/runtime/queries/julia/injections.scm b/runtime/queries/julia/injections.scm index 1c1e804ec84b..3eab231faac3 100644 --- a/runtime/queries/julia/injections.scm +++ b/runtime/queries/julia/injections.scm @@ -1,5 +1,8 @@ -((triple_string) @injection.content - (#set! injection.language "markdown")) +; ((string_literal) @injection.content +; (#set! injection.language "markdown")) -((comment) @injection.content - (#set! injection.language "comment")) +; ((line_comment) @injection.content +; (#set! injection.language "comment")) + +; ((block_comment) @injection.content +; (#set! injection.language "comment")) From 9f12c7ac3fbc9a852afd1aed3bdeb6f153c1d69a Mon Sep 17 00:00:00 2001 From: Christoph Horn Date: Mon, 22 Aug 2022 18:13:21 +0200 Subject: [PATCH 2/5] Julia queries: major refactoring of `highlights.scm`, add regex injections, locals --- runtime/queries/julia/highlights.scm | 325 ++++++++++++++++----------- runtime/queries/julia/injections.scm | 20 +- runtime/queries/julia/locals.scm | 82 +++---- 3 files changed, 238 insertions(+), 189 deletions(-) diff --git a/runtime/queries/julia/highlights.scm b/runtime/queries/julia/highlights.scm index 318488888a63..c4f6999ac4ca 100644 --- a/runtime/queries/julia/highlights.scm +++ b/runtime/queries/julia/highlights.scm @@ -1,112 +1,142 @@ +; ---------- +; Primitives +; ---------- + +[ + (line_comment) + (block_comment) +] @comment + +(((identifier) @constant.builtin) (match? @constant.builtin "^(nothing|missing|Inf|NaN)$")) +(((identifier) @constant.builtin.boolean) (#eq? @constant.builtin.boolean "true")) +(((identifier) @constant.builtin.boolean) (#eq? @constant.builtin.boolean "false")) + +(integer_literal) @constant.numeric.integer +(float_literal) @constant.numeric.float + +(character_literal) @constant.character +(escape_sequence) @constant.character.escape (string_literal) @string (prefixed_string_literal - prefix: (identifier) @constant.builtin) + prefix: (identifier) @function.macro) @string -(macro_identifier) @function.macro -(macro_identifier (identifier) @function.macro) ; for any one using the variable highlight -(macro_definition - name: (identifier) @function.macro - ["macro" "end" @keyword]) +(quote_expression + (identifier) @string.special.symbol) + +; ------------------- +; Modules and Imports +; ------------------- + +(module_definition + name: (identifier) @namespace) + +(import_statement + (identifier) @namespace) + +(selected_import + . (identifier) @namespace) + +(scoped_identifier + (identifier) @namespace) + +; ----- +; Types +; ----- + +(abstract_definition + name: (identifier) @type) + +(primitive_definition + name: (identifier) @type) -(field_expression - (identifier) - (identifier) @variable.other.member .) +(struct_definition + name: (identifier) @type) + +(struct_definition + . (_) + (identifier) @variable.other.member) + +(struct_definition + . (_) + (typed_expression + . (identifier) @variable.other.member)) + +(type_parameter_list + (identifier) @type) + +(constrained_parameter + (identifier) @type) + +(subtype_clause + (identifier) @type) + +(typed_expression + (identifier) @type . ) + +(parameterized_identifier + (identifier) @type) + +(type_argument_list + (identifier) @type) + +; ------------------- +; Function definition +; ------------------- (function_definition name: (identifier) @function) -(call_expression - (identifier) @function) -(call_expression - (field_expression (identifier) @function.method .)) -(broadcast_call_expression - (identifier) @function) -(broadcast_call_expression - (field_expression (identifier) @function.method .)) + (parameter_list (identifier) @variable.parameter) -(parameter_list - (optional_parameter . - (identifier) @variable.parameter)) -(typed_parameter - (identifier) @variable.parameter - (identifier) @type) -(type_parameter_list - (identifier) @type) + (typed_parameter (identifier) @variable.parameter - (parameterized_identifier) @type) -(function_expression + (identifier)? @type) + +(optional_parameter . (identifier) @variable.parameter) -(spread_parameter) @variable.parameter + (spread_parameter (identifier) @variable.parameter) -(named_argument - . (identifier) @variable.parameter) -(argument_list - (typed_expression - (identifier) @variable.parameter - (identifier) @type)) -(argument_list - (typed_expression - (identifier) @variable.parameter - (parameterized_identifier) @type)) -;; Symbol expressions (:my-wanna-be-lisp-keyword) -(quote_expression - (identifier)) @string.special.symbol +(function_expression + . (identifier) @variable.parameter) -;; Parsing error! foo (::Type) get's parsed as two quote expressions -(argument_list - (quote_expression - (quote_expression - (identifier) @type))) +; --------------- +; Functions calls +; --------------- -(type_argument_list - (identifier) @type) -(parameterized_identifier (_)) @type -(argument_list - (typed_expression . (identifier) @variable.parameter)) +(call_expression + (identifier) @function) -(typed_expression - (identifier) @type .) -(typed_expression - (parameterized_identifier) @type .) +(broadcast_call_expression + (identifier) @function) -(struct_definition - name: (identifier) @type) +(call_expression + (field_expression (identifier) @function .)) -(integer_literal) @constant.numeric.integer -(float_literal) @constant.numeric.float +(broadcast_call_expression + (field_expression (identifier) @function .)) + +; ------ +; Macros +; ------ + +(macro_definition + name: (identifier) @function.macro) -(range_expression - (identifier) @constant.numeric.integer - (eq? @constant.numeric.integer "end")) -(range_expression - (_ - (identifier) @constant.numeric.integer - (eq? @constant.numeric.integer "end"))) -(coefficient_expression - [ - (integer_literal) - (float_literal) - ] - (identifier) @constant.builtin) - -;; TODO: operators. -;; Those are a bit difficult to implement since the respective nodes are hidden right now (_power_operator) -;; and heavily use Unicode chars (support for those are bad in vim/lua regexes) -;[; - ;(power_operator); - ;(times_operator); - ;(plus_operator); - ;(arrow_operator); - ;(comparison_operator); - ;(assign_operator); -;] @operator ; - -"end" @keyword +(macro_identifier + "@" @function.macro + (identifier) @function.macro) + +; -------- +; Keywords +; -------- + +(function_definition + ["function" "end"] @keyword.function) (if_statement ["if" "end"] @keyword.control.conditional) @@ -117,36 +147,6 @@ (ternary_expression ["?" ":"] @keyword.control.conditional) -(function_definition ["function" "end"] @keyword.function) - -[ - (line_comment) - (block_comment) -] @comment - -[ - "const" - "return" - "macro" - "struct" - "primitive" - "type" -] @keyword - -((identifier) @keyword (match? @keyword "global|local")) - -(compound_expression - ["begin" "end"] @keyword) -(try_statement - ["try" "end" ] @keyword.control.exception) -(finally_clause - "finally" @keyword.control.exception) -(catch_clause - "catch" @keyword.control.exception) -(quote_statement - ["quote" "end"] @keyword) -(let_statement - ["let" "end"] @keyword) (for_statement ["for" "end"] @keyword.control.repeat) (while_statement @@ -157,33 +157,90 @@ "in" @keyword.control.repeat) (for_clause "for" @keyword.control.repeat) -(do_clause - ["do" "end"] @keyword) -(export_statement - ["export"] @keyword.control.import) +(try_statement + ["try" "end" ] @keyword.control.exception) +(finally_clause + "finally" @keyword.control.exception) +(catch_clause + "catch" @keyword.control.exception) [ - "using" - "module" + "export" "import" + "using" ] @keyword.control.import -((identifier) @keyword.control.import (#eq? @keyword.control.import "baremodule")) +[ + "abstract" + "begin" + "const" + "do" + "end" + "let" + "macro" + "module" + "mutable" + "primitive" + "quote" + "return" + "struct" + "type" +] @keyword + +; TODO: fix this +((identifier) @keyword (match? @keyword "global|local")) -(((identifier) @constant.builtin) (match? @constant.builtin "^(nothing|Inf|NaN)$")) -(((identifier) @constant.builtin.boolean) (#eq? @constant.builtin.boolean "true")) -(((identifier) @constant.builtin.boolean) (#eq? @constant.builtin.boolean "false")) +; --------- +; Operators +; --------- + +[ + (operator) + "::" + "<:" + ":" + "=>" + "..." + "$" +] @operator + +; ------------ +; Punctuations +; ------------ + +[ + "." + "," + ";" +] @punctuation.delimiter + +[ + "[" + "]" + "(" + ")" + "{" + "}" +] @punctuation.bracket + +; --------------------- +; Remaining identifiers +; --------------------- + +; could also be namespace but this should cover the majority +(field_expression + . (_) + (identifier) @variable.other.member) +(const_statement + (variable_declaration + . (identifier) @constant)) -["::" ":" "." "," "..."] @punctuation.delimiter -["[" "]" "(" ")" "{" "}"] @punctuation.bracket +((identifier) @type + (match? @type "([A-Z][a-z0-9]+)+$")) -["="] @operator +((identifier) @constant + (match? @constant "^[A-Z][A-Z0-9_]+$")) (identifier) @variable -;; In case you want type highlighting based on Julia naming conventions (this might collide with mathematical notation) -;((identifier) @type ; exception: mark `A_foo` sort of identifiers as variables - ;(match? @type "^[A-Z][^_]")) -((identifier) @constant - (match? @constant "^[A-Z][A-Z_]{2}[A-Z_]*$")) diff --git a/runtime/queries/julia/injections.scm b/runtime/queries/julia/injections.scm index 3eab231faac3..caed916f3b13 100644 --- a/runtime/queries/julia/injections.scm +++ b/runtime/queries/julia/injections.scm @@ -1,8 +1,16 @@ -; ((string_literal) @injection.content -; (#set! injection.language "markdown")) +( + (string_literal) @injection.content + (#set! injection.language "markdown")) -; ((line_comment) @injection.content -; (#set! injection.language "comment")) +( + [ + (line_comment) + (block_comment) + ] @injection.content + (#set! injection.language "comment")) -; ((block_comment) @injection.content -; (#set! injection.language "comment")) +( + (prefixed_string_literal + prefix: (identifier) @function.macro) @injection.content + (#eq? @function.macro "re") + (#set! injection.language "regex")) diff --git a/runtime/queries/julia/locals.scm b/runtime/queries/julia/locals.scm index d5ac794e86f1..07b96f2c66e1 100644 --- a/runtime/queries/julia/locals.scm +++ b/runtime/queries/julia/locals.scm @@ -1,59 +1,43 @@ +; ----------- +; Definitions +; ----------- +; Imports (import_statement - (identifier) @definition.import) -(variable_declaration - (identifier) @local.definition) -(variable_declaration - (tuple_expression - (identifier) @local.definition)) -(for_binding - (identifier) @local.definition) -(for_binding - (tuple_expression - (identifier) @local.definition)) - -(assignment_expression - (tuple_expression - (identifier) @local.definition)) -(assignment_expression - (bare_tuple_expression - (identifier) @local.definition)) -(assignment_expression - (identifier) @local.definition) - -(type_parameter_list - (identifier) @definition.type) -(type_argument_list - (identifier) @definition.type) -(struct_definition - name: (identifier) @definition.type) + (identifier) @local.definition) + +; Constants +(const_statement + (variable_declaration + . (identifier) @local.definition)) +; Parameters (parameter_list - (identifier) @definition.parameter) + (identifier) @local.definition) + (typed_parameter - (identifier) @definition.parameter - (identifier)) -(function_expression - . (identifier) @definition.parameter) -(argument_list - (typed_expression - (identifier) @definition.parameter - (identifier))) -(spread_parameter - (identifier) @definition.parameter) + . (identifier) @local.definition) -(function_definition - name: (identifier) @definition.function) @local.scope -(macro_definition - name: (identifier) @definition.macro) @local.scope +(optional_parameter . + (identifier) @local.definition) -(identifier) @local.reference +(spread_parameter + (identifier) @local.definition) + +(function_expression + . (identifier) @local.definition) + +; ------ +; Scopes +; ------ [ - (try_statement) - (finally_clause) - (quote_statement) - (let_statement) - (compound_expression) - (for_statement) + (function_definition) + (macro_definition) ] @local.scope + +; ---------- +; References +; ---------- + +(identifier) @local.reference From 0d41485b2427d4c68e9cb859b525b5367a3c35ae Mon Sep 17 00:00:00 2001 From: Christoph Horn Date: Wed, 24 Aug 2022 08:56:42 +0200 Subject: [PATCH 3/5] Julia queries: limit markdown highlighting to docstrings --- runtime/queries/julia/injections.scm | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/runtime/queries/julia/injections.scm b/runtime/queries/julia/injections.scm index caed916f3b13..5b2e7584e89b 100644 --- a/runtime/queries/julia/injections.scm +++ b/runtime/queries/julia/injections.scm @@ -1,5 +1,18 @@ ( - (string_literal) @injection.content + (source_file + ( + (string_literal) @injection.content + [ + (module_definition) + (function_definition) + (macro_definition) + (primitive_definition) + (abstract_definition) + (struct_definition) + (assignment_expression) + (const_statement) + ] + )) (#set! injection.language "markdown")) ( From 24758835c31973394192be04f151f8b166002dab Mon Sep 17 00:00:00 2001 From: Christoph Horn Date: Wed, 24 Aug 2022 10:32:08 +0200 Subject: [PATCH 4/5] Julia queries: prevent constructors to be highlighted as functions Also improves the captures of the remaining identifiers. --- runtime/queries/julia/highlights.scm | 56 ++++++++++++++++++---------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/runtime/queries/julia/highlights.scm b/runtime/queries/julia/highlights.scm index c4f6999ac4ca..9271615fbb1b 100644 --- a/runtime/queries/julia/highlights.scm +++ b/runtime/queries/julia/highlights.scm @@ -85,8 +85,11 @@ ; Function definition ; ------------------- -(function_definition - name: (identifier) @function) +( + (function_definition + name: (identifier) @function) + ; prevent constructors (PascalCase) to be highlighted as functions + (#match? @function "^[^A-Z]")) (parameter_list (identifier) @variable.parameter) @@ -108,17 +111,26 @@ ; Functions calls ; --------------- -(call_expression - (identifier) @function) +( + (call_expression + (identifier) @function) + ; prevent constructors (PascalCase) to be highlighted as functions + (#match? @function "^[^A-Z]")) -(broadcast_call_expression - (identifier) @function) +( + (broadcast_call_expression + (identifier) @function) + (#match? @function "^[^A-Z]")) -(call_expression - (field_expression (identifier) @function .)) +( + (call_expression + (field_expression (identifier) @function .)) + (#match? @function "^[^A-Z]")) -(broadcast_call_expression - (field_expression (identifier) @function .)) +( + (broadcast_call_expression + (field_expression (identifier) @function .)) + (#match? @function "^[^A-Z]")) ; ------ ; Macros @@ -228,19 +240,25 @@ ; Remaining identifiers ; --------------------- -; could also be namespace but this should cover the majority -(field_expression - . (_) - (identifier) @variable.other.member) - (const_statement (variable_declaration . (identifier) @constant)) -((identifier) @type - (match? @type "([A-Z][a-z0-9]+)+$")) +; SCREAMING_SNAKE_CASE +( + (identifier) @constant + (match? @constant "^[A-Z][A-Z0-9_]*$")) -((identifier) @constant - (match? @constant "^[A-Z][A-Z0-9_]+$")) +; remaining identifiers that start with capital letters should be types (PascalCase) +( + (identifier) @type + (match? @type "^[A-Z]")) + +; Field expressions are either module content or struct fields. +; Module types and constants should already be captured, so this +; assumes the remaining identifiers to be struct fields. +(field_expression + (_) + (identifier) @variable.other.member) (identifier) @variable From 0d6c62be74cc11e170315bcc740bdf223d8de72c Mon Sep 17 00:00:00 2001 From: Christoph Horn Date: Wed, 24 Aug 2022 17:25:36 +0200 Subject: [PATCH 5/5] Julia queries: fix non-consecutive docstring highlight --- runtime/queries/julia/injections.scm | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/runtime/queries/julia/injections.scm b/runtime/queries/julia/injections.scm index 5b2e7584e89b..ce4011f20a4b 100644 --- a/runtime/queries/julia/injections.scm +++ b/runtime/queries/julia/injections.scm @@ -1,18 +1,17 @@ ( (source_file - ( - (string_literal) @injection.content - [ - (module_definition) - (function_definition) - (macro_definition) - (primitive_definition) - (abstract_definition) - (struct_definition) - (assignment_expression) - (const_statement) - ] - )) + (string_literal) @injection.content + . + [ + (module_definition) + (function_definition) + (macro_definition) + (primitive_definition) + (abstract_definition) + (struct_definition) + (assignment_expression) + (const_statement) + ]) (#set! injection.language "markdown")) (