From 2567827ae6c9aa8e465464cbd2d70f79bb05eaf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lan=20Cr=C3=ADstoffer?= Date: Mon, 19 Jun 2023 19:38:08 +0200 Subject: [PATCH] Changes MATLAB parser. --- book/src/generated/lang-support.md | 2 +- languages.toml | 2 +- runtime/queries/matlab/context.scm | 41 ++++++ runtime/queries/matlab/folds.scm | 11 ++ runtime/queries/matlab/highlights.scm | 182 ++++++++++++++----------- runtime/queries/matlab/indents.scm | 23 ++++ runtime/queries/matlab/injections.scm | 2 + runtime/queries/matlab/locals.scm | 19 +++ runtime/queries/matlab/textobjects.scm | 9 ++ 9 files changed, 213 insertions(+), 78 deletions(-) create mode 100644 runtime/queries/matlab/context.scm create mode 100644 runtime/queries/matlab/folds.scm create mode 100644 runtime/queries/matlab/indents.scm create mode 100644 runtime/queries/matlab/injections.scm create mode 100644 runtime/queries/matlab/locals.scm create mode 100644 runtime/queries/matlab/textobjects.scm diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index e2f62734755f..c3eb0f96a58c 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -88,7 +88,7 @@ | markdoc | ✓ | | | `markdoc-ls` | | markdown | ✓ | | | `marksman` | | markdown.inline | ✓ | | | | -| matlab | ✓ | | | | +| matlab | ✓ | ✓ | ✓ | | | mermaid | ✓ | | | | | meson | ✓ | | ✓ | | | mint | | | | `mint` | diff --git a/languages.toml b/languages.toml index ef7b0bf11eda..bfc448ca28d1 100644 --- a/languages.toml +++ b/languages.toml @@ -2285,7 +2285,7 @@ indent = { tab-width = 2, unit = " " } [[grammar]] name = "matlab" -source = { git = "https://github.com/mstanciu552/tree-sitter-matlab", rev = "2d5d3d5193718a86477d4335aba5b34e79147326" } +source = { git = "https://github.com/acristoffers/tree-sitter-matlab", rev = "5a047c1572792ae943b51af7cf9307097b2fcce0" } [[language]] name = "ponylang" diff --git a/runtime/queries/matlab/context.scm b/runtime/queries/matlab/context.scm new file mode 100644 index 000000000000..725c10ca0318 --- /dev/null +++ b/runtime/queries/matlab/context.scm @@ -0,0 +1,41 @@ +(function_definition + (block (_) @context.end) +) @context + +(while_statement + (block (_) @context.end) +) @context + +(for_statement + (block (_) @context.end) +) @context + +(if_statement + (block (_) @context.end) +) @context + +(elseif_clause + (block (_) @context.end) +) @context + +(else_clause + (block (_) @context.end) +) @context + +(switch_statement) @context + +(case_clause + (block (_) @context.end) +) @context + +(otherwise_clause + (block (_) @context.end) +) @context + +(try_statement + "try" + (block (_) @context.end) @context + "end") +(catch_clause + "catch" + (block (_) @context.end) @context) diff --git a/runtime/queries/matlab/folds.scm b/runtime/queries/matlab/folds.scm new file mode 100644 index 000000000000..dad9ea1776a5 --- /dev/null +++ b/runtime/queries/matlab/folds.scm @@ -0,0 +1,11 @@ +[(if_statement) + (for_statement) + (while_statement) + (switch_statement) + (try_statement) + (function_definition) + (class_definition) + (enumeration) + (events) + (methods) + (properties)] @fold diff --git a/runtime/queries/matlab/highlights.scm b/runtime/queries/matlab/highlights.scm index c0e23e917541..bc4c5069ee0d 100644 --- a/runtime/queries/matlab/highlights.scm +++ b/runtime/queries/matlab/highlights.scm @@ -1,97 +1,127 @@ - ; highlights.scm +; Constants -function_keyword: (function_keyword) @keyword.function +(events (identifier) @constant) +(attribute (identifier) @constant) -(function_definition -function_name: (identifier) @function -(end) @function) +"~" @constant.builtin -(parameter_list (identifier) @variable.parameter) +; Fields/Properties -[ - "if" - "elseif" - "else" - "switch" - "case" - "otherwise" -] @keyword.control.conditional +(superclass "." (identifier) @variable.other.member) +(property_name "." (identifier) @variable.other.member) +(property name: (identifier) @variable.other.member) -(if_statement (end) @keyword.control.conditional) -(switch_statement (end) @keyword.control.conditional) +; Types -["for" "while"] @keyword.control.repeat -(for_statement (end) @keyword.control.repeat) -(while_statement (end) @keyword.control.repeat) +(class_definition name: (identifier) @keyword.storage.type) +(attributes (identifier) @constant) +(enum . (identifier) @type.enum.variant) -["try" "catch"] @keyword.control.exception -(try_statement (end) @keyword.control.exception) +; Functions -(function_definition end: (end) @keyword) +(function_definition + "function" @keyword.function + name: (identifier) @function + [ "end" "endfunction" ]? @keyword.function) -["return" "break" "continue"] @keyword.return +(function_signature name: (identifier) @function) +(function_call name: (identifier) @function) +(handle_operator (identifier) @function) +(validation_functions (identifier) @function) +(command (command_name) @function.macro) +(command_argument) @string +(return_statement) @keyword.control.return -( -(identifier) @constant.builtin -(#any-of? @constant.builtin "true" "false") -) +; Assignments -( - (identifier) @constant.builtin - (#eq? @constant.builtin "end") -) +(assignment left: (_) @variable) +(multioutput_variable (_) @variable) -;; Punctuations +; Parameters -[";" ","] @punctuation.special -(argument_list "," @punctuation.delimiter) -(vector_definition ["," ";"] @punctuation.delimiter) -(cell_definition ["," ";"] @punctuation.delimiter) -":" @punctuation.delimiter -(parameter_list "," @punctuation.delimiter) -(return_value "," @punctuation.delimiter) +(function_arguments (identifier) @variable.parameter) -; ;; Brackets +; Operators [ - "(" - ")" - "[" - "]" - "{" - "}" -] @punctuation.bracket - -;; Operators -"=" @operator -(operation [ ">" - "<" - "==" - "<=" - ">=" - "=<" - "=>" - "~=" - "*" - ".*" - "/" - "\\" - "./" - "^" - ".^" - "+"] @operator) - -;; boolean operator -[ - "&&" - "||" + "+" + ".+" + "-" + ".*" + "*" + ".*" + "/" + "./" + "\\" + ".\\" + "^" + ".^" + "'" + ".'" + "|" + "&" + "?" + "@" + "<" + "<=" + ">" + ">=" + "==" + "~=" + "=" + "&&" + "||" + ":" ] @operator -;; Number -(number) @constant.numeric +; Conditionals + +(if_statement [ "if" "end" ] @keyword.control.conditional) +(elseif_clause "elseif" @keyword.control.conditional) +(else_clause "else" @keyword.control.conditional) +(switch_statement [ "switch" "end" ] @keyword.control.conditional) +(case_clause "case" @keyword.control.conditional) +(otherwise_clause "otherwise" @keyword.control.conditional) +(break_statement) @keyword.control.conditional + +; Repeats + +(for_statement [ "for" "parfor" "end" ] @keyword.control.repeat) +(while_statement [ "while" "end" ] @keyword.control.repeat) +(continue_statement) @keyword.control.repeat + +; Exceptions -;; String +(try_statement [ "try" "end" ] @keyword.control.exception) +(catch_clause "catch" @keyword.control.exception) + +; Punctuation + +[ ";" "," "." ] @punctuation.delimiter +[ "(" ")" "[" "]" "{" "}" ] @punctuation.bracket + +; Literals + +(escape_sequence) @constant.character.escape +(formatting_sequence) @constant.character.escape (string) @string +(number) @constant.numeric.float +(boolean) @constant.builtin.boolean -;; Comment -(comment) @comment +; Comments + +[ (comment) (line_continuation) ] @comment.line + +; Keywords + +"classdef" @keyword.storage.type +[ + "arguments" + "end" + "enumeration" + "events" + "global" + "methods" + "persistent" + "properties" +] @keyword diff --git a/runtime/queries/matlab/indents.scm b/runtime/queries/matlab/indents.scm new file mode 100644 index 000000000000..b2a8e55dd5e8 --- /dev/null +++ b/runtime/queries/matlab/indents.scm @@ -0,0 +1,23 @@ +[ + (if_statement) + (for_statement) + (while_statement) + (switch_statement) + (try_statement) + (function_definition) + (class_definition) + (enumeration) + (events) + (methods) + (properties) +] @indent + +[ + (elseif_clause) + (else_clause) + (case_clause) + (otherwise_clause) + (catch_clause) +] @indent @extend + +[ "end" ] @outdent diff --git a/runtime/queries/matlab/injections.scm b/runtime/queries/matlab/injections.scm new file mode 100644 index 000000000000..321c90add371 --- /dev/null +++ b/runtime/queries/matlab/injections.scm @@ -0,0 +1,2 @@ +((comment) @injection.content + (#set! injection.language "comment")) diff --git a/runtime/queries/matlab/locals.scm b/runtime/queries/matlab/locals.scm new file mode 100644 index 000000000000..d0589a1dbb02 --- /dev/null +++ b/runtime/queries/matlab/locals.scm @@ -0,0 +1,19 @@ +(function_definition name: (identifier) @local.definition ?) @local.scope +(function_arguments (identifier)* @local.definition) + +(lambda (arguments (identifier) @local.definition)) @local.scope + +(assignment left: ((function_call + name: (identifier) @local.definition))) +(assignment left: ((field_expression . [(function_call + name: (identifier) @local.definition) + (identifier) @local.definition]))) +(assignment left: (_) @local.definition) +(assignment (multioutput_variable (_) @local.definition)) + +(iterator . (identifier) @local.definition) +(global_operator (identifier) @local.definition) +(persistent_operator (identifier) @local.definition) +(catch_clause (identifier) @local.definition) + +(identifier) @local.reference diff --git a/runtime/queries/matlab/textobjects.scm b/runtime/queries/matlab/textobjects.scm new file mode 100644 index 000000000000..d94a6653b4f2 --- /dev/null +++ b/runtime/queries/matlab/textobjects.scm @@ -0,0 +1,9 @@ +(arguments ((_) @parameter.inside . ","? @parameter.around) @parameter.around) +(function_arguments ((_) @parameter.inside . ","? @parameter.around) @parameter.around) + +(lambda expression: (_) @function.inside) @function.around +(function_definition (block) @function.inside) @function.around + +(class_definition) @class.inside @class.around + +(comment) @comment.inside @comment.around