feat(lua): vim.text.indent()

Problem:
Indenting text is a common task in plugins/scripts for
presentation/formatting, yet vim has no way of doing it (especially
"dedent", and especially non-buffer text).

Solution:
Introduce `vim.text.indent()`. It sets the *exact* indentation because
that's a more difficult (and thus more useful) task than merely
"increasing the current indent" (which is somewhat easy with a `gsub()`
one-liner).
This commit is contained in:
Justin M. Keyes
2025-02-21 02:02:32 +01:00
parent f4921e2b7d
commit be1fbe38b3
31 changed files with 533 additions and 331 deletions

View File

@@ -766,18 +766,8 @@ local function scope_more_doc(o)
end
--- @param x string
--- @return string
local function dedent(x)
local xs = split(x)
local leading_ws = xs[1]:match('^%s*') --[[@as string]]
local leading_ws_pat = '^' .. leading_ws
for i in ipairs(xs) do
local strip_pat = xs[i]:match(leading_ws_pat) and leading_ws_pat or '^%s*'
xs[i] = xs[i]:gsub(strip_pat, '')
end
return table.concat(xs, '\n')
return (vim.text.indent(0, (x:gsub('\n%s-([\n]?)$', '\n%1'))))
end
--- @return table<string,vim.option_meta>

View File

@@ -148,10 +148,6 @@ local function url_encode(s)
)
end
local function expandtabs(s)
return s:gsub('\t', (' '):rep(8)) --[[ @as string ]]
end
local function to_titlecase(s)
local text = ''
for w in vim.gsplit(s, '[ \t]+') do
@@ -275,25 +271,13 @@ end
---
--- Blank lines (empty or whitespace-only) are ignored.
local function get_indent(s)
local min_indent = nil
for line in vim.gsplit(s, '\n') do
if line and not is_blank(line) then
local ws = expandtabs(line:match('^%s+') or '')
min_indent = (not min_indent or ws:len() < min_indent) and ws:len() or min_indent
end
end
return min_indent or 0
local _, indent = vim.text.indent(0, s, { expandtab = 8 })
return indent
end
--- Removes the common indent level, after expanding tabs to 8 spaces.
local function trim_indent(s)
local indent_size = get_indent(s)
local trimmed = ''
for line in vim.gsplit(s, '\n') do
line = expandtabs(line)
trimmed = ('%s%s\n'):format(trimmed, line:sub(indent_size + 1))
end
return trimmed:sub(1, -2)
return vim.text.indent(0, s, { expandtab = 8 })
end
--- Gets raw buffer text in the node's range (+/- an offset), as a newline-delimited string.