generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
170 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- lua/hacker-helper/selection_util.lua | ||
|
||
local M = {} | ||
|
||
-- Utility function to capture the visual selection, apply a transformation, and replace the selection | ||
M.transform_selection = function(transform_func) | ||
-- Reselect the current visual block to ensure the latest selection is active | ||
vim.cmd("normal! gv") | ||
|
||
-- Get the visual selection range using visual marks | ||
local start_pos = vim.fn.getpos("'<") -- Start of the visual selection | ||
local end_pos = vim.fn.getpos("'>") -- End of the visual selection | ||
|
||
-- Determine start and end lines and columns | ||
local start_line = math.min(start_pos[2], end_pos[2]) | ||
local end_line = math.max(start_pos[2], end_pos[2]) | ||
local start_col = start_pos[3] - 1 -- 0-based index for inline selection | ||
local end_col = end_pos[3] -- inclusive for inline selection | ||
|
||
-- Get the selected lines | ||
local lines = vim.fn.getline(start_line, end_line) | ||
|
||
-- Handle visual line selection (V) and inline selection (v) | ||
if vim.fn.visualmode() == "V" then | ||
-- Full line selection | ||
for i, line in ipairs(lines) do | ||
lines[i] = transform_func(line, "full_line") | ||
end | ||
-- Replace the selected lines with the transformed text | ||
vim.fn.setline(start_line, lines) | ||
else | ||
-- Inline selection (v mode) | ||
if start_line == end_line then | ||
-- Handle inline selection on a single line | ||
local line = lines[1] | ||
-- Capture the selected part of the line | ||
local selection = string.sub(line, start_col + 1, end_col) | ||
-- Transform the selection and pass that it's a specific selection | ||
local transformed = transform_func(selection, "specific_selection") | ||
-- Replace the selected part with the transformed text | ||
local new_line = string.sub(line, 1, start_col) .. transformed .. string.sub(line, end_col + 1) | ||
vim.fn.setline(start_line, new_line) | ||
else | ||
-- Handle multi-line partial selection | ||
local first_line = string.sub(lines[1], start_col + 1) | ||
local last_line = string.sub(lines[#lines], 1, end_col) | ||
lines[1] = string.sub(lines[1], 1, start_col) .. transform_func(first_line, "multi_line") | ||
lines[#lines] = transform_func(last_line, "multi_line") .. string.sub(lines[#lines], end_col + 1) | ||
for i = 2, #lines - 1 do | ||
lines[i] = transform_func(lines[i], "multi_line") | ||
end | ||
-- Replace the selected lines with the transformed text | ||
vim.fn.setline(start_line, lines) | ||
end | ||
end | ||
|
||
-- Reselect the visual selection after transformation (optional) | ||
vim.cmd("normal! gv") | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters