1

Improve wrapping

This commit is contained in:
Alex Yatskov 2024-05-02 20:14:54 -07:00
parent 75cc3a3cb5
commit 0deb8db17e
2 changed files with 33 additions and 25 deletions

View File

@ -7,6 +7,7 @@ local opt_curr = {
comma_prefix = false, comma_prefix = false,
comma_prefix_indent = false, comma_prefix_indent = false,
line_prefix = '', line_prefix = '',
trim_inner_spaces = true,
}, },
go = { go = {
comma_last = true, comma_last = true,

View File

@ -18,7 +18,7 @@ function Cursor:is_literal()
assert(self:is_valid()) assert(self:is_valid())
local syn_id = vim.fn.synID(self.row, self.col, false) local syn_id = vim.fn.synID(self.row, self.col, false)
local syn_attr = vim.fn.synIDattr(syn_id, 'name') local syn_attr = vim.fn.synIDattr(syn_id, 'name')
return syn_attr:find('String$') or syn_attr:find('Comment$') return syn_attr:find('String$') ~= nil or syn_attr:find('Comment$') ~= nil
end end
function Cursor.get_current() function Cursor.get_current()
@ -223,11 +223,12 @@ end
local Param = {} local Param = {}
Param.__index = Param Param.__index = Param
function Param.new(pair) function Param.new(pair, opt)
local param = { local param = {
pair = pair, pair = pair,
opt = opt,
text = '', text = '',
cursors = {}, literals = {},
offset = nil, offset = nil,
} }
@ -238,7 +239,7 @@ function Param:append(char, cursor)
assert(cursor:is_valid()) assert(cursor:is_valid())
self.text = self.text .. char self.text = self.text .. char
table.insert(self.cursors, cursor) table.insert(self.literals, cursor:is_literal())
if cursor == Cursor.get_current() then if cursor == Cursor.get_current() then
self.offset = #self.text self.offset = #self.text
@ -250,18 +251,18 @@ function Param:is_active()
end end
function Param:slice(start, stop) function Param:slice(start, stop)
assert(#self.text == #self.cursors) assert(#self.text == #self.literals)
local text = '' local text = ''
local cursors = {} local literals = {}
for i = start, stop do for i = start, stop do
text = text .. self.text:sub(i, i) text = text .. self.text:sub(i, i)
table.insert(cursors, self.cursors[i]) table.insert(literals, self.literals[i])
end end
self.text = text self.text = text
self.cursors = cursors self.literals = literals
if self.offset then if self.offset then
self.offset = math.min(self.offset, stop) self.offset = math.min(self.offset, stop)
@ -269,27 +270,32 @@ function Param:slice(start, stop)
end end
end end
function Param:strip() function Param:trim()
assert(#self.text == #self.cursors) assert(#self.text == #self.literals)
self:slice(1, #self.text - #self.text:match('%s*$')) self:slice(1, #self.text - #self.text:match('%s*$'))
self:slice(1 + #self.text:match('^%s*'), #self.text) self:slice(1 + #self.text:match('^%s*'), #self.text)
if self.opt.trim_inner_spaces then
local text = '' local text = ''
local cursors = {} local literals = {}
local offset = self.offset
for i = 1, #self.text do for i = 1, #self.text do
local char = self.text:sub(i, i) local char = self.text:sub(i, i)
local cursor = self.cursors[i] local literal = self.literals[i]
if cursor:is_literal() or not char:match('%s') or not text:match('%s$') then if literal or not char:match('%s') or not text:match('%s$') then
text = text .. char text = text .. char
table.insert(cursors, cursor) table.insert(literals, literal)
elseif offset and offset >= i then
self.offset = math.max(1, self.offset - 1)
end end
end end
self.text = text self.text = text
self.cursors = cursors self.literals = literals
end
return #self.text > 0 return #self.text > 0
end end
@ -301,9 +307,10 @@ end
local ParamList = {} local ParamList = {}
ParamList.__index = ParamList ParamList.__index = ParamList
function ParamList.new(range) function ParamList.new(range, opt)
local params = { local params = {
range = range, range = range,
opt = opt,
current = nil, current = nil,
parsed = {}, parsed = {},
} }
@ -313,7 +320,7 @@ end
function ParamList:flush() function ParamList:flush()
if self.current then if self.current then
if self.current:strip() then if self.current:trim() then
table.insert(self.parsed, self.current) table.insert(self.parsed, self.current)
end end
self.current = nil self.current = nil
@ -330,7 +337,7 @@ function ParamList:update(char, brace_stack, cursor)
end end
if not self.current then if not self.current then
self.current = Param.new(self.range) self.current = Param.new(self.range, self.opt)
end end
self.current:append(char, cursor) self.current:append(char, cursor)
@ -465,7 +472,7 @@ function WrapContext:parse()
self.prefix = first_line:sub(self.indent_level * #self.indent_block + 1, self.range.start.col) self.prefix = first_line:sub(self.indent_level * #self.indent_block + 1, self.range.start.col)
self.suffix = last_line:sub(self.range.stop.col) self.suffix = last_line:sub(self.range.stop.col)
self.params = ParamList.new(self.range) self.params = ParamList.new(self.range, self.opt)
self.params:parse() self.params:parse()
return true return true