prompt_buffer_spec.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local Screen = require('test.functional.ui.screen')
  4. local feed = n.feed
  5. local source = n.source
  6. local clear = n.clear
  7. local command = n.command
  8. local expect = n.expect
  9. local poke_eventloop = n.poke_eventloop
  10. local api = n.api
  11. local eq = t.eq
  12. local neq = t.neq
  13. describe('prompt buffer', function()
  14. local screen
  15. before_each(function()
  16. clear()
  17. screen = Screen.new(25, 10)
  18. command('set laststatus=0 nohidden')
  19. end)
  20. local function source_script()
  21. source([[
  22. func TextEntered(text)
  23. if a:text == "exit"
  24. " Reset &modified to allow the buffer to be closed.
  25. set nomodified
  26. stopinsert
  27. close
  28. else
  29. " Add the output above the current prompt.
  30. call append(line("$") - 1, 'Command: "' . a:text . '"')
  31. " Reset &modified to allow the buffer to be closed.
  32. set nomodified
  33. call timer_start(20, {id -> TimerFunc(a:text)})
  34. endif
  35. endfunc
  36. func TimerFunc(text)
  37. " Add the output above the current prompt.
  38. call append(line("$") - 1, 'Result: "' . a:text .'"')
  39. " Reset &modified to allow the buffer to be closed.
  40. set nomodified
  41. endfunc
  42. func SwitchWindows()
  43. call timer_start(0, {-> execute("wincmd p", "")})
  44. endfunc
  45. call setline(1, "other buffer")
  46. set nomodified
  47. new
  48. set buftype=prompt
  49. call prompt_setcallback(bufnr(''), function("TextEntered"))
  50. eval bufnr("")->prompt_setprompt("cmd: ")
  51. startinsert
  52. ]])
  53. screen:expect([[
  54. cmd: ^ |
  55. {1:~ }|*3
  56. {3:[Prompt] [+] }|
  57. other buffer |
  58. {1:~ }|*3
  59. {5:-- INSERT --} |
  60. ]])
  61. end
  62. -- oldtest: Test_prompt_basic()
  63. it('works', function()
  64. source_script()
  65. feed('hello\n')
  66. screen:expect([[
  67. cmd: hello |
  68. Command: "hello" |
  69. Result: "hello" |
  70. cmd: ^ |
  71. {3:[Prompt] }|
  72. other buffer |
  73. {1:~ }|*3
  74. {5:-- INSERT --} |
  75. ]])
  76. feed('exit\n')
  77. screen:expect([[
  78. ^other buffer |
  79. {1:~ }|*8
  80. |
  81. ]])
  82. end)
  83. -- oldtest: Test_prompt_editing()
  84. it('editing', function()
  85. source_script()
  86. feed('hello<BS><BS>')
  87. screen:expect([[
  88. cmd: hel^ |
  89. {1:~ }|*3
  90. {3:[Prompt] [+] }|
  91. other buffer |
  92. {1:~ }|*3
  93. {5:-- INSERT --} |
  94. ]])
  95. feed('<Left><Left><Left><BS>-')
  96. screen:expect([[
  97. cmd: -^hel |
  98. {1:~ }|*3
  99. {3:[Prompt] [+] }|
  100. other buffer |
  101. {1:~ }|*3
  102. {5:-- INSERT --} |
  103. ]])
  104. feed('<C-O>lz')
  105. screen:expect([[
  106. cmd: -hz^el |
  107. {1:~ }|*3
  108. {3:[Prompt] [+] }|
  109. other buffer |
  110. {1:~ }|*3
  111. {5:-- INSERT --} |
  112. ]])
  113. feed('<End>x')
  114. screen:expect([[
  115. cmd: -hzelx^ |
  116. {1:~ }|*3
  117. {3:[Prompt] [+] }|
  118. other buffer |
  119. {1:~ }|*3
  120. {5:-- INSERT --} |
  121. ]])
  122. feed('<C-U>exit\n')
  123. screen:expect([[
  124. ^other buffer |
  125. {1:~ }|*8
  126. |
  127. ]])
  128. end)
  129. -- oldtest: Test_prompt_switch_windows()
  130. it('switch windows', function()
  131. source_script()
  132. feed('<C-O>:call SwitchWindows()<CR>')
  133. screen:expect([[
  134. cmd: |
  135. {1:~ }|*3
  136. {2:[Prompt] [+] }|
  137. ^other buffer |
  138. {1:~ }|*3
  139. |
  140. ]])
  141. feed('<C-O>:call SwitchWindows()<CR>')
  142. screen:expect([[
  143. cmd: ^ |
  144. {1:~ }|*3
  145. {3:[Prompt] [+] }|
  146. other buffer |
  147. {1:~ }|*3
  148. {5:-- INSERT --} |
  149. ]])
  150. feed('<Esc>')
  151. screen:expect([[
  152. cmd:^ |
  153. {1:~ }|*3
  154. {3:[Prompt] [+] }|
  155. other buffer |
  156. {1:~ }|*3
  157. |
  158. ]])
  159. end)
  160. -- oldtest: Test_prompt_while_writing_to_hidden_buffer()
  161. it('keeps insert mode after aucmd_restbuf in callback', function()
  162. source_script()
  163. source [[
  164. let s:buf = nvim_create_buf(1, 1)
  165. call timer_start(0, {-> nvim_buf_set_lines(s:buf, -1, -1, 0, ['walrus'])})
  166. ]]
  167. poke_eventloop()
  168. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  169. end)
  170. -- oldtest: Test_prompt_appending_while_hidden()
  171. it('accessing hidden prompt buffer does not start insert mode', function()
  172. local prev_win = api.nvim_get_current_win()
  173. source([[
  174. new prompt
  175. set buftype=prompt
  176. set bufhidden=hide
  177. func s:TextEntered(text)
  178. if a:text == 'exit'
  179. close
  180. endif
  181. let g:entered = a:text
  182. endfunc
  183. call prompt_setcallback(bufnr(), function('s:TextEntered'))
  184. func DoAppend()
  185. call appendbufline('prompt', '$', 'Test')
  186. return ''
  187. endfunc
  188. ]])
  189. feed('asomething<CR>')
  190. eq('something', api.nvim_get_var('entered'))
  191. neq(prev_win, api.nvim_get_current_win())
  192. feed('exit<CR>')
  193. eq(prev_win, api.nvim_get_current_win())
  194. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  195. command('call DoAppend()')
  196. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  197. feed('i')
  198. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  199. command('call DoAppend()')
  200. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  201. end)
  202. -- oldtest: Test_prompt_leave_modify_hidden()
  203. it('modifying hidden buffer does not prevent prompt buffer mode change', function()
  204. source([[
  205. file hidden
  206. set bufhidden=hide
  207. enew
  208. new prompt
  209. set buftype=prompt
  210. inoremap <buffer> w <Cmd>wincmd w<CR>
  211. inoremap <buffer> q <Cmd>bwipe!<CR>
  212. autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
  213. autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
  214. autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
  215. ]])
  216. feed('a')
  217. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  218. feed('w')
  219. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  220. feed('<C-W>w')
  221. eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
  222. feed('q')
  223. eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
  224. command('bwipe!')
  225. expect([[
  226. Leave
  227. Enter
  228. Leave
  229. Close]])
  230. end)
  231. end)