Re: [code] How to add code folding keywords to an existing lexer?

From: Mitchell <m.att.foicica.com>
Date: Wed, 8 Mar 2017 09:38:14 -0500 (EST)

Hi Sebastian,

On Wed, 8 Mar 2017, cryo shock wrote:

> Hi guys there's another thing I'd love to solve but I can't find a solution
> in the manual.
>
> I am using TA mainly for ConTeXt documents, so I bound two formats to TA's
> "context.lua" lexer by adding the following lines to my user/init.lua:
>
> textadept.file_types.extensions.tex = 'context'
> textadept.file_types.extensions.cxd = 'context'
>
> Everything works fine, except I am missing a keyword pair for code folding:
>
> \beginTXT ... \endTXT
>
> So how can I add this keyword pair and where should I add it?
> In my user/init.lua or in TA/lexers/context.lua?

You should copy *TA/lexers/context.lua* to *~/.textadept/lexers/* first,
and then edit that copy of context.lua.

> In context.lua I found the following code:
>
> ----------------------------------------------------------------
> -- ConTeXt environments.
> local environment = token('environment', '\\' * (P('start') + 'stop') *
> l.word)
>
> [...]
>
> M._foldsymbols = {
> _patterns = {'\\start', '\\stop', '[{}]', '%%'},
> ['environment'] = {['\\start'] = 1, ['\\stop'] = -1},
> [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
> [l.COMMENT] = {['%'] = l.fold_line_comments('%')}
> }
> -----------------------------------------------------------------
>
> I tried to fiddle around by adding begin and end commands in between
> somewhere but so far I didn't find a way to make them work simultanously.
>
> Any advice that can help me? Thanks in advance.

This should do the trick:

   M._foldsymbols = {
     _patterns = {'\\start', '\\stop', '[{}]', '%%', '\\beginTXT', '\\endTXT'},
     ['environment'] = {['\\start'] = 1, ['\\stop'] = -1},
     [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
     [l.COMMENT] = {['%'] = l.fold_line_comments('%')},
     [l.KEYWORD] = {['\\beginTXT'] = 1, ['\\endTXT'] = -1}
   }

You first need to tell the lexer to look for `\beginTXT` and `\endTXT`
patterns (via `_patterns`). Since the lexer considers them to be commands
in the `l.KEYWORD` style (not environment as you posted, but further up),
you need to add the appropriate fold levels for that style.

Cheers,
Mitchell

-- 
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 Wed 08 Mar 2017 - 09:38:14 EST

This archive was generated by hypermail 2.2.0 : Thu 09 Mar 2017 - 06:32:09 EST