diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-08-21 12:26:02 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-08-21 14:16:16 +0800 |
commit | 6bf5b2428b146330688922d66438357c0568725d (patch) | |
tree | 89372aa73110ab7d3446d29a43ae020ce28cb70f | |
parent | 7ce2acd59be89e5e6d2ac778ad074a9ae42951cd (diff) | |
download | rneovim-6bf5b2428b146330688922d66438357c0568725d.tar.gz rneovim-6bf5b2428b146330688922d66438357c0568725d.tar.bz2 rneovim-6bf5b2428b146330688922d66438357c0568725d.zip |
vim-patch:8.1.2066: no tests for state()
Problem: No tests for state().
Solution: Add tests. Clean up some feature checks. Make "a" flag work.
https://github.com/vim/vim/commit/c2585490321854ca3df115efcf0b40986901d96c
Co-authored-by: Bram Moolenaar <Bram@vim.org>
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 2 | ||||
-rw-r--r-- | test/functional/vimscript/state_spec.lua | 69 | ||||
-rw-r--r-- | test/old/testdir/test_functions.vim | 59 |
4 files changed, 130 insertions, 1 deletions
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 64b875c981..e786901c2f 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -10483,6 +10483,7 @@ M.funcs = { recursiveness up to "ccc") s screen has scrolled for messages ]=], + fast = true, name = 'state', params = { { 'what', 'string' } }, signature = 'state([{what}])', diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 8c07623369..3c232a7163 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -4952,7 +4952,7 @@ static void f_state(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (autocmd_busy) { may_add_state_char(&ga, include, 'x'); } - if (!ctrl_x_mode_none()) { + if (ins_compl_active()) { may_add_state_char(&ga, include, 'a'); } if (!get_was_safe_state()) { diff --git a/test/functional/vimscript/state_spec.lua b/test/functional/vimscript/state_spec.lua new file mode 100644 index 0000000000..dcff3c8d7e --- /dev/null +++ b/test/functional/vimscript/state_spec.lua @@ -0,0 +1,69 @@ +local helpers = require('test.functional.helpers')(after_each) +local clear = helpers.clear +local eq = helpers.eq +local exec = helpers.exec +local exec_lua = helpers.exec_lua +local feed = helpers.feed +local meths = helpers.meths +local poke_eventloop = helpers.poke_eventloop + +before_each(clear) + +describe('state() function', function() + it('works', function() + meths.ui_attach(80, 24, {}) -- Allow hit-enter-prompt + + exec_lua([[ + function _G.Get_state_mode() + _G.res = { vim.fn.state(), vim.api.nvim_get_mode().mode:sub(1, 1) } + end + + function _G.Run_timer() + local timer = vim.uv.new_timer() + timer:start(0, 0, function() + _G.Get_state_mode() + timer:close() + end) + end + ]]) + exec([[ + call setline(1, ['one', 'two', 'three']) + map ;; gg + func RunTimer() + call timer_start(0, {id -> v:lua.Get_state_mode()}) + endfunc + au Filetype foobar call v:lua.Get_state_mode() + ]]) + + -- Using a ":" command Vim is busy, thus "S" is returned + feed([[:call v:lua.Get_state_mode()<CR>]]) + eq({ 'S', 'n' }, exec_lua('return _G.res')) + + -- Using a timer callback + feed([[:call RunTimer()<CR>]]) + poke_eventloop() -- Allow polling for events + eq({ 'c', 'n' }, exec_lua('return _G.res')) + + -- Halfway a mapping + feed([[:call v:lua.Run_timer()<CR>;]]) + meths.get_mode() -- Allow polling for fast events + feed(';') + eq({ 'mS', 'n' }, exec_lua('return _G.res')) + + -- Insert mode completion + feed([[:call RunTimer()<CR>Got<C-X><C-N>]]) + poke_eventloop() -- Allow polling for events + feed('<Esc>') + eq({ 'aSc', 'i' }, exec_lua('return _G.res')) + + -- Autocommand executing + feed([[:set filetype=foobar<CR>]]) + eq({ 'xS', 'n' }, exec_lua('return _G.res')) + + -- messages scrolled + feed([[:call v:lua.Run_timer() | echo "one\ntwo\nthree"<CR>]]) + meths.get_mode() -- Allow polling for fast events + feed('<CR>') + eq({ 'Ss', 'r' }, exec_lua('return _G.res')) + end) +end) diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim index 4ad01c8531..759cde7d5a 100644 --- a/test/old/testdir/test_functions.vim +++ b/test/old/testdir/test_functions.vim @@ -2565,6 +2565,65 @@ func Test_bufadd_bufload() call delete('XotherName') endfunc +func Test_state() + CheckRunVimInTerminal + + let lines =<< trim END + call setline(1, ['one', 'two', 'three']) + map ;; gg + func RunTimer() + call timer_start(10, {id -> execute('let g:state = state()') .. execute('let g:mode = mode()')}) + endfunc + au Filetype foobar let g:state = state()|let g:mode = mode() + END + call writefile(lines, 'XState') + let buf = RunVimInTerminal('-S XState', #{rows: 6}) + + " Using a ":" command Vim is busy, thus "S" is returned + call term_sendkeys(buf, ":echo 'state: ' .. state() .. '; mode: ' .. mode()\<CR>") + call WaitForAssert({-> assert_match('state: S; mode: n', term_getline(buf, 6))}, 1000) + call term_sendkeys(buf, ":\<CR>") + + " Using a timer callback + call term_sendkeys(buf, ":call RunTimer()\<CR>") + call term_wait(buf, 50) + let getstate = ":echo 'state: ' .. g:state .. '; mode: ' .. g:mode\<CR>" + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: c; mode: n', term_getline(buf, 6))}, 1000) + + " Halfway a mapping + call term_sendkeys(buf, ":call RunTimer()\<CR>;") + call term_wait(buf, 50) + call term_sendkeys(buf, ";") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: mSc; mode: n', term_getline(buf, 6))}, 1000) + + " Insert mode completion + call term_sendkeys(buf, ":call RunTimer()\<CR>Got\<C-N>") + call term_wait(buf, 50) + call term_sendkeys(buf, "\<Esc>") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: aSc; mode: i', term_getline(buf, 6))}, 1000) + + " Autocommand executing + call term_sendkeys(buf, ":set filetype=foobar\<CR>") + call term_wait(buf, 50) + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: xS; mode: n', term_getline(buf, 6))}, 1000) + + " Todo: "w" - waiting for ch_evalexpr() + + " messages scrolled + call term_sendkeys(buf, ":call RunTimer()\<CR>:echo \"one\\ntwo\\nthree\"\<CR>") + call term_wait(buf, 50) + call term_sendkeys(buf, "\<CR>") + call term_sendkeys(buf, getstate) + call WaitForAssert({-> assert_match('state: Scs; mode: r', term_getline(buf, 6))}, 1000) + + call StopVimInTerminal(buf) + call delete('XState') +endfunc + func Test_range() " destructuring let [x, y] = range(2) |