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

Add partial support for getcmdtype() function #1039

Merged
merged 3 commits into from
Nov 22, 2024

Conversation

jphalip
Copy link
Contributor

@jphalip jphalip commented Nov 14, 2024

The getcmdtype() could be called from inside a prompt (search, expression, command), or from a remapping. Apparently IdeaVim currently doesn't support nested prompts (e.g. by typing :<C-r>= to show an expression prompt inside a command prompt), so it wouldn't be functional in that case.

It should work in a remapping though, e.g.: :cnoremap <F2> <C-r>=getcmdtype()<CR>. Could you advise on how I could unit test this?

For context, this is the remap that I'm using this for:

cnoremap <expr> <Esc> getcmdtype() =~ '[/?]' ? '<CR><Esc>' : '<Esc>'

This is so that if you press <Escape> during a search before you press enter, it keeps the cursor at the first search result instead of jumping back to where the cursor was before the search started. This enforces a behavior that you find in most text search tools, including IntelliJ's built-in search.

@jphalip
Copy link
Contributor Author

jphalip commented Nov 14, 2024

By the way, do you know what the "input command" (labelled @) corresponds to? For reference, here's the manual entry:

getcmdtype()

		Return the current command-line type. Possible return values
		are:
		    :	normal Ex command
		    >	debug mode command [debug-mode](https://neovim.io/doc/user/repeat.html#debug-mode)
		    /	forward search command
		    ?	backward search command
		    @	[input()](https://neovim.io/doc/user/builtin.html#input()) command
		    -  	[:insert](https://neovim.io/doc/user/insert.html#%3Ainsert) or [:append](https://neovim.io/doc/user/insert.html#%3Aappend) command
		    =	[i_CTRL-R_=](https://neovim.io/doc/user/insert.html#i_CTRL-R_%3D)  
		Only works when editing the command line, thus requires use of
		[c_CTRL-\_e](https://neovim.io/doc/user/cmdline.html#c_CTRL-%5C_e) or [c_CTRL-R_=](https://neovim.io/doc/user/cmdline.html#c_CTRL-R_%3D) or an expression mapping.
		Returns an empty string otherwise.
		Also see [getcmdpos()](https://neovim.io/doc/user/builtin.html#getcmdpos()), [setcmdpos()](https://neovim.io/doc/user/builtin.html#setcmdpos()) and [getcmdline()](https://neovim.io/doc/user/builtin.html#getcmdline()).

@citizenmatt
Copy link
Member

The input command is the prompt shown when someone calls the input() function. So you could do something like this in Vim:

cmap <expr> z getcmdtype()

Now every time you type z in any kind of prompt, it will add the appropriate symbol to the prompt text. For a command, you'll get a : symbol and search will give / or ?. If you then type:

:call input("Prompt> ")

You'll get an input prompt for some kind of text (e.g. Vim script to capture something) and typing z will add @ to the prompt.

As for testing, a simple map like that would do it. Something like:

enterCommand("cmap <expr> z getcmdtype()")
typeText(":z")
assertEquals(":", injector.commandLine.visibleText)

or

enterCommand("cmap <expr> z getcmdtype()")
typeText("/fooz")
assertEquals("foo/", injector.commandLine.visibleText)

etc. Testing the expression register would be very similar, but with a different map:

enterCommand("cnoremap <F2> <C-r>=getcmdtype()<CR>")
typeText("?foo<F2>")
assertEquals("foo?", injector.commandLine.visibleText)

@jphalip
Copy link
Contributor Author

jphalip commented Nov 20, 2024

@citizenmatt Thanks a lot for the details. I just added some tests for :/?=. However, I'm not sure input, insert, and append command would be supported since I think they'd require nested prompts, which doesn't work in IdeaVim. Also, I'm not sure about the debug mode (>) -- Is that reproducible and testable with IdeaVim?

@citizenmatt
Copy link
Member

citizenmatt commented Nov 20, 2024

Looks good to me, thanks. I wouldn't worry about debug-mode, it's not supported by IdeaVim.

@AlexPl292 AlexPl292 enabled auto-merge (rebase) November 22, 2024 14:41
@AlexPl292
Copy link
Member

Sweet, thank you!

@AlexPl292 AlexPl292 merged commit c75e675 into JetBrains:master Nov 22, 2024
4 checks passed
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

Successfully merging this pull request may close these issues.

3 participants