From 515fac2087885bfc17b19a375ae8936c2885993b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Wed, 17 Feb 2016 23:07:52 +0100 Subject: vis-lua: add text object ii to match based on syntax item/token Roughly speaking this should match whatever is colored the same way as the cursor. --- visrc.lua | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'visrc.lua') diff --git a/visrc.lua b/visrc.lua index 98f4ee9..f416a82 100644 --- a/visrc.lua +++ b/visrc.lua @@ -29,6 +29,37 @@ vis.textobject_new = function(vis, key, textobject) return true end +vis:textobject_new("ii", function(win, pos) + + if win.syntax == nil then + return pos, pos + end + + local before, after = pos - 4096, pos + 4096; + 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) + vis.events.win_open = function(win) local files = { -- cgit v1.2.3