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

View File

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