Skip to content

Commit

Permalink
add lua
Browse files Browse the repository at this point in the history
  • Loading branch information
Kl4rry committed Jul 4, 2024
1 parent e30d65d commit 59f720d
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/ferrite-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -104,6 +105,7 @@ default = [
"lang-ini",
"lang-javascript",
"lang-json",
"lang-lua",
"lang-md",
"lang-protobuf",
"lang-python",
Expand Down
11 changes: 11 additions & 0 deletions crates/ferrite-core/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ static LANGUAGES: Lazy<HashMap<&'static str, OnceCell<TreeSitterConfig>>> = 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
});

Expand Down Expand Up @@ -355,6 +357,14 @@ fn get_lang_config(name: &str) -> Option<TreeSitterConfig> {
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,
})
}
Expand Down Expand Up @@ -396,6 +406,7 @@ pub fn get_language_from_path(path: impl AsRef<Path>) -> 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"),
Expand Down
2 changes: 2 additions & 0 deletions crates/ferrite-tree-sitter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions crates/ferrite-tree-sitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
224 changes: 224 additions & 0 deletions queries/lua/highlights.scm
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions queries/lua/injections.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
((comment) @injection.content
(#set! injection.language "comment")
(#set! injection.include-children))
2 changes: 1 addition & 1 deletion themes/solarized_dark.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion themes/solarized_light.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 59f720d

Please sign in to comment.