summaryrefslogtreecommitdiffstats
path: root/pairs.lua
diff options
context:
space:
mode:
authorGeorgi Kirilov <>2020-11-22 15:10:49 +0200
committerGeorgi Kirilov <>2023-10-04 18:24:59 +0800
commitc5e32f958ef1ed600cf76ca6c37a85e6189500cb (patch)
tree578d51903e2cf24a1f9b2798e7027b20b876c92d /pairs.lua
parent08420f8a29c9d53494d1d70154e96cb732658ba7 (diff)
downloadvis-pairs-c5e32f958ef1ed600cf76ca6c37a85e6189500cb.tar.gz
Don't scan for comments/strings past the cursor
Diffstat (limited to 'pairs.lua')
-rw-r--r--pairs.lua28
1 files changed, 22 insertions, 6 deletions
diff --git a/pairs.lua b/pairs.lua
index 81dd847..76e98b9 100644
--- a/pairs.lua
+++ b/pairs.lua
@@ -91,6 +91,25 @@ local function selection_range(pos)
end
end
+local function past(_, position, pos)
+ return position > pos
+end
+
+local function match_at(str, pattern, pos)
+ local string_pos = pos + 1
+ local I = l.Cp()
+ local p = l.P{ I * (pattern/0) * I + 1 * (l.V(1) - l.Cmt(l.Cc(string_pos), past)) }
+ local s, e = 1
+ while true do
+ s, e = p:match(str, s)
+ if not s then return nil end
+ if s <= string_pos and string_pos < e then
+ return s - 1, e - 1
+ end
+ s = e
+ end
+end
+
local function escaping_context(file, range, syntax)
if not syntax then return {} end
local p
@@ -102,8 +121,9 @@ local function escaping_context(file, range, syntax)
end
if not p then return {} end
if not range then return {escape = p} end -- means we are retrying with a "fake" pos
- local e1 = {file:match_at(p, range.start, file.size)}
- local e2 = range.finish - range.start > 1 and {file:match_at(p, range.finish - 1, file.size)} or e1
+ local data = file:content(0, file.size)
+ local e1 = {match_at(data, p, range.start)}
+ local e2 = range.finish - range.start > 1 and {match_at(data, p, range.finish - 1)} or e1
if #e1 == 0 and #e2 == 0 then return {escape = p} end
if #e1 == 0 and #e2 > 0 then return {escape = p, newpos = range.start} end
if #e2 == 0 and #e1 > 0 then return {escape = p, newpos = range.finish - 1} end
@@ -121,10 +141,6 @@ local function escaping_context(file, range, syntax)
end
end
-local function past(_, position, pos)
- return position > pos
-end
-
local function get_range(key, file, pos)
local d = get_pair(key)
if not d then return end