Skip to content
Open
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
57 changes: 50 additions & 7 deletions lua/decipher/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -121,12 +123,37 @@ 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
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 config.float.enter == true then
return
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

---@param codec_func fun(string): string
---@param selection_type decipher.SelectionType
---@param options decipher.Options
Expand All @@ -136,36 +163,51 @@ 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
---@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)

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
Expand All @@ -180,6 +222,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)
Expand Down
17 changes: 1 addition & 16 deletions lua/decipher/ui/float.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -413,6 +397,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)
Expand Down