From ca8c641cf6b2c1e6098d4f4cadafa23cc36916f3 Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Mon, 29 Apr 2024 21:53:47 +0200 Subject: [PATCH 1/4] WIP [skip ci] --- lua/decipher/init.lua | 26 +++++++++++++++++++++++--- lua/decipher/ui/float.lua | 31 ++++++++++++++++--------------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/lua/decipher/init.lua b/lua/decipher/init.lua index 477d598..a410414 100644 --- a/lua/decipher/init.lua +++ b/lua/decipher/init.lua @@ -73,6 +73,7 @@ end ---@param codec_name string ---@param status boolean ---@param value string? +---@return decipher.Float? local function open_float_handler(codec_name, status, value, selection_type) if status and value == nil then value = "Codec not found" @@ -82,7 +83,7 @@ local function open_float_handler(codec_name, status, value, selection_type) -- preview window later on local _selection = selection.get_selection(selection_type) - ui.float.open(codec_name, { value }, config.float, selection_type, _selection) + return ui.float.open(codec_name, { value }, config.float, selection_type, _selection) end --- Handler for setting a text region to a value @@ -111,6 +112,7 @@ end ---@param codec_func fun(string): string ---@param selection_type decipher.SelectionType ---@param options decipher.Options +---@return decipher.Float? local function process_codec(codec_name, codec_func, selection_type, options) local lines = selection.get_text(0, selection_type) local joined = table.concat(lines, "\n") @@ -121,7 +123,7 @@ local function process_codec(codec_name, codec_func, selection_type, options) local _codec_name = ("%s"):format(codec_name) if do_preview then - open_float_handler(_codec_name, status, value, selection_type) + return open_float_handler(_codec_name, status, value, selection_type) else set_text_region_handler(_codec_name, status, value, selection_type) end @@ -155,9 +157,27 @@ end ---@param codec_name decipher.CodecArg ---@param options decipher.Options function decipher.encode_motion(codec_name, options) + ---@type decipher.Float? + local float + motion.start_motion(function() - process_codec(codec_name, decipher.encode, "motion", options) + float = process_codec(codec_name, decipher.encode, "motion", options) end) + + if options and options.preview == true and float ~= nil then + if config.float.autoclose == true then + -- Set up the autocmd to close the floating window *after* the motion + -- has been executed to avoid triggering CursorMoved too early + vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { + callback = function() + float:close() + end, + once = true, + buffer = vim.api.nvim_get_current_buf(), + desc = "Closes the decipher floating window when insert mode is entered or the cursor is moved", + }) + end + end end ---@param codec_name decipher.CodecArg diff --git a/lua/decipher/ui/float.lua b/lua/decipher/ui/float.lua index af756cf..c14ddae 100644 --- a/lua/decipher/ui/float.lua +++ b/lua/decipher/ui/float.lua @@ -204,21 +204,21 @@ function Float:open(position) self:set_mappings() self:set_options() - if self.window_config.autoclose then - -- We defer execution of the autocmd because a motion moves the cursor if - -- the position is not at the start of what the motion ends up encompasses and so - -- triggers the CursorMoved event immediately, closing the float - vim.defer_fn(function() - vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { - callback = function() - self:close() - end, - once = true, - buffer = self.parent_bufnr, - desc = "Closes the decipher floating window when insert mode is entered or the cursor is moved", - }) - end, 0) - end + -- if self.window_config.autoclose then + -- -- We defer execution of the autocmd because a motion moves the cursor if + -- -- the position is not at the start of what the motion ends up encompasses and so + -- -- triggers the CursorMoved event immediately, closing the float + -- vim.defer_fn(function() + -- vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { + -- callback = function() + -- self:close() + -- end, + -- once = true, + -- buffer = self.parent_bufnr, + -- desc = "Closes the decipher floating window when insert mode is entered or the cursor is moved", + -- }) + -- end, 0) + -- end end -- Render a page of the float @@ -413,6 +413,7 @@ end ---@param window_config? decipher.WindowConfig ---@param selection_type decipher.SelectionType ---@param _selection decipher.Region +---@return decipher.Float? function float.open(title, contents, window_config, selection_type, _selection) if not has_floating_window then errors.error_message("No support for floating windows", true) From 26a3a15bd644b59c33c70744de17082b19d9514d Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Wed, 1 May 2024 22:28:40 +0200 Subject: [PATCH 2/4] Set float autocmds after selection/motion --- lua/decipher/init.lua | 61 ++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/lua/decipher/init.lua b/lua/decipher/init.lua index a410414..fd4d1d0 100644 --- a/lua/decipher/init.lua +++ b/lua/decipher/init.lua @@ -129,6 +129,33 @@ local function process_codec(codec_name, codec_func, selection_type, options) end end +--- Optionally set autoclose autocmds for a float +---@param float decipher.Float? +---@param options decipher.Options? +local function set_float_autoclose_autocmds(float, options) + -- If there is no float or we enter the floating window, do not set up + -- autoclose autocmds + if float == nil or not config.float.enter then + return + end + + if options and options.preview == true then + if config.float.autoclose == true then + -- Set up the autocmd to close the floating window *after* a visual + -- selection or motion has been executed to avoid triggering + -- CursorMoved too early + vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { + callback = function() + float:close() + end, + once = true, + buffer = float.parent_bufnr, + desc = "Closes a decipher floating window when insert mode is entered or the cursor is moved", + }) + end + end +end + ---@param codec_func fun(string): string ---@param selection_type decipher.SelectionType ---@param options decipher.Options @@ -138,20 +165,25 @@ local function process_codec_prompt(codec_func, selection_type, options) return end - process_codec(codec_name, codec_func, selection_type, options) + local float = process_codec(codec_name, codec_func, selection_type, options) + set_float_autoclose_autocmds(float, options) end) end ---@param codec_name decipher.CodecArg ---@param options decipher.Options function decipher.encode_selection(codec_name, options) - process_codec(codec_name, decipher.encode, "visual", options) + local float = process_codec(codec_name, decipher.encode, "visual", options) + + set_float_autoclose_autocmds(float, options) end ---@param codec_name decipher.CodecArg ---@param options decipher.Options function decipher.decode_selection(codec_name, options) - process_codec(codec_name, decipher.decode, "visual", options) + local float = process_codec(codec_name, decipher.decode, "visual", options) + + set_float_autoclose_autocmds(float, options) end ---@param codec_name decipher.CodecArg @@ -164,28 +196,20 @@ function decipher.encode_motion(codec_name, options) float = process_codec(codec_name, decipher.encode, "motion", options) end) - if options and options.preview == true and float ~= nil then - if config.float.autoclose == true then - -- Set up the autocmd to close the floating window *after* the motion - -- has been executed to avoid triggering CursorMoved too early - vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { - callback = function() - float:close() - end, - once = true, - buffer = vim.api.nvim_get_current_buf(), - desc = "Closes the decipher floating window when insert mode is entered or the cursor is moved", - }) - end - end + set_float_autoclose_autocmds(float, options) end ---@param codec_name decipher.CodecArg ---@param options decipher.Options function decipher.decode_motion(codec_name, options) + ---@type decipher.Float? + local float + motion.start_motion(function() - process_codec(codec_name, decipher.decode, "motion", options) + float = process_codec(codec_name, decipher.decode, "motion", options) end) + + set_float_autoclose_autocmds(float, options) end ---@param options decipher.Options @@ -200,6 +224,7 @@ end ---@param options decipher.Options function decipher.encode_motion_prompt(options) + -- TODO: Will this work? motion.start_motion(function() process_codec_prompt(decipher.encode, "motion", options) end) From cd6dccb164e95e2673dc910c088f0ae3c3aa3424 Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Wed, 1 May 2024 22:32:25 +0200 Subject: [PATCH 3/4] Remove deferred autocmds --- lua/decipher/ui/float.lua | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lua/decipher/ui/float.lua b/lua/decipher/ui/float.lua index c14ddae..60dc72f 100644 --- a/lua/decipher/ui/float.lua +++ b/lua/decipher/ui/float.lua @@ -203,22 +203,6 @@ function Float:open(position) self:render_page("main") self:set_mappings() self:set_options() - - -- if self.window_config.autoclose then - -- -- We defer execution of the autocmd because a motion moves the cursor if - -- -- the position is not at the start of what the motion ends up encompasses and so - -- -- triggers the CursorMoved event immediately, closing the float - -- vim.defer_fn(function() - -- vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { - -- callback = function() - -- self:close() - -- end, - -- once = true, - -- buffer = self.parent_bufnr, - -- desc = "Closes the decipher floating window when insert mode is entered or the cursor is moved", - -- }) - -- end, 0) - -- end end -- Render a page of the float From a90609f005120db0ad7a2cf705c3d2765e1d6575 Mon Sep 17 00:00:00 2001 From: MisanthropicBit Date: Wed, 1 May 2024 22:32:40 +0200 Subject: [PATCH 4/4] Fix and simplify conditions --- lua/decipher/init.lua | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lua/decipher/init.lua b/lua/decipher/init.lua index fd4d1d0..a703c06 100644 --- a/lua/decipher/init.lua +++ b/lua/decipher/init.lua @@ -135,24 +135,22 @@ end local function set_float_autoclose_autocmds(float, options) -- If there is no float or we enter the floating window, do not set up -- autoclose autocmds - if float == nil or not config.float.enter then + if float == nil or config.float.enter == true then return end - if options and options.preview == true then - if config.float.autoclose == true then - -- Set up the autocmd to close the floating window *after* a visual - -- selection or motion has been executed to avoid triggering - -- CursorMoved too early - vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { - callback = function() - float:close() - end, - once = true, - buffer = float.parent_bufnr, - desc = "Closes a decipher floating window when insert mode is entered or the cursor is moved", - }) - end + if options and options.preview == true and config.float.autoclose == true then + -- Set up the autocmd to close the floating window *after* a visual + -- selection or motion has been executed to avoid triggering + -- CursorMoved too early + vim.api.nvim_create_autocmd({ "InsertEnter", "CursorMoved" }, { + callback = function() + float:close() + end, + once = true, + buffer = float.parent_bufnr, + desc = "Closes a decipher floating window when insert mode is entered or the cursor is moved", + }) end end