From fc8e2a06ee2262c8e82abb88e0c068664648f908 Mon Sep 17 00:00:00 2001 From: Lennard Hofmann Date: Wed, 12 Oct 2022 15:45:56 +0200 Subject: [PATCH] Update tree-sitter-lua and add textobjects for Lua (#3552) --- book/src/generated/lang-support.md | 2 +- languages.toml | 2 +- runtime/queries/lua/folds.scm | 10 ++++ runtime/queries/lua/highlights.scm | 77 +++++++++++++++-------------- runtime/queries/lua/indents.scm | 10 ++-- runtime/queries/lua/injections.scm | 3 +- runtime/queries/lua/textobjects.scm | 15 ++++++ 7 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 runtime/queries/lua/folds.scm create mode 100644 runtime/queries/lua/textobjects.scm diff --git a/book/src/generated/lang-support.md b/book/src/generated/lang-support.md index deeef34fb6e1..74e63796104a 100644 --- a/book/src/generated/lang-support.md +++ b/book/src/generated/lang-support.md @@ -66,7 +66,7 @@ | llvm | ✓ | ✓ | ✓ | | | llvm-mir | ✓ | ✓ | ✓ | | | llvm-mir-yaml | ✓ | | ✓ | | -| lua | ✓ | | ✓ | `lua-language-server` | +| lua | ✓ | ✓ | ✓ | `lua-language-server` | | make | ✓ | | | | | markdown | ✓ | | | `marksman` | | markdown.inline | ✓ | | | | diff --git a/languages.toml b/languages.toml index 08645af8c273..892f3117f15a 100644 --- a/languages.toml +++ b/languages.toml @@ -667,7 +667,7 @@ language-server = { command = "lua-language-server", args = [] } [[grammar]] name = "lua" -source = { git = "https://github.com/nvim-treesitter/tree-sitter-lua", rev = "6f5d40190ec8a0aa8c8410699353d820f4f7d7a6" } +source = { git = "https://github.com/MunifTanjim/tree-sitter-lua", rev = "887dfd4e83c469300c279314ff1619b1d0b85b91" } [[language]] name = "svelte" diff --git a/runtime/queries/lua/folds.scm b/runtime/queries/lua/folds.scm new file mode 100644 index 000000000000..d8f0b42df3ac --- /dev/null +++ b/runtime/queries/lua/folds.scm @@ -0,0 +1,10 @@ +[ + (do_statement) + (while_statement) + (repeat_statement) + (if_statement) + (for_statement) + (function_declaration) + (function_definition) + (table_constructor) +] @fold diff --git a/runtime/queries/lua/highlights.scm b/runtime/queries/lua/highlights.scm index e73b32d6e9a4..f48e607c597f 100644 --- a/runtime/queries/lua/highlights.scm +++ b/runtime/queries/lua/highlights.scm @@ -1,7 +1,8 @@ ;;; Highlighting for lua ;;; Builtins -(self) @variable.builtin +((identifier) @variable.builtin + (#eq? @variable.builtin "self")) ;; Keywords @@ -12,20 +13,20 @@ "end" ] @keyword.control.conditional) +(elseif_statement [ - "else" "elseif" "then" -] @keyword.control.conditional + "end" +] @keyword.control.conditional) -(for_statement +(else_statement [ - "for" - "do" + "else" "end" -] @keyword.control.repeat) +] @keyword.control.conditional) -(for_in_statement +(for_statement [ "for" "do" @@ -51,21 +52,34 @@ "end" ] @keyword) +"return" @keyword.control.return + [ "in" "local" (break_statement) "goto" - "return" ] @keyword +(function_declaration +[ + "function" + "end" +] @keyword.function) + +(function_definition +[ + "function" + "end" +] @keyword.function) + ;; Operators [ "not" "and" "or" -] @operator +] @keyword.operator [ "=" @@ -95,6 +109,7 @@ ["," "." ":" ";"] @punctuation.delimiter ;; Brackets + [ "(" ")" @@ -110,7 +125,8 @@ (true) ] @constant.builtin.boolean (nil) @constant.builtin -(spread) @constant ;; "..." +(vararg_expression) @constant + ((identifier) @constant (#match? @constant "^[A-Z][A-Z_0-9]*$")) @@ -119,45 +135,32 @@ (identifier) @variable.parameter) ; ;; Functions -(function [(function_name) (identifier)] @function) -(function ["function" "end"] @keyword.function) - -(function - (function_name - (function_name_field - (property_identifier) @function .))) - -(local_function (identifier) @function) -(local_function ["function" "end"] @keyword.function) +(function_declaration name: (identifier) @function) +(function_call name: (identifier) @function.call) -(variable_declaration - (variable_declarator (identifier) @function) (function_definition)) -(local_variable_declaration - (variable_declarator (identifier) @function) (function_definition)) +(function_declaration name: (dot_index_expression field: (identifier) @function)) +(function_call name: (dot_index_expression field: (identifier) @function.call)) -(function_definition ["function" "end"] @keyword.function) +; TODO: incorrectly highlights variable N in `N, nop = 42, function() end` +(assignment_statement + (variable_list + name: (identifier) @function) + (expression_list + value: (function_definition))) -(function_call - [ - ((identifier) @variable (method) @function.method) - ((_) (method) @function.method) - (identifier) @function - (field_expression (property_identifier) @function) - ] - . (arguments)) +(method_index_expression method: (identifier) @function.method) ;; Nodes -(table ["{" "}"] @constructor) (comment) @comment (string) @string (number) @constant.numeric.integer (label_statement) @label ; A bit of a tricky one, this will only match field names (field . (identifier) @variable.other.member (_)) -(shebang) @comment +(hash_bang_line) @comment ;; Property -(property_identifier) @variable.other.member +(dot_index_expression field: (identifier) @variable.other.member) ;; Variable (identifier) @variable diff --git a/runtime/queries/lua/indents.scm b/runtime/queries/lua/indents.scm index 55a812c51ba8..2e5a108e62cf 100644 --- a/runtime/queries/lua/indents.scm +++ b/runtime/queries/lua/indents.scm @@ -1,17 +1,13 @@ [ (function_definition) - (variable_declaration) - (local_variable_declaration) + (function_declaration) + (method_index_expression) (field) - (local_function) - (function) (if_statement) (for_statement) - (for_in_statement) (repeat_statement) - (return_statement) (while_statement) - (table) + (table_constructor) (arguments) (do_statement) ] @indent diff --git a/runtime/queries/lua/injections.scm b/runtime/queries/lua/injections.scm index 321c90add371..fca94d58fab7 100644 --- a/runtime/queries/lua/injections.scm +++ b/runtime/queries/lua/injections.scm @@ -1,2 +1,3 @@ ((comment) @injection.content - (#set! injection.language "comment")) + (#set! injection.language "comment") + (#set! injection.include-children)) diff --git a/runtime/queries/lua/textobjects.scm b/runtime/queries/lua/textobjects.scm new file mode 100644 index 000000000000..6fb2000df40e --- /dev/null +++ b/runtime/queries/lua/textobjects.scm @@ -0,0 +1,15 @@ +(function_definition + body: (_) @function.inside) @function.around + +(function_declaration + body: (_) @function.inside) @function.around + +(parameters + ((_) @parameter.inside . ","? @parameter.around) @parameter.around) + +(arguments + ((_) @parameter.inside . ","? @parameter.around) @parameter.around) + +(comment) @comment.inside + +(comment)+ @comment.around