aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSteven Sojka <Steven.Sojka@tdameritrade.com>2021-03-02 13:51:08 -0600
committerSteven Sojka <Steven.Sojka@tdameritrade.com>2021-04-02 12:48:16 -0500
commit8bea39f3725bd2490ec3b6b32cf61aab0c08db68 (patch)
tree1cef1807392d0129dfc4a49284af5196cfed3848 /test
parentf2df01900e4b981c88e0ab9fa738167a250ae22a (diff)
downloadrneovim-8bea39f3725bd2490ec3b6b32cf61aab0c08db68.tar.gz
rneovim-8bea39f3725bd2490ec3b6b32cf61aab0c08db68.tar.bz2
rneovim-8bea39f3725bd2490ec3b6b32cf61aab0c08db68.zip
feat(treesitter): allow injections to be configured through directives
Diffstat (limited to 'test')
-rw-r--r--test/functional/treesitter/highlight_spec.lua2
-rw-r--r--test/functional/treesitter/parser_spec.lua59
2 files changed, 33 insertions, 28 deletions
diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua
index d80d0fdbaf..05e0c5fe2c 100644
--- a/test/functional/treesitter/highlight_spec.lua
+++ b/test/functional/treesitter/highlight_spec.lua
@@ -445,7 +445,7 @@ describe('treesitter highlighting', function()
exec_lua [[
local parser = vim.treesitter.get_parser(0, "c", {
- queries = {c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}
+ injections = {c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}
})
local highlighter = vim.treesitter.highlighter
test_hl = highlighter.new(parser, {queries = {c = hl_query}})
diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua
index f99362fbdf..72ff6f2fb6 100644
--- a/test/functional/treesitter/parser_spec.lua
+++ b/test/functional/treesitter/parser_spec.lua
@@ -468,7 +468,7 @@ int x = INT_MAX;
it("should inject a language", function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
- queries = {
+ injections = {
c = "(preproc_def (preproc_arg) @c) (preproc_function_def value: (preproc_arg) @c)"}})
]])
@@ -489,7 +489,7 @@ int x = INT_MAX;
it("should inject a language", function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
- queries = {
+ injections = {
c = "(preproc_def (preproc_arg) @c @combined) (preproc_function_def value: (preproc_arg) @c @combined)"}})
]])
@@ -506,11 +506,39 @@ int x = INT_MAX;
end)
end)
+ describe("when providing parsing information through a directive", function()
+ it("should inject a language", function()
+ exec_lua([=[
+ vim.treesitter.add_directive("inject-clang!", function(match, _, _, pred, metadata)
+ metadata.language = "c"
+ metadata.combined = true
+ metadata.content = pred[2]
+ end)
+
+ parser = vim.treesitter.get_parser(0, "c", {
+ injections = {
+ c = "(preproc_def ((preproc_arg) @_c (#inject-clang! @_c)))" ..
+ "(preproc_function_def value: ((preproc_arg) @_a (#inject-clang! @_a)))"}})
+ ]=])
+
+ eq("table", exec_lua("return type(parser:children().c)"))
+ eq(2, exec_lua("return #parser:children().c:trees()"))
+ eq({
+ {0, 0, 7, 0}, -- root tree
+ {3, 14, 5, 18}, -- VALUE 123
+ -- VALUE1 123
+ -- VALUE2 123
+ {1, 26, 2, 68} -- READ_STRING(x, y) (char_u *)read_string((x), (size_t)(y))
+ -- READ_STRING_OK(x, y) (char_u *)read_string((x), (size_t)(y))
+ }, get_ranges())
+ end)
+ end)
+
describe("when using the offset directive", function()
it("should shift the range by the directive amount", function()
exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
- queries = {
+ injections = {
c = "(preproc_def ((preproc_arg) @c (#offset! @c 0 2 0 -1))) (preproc_function_def value: (preproc_arg) @c)"}})
]])
@@ -538,7 +566,7 @@ int x = INT_MAX;
it("should return the correct language tree", function()
local result = exec_lua([[
parser = vim.treesitter.get_parser(0, "c", {
- queries = { c = "(preproc_def (preproc_arg) @c)"}})
+ injections = { c = "(preproc_def (preproc_arg) @c)"}})
local sub_tree = parser:language_for_range({1, 18, 1, 19})
@@ -572,28 +600,5 @@ int x = INT_MAX;
eq(result, "value")
end)
end)
-
- describe("when setting for a capture match", function()
- it("should set/get the data correctly", function()
- insert([[
- int x = 3;
- ]])
-
- local result = exec_lua([[
- local result
-
- query = vim.treesitter.parse_query("c", '((number_literal) @number (#set! @number "key" "value"))')
- parser = vim.treesitter.get_parser(0, "c")
-
- for pattern, match, metadata in query:iter_matches(parser:parse()[1]:root(), 0) do
- result = metadata[pattern].key
- end
-
- return result
- ]])
-
- eq(result, "value")
- end)
- end)
end)
end)