[code] Silly little templating snippets system

From: Michael Richter <ttmrichter.att.gmail.com>
Date: Sun, 10 Jan 2016 00:08:31 +0800

I've come up with a really stupid little templating-based snippets system
for Textadept. It works like this:

   - Put all the templates into _USERHOME .. '/templates/'
   - The files should be named according to this pattern:
   "<language>.<snippets key>". So for example if you want "mod<tab>": to
   expand into a full Rexx module, you'd put the code for the Rexx module into
   the file "rexx.mod". The boilerplate for a Prolog predicate might be found
   in "prolog.pred" to let "pred<tab>" expand into the stuff you want at the
   head of all your predicates.
   - Inside those files you put in exactly the same stuff you'd put into
   the snippets string you'd otherwise have put into your init.lua (or
   whatever) file.
   - Inside init.lua add the following code:

local templates_dir = _USERHOME .. '/templates/'
> local function load_snippets(language)
> for template in lfs.dir(templates_dir) do
> b, _, key = string.find(template, '^' .. language .. '%.(.+)$')
> if b then
> file = io.open(template, 'r')
> snippet = file:read('a')
> file:close()
> snippets[language] = snippets[language] or {}
> snippets[language][key] = snippet
> end
> end
> end
> events.connect(events.LEXER_LOADED, load_snippets)
>

(As an alternative you could just call load_snippets inside your existing
LEXER_LOADED event should you have one, which is in fact what I do.)

Now any snippets you want to add to any language can be easily maintained
in a separate snippets database.

Motivation:

   - I like Textadept's snippets a lot (simple, yet powerful), but I really
   don't like constantly fiddling with the Lua code in init.lua. Too many
   chances for totally screwing up my configuration.
   - Some of my "snippets" are pretty damned big. My Rexx module
   "snippet", for example, is 204 lines of error handling boilerplate. When
   this was in my init.lua it was a bear to navigate and find the specific
   snippets I was looking for.
   - It's easier to share snippets that aren't married to your
   idiosyncratic init.lua organization.

In case anybody else finds matching or overlapping motivations for this
feature, I thought I'd share it.

Tested on Textadept 8.5.

-- 
"Perhaps people don't believe this, but throughout all of the discussions
of entering China our focus has really been what's best for the Chinese
people. It's not been about our revenue or profit or whatnot."
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.
-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Sat 09 Jan 2016 - 11:08:31 EST

This archive was generated by hypermail 2.2.0 : Sun 10 Jan 2016 - 06:34:48 EST