Skip to content

Commit

Permalink
fix: normal mode transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Jul 9, 2024
1 parent d78f246 commit 7b7cb40
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
26 changes: 13 additions & 13 deletions lua/text-transform/replacers.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local D = require("text-transform.utils.debug")
local state = require("text-transform.state")
local S = require("text-transform.state")
local t = require("text-transform.transformers")

local TextTransform = {}
Expand Down Expand Up @@ -153,32 +153,32 @@ end
--- This allows to treat all ranges equally and allows to work on each selection without knowing
--- the full information around the selection logic.
function TextTransform.get_visual_selection_details()
if not state.state.positions then
if not S.state.positions.pos then
D.log("get_visual_selection_details", "No positions saved")
return {}
end
D.log(
"get_visual_selection_details",
"Getting visual selection details - mode: %s, is_visual: %s, is_block: %s",
state.state.positions.mode,
state.is_visual_mode(),
state.is_block_visual_mode()
S.state.mode,
S.is_visual_mode(),
S.is_block_visual_mode()
)

-- Get the start and end positions of the selection
local start_pos = state.state.positions.visual_start
local end_pos = state.state.positions.visual_end
local start_pos = S.state.positions.visual_start
local end_pos = S.state.positions.visual_end
local start_line, start_col = start_pos[2], start_pos[3]
local end_line, end_col = end_pos[2], end_pos[3]

-- Check if currently in visual mode; if not, return the cursor position
if
not state.is_visual_mode()
and not state.is_block_visual_mode()
and not state.has_range(start_pos, end_pos)
not S.is_visual_mode()
and not S.is_block_visual_mode()
and not S.has_range(start_pos, end_pos)
then
local pos = state.positions.pos
D.log("get_visual_selection_details", "Returning single cursor position")
D.log("get_visual_selection_details", "Returning single cursor position: " .. vim.inspect(S))
local pos = S.state.positions.pos
return {
{
start_line = pos[2],
Expand All @@ -196,7 +196,7 @@ function TextTransform.get_visual_selection_details()
end

-- If it's block visual mode, return table for each row
if state.is_block_visual_mode() or state.has_range(start_pos, end_pos) then
if S.is_block_visual_mode() or S.has_range(start_pos, end_pos) then
local block_selection = {}
for line = start_line, end_line do
if start_col == end_col then
Expand Down
5 changes: 3 additions & 2 deletions lua/text-transform/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ local TextTransform = {
state = {
-- A table containing cursor position and visual selection details,
-- saved using `save_position()` and can be restored using `restore_positions()`
positions = nil,
--@type {buf: number, mode: string, pos: table, visual_start: table, visual_end: table}
positions = {},
},
}

Expand Down Expand Up @@ -119,7 +120,7 @@ function TextTransform.restore_positions(positions)
D.log("restore_positions", [[Restored visual mode %s using "%s"]], positions.mode, command)
end

TextTransform.state.positions = nil
TextTransform.state.positions = {}
end

return TextTransform

0 comments on commit 7b7cb40

Please sign in to comment.