1
This commit is contained in:
Alex Yatskov 2024-01-21 17:35:26 -08:00
parent 158f7f50b5
commit 812c748ead

View File

@ -227,20 +227,20 @@ local function parse_brace_range_params(brace_range)
local indenting = true local indenting = true
for col = col1, col2 do for col = col1, col2 do
local char = line:sub(col, col) local char = line:sub(col, col)
local padding = false -- local padding = false
assert(#char > 0) assert(#char > 0)
if indenting then -- if indenting then
if char:match('%s') then -- if char:match('%s') then
if pad_newline and col == col1 then -- if pad_newline and col == col1 then
char = ' ' -- char = ' '
else -- else
padding = true -- padding = true
end -- end
else -- else
indenting = false -- indenting = false
end -- end
end -- end
table.insert(brace_range_elements, { table.insert(brace_range_elements, {
row = row, row = row,
@ -251,11 +251,11 @@ local function parse_brace_range_params(brace_range)
literal = is_string_literal({row = row, col = col}), literal = is_string_literal({row = row, col = col}),
}) })
if char == ',' then -- if char == ',' then
pad_newline = true -- pad_newline = true
else -- else
pad_newline = false -- pad_newline = false
end -- end
end end
end end
@ -288,17 +288,9 @@ local function parse_brace_range_params(brace_range)
cursor_pos.col <= param.col + #param.text cursor_pos.col <= param.col + #param.text
if contains_row and contains_col then if contains_row and contains_col then
brace_range.active_param = { param.offset = cursor_pos.col - param.col
index = i,
offset = cursor_pos.col - param.col
}
break break
end end
brace_range.active_param = {
index = i,
offset = #param.text
}
end end
end end
end end
@ -308,6 +300,7 @@ local function wrap_brace_range(brace_range)
vim.fn.setline(brace_range.row1, brace_range.indent .. brace_range.prefix) vim.fn.setline(brace_range.row1, brace_range.indent .. brace_range.prefix)
local cursor_pos = nil
local row = brace_range.row1 local row = brace_range.row1
for i, param in ipairs(brace_range.params) do for i, param in ipairs(brace_range.params) do
local on_last_row = i == #brace_range.params local on_last_row = i == #brace_range.params
@ -324,17 +317,35 @@ local function wrap_brace_range(brace_range)
vim.fn.append(row, line) vim.fn.append(row, line)
vim.fn.execute(string.format('%d>', row + 1)) vim.fn.execute(string.format('%d>', row + 1))
if param.offset then
cursor_pos = get_cursor_pos()
cursor_pos.col = cursor_pos.col + param.offset
cursor_pos.row = row + 1
end
row = row + 1 row = row + 1
end end
if config.wrap_closing_brace then if config.wrap_closing_brace then
vim.fn.append(row, brace_range.indent .. brace_range.suffix) vim.fn.append(row, brace_range.indent .. brace_range.suffix)
end end
if cursor_pos then
vim.fn.setcursorcharpos({cursor_pos.row, cursor_pos.col})
end
end end
local function unwrap_brace_range(brace_range) local function unwrap_brace_range(brace_range)
local cursor_pos = nil
local line = brace_range.indent .. brace_range.prefix local line = brace_range.indent .. brace_range.prefix
for i, param in ipairs(brace_range.params) do for i, param in ipairs(brace_range.params) do
if param.offset then
cursor_pos = {
row = brace_range.row1,
col = #line + param.offset + 1
}
end
line = line .. param.text line = line .. param.text
if i < #brace_range.params then if i < #brace_range.params then
line = line .. ', ' line = line .. ', '
@ -344,6 +355,10 @@ local function unwrap_brace_range(brace_range)
vim.fn.setline(brace_range.row1, line) vim.fn.setline(brace_range.row1, line)
vim.fn.execute(string.format('%d,%dd_', brace_range.row1 + 1, brace_range.row2)) vim.fn.execute(string.format('%d,%dd_', brace_range.row1 + 1, brace_range.row2))
if cursor_pos then
vim.fn.setcursorcharpos({cursor_pos.row, cursor_pos.col})
end
end end
local function reflow() local function reflow()