1

Compare commits

..

No commits in common. "2b2d83183713ed3df772232eeba74f98d9cd1657" and "09db9fb7aaa8fafb860172f384c90a05b9602505" have entirely different histories.

3 changed files with 122 additions and 187 deletions

View File

@ -1,4 +1,4 @@
local opt_curr = { local configs = {
default = { default = {
brace_last_indent = false, brace_last_indent = false,
brace_last_wrap = true, brace_last_wrap = true,
@ -6,50 +6,57 @@ local opt_curr = {
comma_last = false, comma_last = false,
comma_prefix = false, comma_prefix = false,
comma_prefix_indent = false, comma_prefix_indent = false,
line_max = 32,
line_prefix = '', line_prefix = '',
trim_inner_spaces = true,
}, },
go = { go = {
comma_last = true, comma_last = true,
}, },
vim = { vim = {
line_prefix = '\\', brace_last_wrap = false,
line_prefix = '\\ ',
} }
} }
local function setup(opt) local function set(opts, filetypes)
for file_type, file_opt in pairs(opt) do if opts then
local file_opt_curr = opt_curr[file_type] if type(filetypes) == 'string' then
if not file_opt_curr then filetypes = {filetypes}
file_opt_curr = {} elseif not filetypes then
opt_curr[file_type] = file_opt_curr filetypes = {'default'}
end end
for param_name, param_value in pairs(file_opt) do for _, filetype in ipairs(filetypes) do
file_opt_curr[param_name] = param_value local config = configs[filetype]
if not config then
config = {}
configs[filetype] = config
end
for key, value in pairs(opts) do
config[key] = value
end
end end
end end
end end
local function get() local function get()
local file_opt_curr = opt_curr[vim.bo.filetype] local file_config = configs[vim.bo.filetype]
local file_opt = {} local config = {}
for param_name, param_value in pairs(opt_curr.default) do for key, value in pairs(configs.default) do
file_opt[param_name] = param_value config[key] = value
if file_opt_curr then if file_config then
param_value = file_opt_curr[param_name] local file_value = file_config[key]
if param_value ~= nil then if file_value ~= nil then
file_opt[param_name] = param_value config[key] = file_config[key]
end end
end end
end end
return file_opt return config
end end
return { return {
setup = setup, set = set,
get = get, get = get,
} }

View File

@ -10,5 +10,5 @@ end
return { return {
reflow = reflow, reflow = reflow,
setup = config.setup, setup = config.set,
} }

View File

@ -14,11 +14,11 @@ function Cursor:is_valid()
return self.row > 0 and self.col > 0 return self.row > 0 and self.col > 0
end end
function Cursor:is_literal() function Cursor:is_string()
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$') ~= nil or syn_attr:find('Comment$') ~= nil return syn_attr:find('String$')
end end
function Cursor.get_current() function Cursor.get_current()
@ -86,7 +86,7 @@ function BracePair:find_closest(backward)
local ignore_func = function() local ignore_func = function()
local cursor = Cursor.get_current() local cursor = Cursor.get_current()
return cursor:is_literal() return cursor:is_string()
end end
local escaped_pair = self:get_escaped() local escaped_pair = self:get_escaped()
@ -191,10 +191,6 @@ function BraceRange.find_closest_any()
end end
end end
function BraceRange:get_rows()
return self.stop.row - self.start.row
end
function BraceRange:is_wrapped() function BraceRange:is_wrapped()
return self.start.row < self.stop.row return self.start.row < self.stop.row
end end
@ -227,100 +223,35 @@ end
local Param = {} local Param = {}
Param.__index = Param Param.__index = Param
function Param.new(pair, opt) function Param.new(text, pair)
local param = { local param = {
text = text,
pair = pair, pair = pair,
opt = opt,
text = '',
literals = {},
offset = nil, offset = nil,
start = nil,
stop = nil,
terminator = nil,
} }
return setmetatable(param, Param) return setmetatable(param, Param)
end end
function Param:append(char, cursor) function Param:append(char)
assert(cursor:is_valid())
self.text = self.text .. char self.text = self.text .. char
table.insert(self.literals, cursor:is_literal())
if cursor == Cursor.get_current() then
self.offset = #self.text
end
if not self.start then
self.start = cursor
end
self.stop = cursor
end end
function Param:terminate(cursor) function Param:activate()
self.terminator = cursor
if self.terminator == Cursor.get_current() then
self.offset = #self.text self.offset = #self.text
end
end end
function Param:is_active() function Param:is_active()
return self.offset ~= nil return self.offset ~= nil
end end
function Param:slice(start, stop) function Param:flush()
assert(#self.text == #self.literals)
local text = ''
local literals = {}
for i = start, stop do
text = text .. self.text:sub(i, i)
table.insert(literals, self.literals[i])
end
self.text = text
self.literals = literals
if self.offset then if self.offset then
self.offset = math.min(self.offset, stop) self.offset = math.min(self.offset, #self.text:match('(.-)%s*$'))
self.offset = math.max(self.offset - start + 1, 1) self.offset = math.max(self.offset - #self.text:match('^%s*'), 1)
end
end
function Param:trim()
assert(#self.text == #self.literals)
self:slice(1, #self.text - #self.text:match('%s*$'))
self:slice(1 + #self.text:match('^%s*'), #self.text)
if self.text:match('^' .. self.opt.line_prefix) then
self:slice(1 + #self.opt.line_prefix, #self.text)
end
if self.opt.trim_inner_spaces then
local text = ''
local literals = {}
local offset = self.offset
for i = 1, #self.text do
local char = self.text:sub(i, i)
local literal = self.literals[i]
if literal or not char:match('%s') or not text:match('%s$') then
text = text .. char
table.insert(literals, literal)
elseif offset and offset >= i then
self.offset = math.max(1, self.offset - 1)
end
end
self.text = text
self.literals = literals
end end
self.text = self.text:match('^%s*(.-)%s*$')
return #self.text > 0 return #self.text > 0
end end
@ -331,10 +262,9 @@ end
local ParamList = {} local ParamList = {}
ParamList.__index = ParamList ParamList.__index = ParamList
function ParamList.new(range, opt) function ParamList.new(range)
local params = { local params = {
range = range, range = range,
opt = opt,
current = nil, current = nil,
parsed = {}, parsed = {},
} }
@ -342,34 +272,33 @@ function ParamList.new(range, opt)
return setmetatable(params, ParamList) return setmetatable(params, ParamList)
end end
function ParamList:flush(cursor) function ParamList:flush()
if self.current then if self.current then
if cursor then if self.current:flush() then
self.current:terminate(cursor)
end
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
end end
end end
function ParamList:update(char, brace_stack, cursor) function ParamList:update(char, brace_stack, cursor)
if not cursor:is_literal() then if not cursor:is_string() then
brace_stack:update(char) brace_stack:update(char)
if brace_stack:empty() and char == ',' then if brace_stack:empty() and char == ',' then
self:flush(cursor) self:flush()
return return
end end
end end
if not self.current then if self.current then
self.current = Param.new(self.range, self.opt) self.current:append(char)
else
self.current = Param.new(char, self.range)
end end
self.current:append(char, cursor) if cursor == Cursor.get_current() then
self.current:activate()
end
end end
function ParamList:parse() function ParamList:parse()
@ -391,9 +320,9 @@ function ParamList:parse()
for col = start_col, stop_col do for col = start_col, stop_col do
self:update(line:sub(col, col), brace_stack, Cursor.new(row, col)) self:update(line:sub(col, col), brace_stack, Cursor.new(row, col))
end end
end
self:flush() self:flush()
end
end end
-- --
@ -447,8 +376,7 @@ WrapContext.__index = WrapContext
function WrapContext.new(opt) function WrapContext.new(opt)
local wrap_context = { local wrap_context = {
opt = nil, opt = opt,
base_opt = opt,
indent = '', indent = '',
prefix = '', prefix = '',
suffix = '', suffix = '',
@ -459,71 +387,50 @@ function WrapContext.new(opt)
return setmetatable(wrap_context, WrapContext) return setmetatable(wrap_context, WrapContext)
end end
function WrapContext:config_opt()
self.opt = {}
for key, value in pairs(self.base_opt) do
if type(value) == 'table' then
self.opt[key] = false
for _, brace in ipairs(value) do
if self.range.pair == BracePair.from_brace(brace) then
self.opt[key] = true
break
end
end
else
self.opt[key] = value
end
end
end
function WrapContext:config_indent(line)
local padding = #line:match('^(%s*)')
if vim.o.expandtab then
self.indent_level = math.floor(padding / vim.o.shiftwidth)
self.indent_block = string.rep(' ', vim.o.shiftwidth)
else
self.indent_level = padding
self.indent_block = '\t'
end
end
function WrapContext:parse() function WrapContext:parse()
self.range = BraceRange.find_closest_any() self.range = BraceRange.find_closest_any()
if not self.range then if not self.range then
return false return false
end end
self:config_opt() local first_line = vim.fn.getline(self.range.start.row)
local indent = #first_line:match('^(%s*)')
if self.range:get_rows() > self.opt.line_max then self.prefix = first_line:sub(indent + 1, self.range.start.col)
return false if vim.o.expandtab then
self.indent_level = indent / vim.o.shiftwidth
self.indent_block = string.rep(' ', vim.o.shiftwidth)
else
self.indent_level = indent
self.indent_block = '\t'
end end
local first_line = vim.fn.getline(self.range.start.row)
local last_line = vim.fn.getline(self.range.stop.row) local last_line = vim.fn.getline(self.range.stop.row)
self:config_indent(first_line)
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.opt) self.params = ParamList.new(self.range)
self.params:parse() self.params:parse()
return true return true
end end
function WrapContext:update_builder_param(builder, param) function WrapContext:update_builder_param(builder, param, opt)
local text = param.text
if #opt.line_prefix > 0 then
text = param.text:match('^%s*[' .. opt.line_prefix .. ']?%s*(.*)')
end
local cursor = nil local cursor = nil
if param:is_active() then if param:is_active() then
cursor = builder:get_offset() cursor = builder:get_offset()
cursor.row = cursor.row + self.range.start.row cursor.row = cursor.row + self.range.start.row
cursor.col = cursor.col + param.offset cursor.col = cursor.col + param.offset - (#param.text - #text)
end end
builder:update(param.text) builder:update(text)
return cursor return cursor
end end
function WrapContext:wrap() function WrapContext:wrap(opt)
local builder = Builder.new(self.indent_level, self.indent_block) local builder = Builder.new(self.indent_level, self.indent_block)
builder:update(self.prefix) builder:update(self.prefix)
builder:flush() builder:flush()
@ -534,35 +441,36 @@ function WrapContext:wrap()
local is_first_param = i == 1 local is_first_param = i == 1
local is_last_param = i == #self.params.parsed local is_last_param = i == #self.params.parsed
if self.opt.comma_prefix then if opt.comma_prefix then
builder:update(self.opt.line_prefix) builder:update(opt.line_prefix)
if not is_first_param then if not is_first_param then
builder:update(', ') builder:update(', ')
elseif self.opt.comma_prefix_indent and not is_last_param then elseif opt.comma_prefix_indent and not is_last_param then
builder:update(' ') builder:update(' ')
end end
cursor = self:update_builder_param(builder, param) or cursor
cursor = self:update_builder_param(builder, param, opt) or cursor
else else
builder:update(self.opt.line_prefix) builder:update(opt.line_prefix)
cursor = self:update_builder_param(builder, param) or cursor cursor = self:update_builder_param(builder, param, opt) or cursor
if not is_last_param or self.opt.comma_last then
if not is_last_param or opt.comma_last then
builder:update(',') builder:update(',')
end end
end end
if is_last_param and not self.opt.brace_last_wrap then if is_last_param and not opt.brace_last_wrap then
builder:update(self.suffix) builder:update(self.suffix)
end end
builder:flush() builder:flush()
end end
if not self.opt.brace_last_indent then if not opt.brace_last_indent then
builder:unindent() builder:unindent()
end end
if self.opt.brace_last_wrap then if opt.brace_last_wrap then
builder:update(self.opt.line_prefix)
builder:update(self.suffix) builder:update(self.suffix)
builder:flush() builder:flush()
end end
@ -577,16 +485,14 @@ function WrapContext:wrap()
end end
end end
if not cursor then if cursor then
cursor = self.range.start
end
cursor:set_current() cursor:set_current()
end
end end
function WrapContext:unwrap() function WrapContext:unwrap(opt)
local padding = '' local padding = ''
if self.opt.brace_pad then if opt.brace_pad then
padding = ' ' padding = ' '
end end
@ -596,7 +502,7 @@ function WrapContext:unwrap()
local cursor = nil local cursor = nil
for i, param in ipairs(self.params.parsed) do for i, param in ipairs(self.params.parsed) do
cursor = self:update_builder_param(builder, param) or cursor cursor = self:update_builder_param(builder, param, opt) or cursor
if i < #self.params.parsed then if i < #self.params.parsed then
builder:update(', ') builder:update(', ')
end end
@ -607,23 +513,45 @@ function WrapContext:unwrap()
builder:flush() builder:flush()
vim.fn.setline(self.range.start.row, builder.lines[1]) vim.fn.setline(self.range.start.row, builder.lines[1])
vim.fn.execute(string.format('%d,%dd_', self.range.start.row + 1, self.range.stop.row)) vim.fn.execute(string.format(
'%d,%dd_',
if not cursor then self.range.start.row + 1,
cursor = self.range.start self.range.stop.row
end ))
if cursor then
cursor:set_current() cursor:set_current()
end
end end
function WrapContext:toggle() function WrapContext:toggle()
local opt = self:specialize_opt()
if self.range:is_wrapped() then if self.range:is_wrapped() then
self:unwrap() self:unwrap(opt)
else else
self:wrap() self:wrap(opt)
end end
end end
function WrapContext:specialize_opt()
local opt = {}
for key, value in pairs(self.opt) do
if type(value) == 'table' then
opt[key] = false
for _, brace in ipairs(value) do
if self.range.pair == BracePair.from_brace(brace) then
opt[key] = true
break
end
end
else
opt[key] = value
end
end
return opt
end
return { return {
WrapContext = WrapContext, WrapContext = WrapContext,
} }