mirror of
https://github.com/chenasraf/text-transform.nvim.git
synced 2026-05-18 01:48:57 +00:00
fix: range commands
This commit is contained in:
@@ -19,14 +19,18 @@ local function find_word_boundaries(line, start_col)
|
||||
local word_end_col = vim.fn.match(line_text:sub(word_start_col), non_word_pat)
|
||||
+ word_start_col
|
||||
- 1
|
||||
D.log("replacers", "Found word boundaries: %s", vim.inspect({ word_start_col, word_end_col }))
|
||||
D.log("replacers", "Word text: %s", line_text:sub(word_start_col, word_end_col))
|
||||
D.log("replacers", "Line text: %s", line_text)
|
||||
D.log(
|
||||
"find_word_boundaries",
|
||||
"Found word boundaries: %s",
|
||||
vim.inspect({ word_start_col, word_end_col })
|
||||
)
|
||||
D.log("find_word_boundaries", "Word text: %s", line_text:sub(word_start_col, word_end_col))
|
||||
D.log("find_word_boundaries", "Line text: %s", line_text)
|
||||
return word_start_col, word_end_col
|
||||
end
|
||||
|
||||
function TextTransform.replace_range(start_line, start_col, end_line, end_col, transform_name)
|
||||
D.log("replacers", "Replacing range with %s", transform_name)
|
||||
D.log("replace_range", "Replacing range with %s", transform_name)
|
||||
local transform = t["to_" .. transform_name]
|
||||
local lines = vim.fn.getline(start_line, end_line) --- @type any
|
||||
local transformed = {}
|
||||
@@ -58,7 +62,7 @@ end
|
||||
--- @param transform_name string The transformer name
|
||||
--- @param position table|nil A table containing the position of the word to replace
|
||||
function TextTransform.replace_word(transform_name, position)
|
||||
D.log("replacers", "Replacing word with %s", transform_name)
|
||||
D.log("replace_word", "Replacing word with %s", transform_name)
|
||||
local word, line, col, start_col, end_col
|
||||
if not position then
|
||||
word = vim.fn.expand("<cword>")
|
||||
@@ -67,11 +71,11 @@ function TextTransform.replace_word(transform_name, position)
|
||||
start_col, end_col = find_word_boundaries(line, col)
|
||||
word = vim.fn.getline(line):sub(start_col, end_col)
|
||||
end
|
||||
D.log("replacers", "Found word %s", word)
|
||||
D.log("replacers", "Using transformer %s", transform_name)
|
||||
D.log("replace_word", "Found word %s", word)
|
||||
D.log("replace_word", "Using transformer %s", transform_name)
|
||||
local transformer = t["to_" .. transform_name]
|
||||
local transformed = transformer(word)
|
||||
D.log("replacers", "New value %s", transformed)
|
||||
D.log("replace_word", "New value %s", transformed)
|
||||
if not position then
|
||||
vim.cmd("normal ciw" .. transformed)
|
||||
else
|
||||
@@ -83,7 +87,7 @@ end
|
||||
--- Assumes that the each selection is 1 character and operates on the whole word under each cursor.
|
||||
function TextTransform.replace_columns(transform_name)
|
||||
local selections = TextTransform.get_visual_selection_details()
|
||||
D.log("replacers", "Replacing columns with %s", transform_name)
|
||||
D.log("replace_columns", "Replacing columns with %s", transform_name)
|
||||
for _, sel in ipairs(selections) do
|
||||
TextTransform.replace_word(transform_name, { 0, sel.start_line, sel.start_col, 0 })
|
||||
end
|
||||
@@ -95,20 +99,22 @@ end
|
||||
---
|
||||
--- @param transform_name string The transformer name
|
||||
function TextTransform.replace_selection(transform_name)
|
||||
D.log("replacers", "Replacing selection with %s", transform_name)
|
||||
D.log("replace_selection", "Replacing selection with %s", transform_name)
|
||||
-- determine if cursor is a 1-width column across multiple lines or a normal selection
|
||||
-- local start_line, start_col, end_line, end_col = unpack(vim.fn.getpos("'<"))
|
||||
local selections = TextTransform.get_visual_selection_details()
|
||||
|
||||
D.log("replacers", "Selections: %s", utils.dump(selections))
|
||||
D.log("replace_selection", "Selections: %s", utils.dump(selections))
|
||||
local is_multiline = #selections > 1
|
||||
local is_column = is_multiline and selections[1].start_col == selections[#selections].end_col
|
||||
local is_single_cursor = not is_multiline
|
||||
and not is_column
|
||||
and selections
|
||||
and selections[1]
|
||||
and selections[1].start_col == selections[1].end_col
|
||||
|
||||
D.log(
|
||||
"replacers",
|
||||
"replace_selection",
|
||||
"is_multiline: %s, is_column: %s, is_word: %s",
|
||||
is_multiline,
|
||||
is_column,
|
||||
@@ -139,19 +145,31 @@ end
|
||||
--- the full information around the selection logic.
|
||||
function TextTransform.get_visual_selection_details()
|
||||
if not state.positions then
|
||||
D.log("replacers", "No positions saved")
|
||||
D.log("get_visual_selection_details", "No positions saved")
|
||||
return {}
|
||||
end
|
||||
D.log(
|
||||
"replacers",
|
||||
"get_visual_selection_details",
|
||||
"Getting visual selection details - mode: %s, is_visual: %s, is_block: %s",
|
||||
state.positions.mode,
|
||||
utils.is_visual_mode(),
|
||||
utils.is_block_visual_mode()
|
||||
)
|
||||
|
||||
-- Get the start and end positions of the selection
|
||||
local start_pos = state.positions.visual_start
|
||||
local end_pos = 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 utils.is_visual_mode() and not utils.is_block_visual_mode() then
|
||||
if
|
||||
not utils.is_visual_mode()
|
||||
and not utils.is_block_visual_mode()
|
||||
and not state.has_range(start_pos, end_pos)
|
||||
then
|
||||
local pos = state.positions.pos
|
||||
D.log("get_visual_selection_details", "Returning single cursor position")
|
||||
return {
|
||||
{
|
||||
start_line = pos[2],
|
||||
@@ -162,12 +180,6 @@ function TextTransform.get_visual_selection_details()
|
||||
}
|
||||
end
|
||||
|
||||
-- Get the start and end positions of the selection
|
||||
local start_pos = state.positions.visual_start
|
||||
local end_pos = 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]
|
||||
|
||||
-- Swap if selection is made upwards or backwards
|
||||
if start_line > end_line or (start_line == end_line and start_col > end_col) then
|
||||
start_line, end_line = end_line, start_line
|
||||
@@ -175,7 +187,7 @@ function TextTransform.get_visual_selection_details()
|
||||
end
|
||||
|
||||
-- If it's block visual mode, return table for each row
|
||||
if utils.is_block_visual_mode() then
|
||||
if utils.is_block_visual_mode() or state.has_range(start_pos, end_pos) then
|
||||
local block_selection = {}
|
||||
for line = start_line, end_line do
|
||||
if start_col == end_col then
|
||||
@@ -189,9 +201,15 @@ function TextTransform.get_visual_selection_details()
|
||||
end_col = start_col,
|
||||
})
|
||||
end
|
||||
D.log(
|
||||
"get_visual_selection_details",
|
||||
"Returning block selection: %s",
|
||||
utils.dump(block_selection)
|
||||
)
|
||||
return block_selection
|
||||
else
|
||||
-- Normal visual mode, return single table entry
|
||||
D.log("get_visual_selection_details", "Returning normal selection")
|
||||
return {
|
||||
{
|
||||
start_line = start_line,
|
||||
|
||||
Reference in New Issue
Block a user