inlay_hint_spec.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local t_lsp = require('test.functional.plugin.lsp.testutil')
  5. local eq = t.eq
  6. local dedent = t.dedent
  7. local exec_lua = n.exec_lua
  8. local insert = n.insert
  9. local api = n.api
  10. local clear_notrace = t_lsp.clear_notrace
  11. local create_server_definition = t_lsp.create_server_definition
  12. describe('vim.lsp.inlay_hint', function()
  13. local text = dedent([[
  14. auto add(int a, int b) { return a + b; }
  15. int main() {
  16. int x = 1;
  17. int y = 2;
  18. return add(x,y);
  19. }
  20. }]])
  21. local response = [==[
  22. [
  23. {"kind":1,"paddingLeft":false,"label":"-> int","position":{"character":22,"line":0},"paddingRight":false},
  24. {"kind":2,"paddingLeft":false,"label":"a:","position":{"character":15,"line":5},"paddingRight":true},
  25. {"kind":2,"paddingLeft":false,"label":"b:","position":{"character":17,"line":5},"paddingRight":true}
  26. ]
  27. ]==]
  28. local grid_without_inlay_hints = [[
  29. auto add(int a, int b) { return a + b; } |
  30. |
  31. int main() { |
  32. int x = 1; |
  33. int y = 2; |
  34. return add(x,y); |
  35. } |
  36. ^} |
  37. |
  38. ]]
  39. local grid_with_inlay_hints = [[
  40. auto add(int a, int b){1:-> int} { return a + b; } |
  41. |
  42. int main() { |
  43. int x = 1; |
  44. int y = 2; |
  45. return add({1:a:} x,{1:b:} y); |
  46. } |
  47. ^} |
  48. |
  49. ]]
  50. --- @type test.functional.ui.screen
  51. local screen
  52. --- @type integer
  53. local client_id
  54. --- @type integer
  55. local bufnr
  56. before_each(function()
  57. clear_notrace()
  58. screen = Screen.new(50, 9)
  59. bufnr = n.api.nvim_get_current_buf()
  60. exec_lua(create_server_definition)
  61. client_id = exec_lua(function()
  62. _G.server = _G._create_server({
  63. capabilities = {
  64. inlayHintProvider = true,
  65. },
  66. handlers = {
  67. ['textDocument/inlayHint'] = function(_, _, callback)
  68. callback(nil, vim.json.decode(response))
  69. end,
  70. },
  71. })
  72. return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
  73. end)
  74. insert(text)
  75. exec_lua(function()
  76. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  77. end)
  78. screen:expect({ grid = grid_with_inlay_hints })
  79. end)
  80. after_each(function()
  81. api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  82. end)
  83. it('clears inlay hints when sole client detaches', function()
  84. exec_lua(function()
  85. vim.lsp.stop_client(client_id)
  86. end)
  87. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  88. end)
  89. it('does not clear inlay hints when one of several clients detaches', function()
  90. local client_id2 = exec_lua(function()
  91. _G.server2 = _G._create_server({
  92. capabilities = {
  93. inlayHintProvider = true,
  94. },
  95. handlers = {
  96. ['textDocument/inlayHint'] = function(_, _, callback)
  97. callback(nil, {})
  98. end,
  99. },
  100. })
  101. local client_id2 = vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd })
  102. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  103. return client_id2
  104. end)
  105. exec_lua(function()
  106. vim.lsp.stop_client(client_id2)
  107. end)
  108. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  109. end)
  110. describe('enable()', function()
  111. it('validation', function()
  112. t.matches(
  113. 'enable: expected boolean, got table',
  114. t.pcall_err(exec_lua, function()
  115. --- @diagnostic disable-next-line:param-type-mismatch
  116. vim.lsp.inlay_hint.enable({}, { bufnr = bufnr })
  117. end)
  118. )
  119. t.matches(
  120. 'enable: expected boolean, got number',
  121. t.pcall_err(exec_lua, function()
  122. --- @diagnostic disable-next-line:param-type-mismatch
  123. vim.lsp.inlay_hint.enable(42)
  124. end)
  125. )
  126. t.matches(
  127. 'filter: expected table, got number',
  128. t.pcall_err(exec_lua, function()
  129. --- @diagnostic disable-next-line:param-type-mismatch
  130. vim.lsp.inlay_hint.enable(true, 42)
  131. end)
  132. )
  133. end)
  134. describe('clears/applies inlay hints when passed false/true/nil', function()
  135. local bufnr2 --- @type integer
  136. before_each(function()
  137. bufnr2 = exec_lua(function()
  138. local bufnr2_0 = vim.api.nvim_create_buf(true, false)
  139. vim.lsp.buf_attach_client(bufnr2_0, client_id)
  140. vim.api.nvim_win_set_buf(0, bufnr2_0)
  141. return bufnr2_0
  142. end)
  143. insert(text)
  144. exec_lua(function()
  145. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr2 })
  146. end)
  147. n.api.nvim_win_set_buf(0, bufnr)
  148. screen:expect({ grid = grid_with_inlay_hints })
  149. end)
  150. it('for one single buffer', function()
  151. exec_lua(function()
  152. vim.lsp.inlay_hint.enable(false, { bufnr = bufnr })
  153. vim.api.nvim_win_set_buf(0, bufnr2)
  154. end)
  155. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  156. n.api.nvim_win_set_buf(0, bufnr)
  157. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  158. exec_lua(function()
  159. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  160. end)
  161. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  162. exec_lua(function()
  163. vim.lsp.inlay_hint.enable(
  164. not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }),
  165. { bufnr = bufnr }
  166. )
  167. end)
  168. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  169. exec_lua(function()
  170. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  171. end)
  172. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  173. end)
  174. it('for all buffers', function()
  175. exec_lua(function()
  176. vim.lsp.inlay_hint.enable(false)
  177. end)
  178. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  179. n.api.nvim_win_set_buf(0, bufnr2)
  180. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  181. exec_lua(function()
  182. vim.lsp.inlay_hint.enable(true)
  183. end)
  184. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  185. n.api.nvim_win_set_buf(0, bufnr)
  186. screen:expect({ grid = grid_with_inlay_hints, unchanged = true })
  187. end)
  188. end)
  189. end)
  190. describe('get()', function()
  191. it('returns filtered inlay hints', function()
  192. --- @type lsp.InlayHint[]
  193. local expected = vim.json.decode(response)
  194. local expected2 = {
  195. kind = 1,
  196. paddingLeft = false,
  197. label = ': int',
  198. position = {
  199. character = 10,
  200. line = 2,
  201. },
  202. paddingRight = false,
  203. }
  204. exec_lua(function()
  205. _G.server2 = _G._create_server({
  206. capabilities = {
  207. inlayHintProvider = true,
  208. },
  209. handlers = {
  210. ['textDocument/inlayHint'] = function(_, _, callback)
  211. callback(nil, { expected2 })
  212. end,
  213. },
  214. })
  215. _G.client2 = vim.lsp.start({ name = 'dummy2', cmd = _G.server2.cmd })
  216. vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })
  217. end)
  218. --- @type vim.lsp.inlay_hint.get.ret
  219. eq(
  220. {
  221. { bufnr = 1, client_id = 1, inlay_hint = expected[1] },
  222. { bufnr = 1, client_id = 1, inlay_hint = expected[2] },
  223. { bufnr = 1, client_id = 1, inlay_hint = expected[3] },
  224. { bufnr = 1, client_id = 2, inlay_hint = expected2 },
  225. },
  226. exec_lua(function()
  227. return vim.lsp.inlay_hint.get()
  228. end)
  229. )
  230. eq(
  231. {
  232. { bufnr = 1, client_id = 2, inlay_hint = expected2 },
  233. },
  234. exec_lua(function()
  235. return vim.lsp.inlay_hint.get({
  236. range = {
  237. start = { line = 2, character = 10 },
  238. ['end'] = { line = 2, character = 10 },
  239. },
  240. })
  241. end)
  242. )
  243. eq(
  244. {
  245. { bufnr = 1, client_id = 1, inlay_hint = expected[2] },
  246. { bufnr = 1, client_id = 1, inlay_hint = expected[3] },
  247. },
  248. exec_lua(function()
  249. return vim.lsp.inlay_hint.get({
  250. bufnr = vim.api.nvim_get_current_buf(),
  251. range = {
  252. start = { line = 4, character = 18 },
  253. ['end'] = { line = 5, character = 17 },
  254. },
  255. })
  256. end)
  257. )
  258. eq(
  259. {},
  260. exec_lua(function()
  261. return vim.lsp.inlay_hint.get({
  262. bufnr = vim.api.nvim_get_current_buf() + 1,
  263. })
  264. end)
  265. )
  266. end)
  267. end)
  268. end)
  269. describe('Inlay hints handler', function()
  270. local text = dedent([[
  271. test text
  272. ]])
  273. local response = [==[
  274. [
  275. { "position": { "line": 0, "character": 0 }, "label": "0" },
  276. { "position": { "line": 0, "character": 0 }, "label": "1" },
  277. { "position": { "line": 0, "character": 0 }, "label": "2" },
  278. { "position": { "line": 0, "character": 0 }, "label": "3" },
  279. { "position": { "line": 0, "character": 0 }, "label": "4" }
  280. ]
  281. ]==]
  282. local grid_without_inlay_hints = [[
  283. test text |
  284. ^ |
  285. |
  286. ]]
  287. local grid_with_inlay_hints = [[
  288. {1:01234}test text |
  289. ^ |
  290. |
  291. ]]
  292. --- @type test.functional.ui.screen
  293. local screen
  294. --- @type integer
  295. local client_id
  296. --- @type integer
  297. local bufnr
  298. before_each(function()
  299. clear_notrace()
  300. screen = Screen.new(50, 3)
  301. exec_lua(create_server_definition)
  302. bufnr = n.api.nvim_get_current_buf()
  303. client_id = exec_lua(function()
  304. _G.server = _G._create_server({
  305. capabilities = {
  306. inlayHintProvider = true,
  307. },
  308. handlers = {
  309. ['textDocument/inlayHint'] = function(_, _, callback)
  310. callback(nil, vim.json.decode(response))
  311. end,
  312. },
  313. })
  314. vim.api.nvim_win_set_buf(0, bufnr)
  315. return vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
  316. end)
  317. insert(text)
  318. end)
  319. it('renders hints with same position in received order', function()
  320. exec_lua([[vim.lsp.inlay_hint.enable(true, { bufnr = bufnr })]])
  321. screen:expect({ grid = grid_with_inlay_hints })
  322. exec_lua(function()
  323. vim.lsp.stop_client(client_id)
  324. end)
  325. screen:expect({ grid = grid_without_inlay_hints, unchanged = true })
  326. end)
  327. after_each(function()
  328. api.nvim_exec_autocmds('VimLeavePre', { modeline = false })
  329. end)
  330. end)