Skip to content

Commit

Permalink
refactor: clean ups
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed May 5, 2024
1 parent 5c13d33 commit b0ea44e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion doc/tags
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
TextTransform.config text-transform.txt /*TextTransform.config*
TextTransform.enable() text-transform.txt /*TextTransform.enable()*
TextTransform.get_visual_selection_details() text-transform.txt /*TextTransform.get_visual_selection_details()*
TextTransform.init_commands() text-transform.txt /*TextTransform.init_commands()*
TextTransform.options text-transform.txt /*TextTransform.options*
TextTransform.replace_columns() text-transform.txt /*TextTransform.replace_columns()*
TextTransform.replace_selection() text-transform.txt /*TextTransform.replace_selection()*
TextTransform.replace_word() text-transform.txt /*TextTransform.replace_word()*
Expand Down
6 changes: 3 additions & 3 deletions doc/text-transform.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Initializes user commands

==============================================================================
------------------------------------------------------------------------------
*TextTransform.options*
`TextTransform.options`
*TextTransform.config*
`TextTransform.config`
Your plugin configuration with its default values.

Default values:
>
TextTransform.options = {
TextTransform.config = {
--- Prints information about internals of the plugin. Very verbose, only useful for debugging.
debug = false,
--- Keymap configurations
Expand Down
8 changes: 4 additions & 4 deletions lua/text-transform/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local TextTransform = {}
---
--- Default values:
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
TextTransform.options = {
TextTransform.config = {
--- Prints information about internals of the plugin. Very verbose, only useful for debugging.
debug = false,
--- Keymap configurations
Expand Down Expand Up @@ -48,7 +48,7 @@ TextTransform.options = {

--- @internal
local function init()
local o = TextTransform.options
local o = TextTransform.config
D.log("config", "Initializing TextTransform with %s", utils.dump(o))
commands.init_commands()

Expand All @@ -71,7 +71,7 @@ end
function TextTransform.setup(options)
options = options or {}

TextTransform.options = utils.merge(TextTransform.options, options)
TextTransform.config = utils.merge(TextTransform.config, options)

if vim.api.nvim_get_vvar("vim_did_enter") == 0 then
vim.defer_fn(function()
Expand All @@ -81,7 +81,7 @@ function TextTransform.setup(options)
init()
end

return TextTransform.options
return TextTransform.config
end

return TextTransform
39 changes: 23 additions & 16 deletions lua/text-transform/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local D = require("text-transform.util.debug")
local function ensure_config()
-- when the config is not set to the global object, we set it
if _G.TextTransform.config == nil then
_G.TextTransform.config = require("text-transform.config").options
_G.TextTransform.config = require("text-transform.config").config
end
end

Expand Down Expand Up @@ -64,35 +64,42 @@ local function get_mode_type(mode)
return mode_map[mode] or "normal"
end

local function capture_part(start_sel, end_sel, return_type)
local l, sel
if return_type == "start" then
l = math.min(start_sel[2], end_sel[2])
sel = start_sel
else
l = math.max(start_sel[2], end_sel[2])
sel = end_sel
end
return { sel[1], l, sel[3], sel[4] }
end

--- Save the current cursor position, mode, and visual selection ranges
--- @private
function TextTransform.save_positions()
local buf = vim.api.nvim_get_current_buf()
local mode_info = vim.api.nvim_get_mode()
local mode = get_mode_type(mode_info.mode)
local pos = vim.fn.getcurpos()
-- leave mode
-- leave mode, required to get the positions - they only register on mode leave
-- in case of visual mode
local esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
vim.api.nvim_feedkeys(esc, "x", true)
local visual_start = vim.fn.getpos("'<")
local visual_end = vim.fn.getpos("'>")
D.log("popup_menu", "Saved mode %s, cursor %s", mode, vim.inspect(pos))
D.log("save_positions", "Saved mode %s, cursor %s", mode, vim.inspect(pos))

if mode == "visual" or mode == "line" or mode == "block" then
if mode == "block" then -- for block visual mode
D.log("popup_menu", "Visual mode is block, %s", vim.inspect({ visual_start, visual_end }))
D.log("save_positions", "Visual mode is block, %s", vim.inspect({ visual_start, visual_end }))
-- Adjust the positions to correctly capture the entire block
visual_start = {
visual_start[1],
math.min(visual_start[2], visual_end[2]),
visual_start[3],
visual_start[4],
}
visual_end =
{ visual_end[1], math.max(visual_start[2], visual_end[2]), visual_end[3], visual_end[4] }
visual_start = capture_part(visual_start, visual_end, "start")
visual_end = capture_part(visual_start, visual_end, "end")
end
D.log(
"popup_menu",
"state",
"Saved visual mode %s, cursor %s",
mode,
vim.inspect({ visual_start, visual_end })
Expand All @@ -107,7 +114,7 @@ function TextTransform.save_positions()
visual_end = visual_end,
}

D.log("popup_menu", "State: %s", vim.inspect(state))
D.log("save_positions", "State: %s", vim.inspect(state))
TextTransform.positions = state
return state
end
Expand All @@ -118,7 +125,7 @@ function TextTransform.restore_positions(state)
state = state or TextTransform.positions
vim.api.nvim_set_current_buf(state.buf)
vim.fn.setpos(".", state.pos)
D.log("popup_menu", "Restored mode %s, cursor %s", state.mode, vim.inspect(state.pos))
D.log("restore_positions", "Restored mode %s, cursor %s", state.mode, vim.inspect(state.pos))

-- Attempt to restore visual mode accurately
if
Expand All @@ -130,7 +137,7 @@ function TextTransform.restore_positions(state)
vim.fn.setpos("'>", state.visual_end)
local command = "normal! gv"
vim.cmd(command)
D.log("popup_menu", [[Restored visual mode %s using "%s"]], state.mode, command)
D.log("restore_positions", [[Restored visual mode %s using "%s"]], state.mode, command)
end
TextTransform.positions = nil
end
Expand Down

0 comments on commit b0ea44e

Please sign in to comment.