Skip to content

Make custom indent possible #25

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

Merged
merged 1 commit into from
Nov 17, 2021
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
17 changes: 12 additions & 5 deletions lua/snippy/reader/snipmate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/snippets/custom.snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
snippet trigger
This is indented with two spaces.
This is indented with four spaces.
This is indented with eight spaces.
19 changes: 19 additions & 0 deletions test/unit/reader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down