Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions crates/tests/fixtures/nvim/focus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ package.loaded['argon.client'] = {
local focus = require('argon.focus')

local stage = 1
local exit_keys = { '<CR>', '<CR>', '<Esc>', '<C-C>', '<Esc>' }
local exit_keys = { '<CR>', '<CR>', '<Esc>', '<C-C>', '<Esc>', '<CR>' }
local pending_command = 'let g:argon_focus_pending = 1'
local pending_replaced = false

local function checked(callback)
local ok, err = pcall(callback)
Expand All @@ -26,13 +28,28 @@ end

vim.api.nvim_create_autocmd('CmdlineEnter', {
callback = function()
-- The pending command line stays open until the GUI replaces it.
if stage == 6 and not pending_replaced then
return
end
local exit_key = assert(exit_keys[stage])
vim.schedule(function()
vim.api.nvim_input(exit_key)
end)
end,
})

vim.api.nvim_create_autocmd('CmdlineChanged', {
callback = function()
if stage == 6 and not pending_replaced and vim.fn.getcmdline() == pending_command then
pending_replaced = true
vim.schedule(function()
focus.editor('let g:argon_focus_replaced = 1')
end)
end
end,
})

vim.api.nvim_create_autocmd('CmdlineLeave', {
callback = function()
vim.defer_fn(function()
Expand All @@ -57,9 +74,14 @@ vim.api.nvim_create_autocmd('CmdlineLeave', {
assert(gui_focus_count == 3, 'interrupting a GUI command should focus the GUI')
stage = 5
vim.api.nvim_input(':let g:argon_ordinary_command = 1')
else
elseif stage == 5 then
assert(vim.g.argon_ordinary_command == nil)
assert(gui_focus_count == 3, 'cancelling an ordinary command should remain in Neovim')
stage = 6
vim.api.nvim_input(':' .. pending_command)
elseif vim.g.argon_focus_replaced == 1 then
assert(vim.g.argon_focus_pending == nil, 'a GUI command should abandon a pending command line')
assert(gui_focus_count == 4, 'executing a GUI command should focus the GUI')
vim.cmd('quitall!')
end
end)
Expand Down
3 changes: 2 additions & 1 deletion crates/tests/src/nvim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ mod tests {
#[tokio::test]
async fn gui_commands_respect_return_focus_configuration() {
let mut command = crate::nvim_command();
// Sourced rather than run with `-l`, which exits before typeahead is processed.
command
.arg("-l")
.arg("-S")
.arg(crate::repository_root().join("crates/tests/fixtures/nvim/focus.lua"));
let child = command.spawn().expect("start headless Neovim");
crate::finish_nvim(child).await;
Expand Down
7 changes: 4 additions & 3 deletions lua/argon/focus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ end
---@param opts? { return_to_gui?: boolean }
function M.editor(command, opts)
opts = opts or {}
local mode = vim.api.nvim_get_mode().mode
local keys = mode:sub(1, 1) == 't' and '<C-\\><C-N>:' or '<Esc>:'
-- <C-\><C-N> reaches Normal mode from any mode. A fed <Esc> would instead
-- execute a pending command line, and an error there drops the keys after it.
local keys = '<C-\\><C-N>:'
if type(command) == 'string' then
keys = keys .. command
end

-- Wait for this command line to be entered before watching it leave. The
-- leading <Esc> may itself leave a pre-existing command line or prompt.
-- leading <C-\><C-N> may itself leave a pre-existing command line.
vim.api.nvim_clear_autocmds({ group = gui_command_group })
if opts.return_to_gui ~= false then
return_to_gui_after_command()
Expand Down
Loading