vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
textobject-lexer.lua
(735B)
1 -- text object matching a lexer token
2
3 local MAX_CONTEXT = 32768
4
5 vis:textobject_new("ii", function(win, pos)
6
7 if not win.syntax or not vis.lexers.load then
8 return nil
9 end
10
11 local lexer = vis.lexers.load(win.syntax, nil, true)
12 if not lexer then
13 return nil
14 end
15
16 local before, after = pos - MAX_CONTEXT, pos + MAX_CONTEXT
17 if before < 0 then
18 before = 0
19 end
20 -- TODO make sure we start at a line boundary?
21
22 local data = win.file:content(before, after - before)
23 local tokens = lexer:lex(data)
24 local cur = before
25
26 for i = 1, #tokens, 2 do
27 local token_next = before + tokens[i+1] - 1
28 if cur <= pos and pos < token_next then
29 return cur, token_next
30 end
31 cur = token_next
32 end
33
34 return nil
35 end, "Current lexer token")