From 065a804d282ac99dd93e9ebaf7bc986ccf53e75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 7 Dec 2016 19:50:43 +0100 Subject: 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 --- lua/plugins/textobject-lexer.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lua/plugins/textobject-lexer.lua (limited to 'lua/plugins/textobject-lexer.lua') 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) -- cgit v1.2.3