Skip to content

Commit c98c3b2

Browse files
committed
Fix #28
1 parent 20b9d76 commit c98c3b2

File tree

4 files changed

+96
-5
lines changed

4 files changed

+96
-5
lines changed

lua/snippy.lua

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ local function make_unique_ids(stops)
121121
stop.id = max_id
122122
end
123123
end
124+
for _, stop in ipairs(stops) do
125+
if stop.parent then
126+
stop.parent = id_map[stop.parent]
127+
end
128+
end
124129
M.max_id = max_id
125130
end
126131

lua/snippy/buf.lua

+37-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,39 @@ local function add_mark(id, startrow, startcol, endrow, endcol, right_gravity, e
4242
return mark
4343
end
4444

45+
local function get_parents(number)
46+
local value = M.state().stops[number]
47+
if not value.spec.parent then
48+
return {}
49+
end
50+
for n, stop in ipairs(M.state().stops) do
51+
if stop.id == value.spec.parent and stop.spec.type == 'placeholder' then
52+
return vim.list_extend({ n }, get_parents(n))
53+
end
54+
end
55+
return {}
56+
end
57+
58+
local function activate_parents(number)
59+
local parents = get_parents(number)
60+
for _, n in ipairs(parents) do
61+
local stop = M.state().stops[n]
62+
local from, to = stop:get_range()
63+
local mark_id = stop.mark
64+
local _ = add_mark(mark_id, from[1], from[2], to[1], to[2], false, true)
65+
end
66+
end
67+
68+
local function deactivate_parents(number)
69+
local parents = get_parents(number)
70+
for _, n in ipairs(parents) do
71+
local stop = M.state().stops[n]
72+
local from, to = stop:get_range()
73+
local mark_id = stop.mark
74+
local _ = add_mark(mark_id, from[1], from[2], to[1], to[2], true, true)
75+
end
76+
end
77+
4578
function M.state()
4679
local bufnr = api.nvim_buf_get_number(0)
4780
if not M._state[bufnr] then
@@ -75,11 +108,12 @@ end
75108
-- Change the extmarks to expand on change
76109
function M.activate_stop(number)
77110
local value = M.state().stops[number]
78-
for _, stop in ipairs(M.state().stops) do
111+
for n, stop in ipairs(M.state().stops) do
79112
if stop.id == value.id then
80113
local from, to = stop:get_range()
81114
local mark_id = stop.mark
82115
local _ = add_mark(mark_id, from[1], from[2], to[1], to[2], false, true)
116+
activate_parents(n)
83117
end
84118
end
85119
M.state().current_stop = number
@@ -89,11 +123,12 @@ end
89123
-- Change the extmarks NOT to expand on change
90124
function M.deactivate_stop(number)
91125
local value = M.state().stops[number]
92-
for _, stop in ipairs(M.state().stops) do
126+
for n, stop in ipairs(M.state().stops) do
93127
if stop.id == value.id then
94128
local from, to = stop:get_range()
95129
local mark_id = stop.mark
96130
local _ = add_mark(mark_id, from[1], from[2], to[1], to[2], true, true)
131+
deactivate_parents(n)
97132
end
98133
end
99134
end

lua/snippy/builder.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function Builder:evaluate_variable(variable)
164164
end
165165
end
166166

167-
function Builder:process_structure(structure)
167+
function Builder:process_structure(structure, parent)
168168
if type(structure) == 'table' then
169169
for _, value in ipairs(structure) do
170170
if type(value) == 'table' then
@@ -176,10 +176,11 @@ function Builder:process_structure(structure)
176176
endpos = { self.row, self.col },
177177
placeholder = '',
178178
transform = value.transform,
179+
parent = parent,
179180
})
180181
elseif value.type == 'placeholder' then
181182
local startrow, startcol = self.row, self.col
182-
self:process_structure(value.children)
183+
self:process_structure(value.children, value.id)
183184
table.insert(self.stops, {
184185
type = value.type,
185186
id = value.id,

test/functional/snippy_spec.lua

+51-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local insert = helpers.insert
66
local eq, neq, ok = helpers.eq, helpers.neq, helpers.ok
77
local sleep, exec_lua = helpers.sleep, helpers.exec_lua
88

9-
describe('Snippy tests', function()
9+
describe('Snippy', function()
1010
local screen
1111
local snippy_src = os.getenv('SNIPPY_PATH') or '.'
1212

@@ -512,6 +512,56 @@ describe('Snippy tests', function()
512512
eq(false, meths.execute_lua([[return snippy.is_active()]], {}))
513513
end)
514514

515+
it('mirrors nested tab stops', function()
516+
local snip = 'local ${1:module} = require("${2:$1}")'
517+
command('lua snippy.expand_snippet([[' .. snip .. ']])')
518+
feed('util')
519+
-- sleep(10)
520+
feed('<plug>(snippy-next)')
521+
screen:expect({
522+
grid = [[
523+
local util = require("^u{3:til}") |
524+
{1:~ }|
525+
{1:~ }|
526+
{1:~ }|
527+
{1:~ }|
528+
{1:~ }|
529+
{1:~ }|
530+
{1:~ }|
531+
{1:~ }|
532+
{1:~ }|
533+
{1:~ }|
534+
{1:~ }|
535+
{1:~ }|
536+
{1:~ }|
537+
{2:-- SELECT --} |
538+
]],
539+
})
540+
feed('snippy.util')
541+
-- sleep(10)
542+
feed('<plug>(snippy-next)')
543+
screen:expect({
544+
grid = [[
545+
local util = require("snippy.util")^ |
546+
{1:~ }|
547+
{1:~ }|
548+
{1:~ }|
549+
{1:~ }|
550+
{1:~ }|
551+
{1:~ }|
552+
{1:~ }|
553+
{1:~ }|
554+
{1:~ }|
555+
{1:~ }|
556+
{1:~ }|
557+
{1:~ }|
558+
{1:~ }|
559+
{2:-- INSERT --} |
560+
]],
561+
})
562+
neq(true, exec_lua([[return snippy.is_active()]]))
563+
end)
564+
515565
it('jumps correctly when unicode chars present', function()
516566
local snip = 'local ${1:var} = $2 -- ▴ $0'
517567
feed('')

0 commit comments

Comments
 (0)