Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(lua): variable lists #18

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions lua/splitjoin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ local function get_node(bufnr, winnr)

local node = nodes[#nodes]
if node then
local getter = U.get_config_operative_node(lang, node:type())
if getter then
node = getter(node) or node
end
local start_row, start_col, end_row, end_col = node:range()
local range = { start_row, start_col, end_row, end_col }
local source = vim.treesitter.get_node_text(node, bufnr)
Expand Down Expand Up @@ -92,13 +88,11 @@ local function splitjoin(op)
local type = node:type()
local handler = U.get_config_handlers(lang, type, op)
if handler then return handler(node) end
local after = U.get_config_after(lang, type)
local before = U.get_config_before(lang, type)
local indent = U.get_config_indent(lang, type) or ' '
local sep = U.get_config_separators(lang, type) or ','
local open, close = U.get_config_surrounds(lang, type, source)
local row, col, end_row, end_col = unpack(range)
local base_indent = U.get_base_indent(node)
local base_indent = U.node_get_base_indent(node)

local lines = flatten(operation(source,
lang,
Expand All @@ -109,24 +103,19 @@ local function splitjoin(op)
indent,
base_indent))

local final = before(op,
node,
base_indent,
lines) or lines

vim.api.nvim_buf_set_text(bufnr,
row,
col,
end_row,
end_col,
final)
lines)

after(op, node, bufnr, winnr, row, col)
U.node_cursor_to_end(node)

if op == 'split' and base_indent:len() > 0 and close then
local last_row = row + #final - 1
local last_row = row + #lines - 1
vim.api.nvim_buf_set_lines(bufnr, last_row, last_row + 1, false, {
base_indent .. U.get_line(bufnr, last_row)
base_indent .. U.buffer_get_line(bufnr, last_row)
})
end
end
Expand Down
7 changes: 0 additions & 7 deletions lua/splitjoin/languages/css.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,4 @@ return {
separators = {
block = ';',
},
before = {
block = function(op, _, _, lines)
if op == 'join' then
-- lines[#lines] = lines[#lines] .. ';'
end
end
}
}
67 changes: 22 additions & 45 deletions lua/splitjoin/languages/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ return {
if_statement = {
split = function(node)
local indent = U.get_config_indent('lua', 'if_statement') or ' '
U.replace_node(node, vim.treesitter.get_node_text(node, 0)
U.node_replace(node, vim.treesitter.get_node_text(node, 0)
:gsub('%s+then%s+', ' then\n'..indent)
:gsub('%s+else%s+', '\nelse\n'..indent)
:gsub('%s+end%s*', '\nend')
:gsub('%s*end%s*', '\nend')
:gsub(
'%s+elseif%s+(.*)then%s+',
function(s)
Expand All @@ -28,18 +28,36 @@ return {
..indent
end
))
U.cursor_to_node_end(node)
U.node_cursor_to_end(node)
end,
join = function(node)
local source = vim.treesitter.get_node_text(node, 0)
U.replace_node(node, source
U.node_replace(node, source
:gsub('if%s+', 'if ')
:gsub('%s*then%s+', ' then ')
:gsub('%s*elseif%s+', ' elseif ')
:gsub('%s*else%s+', ' else ')
:gsub('%s*end%s*', ' end'))
end
},

variable_list = {
split = function(node)
local source = vim.treesitter.get_node_text(node, 0)
local is_variable_decl = U.node_is_child_of('variable_declaration', node)
local indent = is_variable_decl and ' ' or ''
local new = source:gsub(',%s*',',\n'..indent)
U.node_replace(node, new)
U.node_cursor_to_end(node)
if is_variable_decl then U.node_trim_line_end(node) end
end,
join = function(node)
local source = vim.treesitter.get_node_text(node, 0)
local next = source:gsub('%s+', ' ')
U.node_replace(node, next)
U.node_cursor_to_end(node)
end
}
},

no_trailing_comma = {
Expand All @@ -55,45 +73,4 @@ return {
table_constructor = { '{', '}' },
},

before = {
variable_list = function(op, node, base_indent, lines)
if op == 'split' then
local parent = node:parent()
local gp = parent and parent:parent()
if gp and gp:type() == 'variable_declaration' then
table.insert(lines, 1, '')
return lines
else
local next = vim.tbl_map(function(x)
return x:gsub('^%s*', base_indent or '')
end, lines)
next[1] = next[1]:gsub('^%s+', '')
return next
end
else
return lines
end
end
},

after = {
variable_list = function(op, node, bufnr, winnr, row, col)
local parent = node:parent()
local gp = parent and parent:parent()
if op == 'split' then
if gp and gp:type() == 'variable_declaration' then
U.trim_end(op, node, bufnr, winnr, row, col)
else
U.jump_to_node_end_at(op, node, bufnr, winnr, row, col)
end
else -- 'join'
if gp and gp:type() == 'variable_declaration' then
-- vim.cmd.norm'k"_dd'
vim.cmd.norm'kJl'
else
U.jump_to_node_end_at(op, node, bufnr, winnr, row, col)
end
end
end
}
}
51 changes: 27 additions & 24 deletions lua/splitjoin/languages/typescript.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
local U = require'splitjoin.util'

local flatten = vim.tbl_flatten
local map = vim.tbl_map

---@type SplitjoinLanguageConfig
return {
extends = 'ecmascript',
Expand All @@ -16,38 +13,44 @@ return {
separators = {
union_type = '|',
},

surround = {
type_parameters = {'<', '>'},
type_arguments = {'<', '>'},
},

no_trailing_comma = {
type_arguments = true,
},

-- hooks
operative_node = {
union_type = function(node)
local n = node
local p = n:parent()
while p and p:type() == 'union_type' do
n = p
p = n:parent()
end
return n
end,
},
before = {
union_type = function(op, _, _, lines)
if op == 'split' then
handlers = {
union_type = {
split = function(node)
local n = node while U.node_is_child_of('union_type', n) do n = n:parent() end
local sep_first = U.node_is_sep_first('typescript', 'union_type')
local source = vim.treesitter.get_node_text(n, 0)
local base_indent = U.node_get_base_indent(n) or ''
local indent = base_indent -- .. (U.get_config_indent('typescript', 'union_type') or ' ')
local sep = sep_first and ('\n' .. indent .. '| ') or (' |\n'..indent)
local prefix = sep_first and '\n'..indent..indent..'| ' or indent
local replacement = prefix..source:gsub('|', sep)
U.node_replace(n, replacement)
U.node_trim_line_end(node)
U.node_cursor_to_end(node)
vim.cmd.norm'h'
end,
join = function(node)
local n = node while U.node_is_child_of('union_type', n) do n = n:parent() end
local source = vim.treesitter.get_node_text(n, 0)
local row = n:range()
U.node_replace(n, source:gsub('%s*', ''):gsub('^|', ''))
local sep_first = U.node_is_sep_first('typescript', 'union_type')
if sep_first then
table.insert(lines, 1, '')
U.buffer_join_row_below(row - 1)
end
return lines
end
end
},
after = {
union_type = U.trim_end
end,
}
},

}
Loading