diff options
author | 2016-06-27 21:37:21 -0400 | |
---|---|---|
committer | 2016-06-27 21:37:21 -0400 | |
commit | 1b8baa82041e1a322b5b347cfa721dca6a28f900 (patch) | |
tree | 5adcf5eec238c4f01f4c52a05dcdccb4807003cc /modules/textadept | |
parent | 1ac7b5d531cc96220b1d29f26e7254b3eb4ba80f (diff) | |
download | textadept-1b8baa82041e1a322b5b347cfa721dca6a28f900.tar.gz textadept-1b8baa82041e1a322b5b347cfa721dca6a28f900.zip |
Added `textadept.snippets._paths` for file-based snippets.
Thanks to Michael Richter for the idea.
Diffstat (limited to 'modules/textadept')
-rw-r--r-- | modules/textadept/snippets.lua | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index ebb53505..0a44c2b2 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -108,6 +108,18 @@ module('textadept.snippets')]=] M.INDIC_PLACEHOLDER = _SCINTILLA.next_indic_number() +--- +-- List of directory paths to look for snippet files in. +-- Filenames are of the form *lexer.trigger.ext* or *trigger.ext* (*.ext* is an +-- optional, arbitrary file extension). If the global `snippets` table does not +-- contain a snippet for a given trigger, this table is consulted for a matching +-- filename, and the contents of that file is inserted as a snippet. +-- Note: If a directory has multiple snippets with the same trigger, the snippet +-- chosen for insertion is not defined and may not be constant. +-- @class table +-- @name _paths +M._paths = {} + local INDIC_SNIPPET = _SCINTILLA.next_indic_number() local INDIC_CURRENTPLACEHOLDER = _SCINTILLA.next_indic_number() @@ -318,6 +330,21 @@ function M._insert(text) trigger = buffer:text_range(buffer:word_start_position(buffer.current_pos), buffer.current_pos) text = type(M[lexer]) == 'table' and M[lexer][trigger] or M[trigger] + if not text then + for i = 1, #M._paths do + for basename in lfs.dir(M._paths[i]) do + -- Snippet files are either of the form "lexer.trigger.ext" or + -- "trigger.ext". Prefer "lexer."-prefixed snippets. + local first, second = basename:match('^([^.]+)%.?([^.]*)') + if first == lexer and second == trigger or + first == trigger and second == '' and not text then + local f = io.open(M._paths[i]..'/'..basename) + text = f:read('*a') + f:close() + end + end + end + end end if type(text) == 'function' and not trigger:find('^_') then text = text() end local snippet = type(text) == 'string' and new_snippet(text, trigger) or |