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

fix: closest node #16

Merged
merged 3 commits into from
Feb 21, 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
18 changes: 14 additions & 4 deletions lua/splitjoin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,26 @@ local function get_node(bufnr, winnr)
local langtree = tsparser:language_for_range { row, col, row, col };
local lang = langtree:lang()
local query = vim.treesitter.get_query(lang, 'splitjoin')
local nodes = {}
if query then
for _, node, _ in query:iter_captures(tstree:root(), bufnr, row - 1, row) do
if vim.treesitter.is_in_node_range(node, row - 1, col) then
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)
return node, range, source, lang
table.insert(nodes, node)
end
end
end

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)
return node, range, source, lang
end
end

---@return string[]
Expand Down
7 changes: 7 additions & 0 deletions lua/splitjoin/languages/css.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ return {
separators = {
block = ';',
},
before = {
block = function(op, _, _, lines)
if op == 'join' then
-- lines[#lines] = lines[#lines] .. ';'
end
end
}
}
31 changes: 26 additions & 5 deletions lua/splitjoin/languages/typescript.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local U = require'splitjoin.util'

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

---@type SplitjoinLanguageConfig
return {
extends = 'ecmascript',
Expand All @@ -8,18 +11,36 @@ return {
union_type = true,
},
},

-- config
separators = {
union_type = '|',
},

-- 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, node, base_indent, lines)
if op == 'split' and U.node_is_sep_first('typescript', node:type()) then
table.insert(lines, 1, '')
union_type = function(op, _, _, lines)
if op == 'split' then
local sep_first = U.node_is_sep_first('typescript', 'union_type')
if sep_first then
table.insert(lines, 1, '')
end
return lines
end
return lines
end
},
after = {
union_type = U.trim_end,
union_type = U.trim_end
},
}
1 change: 1 addition & 0 deletions lua/splitjoin/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ M.node_is_no_trailing_comma = get_config_for('no_trailing_comma')

M.get_config_after = get_config_for('after', M.jump_to_node_end_at)
M.get_config_before = get_config_for('before', function(_, _, _, lines) return lines end)
M.get_config_operative_node = get_config_for('operative_node')
M.get_config_separators = get_config_for('separators')
M.get_config_indent = get_config_for('default_indent')

Expand Down
33 changes: 25 additions & 8 deletions test/css_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
local strings = require'plenary.strings'
local d = require'plenary.strings'.dedent

local H = require'test.helpers'

describe('css', function()
H.make_suite(
'css',
'block',
'a { color: blue; font: 12px "Fira Code", monospace }',
strings.dedent[[
local lang = 'css'

describe(lang, function()

H.make_suite(lang, 'block',
d[[
a { color: blue }
]],
d[[
a {
color: blue;
}
]],
':'
)

H.make_suite(lang, 'block with list',
d[[
a { color: blue; font: 12px "Fira Code", monospace }
]],
d[[
a {
color: blue;
font: 12px "Fira Code", monospace;
}]],
}
]],
';'
)

end)
2 changes: 2 additions & 0 deletions test/fixtures/css.css
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
a { color: blue }

a { color: blue; font: 12px "Fira Code", monospace }
2 changes: 2 additions & 0 deletions test/fixtures/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ const notObj = 1
function thingy() {
return new Knaidlach(a, b, c);
}

f(a, b, g(c, d))
2 changes: 2 additions & 0 deletions test/fixtures/lua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ end
local a, b, c = d

a, b, c = d

f(a, b, c, g(d, e))
69 changes: 43 additions & 26 deletions test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,56 @@ function M.read_lines(path)
return lines
end

function M.skip(lang, name, fixture, split_expected, go_to)

end

function M.get_buf_text(bufnr)
return table.concat(vim.api.nvim_buf_get_text(bufnr, 0, 0, -1, -1, {}), '\n')
end

function M.make_suite(lang, name, fixture, split_expected, go_to)
local assert = require 'luassert'
local splitjoin = require'splitjoin'

describe(name, function()
before_each(function()
bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(0, bufnr)
vim.opt.filetype = lang
local lines = vim.split(fixture, '\n', { plain = true, trimempty = false })
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
if type(go_to) == 'string' then
vim.cmd.norm('f'..go_to)
elseif type(go_to) == 'table' then
vim.api.nvim_win_set_cursor(0, go_to)
end
end)

after_each(function()
vim.api.nvim_buf_delete(bufnr, { force = true })
end)
describe('splits', function()
local bufnr

it('splits', function()
splitjoin.split()
assert.same(split_expected,
table.concat(vim.api.nvim_buf_get_text(bufnr, 0, 0, -1, -1, {}), '\n'))
end)
local function create ()
if not bufnr then
bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_win_set_buf(0, bufnr)
vim.opt.filetype = lang
local lines = vim.split(fixture, '\n', { plain = true, trimempty = false })
vim.api.nvim_buf_set_lines(bufnr, 0, 1, false, lines)
if type(go_to) == 'string' then
vim.cmd.norm('f'..go_to)
elseif type(go_to) == 'table' then
vim.api.nvim_win_set_cursor(0, go_to)
end
end
end

local function destroy()
vim.api.nvim_buf_delete(bufnr, { force = true })
bufnr = nil
end

before_each(create)
after_each(destroy)

it('and rejoins', function()
splitjoin.split()
splitjoin.join()
assert.same(fixture,
table.concat(vim.api.nvim_buf_get_text(bufnr, 0, 0, -1, -1, {}), '\n'))
it('as expected', function()
splitjoin.split()
assert.same(split_expected, M.get_buf_text(bufnr))
end)
describe('and rejoins', function()
it('as expected', function()
splitjoin.split()
splitjoin.join()
assert.same(fixture, M.get_buf_text(bufnr))
end)
end)
end)
end)
end
Expand Down
Loading