aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+667e-11@users.noreply.github.com>2016-06-27 21:37:21 -0400
committerGravatar mitchell <70453897+667e-11@users.noreply.github.com>2016-06-27 21:37:21 -0400
commit1b8baa82041e1a322b5b347cfa721dca6a28f900 (patch)
tree5adcf5eec238c4f01f4c52a05dcdccb4807003cc
parent1ac7b5d531cc96220b1d29f26e7254b3eb4ba80f (diff)
Added `textadept.snippets._paths` for file-based snippets.
Thanks to Michael Richter for the idea.
-rw-r--r--doc/manual.md3
-rw-r--r--modules/textadept/snippets.lua27
2 files changed, 30 insertions, 0 deletions
diff --git a/doc/manual.md b/doc/manual.md
index 6ab62b02..666aca6c 100644
--- a/doc/manual.md
+++ b/doc/manual.md
@@ -2003,6 +2003,8 @@ build() |Changed |[build][](root\_directory)
[error\_patterns][] |Changed |_(changed format)_
syntax\_commands |Removed |
syntax\_error\_patterns |Removed |
+**textadept.snippets** | |
+N/A |Added |[\_paths][]
**textadept.session** | |
DEFAULT\_SESSION |Renamed |[default\_session][]
SAVE\_ON\_QUIT |Renamed |[save\_on\_quit][]
@@ -2033,6 +2035,7 @@ MAX\_RECENT\_FILES |Renamed |[max\_recent\_files][]
[run]: api.html#textadept.run.run
[build]: api.html#textadept.run.build
[error\_patterns]: api.html#textadept.run.error_patterns
+[\_paths]: api.html#textadept.snippets._paths
[default\_session]: api.html#textadept.session.default_session
[save\_on\_quit]: api.html#textadept.session.save_on_quit
[max\_recent\_files]: api.html#textadept.session.max_recent_files
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