From 59f720d1651041d6b3df9510c5abf38805a3c0aa Mon Sep 17 00:00:00 2001 From: Axel Kappel <69117984+Kl4rry@users.noreply.github.com> Date: Thu, 4 Jul 2024 21:41:32 +0200 Subject: [PATCH] add lua --- Cargo.lock | 10 ++ crates/ferrite-core/Cargo.toml | 2 + crates/ferrite-core/src/language.rs | 11 ++ crates/ferrite-tree-sitter/Cargo.toml | 2 + crates/ferrite-tree-sitter/src/lib.rs | 2 + queries/lua/highlights.scm | 224 ++++++++++++++++++++++++++ queries/lua/injections.scm | 3 + themes/solarized_dark.toml | 2 +- themes/solarized_light.toml | 2 +- 9 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 queries/lua/highlights.scm create mode 100644 queries/lua/injections.scm diff --git a/Cargo.lock b/Cargo.lock index 6d10850..87fd42b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -921,6 +921,7 @@ dependencies = [ "tree-sitter-ini", "tree-sitter-javascript", "tree-sitter-json", + "tree-sitter-lua", "tree-sitter-md", "tree-sitter-protobuf", "tree-sitter-python", @@ -3173,6 +3174,15 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-lua" +version = "0.0.19" +source = "git+https://github.com/tree-sitter-grammars/tree-sitter-lua?rev=88e446476a1e97a8724dff7a23e2d709855077f2#88e446476a1e97a8724dff7a23e2d709855077f2" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "tree-sitter-md" version = "0.1.2" diff --git a/crates/ferrite-core/Cargo.toml b/crates/ferrite-core/Cargo.toml index 09aab2a..a4a0e63 100644 --- a/crates/ferrite-core/Cargo.toml +++ b/crates/ferrite-core/Cargo.toml @@ -70,6 +70,7 @@ lang-hyprlang = ["ferrite-tree-sitter/lang-hyprlang"] lang-ini = ["ferrite-tree-sitter/lang-ini"] lang-javascript = ["ferrite-tree-sitter/lang-javascript"] lang-json = ["ferrite-tree-sitter/lang-json"] +lang-lua = ["ferrite-tree-sitter/lang-lua"] lang-md = ["ferrite-tree-sitter/lang-md"] lang-protobuf = ["ferrite-tree-sitter/lang-protobuf"] lang-python = ["ferrite-tree-sitter/lang-python"] @@ -104,6 +105,7 @@ default = [ "lang-ini", "lang-javascript", "lang-json", + "lang-lua", "lang-md", "lang-protobuf", "lang-python", diff --git a/crates/ferrite-core/src/language.rs b/crates/ferrite-core/src/language.rs index c5419bb..318445d 100644 --- a/crates/ferrite-core/src/language.rs +++ b/crates/ferrite-core/src/language.rs @@ -101,6 +101,8 @@ static LANGUAGES: Lazy>> = Lazy langs.insert("dockerfile", OnceCell::new()); #[cfg(feature = "lang-protobuf")] langs.insert("protobuf", OnceCell::new()); + #[cfg(feature = "lang-lua")] + langs.insert("lua", OnceCell::new()); langs }); @@ -355,6 +357,14 @@ fn get_lang_config(name: &str) -> Option { include_str!("../../../queries/protobuf/injections.scm"), "", ), + #[cfg(feature = "lang-lua")] + "lua" => TreeSitterConfig::new( + "lua", + ferrite_tree_sitter::tree_sitter_lua::language(), + include_str!("../../../queries/lua/highlights.scm"), + include_str!("../../../queries/lua/injections.scm"), + "", + ), _ => return None, }) } @@ -396,6 +406,7 @@ pub fn get_language_from_path(path: impl AsRef) -> Option<&'static str> { (Suffix(".go"), "go"), (Suffix(".ts"), "ts"), (Suffix(".proto"), "protobuf"), + (Suffix(".lua"), "lua"), (Name("hyprland.conf"), "hyprlang"), (Name("COMMIT_EDITMSG"), "git-commit"), (Name("git-rebase-todo"), "git-rebase"), diff --git a/crates/ferrite-tree-sitter/Cargo.toml b/crates/ferrite-tree-sitter/Cargo.toml index 2106f6c..795d50b 100644 --- a/crates/ferrite-tree-sitter/Cargo.toml +++ b/crates/ferrite-tree-sitter/Cargo.toml @@ -25,6 +25,7 @@ tree-sitter-hyprlang = { git = "https://github.com/tree-sitter-grammars/tree-sit tree-sitter-ini = { git = "https://github.com/justinmk/tree-sitter-ini", rev = "4d247fb876b4ae6b347687de4a179511bf67fcbc", optional = true } tree-sitter-javascript = { git = "https://github.com/tree-sitter/tree-sitter-javascript", rev = "f772967f7b7bc7c28f845be2420a38472b16a8ee", optional = true } tree-sitter-json = { version = "0.19.0", optional = true } +tree-sitter-lua = { git = "https://github.com/tree-sitter-grammars/tree-sitter-lua", rev = "88e446476a1e97a8724dff7a23e2d709855077f2", optional = true } tree-sitter-md = { git = "https://github.com/MDeiml/tree-sitter-markdown", rev = "272e080bca0efd19a06a7f4252d746417224959e", optional = true } tree-sitter-protobuf = { git = "https://github.com/yusdacra/tree-sitter-protobuf", rev = "19c211a01434d9f03efff99f85e19f967591b175", optional = true } tree-sitter-python = { version = "0.20.2", optional = true } @@ -58,6 +59,7 @@ lang-hyprlang = ["dep:tree-sitter-hyprlang"] lang-ini = ["dep:tree-sitter-ini"] lang-javascript = ["dep:tree-sitter-javascript"] lang-json = ["dep:tree-sitter-json"] +lang-lua = ["dep:tree-sitter-lua"] lang-md = ["dep:tree-sitter-md"] lang-protobuf = ["dep:tree-sitter-protobuf"] lang-python = ["dep:tree-sitter-python"] diff --git a/crates/ferrite-tree-sitter/src/lib.rs b/crates/ferrite-tree-sitter/src/lib.rs index a6b6041..49f71a0 100644 --- a/crates/ferrite-tree-sitter/src/lib.rs +++ b/crates/ferrite-tree-sitter/src/lib.rs @@ -38,6 +38,8 @@ pub use tree_sitter_ini; pub use tree_sitter_javascript; #[cfg(feature = "lang-json")] pub use tree_sitter_json; +#[cfg(feature = "lang-lua")] +pub use tree_sitter_lua; #[cfg(feature = "lang-md")] pub use tree_sitter_md; #[cfg(feature = "lang-protobuf")] diff --git a/queries/lua/highlights.scm b/queries/lua/highlights.scm new file mode 100644 index 0000000..2f3b3c0 --- /dev/null +++ b/queries/lua/highlights.scm @@ -0,0 +1,224 @@ +;;; Highlighting for lua + +;; Keywords + +(if_statement +[ + "if" + "then" + "end" +] @keyword.control.conditional) + +(elseif_statement +[ + "elseif" + "then" + "end" +] @keyword.control.conditional) + +(else_statement +[ + "else" + "end" +] @keyword.control.conditional) + +(for_statement +[ + "for" + "do" + "end" +] @keyword.control.repeat) + +(while_statement +[ + "while" + "do" + "end" +] @keyword.control.repeat) + +(repeat_statement +[ + "repeat" + "until" +] @keyword.control.repeat) + +(do_statement +[ + "do" + "end" +] @keyword) + +"return" @keyword.control.return + +[ + "in" + "local" + (break_statement) + "goto" +] @keyword + +(function_declaration +[ + "function" + "end" +] @keyword.function) + +(function_definition +[ + "function" + "end" +] @keyword.function) + +;; Operators + +[ + "not" + "and" + "or" +] @keyword.operator + +[ +"=" +"~=" +"==" +"<=" +">=" +"<" +">" +"+" +"-" +"%" +"/" +"//" +"*" +"^" +"&" +"~" +"|" +">>" +"<<" +".." +"#" + ] @operator + +;; Punctuation +["," "." ":" ";"] @punctuation.delimiter + +;; Brackets + +[ + "(" + ")" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +; ;; Constants +[ +(false) +(true) +] @constant.builtin.boolean +(nil) @constant.builtin +(vararg_expression) @constant + +((identifier) @constant + (#match? @constant "^[A-Z][A-Z_0-9]*$")) + +;; Tables + +(field name: (identifier) @variable.other.member) + +(dot_index_expression field: (identifier) @variable.other.member) + +(table_constructor +[ + "{" + "}" +] @constructor) + +;; Functions + +(parameters (identifier) @variable.parameter) + +(function_call + (identifier) @function.builtin + (#any-of? @function.builtin + ;; built-in functions in Lua 5.1 + "assert" "collectgarbage" "dofile" "error" "getfenv" "getmetatable" "ipairs" + "load" "loadfile" "loadstring" "module" "next" "pairs" "pcall" "print" + "rawequal" "rawget" "rawset" "require" "select" "setfenv" "setmetatable" + "tonumber" "tostring" "type" "unpack" "xpcall")) + +(function_declaration + name: [ + (identifier) @function + (dot_index_expression + field: (identifier) @function) + ]) + +(function_declaration + name: (method_index_expression + method: (identifier) @function.method)) + +(assignment_statement + (variable_list . + name: [ + (identifier) @function + (dot_index_expression + field: (identifier) @function) + ]) + (expression_list . + value: (function_definition))) + +(table_constructor + (field + name: (identifier) @function + value: (function_definition))) + +(function_call + name: [ + (identifier) @function.call + (dot_index_expression + field: (identifier) @function.call) + (method_index_expression + method: (identifier) @function.method.call) + ]) + +; TODO: incorrectly highlights variable N in `N, nop = 42, function() end` +(assignment_statement + (variable_list + name: (identifier) @function) + (expression_list + value: (function_definition))) + +(method_index_expression method: (identifier) @function.method) + +;; Nodes +(comment) @comment +(string) @string +(escape_sequence) @constant.character.escape +(number) @constant.numeric.integer +(label_statement) @label +; A bit of a tricky one, this will only match field names +(field . (identifier) @variable.other.member (_)) +(hash_bang_line) @comment + +;; Property +(dot_index_expression field: (identifier) @variable.other.member) + +;; Variables +((identifier) @variable.builtin + (#eq? @variable.builtin "self")) + +(variable_list + (attribute + "<" @punctuation.bracket + (identifier) @attribute + ">" @punctuation.bracket)) + +(identifier) @variable + +;; Error +(ERROR) @error diff --git a/queries/lua/injections.scm b/queries/lua/injections.scm new file mode 100644 index 0000000..fca94d5 --- /dev/null +++ b/queries/lua/injections.scm @@ -0,0 +1,3 @@ +((comment) @injection.content + (#set! injection.language "comment") + (#set! injection.include-children)) diff --git a/themes/solarized_dark.toml b/themes/solarized_dark.toml index 439dbc7..78ce27a 100644 --- a/themes/solarized_dark.toml +++ b/themes/solarized_dark.toml @@ -41,7 +41,7 @@ "label" = { fg = "green" } "module" = { fg = "violet" } "tag" = { fg = "magenta" } -"variable" = { fg = "blue"} +"variable" = { fg = "magenta"} "punctuation" = { fg = "base1" } "markup.heading" = { fg = "blue" } diff --git a/themes/solarized_light.toml b/themes/solarized_light.toml index d6c4233..27d8c87 100644 --- a/themes/solarized_light.toml +++ b/themes/solarized_light.toml @@ -41,7 +41,7 @@ "label" = { fg = "green" } "module" = { fg = "violet" } "tag" = { fg = "magenta" } -"variable" = { fg = "blue"} +"variable" = { fg = "magenta"} "punctuation" = { fg = "base1" } "markup.heading" = { fg = "blue" } "markup.list" = { fg = "red" }