aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVanaIgr <vanaigranov@gmail.com>2024-02-11 14:17:15 -0600
committerGitHub <noreply@github.com>2024-02-12 04:17:15 +0800
commit3faedb0509545a01d84a37bdadf4a9150cc03d5c (patch)
tree13883c2bb5b86bcbde4da90ce629e9ec640657bc /test
parent46390635cd3adf6c6ac4f34914ae93777b8fc15a (diff)
downloadrneovim-3faedb0509545a01d84a37bdadf4a9150cc03d5c.tar.gz
rneovim-3faedb0509545a01d84a37bdadf4a9150cc03d5c.tar.bz2
rneovim-3faedb0509545a01d84a37bdadf4a9150cc03d5c.zip
refactor(indent): refactor computing of a string's indent size (#27252)
The `get_indent_str_vtab()` function currently calls `tabstop_padding()` every time a tab is encountered (unless tabstops aren't used). `tabstop_padding()` either does a division by 'tabstop' If 'vartabstop' is not set, or iterates through the 'vartabstop' list to find current tab width. Since the virtual column only increases, we can keep track of where the next tabstop would be, and update this information once it was reached. `get_indent_str_vtab()` also depends on 'listchars' "tab" value from the current window, even though it may be called for a line from the same buffer in a different window. In most cases, it is called with tabstops enabled (last argument was `false`), so I split the function into one that uses tabstops and the other that doesn't. I removed `get_indent_str()` since I couldn't find any calls to it.
Diffstat (limited to 'test')
-rw-r--r--test/unit/indent_spec.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/test/unit/indent_spec.lua b/test/unit/indent_spec.lua
index f93f6d7581..7902918c54 100644
--- a/test/unit/indent_spec.lua
+++ b/test/unit/indent_spec.lua
@@ -1,6 +1,8 @@
local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)
+local to_cstr = helpers.to_cstr
+local ffi = helpers.ffi
local eq = helpers.eq
local indent = helpers.cimport('./src/nvim/indent.h')
@@ -28,3 +30,31 @@ describe('get_sts_value', function()
eq(tabstop, indent.get_sts_value())
end)
end)
+
+describe('indent_size_ts()', function()
+ itp('works for spaces', function()
+ local line = to_cstr((' '):rep(7) .. 'a ')
+ eq(7, indent.indent_size_ts(line, 100, nil))
+ end)
+
+ itp('works for tabs and spaces', function()
+ local line = to_cstr(' \t \t \t\t a ')
+ eq(19, indent.indent_size_ts(line, 4, nil))
+ end)
+
+ itp('works for tabs and spaces with empty vts', function()
+ local vts = ffi.new('int[1]') -- zero initialized => first element (size) == 0
+ local line = to_cstr(' \t \t \t\t a ')
+ eq(23, indent.indent_size_ts(line, 4, vts))
+ end)
+
+ itp('works for tabs and spaces with vts', function()
+ local vts = ffi.new('int[3]')
+ vts[0] = 2 -- zero indexed
+ vts[1] = 7
+ vts[2] = 2
+
+ local line = to_cstr(' \t \t \t\t a ')
+ eq(18, indent.indent_size_ts(line, 4, vts))
+ end)
+end)