command_line_completion_spec.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. local helpers = require('test.functional.helpers')(after_each)
  2. local clear = helpers.clear
  3. local eq = helpers.eq
  4. local exec_lua = helpers.exec_lua
  5. local get_completions = function(input, env)
  6. return exec_lua("return {vim._expand_pat(...)}", '^' .. input, env)
  7. end
  8. local get_compl_parts = function(parts)
  9. return exec_lua("return {vim._expand_pat_get_parts(...)}", parts)
  10. end
  11. before_each(clear)
  12. describe('nlua_expand_pat', function()
  13. it('should complete exact matches', function()
  14. eq({{'exact'}, 0}, get_completions('exact', { exact = true }))
  15. end)
  16. it('should return empty table when nothing matches', function()
  17. eq({{}, 0}, get_completions('foo', { bar = true }))
  18. end)
  19. it('should return nice completions with function call prefix', function()
  20. eq({{'FOO'}, 6}, get_completions('print(F', { FOO = true, bawr = true }))
  21. end)
  22. it('should return keys for nested dictionaries', function()
  23. eq(
  24. {{
  25. 'nvim_buf_set_lines',
  26. 'nvim_buf_set_option'
  27. }, 8
  28. },
  29. get_completions('vim.api.nvim_buf_', {
  30. vim = {
  31. api = {
  32. nvim_buf_set_lines = true,
  33. nvim_buf_set_option = true,
  34. nvim_win_doesnt_match = true,
  35. },
  36. other_key = true,
  37. }
  38. })
  39. )
  40. end)
  41. it('it should work with colons', function()
  42. eq(
  43. {{
  44. 'bawr',
  45. 'baz',
  46. }, 8
  47. },
  48. get_completions('MyClass:b', {
  49. MyClass = {
  50. baz = true,
  51. bawr = true,
  52. foo = false,
  53. }
  54. })
  55. )
  56. end)
  57. it('should return keys for string reffed dictionaries', function()
  58. eq(
  59. {{
  60. 'nvim_buf_set_lines',
  61. 'nvim_buf_set_option'
  62. }, 11
  63. },
  64. get_completions('vim["api"].nvim_buf_', {
  65. vim = {
  66. api = {
  67. nvim_buf_set_lines = true,
  68. nvim_buf_set_option = true,
  69. nvim_win_doesnt_match = true,
  70. },
  71. other_key = true,
  72. }
  73. })
  74. )
  75. end)
  76. it('should return keys for string reffed dictionaries', function()
  77. eq(
  78. {{
  79. 'nvim_buf_set_lines',
  80. 'nvim_buf_set_option'
  81. }, 21
  82. },
  83. get_completions('vim["nested"]["api"].nvim_buf_', {
  84. vim = {
  85. nested = {
  86. api = {
  87. nvim_buf_set_lines = true,
  88. nvim_buf_set_option = true,
  89. nvim_win_doesnt_match = true,
  90. },
  91. },
  92. other_key = true,
  93. }
  94. })
  95. )
  96. end)
  97. it('should be able to interpolate globals', function()
  98. eq(
  99. {{
  100. 'nvim_buf_set_lines',
  101. 'nvim_buf_set_option'
  102. }, 12
  103. },
  104. get_completions('vim[MY_VAR].nvim_buf_', {
  105. MY_VAR = "api",
  106. vim = {
  107. api = {
  108. nvim_buf_set_lines = true,
  109. nvim_buf_set_option = true,
  110. nvim_win_doesnt_match = true,
  111. },
  112. other_key = true,
  113. }
  114. })
  115. )
  116. end)
  117. it('should return everything if the input is of length 0', function()
  118. eq({{"other", "vim"}, 0}, get_completions('', { vim = true, other = true }))
  119. end)
  120. describe('get_parts', function()
  121. it('should return an empty list for no separators', function()
  122. eq({{}, 1}, get_compl_parts("vim"))
  123. end)
  124. it('just the first item before a period', function()
  125. eq({{"vim"}, 5}, get_compl_parts("vim.ap"))
  126. end)
  127. it('should return multiple parts just for period', function()
  128. eq({{"vim", "api"}, 9}, get_compl_parts("vim.api.nvim_buf"))
  129. end)
  130. it('should be OK with colons', function()
  131. eq({{"vim", "api"}, 9}, get_compl_parts("vim:api.nvim_buf"))
  132. end)
  133. it('should work for just one string ref', function()
  134. eq({{"vim", "api"}, 12}, get_compl_parts("vim['api'].nvim_buf"))
  135. end)
  136. it('should work for just one string ref, with double quote', function()
  137. eq({{"vim", "api"}, 12}, get_compl_parts('vim["api"].nvim_buf'))
  138. end)
  139. it('should allows back-to-back string ref', function()
  140. eq({{"vim", "nested", "api"}, 22}, get_compl_parts('vim["nested"]["api"].nvim_buf'))
  141. end)
  142. it('should allows back-to-back string ref with spaces before and after', function()
  143. eq({{"vim", "nested", "api"}, 25}, get_compl_parts('vim[ "nested" ]["api"].nvim_buf'))
  144. end)
  145. it('should allow VAR style loolup', function()
  146. eq({{"vim", {"NESTED"}, "api"}, 20}, get_compl_parts('vim[NESTED]["api"].nvim_buf'))
  147. end)
  148. end)
  149. end)