Skip to content

Commit 70902fe

Browse files
authored
Make custom indent possible (#25)
Closes #8
1 parent 6b16cba commit 70902fe

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lua/snippy/reader/snipmate.lua

+12-5
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ local function read_snippets_file(snippets_file)
2929
assert(prefix, 'prefix is nil: ' .. line .. ', file: ' .. snippets_file)
3030
local description = line:match('%s*"(.+)"%s*$')
3131
local body = {}
32+
local indent = nil
3233
i = i + 1
3334
while i <= #lines do
3435
line = lines[i]
35-
if line:sub(1, 1) == '\t' or line == '' then
36-
-- print('> line =', line)
37-
line = line:sub(2)
36+
if line:find('^%s+') then
37+
if not indent and line ~= '' then
38+
indent = line:match('%s+')
39+
end
40+
line = line:sub(#indent + 1)
41+
line = line:gsub('^' .. indent .. '+', function(m)
42+
return string.rep('\t', #m / #indent)
43+
end)
44+
table.insert(body, line)
45+
i = i + 1
46+
elseif line == '' then
3847
table.insert(body, line)
3948
i = i + 1
4049
else
@@ -52,10 +61,8 @@ local function read_snippets_file(snippets_file)
5261
while i <= #lines do
5362
local line = lines[i]
5463
if line:sub(1, 7) == 'snippet' then
55-
-- print('> parsing snippet - line:', line)
5664
parse_snippet()
5765
elseif line:sub(1, 7) == 'extends' then
58-
-- print('> extends found', i, line)
5966
local scopes = vim.split(vim.trim(line:sub(8)), '%s+')
6067
vim.list_extend(extends, scopes)
6168
i = i + 1

test/snippets/custom.snippets

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
snippet trigger
2+
This is indented with two spaces.
3+
This is indented with four spaces.
4+
This is indented with eight spaces.

test/unit/reader_spec.lua

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ describe('Snippet reader', function()
1717
assert.is_same({ _ = snips }, snippy.snippets)
1818
end)
1919

20+
it('can read snippets with custom indent', function()
21+
snippy.setup({ snippet_dirs = './test/' })
22+
vim.cmd('set filetype=custom')
23+
local snips = {
24+
trigger = {
25+
kind = 'snipmate',
26+
prefix = 'trigger',
27+
body = {
28+
'This is indented with two spaces.',
29+
'\tThis is indented with four spaces.',
30+
'\t\tThis is indented with eight spaces.',
31+
},
32+
},
33+
}
34+
assert.is_truthy(require('snippy.shared').config.snippet_dirs)
35+
assert.is_not.same({}, require('snippy.reader.snipmate').list_available_scopes())
36+
assert.is_same(snips, snippy.snippets.custom)
37+
end)
38+
2039
it('can read vim-snippets snippets', function()
2140
local snippet_dirs = os.getenv('VIM_SNIPPETS_PATH') or './vim-snippets/'
2241
snippy.setup({

0 commit comments

Comments
 (0)