aboutsummaryrefslogtreecommitdiff
path: root/util.lua
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2025-06-08 11:09:30 -0500
committerFlorian Fischer <florian.fischer@muhq.space>2025-06-08 15:35:07 -0500
commit59ce465f6d51f1d3b8ae18a3346264006e99aab5 (patch)
treeebabd52d1adece59d9521b040ca844f13b0190d9 /util.lua
parentcef8b428cff4cfb3fa07f30eab644cba1540b4b2 (diff)
downloadvis-lspc-59ce465f6d51f1d3b8ae18a3346264006e99aab5.tar.gz
vis-lspc-59ce465f6d51f1d3b8ae18a3346264006e99aab5.zip
fix the line number detection with tabs
If a line includes tabs add the visually added space for each tab stop to the line length in order to detect a wrapped line.
Diffstat (limited to 'util.lua')
-rw-r--r--util.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/util.lua b/util.lua
index 04855fb..2b090b2 100644
--- a/util.lua
+++ b/util.lua
@@ -279,4 +279,29 @@ function util.vis_pos_to_sel(win, pos)
return sel
end
+--- Count the visual characters in a line
+-- This is useful to detect wrapped lines including tabs which may add a
+-- unspecified amount of white space to a line depending on their position and
+-- the used tabwidth.
+-- @param win the window containing the line
+-- @param line the line to count
+-- @param nchars the number of characters in the line
+-- @return the number of visual characters
+util.visual_chars_in_line = function(win, line, nchars)
+ -- fast string iteration inspired by:
+ -- https://stackoverflow.com/a/49222705
+ local l = {string.byte(line, 1, nchars)}
+ local line_len = 0
+ for i = 1, nchars do
+ local c = l[i] -- Note: produces char codes instead of chars.
+ if c == 9 then -- '\t'
+ local chars_to_tab_stop = win.options.tabwidth - (line_len % win.options.tabwidth)
+ line_len = line_len + chars_to_tab_stop
+ else
+ line_len = line_len + 1
+ end
+ end
+ return line_len
+end
+
return util