diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-12-07 19:50:43 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-12-07 23:44:25 +0100 |
| commit | 065a804d282ac99dd93e9ebaf7bc986ccf53e75b (patch) | |
| tree | ffba38ecb6dad1852fb79e9ea4f67a3333c9f376 /lua/plugins/textobject-lexer.lua | |
| parent | 1b896d13b8384958e7b898d85869468eee729961 (diff) | |
| download | vis-065a804d282ac99dd93e9ebaf7bc986ccf53e75b.tar.gz vis-065a804d282ac99dd93e9ebaf7bc986ccf53e75b.tar.xz | |
lua: move non-core code out of vis.lua
The following structure is adapted:
* visrc.lua entry point for all Lua code
* vis.lua only implements the Lua part of the core API
* vis-std.lua registers standard event handlers (e.g. syntax highlighting,
statusbar handling, theme changes etc). It is sourced from vis.lua.
* plugins/* non essential editor functionality, needs to be explicitly
enabled by loading it from visrc.lua
Diffstat (limited to 'lua/plugins/textobject-lexer.lua')
| -rw-r--r-- | lua/plugins/textobject-lexer.lua | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lua/plugins/textobject-lexer.lua b/lua/plugins/textobject-lexer.lua new file mode 100644 index 0000000..a995083 --- /dev/null +++ b/lua/plugins/textobject-lexer.lua @@ -0,0 +1,34 @@ +-- text object matching a lexer token + +local MAX_CONTEXT = 32768 + +vis:textobject_new("ii", function(win, pos) + + if win.syntax == nil or not vis.lexers then + return pos, pos + end + + local before, after = pos - MAX_CONTEXT, pos + MAX_CONTEXT + if before < 0 then + before = 0 + end + -- TODO make sure we start at a line boundary? + + local lexer = vis.lexers.load(win.syntax) + local data = win.file:content(before, after - before) + local tokens = lexer:lex(data) + local cur = before + -- print(before..", "..pos..", ".. after) + + for i = 1, #tokens, 2 do + local name = tokens[i] + local token_next = before + tokens[i+1] - 1 + -- print(name..": ["..cur..", "..token_next.."] pos: "..pos) + if cur <= pos and pos < token_next then + return cur, token_next + end + cur = token_next + end + + return pos, pos +end) |
