1

Position keeping

This commit is contained in:
Alex Yatskov 2024-04-25 22:13:08 -07:00
parent a74c1b5c33
commit 52d4710ded

View File

@ -28,7 +28,7 @@ end
function Cursor:set_current() function Cursor:set_current()
assert(self:is_valid()) assert(self:is_valid())
vim.fn.secursorcharpos({self.row, self.col}) vim.fn.setcursorcharpos({self.row, self.col})
end end
function Cursor.mt.__eq(self, other) function Cursor.mt.__eq(self, other)
@ -224,7 +224,7 @@ function Param.new(text, pair)
local param = { local param = {
text = text, text = text,
pair = pair, pair = pair,
offset = nil, active = false,
} }
return setmetatable(param, Param.mt) return setmetatable(param, Param.mt)
@ -234,12 +234,12 @@ function Param:append(char)
self.text = self.text .. char self.text = self.text .. char
end end
function Param:capture() function Param:activate()
self.offset = #self.text self.active = true
end end
function Param:stripped() function Param:flush()
return self.text:match('^%s*(.-)%s*$') self.text = self.text:match('^%s*(.-)%s*$')
end end
-- --
@ -260,6 +260,7 @@ end
function ParamList:flush() function ParamList:flush()
if self.current then if self.current then
self.current:flush()
table.insert(self.parsed, self.current) table.insert(self.parsed, self.current)
self.current = nil self.current = nil
end end
@ -281,7 +282,7 @@ function ParamList:update(char, brace_stack, range, cursor)
end end
if cursor == Cursor.get_current() then if cursor == Cursor.get_current() then
self.current:capture() self.current:activate()
end end
end end
@ -349,12 +350,11 @@ function WrapContext:parse()
end end
function WrapContext:wrap() function WrapContext:wrap()
vim.fn.setline( vim.fn.setline(self.range.start.row, self.indent .. self.prefix)
self.range.start.row,
self.indent .. self.prefix
)
local cursor = nil
local row = self.range.start.row local row = self.range.start.row
for i, param in ipairs(self.params.parsed) do for i, param in ipairs(self.params.parsed) do
local first_param = i == 1 local first_param = i == 1
local last_param = i == #self.params.parsed local last_param = i == #self.params.parsed
@ -365,9 +365,9 @@ function WrapContext:wrap()
if not first_param then if not first_param then
line = line .. ', ' line = line .. ', '
end end
line = line .. param:stripped() line = line .. param.text
else else
line = line .. self.indent .. self.opt.line_prefix .. param:stripped() line = line .. self.indent .. self.opt.line_prefix .. param.text
if not last_param or self.opt.tail_comma then if not last_param or self.opt.tail_comma then
line = line .. ',' line = line .. ','
end end
@ -378,28 +378,41 @@ function WrapContext:wrap()
end end
vim.fn.append(row, line) vim.fn.append(row, line)
vim.fn.execute(string.format('%d>', row + 1)) row = row + 1
vim.fn.execute(string.format('%d>', row))
if first_param and self.opt.comma_first_indent then if first_param and self.opt.comma_first_indent then
local prev_shiftwidth = vim.o.shiftwidth local prev_shiftwidth = vim.o.shiftwidth
vim.o.shiftwidth = prev_shiftwidth - 2 vim.o.shiftwidth = prev_shiftwidth - 2
vim.fn.execute(string.format('%d>', row + 1)) vim.fn.execute(string.format('%d>', row))
vim.o.shiftwidth = prev_shiftwidth vim.o.shiftwidth = prev_shiftwidth
end end
row = row + 1 if param.active then
cursor = Cursor.get_current()
cursor.col = #vim.fn.getline(cursor.row):match('^%s*') + 1
end
end end
if self.opt.wrap_closing_brace then if self.opt.wrap_closing_brace then
vim.fn.append(row, self.indent .. self.suffix) vim.fn.append(row, self.indent .. self.suffix)
end end
if cursor then
cursor:set_current()
end
end end
function WrapContext:unwrap() function WrapContext:unwrap()
local cursor = nil
local line = self.indent .. self.prefix local line = self.indent .. self.prefix
for i, param in ipairs(self.params.parsed) do for i, param in ipairs(self.params.parsed) do
line = line .. param:stripped() if param.active then
cursor = Cursor.new(0, #line + 1)
end
line = line .. param.text
if i < #self.params.parsed then if i < #self.params.parsed then
line = line .. ', ' line = line .. ', '
end end
@ -409,6 +422,16 @@ function WrapContext:unwrap()
vim.fn.setline(self.range.start.row, line) vim.fn.setline(self.range.start.row, line)
vim.fn.execute(string.format('%d,%dd_', self.range.start.row + 1, self.range.stop.row)) vim.fn.execute(string.format('%d,%dd_', self.range.start.row + 1, self.range.stop.row))
for _, param in ipairs(self.params.parsed) do
if param.active then
cursor.row = Cursor:get_current().row - 1
end
end
if cursor then
cursor:set_current()
end
end end
function WrapContext:toggle() function WrapContext:toggle()