diff --git a/lua/snippy/reader/snipmate.lua b/lua/snippy/reader/snipmate.lua index 1651f60..96acdbc 100644 --- a/lua/snippy/reader/snipmate.lua +++ b/lua/snippy/reader/snipmate.lua @@ -29,12 +29,21 @@ local function read_snippets_file(snippets_file) assert(prefix, 'prefix is nil: ' .. line .. ', file: ' .. snippets_file) local description = line:match('%s*"(.+)"%s*$') local body = {} + local indent = nil i = i + 1 while i <= #lines do line = lines[i] - if line:sub(1, 1) == '\t' or line == '' then - -- print('> line =', line) - line = line:sub(2) + if line:find('^%s+') then + if not indent and line ~= '' then + indent = line:match('%s+') + end + line = line:sub(#indent + 1) + line = line:gsub('^' .. indent .. '+', function(m) + return string.rep('\t', #m / #indent) + end) + table.insert(body, line) + i = i + 1 + elseif line == '' then table.insert(body, line) i = i + 1 else @@ -52,10 +61,8 @@ local function read_snippets_file(snippets_file) while i <= #lines do local line = lines[i] if line:sub(1, 7) == 'snippet' then - -- print('> parsing snippet - line:', line) parse_snippet() elseif line:sub(1, 7) == 'extends' then - -- print('> extends found', i, line) local scopes = vim.split(vim.trim(line:sub(8)), '%s+') vim.list_extend(extends, scopes) i = i + 1 diff --git a/test/snippets/custom.snippets b/test/snippets/custom.snippets new file mode 100644 index 0000000..6198484 --- /dev/null +++ b/test/snippets/custom.snippets @@ -0,0 +1,4 @@ +snippet trigger + This is indented with two spaces. + This is indented with four spaces. + This is indented with eight spaces. diff --git a/test/unit/reader_spec.lua b/test/unit/reader_spec.lua index 61cfb44..893766f 100644 --- a/test/unit/reader_spec.lua +++ b/test/unit/reader_spec.lua @@ -17,6 +17,25 @@ describe('Snippet reader', function() assert.is_same({ _ = snips }, snippy.snippets) end) + it('can read snippets with custom indent', function() + snippy.setup({ snippet_dirs = './test/' }) + vim.cmd('set filetype=custom') + local snips = { + trigger = { + kind = 'snipmate', + prefix = 'trigger', + body = { + 'This is indented with two spaces.', + '\tThis is indented with four spaces.', + '\t\tThis is indented with eight spaces.', + }, + }, + } + assert.is_truthy(require('snippy.shared').config.snippet_dirs) + assert.is_not.same({}, require('snippy.reader.snipmate').list_available_scopes()) + assert.is_same(snips, snippy.snippets.custom) + end) + it('can read vim-snippets snippets', function() local snippet_dirs = os.getenv('VIM_SNIPPETS_PATH') or './vim-snippets/' snippy.setup({