aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Seitz <stephan.seitz@fau.de>2021-04-11 20:53:52 +0200
committerStephan Seitz <stephan.seitz@fau.de>2021-07-24 20:57:45 +0200
commitb2a9afef6deaceb5d29440c486818ce8268c1873 (patch)
tree53dbf5d4cfdb4450a0ef81999e4be944ac7b7e1c
parent46009499afbb0232124072d775caa9552d0f71de (diff)
downloadrneovim-b2a9afef6deaceb5d29440c486818ce8268c1873.tar.gz
rneovim-b2a9afef6deaceb5d29440c486818ce8268c1873.tar.bz2
rneovim-b2a9afef6deaceb5d29440c486818ce8268c1873.zip
treesitter: add query.list_directives
-rw-r--r--runtime/doc/treesitter.txt27
-rw-r--r--runtime/lua/vim/treesitter/query.lua5
-rw-r--r--test/functional/treesitter/parser_spec.lua13
3 files changed, 45 insertions, 0 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index 340db92ae3..ce2b287944 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -249,6 +249,33 @@ Here is a list of built-in directives:
`({capture_id}, {start_row}, {start_col}, {end_row}, {end_col}, {key?})`
The default key is "offset".
+ *vim.treesitter.query.add_predicate()*
+vim.treesitter.query.add_predicate({name}, {handler})
+
+This adds a predicate with the name {name} to be used in queries.
+{handler} should be a function whose signature will be : >
+ handler(match, pattern, bufnr, predicate)
+<
+ *vim.treesitter.query.list_predicates()*
+vim.treesitter.query.list_predicates()
+
+This lists the currently available predicates to use in queries.
+
+ *vim.treesitter.query.add_directive()*
+vim.treesitter.query.add_directive({name}, {handler})
+
+This adds a directive with the name {name} to be used in queries.
+{handler} should be a function whose signature will be : >
+ handler(match, pattern, bufnr, predicate, metadata)
+Handlers can set match level data by setting directly on the metadata object `metadata.key = value`
+Handlers can set node level data by using the capture id on the metadata table
+`metadata[capture_id].key = value`
+
+ *vim.treesitter.query.list_directives()*
+vim.treesitter.query.list_directives()
+
+This lists the currently available directives to use in queries.
+
Treesitter syntax highlighting (WIP) *lua-treesitter-highlight*
NOTE: This is a partially implemented feature, and not usable as a default
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index b81eb18945..0ba44ced1a 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -351,6 +351,11 @@ function M.add_directive(name, handler, force)
directive_handlers[name] = handler
end
+--- Returns the list of currently supported directives
+function M.list_directives()
+ return vim.tbl_keys(directive_handlers)
+end
+
--- Returns the list of currently supported predicates
function M.list_predicates()
return vim.tbl_keys(predicate_handlers)
diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua
index d2f9148e8f..d9df5fbc0d 100644
--- a/test/functional/treesitter/parser_spec.lua
+++ b/test/functional/treesitter/parser_spec.lua
@@ -646,6 +646,19 @@ int x = INT_MAX;
{2, 29, 2, 68} -- READ_STRING_OK(x, y) (char_u *)read_string((x), (size_t)(y))
}, get_ranges())
end)
+ it("should list all directives", function()
+ local res_list = exec_lua[[
+ local query = require'vim.treesitter.query'
+
+ local list = query.list_directives()
+
+ table.sort(list)
+
+ return list
+ ]]
+
+ eq({ 'offset!', 'set!' }, res_list)
+ end)
end)
end)