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
184 additions
and
103 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,69 @@ | ||
-- 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) | ||
|
||
-- Ensure all values in the lines array are valid strings | ||
for i = 1, #lines do | ||
lines[i] = tostring(lines[i] or "") -- Ensure that each element is a string | ||
end | ||
|
||
-- 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] or "" | ||
-- Ensure start_col and end_col are valid | ||
start_col = math.max(0, start_col) | ||
end_col = math.min(#line, end_col) | ||
-- 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 or "", "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] or "", start_col + 1) | ||
local last_line = string.sub(lines[#lines] or "", 1, end_col) | ||
lines[1] = string.sub(lines[1] or "", 1, start_col) .. transform_func(first_line or "", "multi_line") | ||
lines[#lines] = transform_func(last_line or "", "multi_line") .. string.sub(lines[#lines] or "", end_col + 1) | ||
for i = 2, #lines - 1 do | ||
lines[i] = transform_func(lines[i] or "", "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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,67 @@ | ||
local plugin = require("hacker-helper") | ||
|
||
describe("setup", function() | ||
it("works with default", function() | ||
assert(plugin.hello() == "Hello!", "my first function with param = Hello!") | ||
end) | ||
|
||
it("works with custom var", function() | ||
plugin.setup({ opt = "custom" }) | ||
assert(plugin.hello() == "custom", "my first function with param = custom") | ||
end) | ||
end) | ||
-- todo: ensure that we test the setup | ||
-- describe("setup", function() | ||
-- it("works with default", function() | ||
-- assert(plugin.hello() == "Hello!", "my first function with param = Hello!") | ||
-- end) | ||
-- | ||
-- it("works with custom var", function() | ||
-- plugin.setup({ opt = "custom" }) | ||
-- assert(plugin.hello() == "custom", "my first function with param = custom") | ||
-- end) | ||
-- end) | ||
|
||
describe("Base64 and URL encoding/decoding", function() | ||
-- Test Base64 encoding and decoding | ||
it("encodes Base64 correctly", function() | ||
local text = "Hello, World!" | ||
local encoded = plugin.transform_text(text, "encode", "base64") | ||
assert(encoded == "SGVsbG8sIFdvcmxkIQ==", "Base64 encoding failed") | ||
local encoded = plugin.transform_func(text, "specific_selection", "encode", "base64") | ||
assert.are.equal("SGVsbG8sIFdvcmxkIQ==", encoded) | ||
end) | ||
|
||
it("decodes Base64 correctly", function() | ||
local encoded_text = "SGVsbG8sIFdvcmxkIQ==" | ||
local decoded = plugin.transform_text(encoded_text, "decode", "base64") | ||
assert(decoded == "Hello, World!", "Base64 decoding failed") | ||
local decoded = plugin.transform_func(encoded_text, "specific_selection", "decode", "base64") | ||
assert.are.equal("Hello, World!", decoded) | ||
end) | ||
|
||
-- Test URL encoding and decoding | ||
it("encodes URL correctly", function() | ||
local text = "Hello, World!" | ||
local encoded = plugin.transform_text(text, "encode", "url") | ||
assert(encoded == "Hello%2C%20World%21", "URL encoding failed") | ||
local encoded = plugin.transform_func(text, "specific_selection", "encode", "url") | ||
assert.are.equal("Hello%2C%20World%21", encoded) | ||
end) | ||
|
||
it("decodes URL correctly", function() | ||
local encoded_text = "Hello%2C%20World%21" | ||
local decoded = plugin.transform_text(encoded_text, "decode", "url") | ||
assert(decoded == "Hello, World!", "URL decoding failed") | ||
local decoded = plugin.transform_func(encoded_text, "specific_selection", "decode", "url") | ||
assert.are.equal("Hello, World!", decoded) | ||
end) | ||
|
||
-- Test full-line and multi-line selections for Base64 | ||
it("encodes a full line with Base64 correctly", function() | ||
local text = "This is a full line." | ||
local encoded = plugin.transform_func(text, "full_line", "encode", "base64") | ||
assert.are.equal("VGhpcyBpcyBhIGZ1bGwgbGluZS4=", encoded) | ||
end) | ||
|
||
it("decodes multi-line Base64 correctly", function() | ||
local text = "VGhpcyBpcyBhIGZ1bGwgbGluZS4=" | ||
local decoded = plugin.transform_func(text, "multi_line", "decode", "base64") | ||
assert.are.equal("This is a full line.", decoded) | ||
end) | ||
|
||
-- Test handling of unknown encoding type | ||
it("returns the same text if encoding type is unknown", function() | ||
local text = "Hello, World!" | ||
local result = plugin.transform_func(text, "specific_selection", "encode", "unknown") | ||
assert.are.equal(text, result) | ||
end) | ||
|
||
-- Test handling of unknown action (not encode/decode) | ||
it("returns the same text if the action is not encode or decode", function() | ||
local text = "Hello, World!" | ||
local result = plugin.transform_func(text, "specific_selection", "unknown_action", "base64") | ||
assert.are.equal(text, result) | ||
end) | ||
end) |