Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

any way to trigger CursorMoved in a test? #2

Closed
justinmk opened this issue Jan 30, 2014 · 5 comments
Closed

any way to trigger CursorMoved in a test? #2

justinmk opened this issue Jan 30, 2014 · 5 comments

Comments

@justinmk
Copy link

From what I can tell after reading docs/web, cursor movement in a script or macro, such as Vader, does not trigger the CursorMoved event. :help CursorMoved actually says something briefly on this:

Not triggered when there is typeahead or when an operator is pending.

So testing effects of a CursorMoved autocmd in Vader seems impossible, but hopefully I'm wrong. I've tried breaking up the moving in separate normal commands, and even tried things like this:

Execute (visual mode forwards):
  normal vwjs42
  call inputsave()
  normal ll
  Assert !exists("#SneakPlugin#CursorMoved#<buffer>")
  call inputrestore()
  normal os55;d

Since inputsave() clears the "typeahead", I was hopeing this would allow the test to trigger CursorMoved. But it doesn't.

You can verify the general problem with these commands:

au CursorMoved * echo 'hi'
normal lh

"hi" is not echoed, though if the user typed lh in normal mode, it would be echoed.

Any ideas? I will ping the vim_use mailing list too.

Thanks!

@junegunn
Copy link
Owner

Hmm, I wasn't aware of the issue, but it seems to be the case and I have no idea if it's possible to change the behavior. Given that, I guess the only way we can test CursorMoved event is to manually trigger it with doautocmd within the test 😕

Execute (setup):
  let g:cnt = 0
  augroup CursorMovedTest
    autocmd!
    autocmd CursorMoved * let g:cnt += 1
  augroup END

Given:
  hello world

Do:
  llllx
  :doautocmd CursorMoved\<CR>

Expect:
  hell world

Execute (cleanup):
  " 1 not 5 :/
  AssertEqual 1, g:cnt
  augroup CursorMovedTest
    autocmd!
  augroup END
  augroup! CursorMovedTest

@justinmk
Copy link
Author

doautocmd is good enough, thanks! I didn't know that existed.

justinmk added a commit to justinmk/vim-sneak that referenced this issue Jan 31, 2014
caveat: CursorMoved is not triggered in scripts/macros, so we must force it.
    junegunn/vader.vim#2
@blueyed
Copy link
Collaborator

blueyed commented Apr 2, 2018

Just for reference: this is not possible to test in Vader, because CursorMoved (and also TextChanged) are checked/emitted in the main loop, which is not used when using +Vader.

Vim itself does not appear to have any tests for CursorMoved, and only a single test for TextChanged (via the terminal, https://github.com/vim/vim/blob/f3ba14ffd32faa1856a99cf657c426bf9d1204ae/src/testdir/test_autocmd.vim#L1310-L1329).

Reminded me of #124, where (Neo)Vim wrapped in tmux could be used for tests like these.

But then it might be better to add support to Vim/Neovim for triggering a main loop iteration / autocommands check manually (via some option, so it would not be necessary to have an equivalent of doautocmd in tests).

@justinmk
Copy link
Author

justinmk commented Apr 2, 2018

so it would not be necessary to have an equivalent of doautocmd in tests

Is that really different than manually triggering the event loop? :)

@blueyed
Copy link
Collaborator

blueyed commented Apr 2, 2018

What I've meant was that you could set an option in test/vimrc which would trigger the event-checking part of the main loop after each command in a script (automatically).
This adds overhead however, so it seems still better to explicitly trigger those autocommands in tests then.

Took me a while to figure this out however.
I've tried to use -s (scriptin) then, which actually triggers the main loop, but only once.
Next step would be -e (exmode), but similar to -s this would require to have the commands in tests to be passed in there separately, and not just call :Vader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants