From d732f34c2c64baff182f9d9dc2463490fc3c7f91 Mon Sep 17 00:00:00 2001 From: dcampos Date: Fri, 14 Oct 2022 21:26:36 -0300 Subject: [PATCH] Fix #84 --- doc/snippy.txt | 4 ++++ lua/snippy/builder.lua | 4 ++-- lua/snippy/reader/snipmate.lua | 3 +++ test/snippets/blank.snippets | 12 ++++++++++++ test/unit/reader_spec.lua | 10 ++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 test/snippets/blank.snippets diff --git a/doc/snippy.txt b/doc/snippy.txt index ff4e4e3..98ee96f 100644 --- a/doc/snippy.txt +++ b/doc/snippy.txt @@ -130,6 +130,10 @@ part of the description: < Please be aware that the legacy syntax doesn't support snippet options. +A single empty line at the end of the snippet body is discarded, regardless of +being indented. This is for maintaining compatibility with SnipMate, but also +allows snippets to be conveniently separated by a single blank line. + *snippy-snippet-options* The `option`s control the behavior of the expansion of the snippet and are diff --git a/lua/snippy/builder.lua b/lua/snippy/builder.lua index ac0785d..3b58462 100644 --- a/lua/snippy/builder.lua +++ b/lua/snippy/builder.lua @@ -17,10 +17,10 @@ local varmap = { return fn.expand('') end, TM_LINE_INDEX = function() - return fn.line('.') - 1 + return tostring(fn.line('.') - 1) end, TM_LINE_NUMBER = function() - return fn.line('.') + return tostring(fn.line('.')) end, TM_FILENAME = function() return fn.expand('%:t') diff --git a/lua/snippy/reader/snipmate.lua b/lua/snippy/reader/snipmate.lua index c40fd06..acba5da 100644 --- a/lua/snippy/reader/snipmate.lua +++ b/lua/snippy/reader/snipmate.lua @@ -97,6 +97,9 @@ local function read_snippets_file(snippets_file) end end if not snips[prefix] or snips[prefix].priority <= priority then + if #body > 1 and body[#body] == '' then + body = vim.list_slice(body, 1, #body - 1) + end snips[prefix] = { kind = 'snipmate', prefix = prefix, diff --git a/test/snippets/blank.snippets b/test/snippets/blank.snippets new file mode 100644 index 0000000..ec13062 --- /dev/null +++ b/test/snippets/blank.snippets @@ -0,0 +1,12 @@ +snippet blank1 + This is line 1. + This is line 2. + Then a blank line. + + +snippet blank2 + Indented line is also removed. + +snippet blank3 + Blank line at the end of the file. + diff --git a/test/unit/reader_spec.lua b/test/unit/reader_spec.lua index 278f664..cee49a4 100644 --- a/test/unit/reader_spec.lua +++ b/test/unit/reader_spec.lua @@ -134,4 +134,14 @@ describe('Snippet reader', function() end assert.is_same({}, total_failed) end) + + it('removes last blank line from snippet', function() + snippy.setup({ snippet_dirs = './test/snippets/', enable_auto = true }) + vim.cmd('set filetype=blank') + assert.is_truthy(require('snippy.shared').config.snippet_dirs) + assert.is_not.same({}, require('snippy.reader.snipmate').list_available_scopes()) + assert.is_same(4, #snippy.snippets.blank.blank1.body) + assert.is_same(1, #snippy.snippets.blank.blank2.body) + assert.is_same(1, #snippy.snippets.blank.blank3.body) + end) end)