inspect_tree_spec.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local insert = n.insert
  5. local dedent = t.dedent
  6. local eq = t.eq
  7. local exec_lua = n.exec_lua
  8. local feed = n.feed
  9. describe('vim.treesitter.inspect_tree', function()
  10. before_each(clear)
  11. local expect_tree = function(x)
  12. local expected = vim.split(vim.trim(dedent(x)), '\n')
  13. local actual = n.buf_lines(0) ---@type string[]
  14. eq(expected, actual)
  15. end
  16. it('working', function()
  17. insert([[
  18. print()
  19. ]])
  20. exec_lua(function()
  21. vim.treesitter.start(0, 'lua')
  22. vim.treesitter.inspect_tree()
  23. end)
  24. expect_tree [[
  25. (chunk ; [0, 0] - [2, 0]
  26. (function_call ; [0, 0] - [0, 7]
  27. name: (identifier) ; [0, 0] - [0, 5]
  28. arguments: (arguments))) ; [0, 5] - [0, 7]
  29. ]]
  30. end)
  31. it('can toggle to show anonymous nodes', function()
  32. insert([[
  33. print('hello')
  34. ]])
  35. exec_lua(function()
  36. vim.treesitter.start(0, 'lua')
  37. vim.treesitter.inspect_tree()
  38. end)
  39. feed('a')
  40. expect_tree [[
  41. (chunk ; [0, 0] - [2, 0]
  42. (function_call ; [0, 0] - [0, 14]
  43. name: (identifier) ; [0, 0] - [0, 5]
  44. arguments: (arguments ; [0, 5] - [0, 14]
  45. "(" ; [0, 5] - [0, 6]
  46. (string ; [0, 6] - [0, 13]
  47. start: "'" ; [0, 6] - [0, 7]
  48. content: (string_content) ; [0, 7] - [0, 12]
  49. end: "'") ; [0, 12] - [0, 13]
  50. ")"))) ; [0, 13] - [0, 14]
  51. ]]
  52. end)
  53. it('works for injected trees', function()
  54. insert([[
  55. ```lua
  56. return
  57. ```
  58. ]])
  59. exec_lua(function()
  60. vim.treesitter.start(0, 'markdown')
  61. vim.treesitter.get_parser():parse()
  62. vim.treesitter.inspect_tree()
  63. end)
  64. expect_tree [[
  65. (document ; [0, 0] - [4, 0]
  66. (section ; [0, 0] - [4, 0]
  67. (fenced_code_block ; [0, 0] - [3, 0]
  68. (fenced_code_block_delimiter) ; [0, 0] - [0, 3]
  69. (info_string ; [0, 3] - [0, 6]
  70. (language)) ; [0, 3] - [0, 6]
  71. (block_continuation) ; [1, 0] - [1, 0]
  72. (code_fence_content ; [1, 0] - [2, 0]
  73. (chunk ; [1, 0] - [2, 0]
  74. (return_statement)) ; [1, 0] - [1, 6]
  75. (block_continuation)) ; [2, 0] - [2, 0]
  76. (fenced_code_block_delimiter)))) ; [2, 0] - [2, 3]
  77. ]]
  78. end)
  79. it('can toggle to show languages', function()
  80. insert([[
  81. ```lua
  82. return
  83. ```
  84. ]])
  85. exec_lua(function()
  86. vim.treesitter.start(0, 'markdown')
  87. vim.treesitter.get_parser():parse()
  88. vim.treesitter.inspect_tree()
  89. end)
  90. feed('I')
  91. expect_tree [[
  92. (document ; [0, 0] - [4, 0] markdown
  93. (section ; [0, 0] - [4, 0] markdown
  94. (fenced_code_block ; [0, 0] - [3, 0] markdown
  95. (fenced_code_block_delimiter) ; [0, 0] - [0, 3] markdown
  96. (info_string ; [0, 3] - [0, 6] markdown
  97. (language)) ; [0, 3] - [0, 6] markdown
  98. (block_continuation) ; [1, 0] - [1, 0] markdown
  99. (code_fence_content ; [1, 0] - [2, 0] markdown
  100. (chunk ; [1, 0] - [2, 0] lua
  101. (return_statement)) ; [1, 0] - [1, 6] lua
  102. (block_continuation)) ; [2, 0] - [2, 0] markdown
  103. (fenced_code_block_delimiter)))) ; [2, 0] - [2, 3] markdown
  104. ]]
  105. end)
  106. it('updates source and tree buffer windows and closes them correctly', function()
  107. insert([[
  108. print()
  109. ]])
  110. -- setup two windows for the source buffer
  111. exec_lua(function()
  112. _G.source_win = vim.api.nvim_get_current_win()
  113. vim.api.nvim_open_win(0, false, {
  114. win = 0,
  115. split = 'left',
  116. })
  117. end)
  118. -- setup three windows for the tree buffer
  119. exec_lua(function()
  120. vim.treesitter.start(0, 'lua')
  121. vim.treesitter.inspect_tree()
  122. _G.tree_win = vim.api.nvim_get_current_win()
  123. _G.tree_win_copy_1 = vim.api.nvim_open_win(0, false, {
  124. win = 0,
  125. split = 'left',
  126. })
  127. _G.tree_win_copy_2 = vim.api.nvim_open_win(0, false, {
  128. win = 0,
  129. split = 'left',
  130. })
  131. end)
  132. -- close original source window
  133. exec_lua('vim.api.nvim_win_close(source_win, false)')
  134. -- navigates correctly to the remaining source buffer window
  135. feed('<CR>')
  136. eq('', n.api.nvim_get_vvar('errmsg'))
  137. -- close original tree window
  138. exec_lua(function()
  139. vim.api.nvim_set_current_win(_G.tree_win_copy_1)
  140. vim.api.nvim_win_close(_G.tree_win, false)
  141. end)
  142. -- navigates correctly to the remaining source buffer window
  143. feed('<CR>')
  144. eq('', n.api.nvim_get_vvar('errmsg'))
  145. -- close source buffer window and all remaining tree windows
  146. t.pcall_err(exec_lua, 'vim.api.nvim_win_close(0, false)')
  147. eq(false, exec_lua('return vim.api.nvim_win_is_valid(tree_win_copy_1)'))
  148. eq(false, exec_lua('return vim.api.nvim_win_is_valid(tree_win_copy_2)'))
  149. end)
  150. end)