| 1 |
local function reformat_parenthesized_content()
|
| 2 |
local bufnr = vim.api.nvim_get_current_buf()
|
| 3 |
local row = vim.api.nvim_win_get_cursor(0)[1]
|
| 4 |
local line = vim.api.nvim_buf_get_lines(bufnr, row - 1, row, false)[1]
|
| 5 |
|
| 6 |
local inside = line:match("%((.-)%)")
|
| 7 |
if not inside then
|
| 8 |
vim.notify(
|
| 9 |
"No content found inside parentheses",
|
| 10 |
vim.log.levels.ERROR
|
| 11 |
)
|
| 12 |
return
|
| 13 |
end
|
| 14 |
|
| 15 |
local prefix = line:match("^(.-)%(") or ""
|
| 16 |
local suffix = line:match("%)(.*)$") or ""
|
| 17 |
|
| 18 |
local parts = vim.split(inside, ",%s*")
|
| 19 |
if #parts == 0 then
|
| 20 |
vim.notify("No comma-separated content found", vim.log.levels.ERROR)
|
| 21 |
return
|
| 22 |
end
|
| 23 |
|
| 24 |
local new_lines = {}
|
| 25 |
table.insert(new_lines, prefix .. "(")
|
| 26 |
for i, part in ipairs(parts) do
|
| 27 |
if i < #parts then
|
| 28 |
table.insert(new_lines, " " .. part .. ",")
|
| 29 |
else
|
| 30 |
table.insert(new_lines, " " .. part)
|
| 31 |
end
|
| 32 |
end
|
| 33 |
table.insert(new_lines, " )" .. suffix)
|
| 34 |
|
| 35 |
vim.api.nvim_buf_set_lines(bufnr, row - 1, row, false, new_lines)
|
| 36 |
end
|
| 37 |
|
| 38 |
vim.keymap.set("n", "<leader>qq", function()
|
| 39 |
reformat_parenthesized_content()
|
| 40 |
end)
|