Skip to content

Commit 4e715c4

Browse files
authored
feat: elixir call with do_block (#182)
* chore: tests * feat: add `call` node with `do_block` * feat: add `defp`, `if`
1 parent 6c61663 commit 4e715c4

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

lua/treesj/langs/elixir.lua

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ return {
6161
space_in_brackets = false,
6262
},
6363
}),
64-
arguments = lang_utils.set_preset_for_args(),
64+
arguments = lang_utils.set_preset_for_args({
65+
split = {
66+
---@param tsj TreeSJ
67+
format_tree = function(tsj) end,
68+
},
69+
}),
6570
tuple = lang_utils.set_preset_for_list({
6671
join = {
6772
space_in_brackets = false,
@@ -74,4 +79,71 @@ return {
7479
binary_operator = {
7580
target_nodes = { 'list', 'map', 'tuple' },
7681
},
82+
call = {
83+
both = {
84+
recursive = false,
85+
space_in_brackets = true,
86+
---@param tsn TSNode
87+
enable = function(tsn)
88+
local trg = tsn:field('target')[1]
89+
if not trg then
90+
return false
91+
end
92+
93+
local text = vim.treesitter.get_node_text(tsn:field('target')[1], 0)
94+
return vim.tbl_contains({ 'def', 'defp', 'if' }, text)
95+
end,
96+
},
97+
split = {
98+
omit = { 'arguments' },
99+
---@param tsj TreeSJ
100+
format_tree = function(tsj)
101+
local args = tsj:child('arguments')
102+
if not args then
103+
return
104+
end
105+
106+
local first, keywords = args:child(1), args:child('keywords')
107+
if not (first and keywords) then
108+
return
109+
end
110+
111+
local _, sc = tsj:tsnode():range()
112+
local p_indent = (' '):rep(sc)
113+
local indent = p_indent .. (' '):rep(vim.fn.shiftwidth())
114+
115+
local kw_txt = vim.trim(keywords
116+
:text()--[[@as string]]
117+
:gsub('^do:', ''))
118+
119+
args:update_text({
120+
first:text() .. ' do',
121+
indent .. kw_txt,
122+
p_indent .. 'end',
123+
})
124+
end,
125+
},
126+
join = {
127+
---@param tsj TreeSJ
128+
format_tree = function(tsj)
129+
local args, do_block = tsj:child('arguments'), tsj:child('do_block')
130+
if not (args and do_block) then
131+
return
132+
end
133+
134+
if do_block:tsnode():named_child_count() > 1 then
135+
return
136+
end
137+
138+
args:update_text(args:text() .. ', ')
139+
140+
local body = do_block:child(2)
141+
if not body then
142+
return
143+
end
144+
145+
do_block:update_text('do: ' .. body:text())
146+
end,
147+
},
148+
},
77149
}

tests/langs/elixir/join_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ local data_for_join = {
8585
expected = { 85, 86 },
8686
result = { 88, 89 },
8787
},
88+
{
89+
path = PATH,
90+
mode = 'join',
91+
lang = LANG,
92+
desc = 'lang "%s", node "call", preset default',
93+
cursor = { 102, 1 },
94+
expected = { 98, 99 },
95+
result = { 101, 102 },
96+
},
8897
}
8998

9099
local treesj = require('treesj')

tests/langs/elixir/split_spec.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ local data_for_split = {
8585
expected = { 90, 96 },
8686
result = { 87, 93 },
8787
},
88+
{
89+
path = PATH,
90+
mode = 'split',
91+
lang = LANG,
92+
desc = 'lang "%s", node "call", preset default',
93+
cursor = { 99, 3 },
94+
expected = { 101, 104 },
95+
result = { 98, 101 },
96+
},
8897
}
8998

9099
local treesj = require('treesj')

tests/sample/index.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ map = %{
9494
map: %{"key" => "value"},
9595
baz: "bar"
9696
}
97+
98+
# RESULT OF JOIN (node "call", preset default)
99+
def foo(a, b), do: a + b
100+
101+
# RESULT OF SPLIT (node "call", preset default)
102+
def foo(a, b) do
103+
a + b
104+
end

0 commit comments

Comments
 (0)