robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
10.0 KiB309 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local insert = n.insert
7local dedent = t.dedent
8local eq = t.eq
9local exec_lua = n.exec_lua
10local feed = n.feed
11
12describe('vim.treesitter.inspect_tree', function()
13  before_each(clear)
14
15  local expect_tree = function(x)
16    local expected = vim.split(vim.trim(dedent(x)), '\n')
17    local actual = n.buf_lines(0) ---@type string[]
18    eq(expected, actual)
19  end
20
21  it('working', function()
22    insert([[
23      print()
24      ]])
25
26    exec_lua(function()
27      vim.treesitter.start(0, 'lua')
28      vim.treesitter.inspect_tree()
29    end)
30
31    expect_tree [[
32      (chunk ; [0, 0] - [2, 0]
33        (function_call ; [0, 0] - [0, 7]
34          name: (identifier) ; [0, 0] - [0, 5]
35          arguments: (arguments))) ; [0, 5] - [0, 7]
36      ]]
37  end)
38
39  it('sets correct buffer name', function()
40    n.api.nvim_set_current_dir(t.paths.test_source_path .. '/test/functional/fixtures')
41    n.command('edit lua/syntax_error.lua')
42    eq('lua/syntax_error.lua', n.fn.bufname('%'))
43    local full_path = n.api.nvim_buf_get_name(0)
44
45    n.exec_lua('vim.treesitter.start(0, "lua")')
46    n.exec_lua('vim.treesitter.inspect_tree()')
47    local expected = [[
48      (chunk ; [0, 0] - [1, 0]
49        (ERROR ; [0, 0] - [0, 21]
50          (identifier) ; [0, 5] - [0, 8]
51          (identifier) ; [0, 9] - [0, 15]
52          (identifier))) ; [0, 16] - [0, 21]
53      ]]
54    expect_tree(expected)
55    eq('Syntax tree for lua/syntax_error.lua', n.fn.bufname('%'))
56
57    n.command('bwipe!')
58    eq('lua/syntax_error.lua', n.fn.bufname('%'))
59
60    n.api.nvim_set_current_dir('pack')
61    eq(full_path, n.fn.bufname('%'))
62
63    n.exec_lua('vim.treesitter.inspect_tree()')
64    expect_tree(expected)
65    eq('Syntax tree for ' .. full_path, n.fn.bufname('%'))
66  end)
67
68  it('can toggle to show anonymous nodes', function()
69    insert([[
70      print('hello')
71      ]])
72
73    exec_lua(function()
74      vim.treesitter.start(0, 'lua')
75      vim.treesitter.inspect_tree()
76    end)
77    feed('a')
78
79    expect_tree [[
80      (chunk ; [0, 0] - [2, 0]
81        (function_call ; [0, 0] - [0, 14]
82          name: (identifier) ; [0, 0] - [0, 5]
83          arguments: (arguments ; [0, 5] - [0, 14]
84            "(" ; [0, 5] - [0, 6]
85            (string ; [0, 6] - [0, 13]
86              start: "'" ; [0, 6] - [0, 7]
87              content: (string_content) ; [0, 7] - [0, 12]
88              end: "'") ; [0, 12] - [0, 13]
89            ")"))) ; [0, 13] - [0, 14]
90      ]]
91  end)
92
93  it('works for injected trees', function()
94    insert([[
95      ```lua
96      return
97      ```
98      ]])
99
100    exec_lua(function()
101      vim.treesitter.start(0, 'markdown')
102      vim.treesitter.get_parser():parse()
103      vim.treesitter.inspect_tree()
104    end)
105
106    expect_tree [[
107      (document ; [0, 0] - [4, 0]
108        (section ; [0, 0] - [4, 0]
109          (fenced_code_block ; [0, 0] - [3, 0]
110            (fenced_code_block_delimiter) ; [0, 0] - [0, 3]
111            (info_string ; [0, 3] - [0, 6]
112              (language)) ; [0, 3] - [0, 6]
113            (block_continuation) ; [1, 0] - [1, 0]
114            (code_fence_content ; [1, 0] - [2, 0]
115              (chunk ; [1, 0] - [2, 0]
116                (return_statement)) ; [1, 0] - [1, 6]
117              (block_continuation)) ; [2, 0] - [2, 0]
118            (fenced_code_block_delimiter)))) ; [2, 0] - [2, 3]
119      ]]
120  end)
121
122  it('works with multiple injection on the same node', function()
123    insert([[--* #include<stdio.h>]])
124    exec_lua(function()
125      vim.treesitter.query.set(
126        'lua',
127        'injections',
128        [[
129        (comment
130          content: (_) @injection.content
131          (#set! injection.language "markdown"))
132        (comment
133          content: (_) @injection.content
134          (#set! injection.language "c")
135          (#offset! @injection.content 0 1 0 0))
136        ]]
137      )
138      vim.treesitter.start(0, 'lua')
139      vim.treesitter.get_parser():parse(true)
140      vim.treesitter.inspect_tree()
141    end)
142    feed('I')
143    expect_tree [[
144      (chunk ; [0, 0] - [1, 0] lua
145        (comment ; [0, 0] - [0, 21] lua
146          content: (comment_content ; [0, 2] - [0, 21] lua
147            (document ; [0, 2] - [0, 21] markdown
148              (section ; [0, 2] - [0, 21] markdown
149                (list ; [0, 2] - [0, 21] markdown
150                  (list_item ; [0, 2] - [0, 21] markdown
151                    (list_marker_star) ; [0, 2] - [0, 4] markdown
152                    (paragraph ; [0, 4] - [0, 21] markdown
153                      (inline ; [0, 4] - [0, 21] markdown
154                        (inline))))))) ; [0, 4] - [0, 21] markdown_inline
155            (translation_unit ; [0, 4] - [0, 21] c
156              (preproc_include ; [0, 4] - [0, 21] c
157                path: (system_lib_string)))))) ; [0, 12] - [0, 21] c
158    ]]
159  end)
160
161  it('can toggle to show languages', function()
162    insert([[
163      ```lua
164      return
165      ```
166      ]])
167
168    exec_lua(function()
169      vim.treesitter.start(0, 'markdown')
170      vim.treesitter.get_parser():parse()
171      vim.treesitter.inspect_tree()
172    end)
173    feed('I')
174
175    expect_tree [[
176      (document ; [0, 0] - [4, 0] markdown
177        (section ; [0, 0] - [4, 0] markdown
178          (fenced_code_block ; [0, 0] - [3, 0] markdown
179            (fenced_code_block_delimiter) ; [0, 0] - [0, 3] markdown
180            (info_string ; [0, 3] - [0, 6] markdown
181              (language)) ; [0, 3] - [0, 6] markdown
182            (block_continuation) ; [1, 0] - [1, 0] markdown
183            (code_fence_content ; [1, 0] - [2, 0] markdown
184              (chunk ; [1, 0] - [2, 0] lua
185                (return_statement)) ; [1, 0] - [1, 6] lua
186              (block_continuation)) ; [2, 0] - [2, 0] markdown
187            (fenced_code_block_delimiter)))) ; [2, 0] - [2, 3] markdown
188      ]]
189  end)
190
191  it('updates source and tree buffer windows and closes them correctly', function()
192    local name = t.tmpname()
193    n.command('edit ' .. name)
194    insert([[
195      print()
196      ]])
197    n.command('set filetype=lua | write')
198
199    -- setup two windows for the source buffer
200    exec_lua(function()
201      _G.source_win = vim.api.nvim_get_current_win()
202      _G.source_win2 = vim.api.nvim_open_win(0, false, {
203        win = 0,
204        split = 'left',
205      })
206    end)
207
208    -- setup three windows for the tree buffer
209    exec_lua(function()
210      vim.treesitter.inspect_tree()
211      _G.tree_win = vim.api.nvim_get_current_win()
212      _G.tree_win2 = vim.api.nvim_open_win(0, false, {
213        win = 0,
214        split = 'left',
215      })
216      _G.tree_win3 = vim.api.nvim_open_win(0, false, {
217        win = 0,
218        split = 'left',
219      })
220    end)
221
222    -- close original source window without closing tree views
223    exec_lua('vim.api.nvim_set_current_win(source_win)')
224    feed(':quit<CR>')
225    eq('', n.api.nvim_get_vvar('errmsg'))
226    eq(true, exec_lua('return vim.api.nvim_win_is_valid(tree_win)'))
227    eq(true, exec_lua('return vim.api.nvim_win_is_valid(tree_win2)'))
228    eq(true, exec_lua('return vim.api.nvim_win_is_valid(tree_win3)'))
229
230    -- navigates correctly to the remaining source buffer window
231    exec_lua('vim.api.nvim_set_current_win(tree_win)')
232    feed('<CR>')
233    eq('', n.api.nvim_get_vvar('errmsg'))
234    eq(true, exec_lua('return vim.api.nvim_get_current_win() == source_win2'))
235
236    -- close original tree window
237    exec_lua(function()
238      vim.api.nvim_set_current_win(_G.tree_win2)
239      vim.api.nvim_win_close(_G.tree_win, false)
240    end)
241
242    -- navigates correctly to the remaining source buffer window
243    feed('<CR>')
244    eq('', n.api.nvim_get_vvar('errmsg'))
245    eq(true, exec_lua('return vim.api.nvim_get_current_win() == source_win2'))
246
247    -- close source buffer window and all remaining tree windows
248    n.expect_exit(n.command, 'quit')
249  end)
250
251  it('shows which nodes are missing', function()
252    insert([[
253      int main() {
254          if (a.) {
255          //    ^ MISSING field_identifier here
256              if (1) d()
257              //        ^ MISSING ";" here
258          }
259      }
260      ]])
261
262    exec_lua(function()
263      vim.treesitter.start(0, 'c')
264      vim.treesitter.inspect_tree()
265    end)
266    feed('a')
267
268    expect_tree [[
269      (translation_unit ; [0, 0] - [8, 0]
270        (function_definition ; [0, 0] - [6, 1]
271          type: (primitive_type) ; [0, 0] - [0, 3]
272          declarator: (function_declarator ; [0, 4] - [0, 10]
273            declarator: (identifier) ; [0, 4] - [0, 8]
274            parameters: (parameter_list ; [0, 8] - [0, 10]
275              "(" ; [0, 8] - [0, 9]
276              ")")) ; [0, 9] - [0, 10]
277          body: (compound_statement ; [0, 11] - [6, 1]
278            "{" ; [0, 11] - [0, 12]
279            (if_statement ; [1, 4] - [5, 5]
280              "if" ; [1, 4] - [1, 6]
281              condition: (parenthesized_expression ; [1, 7] - [1, 11]
282                "(" ; [1, 7] - [1, 8]
283                (field_expression ; [1, 8] - [1, 10]
284                  argument: (identifier) ; [1, 8] - [1, 9]
285                  operator: "." ; [1, 9] - [1, 10]
286                  field: (MISSING field_identifier)) ; [1, 10] - [1, 10]
287                ")") ; [1, 10] - [1, 11]
288              consequence: (compound_statement ; [1, 12] - [5, 5]
289                "{" ; [1, 12] - [1, 13]
290                (comment) ; [2, 4] - [2, 41]
291                (if_statement ; [3, 8] - [4, 36]
292                  "if" ; [3, 8] - [3, 10]
293                  condition: (parenthesized_expression ; [3, 11] - [3, 14]
294                    "(" ; [3, 11] - [3, 12]
295                    (number_literal) ; [3, 12] - [3, 13]
296                    ")") ; [3, 13] - [3, 14]
297                  consequence: (expression_statement ; [3, 15] - [4, 36]
298                    (call_expression ; [3, 15] - [3, 18]
299                      function: (identifier) ; [3, 15] - [3, 16]
300                      arguments: (argument_list ; [3, 16] - [3, 18]
301                        "(" ; [3, 16] - [3, 17]
302                        ")")) ; [3, 17] - [3, 18]
303                    (comment) ; [4, 8] - [4, 36]
304                    (MISSING ";"))) ; [4, 36] - [4, 36]
305                "}")) ; [5, 4] - [5, 5]
306            "}"))) ; [6, 0] - [6, 1]
307      ]]
308  end)
309end)