wet split_line function improvement

This commit is contained in:
2025-07-16 23:57:12 +02:00
parent d821d006b2
commit ae0286e4c8
+16 -4
View File
@@ -667,15 +667,27 @@ do -- split line {{{1
print("The first opening bracket found after the cursor was not closed on this line.")
return
end
if #split_indexes == 0 then
print('No comma separated items within brackets that were opened and closed after the cursor.')
return
end
---@type string
local leading_whitespace = string.match(line, "^%s*")
local first_line = line:sub(1, first_bracket_i)
local last_line = leading_whitespace .. line:sub(last_bracket_i, #line)
if #split_indexes == 0 then
if (last_bracket_i - first_bracket_i == 1) then return end
local row, _ = unpack(vim.api.nvim_win_get_cursor(0))
line = line:sub(first_bracket_i+1, last_bracket_i-1)
local leading_pattern = "^[%s"
for k, _ in pairs(SPLIT_DELIMETERS) do
leading_pattern = leading_pattern .. k
end
leading_pattern = leading_pattern .. "]*"
line = line:gsub(leading_pattern, leading_whitespace .. SPLIT_WHITESPACE, 1)
line = line:gsub("[%s]*$", '', 1)
vim.api.nvim_buf_set_lines(0, row-1, row, false, {first_line})
vim.api.nvim_buf_set_lines(0, row, row, false, {last_line})
vim.api.nvim_buf_set_lines(0, row, row, false, {line})
return
end
local middle_lines = {}
table.insert(