diff options
| author | Justin M. Keyes <justinkz@gmail.com> | 2025-02-21 02:02:32 +0100 |
|---|---|---|
| committer | Justin M. Keyes <justinkz@gmail.com> | 2025-02-26 23:06:22 +0100 |
| commit | be1fbe38b31b6046d396407c4efbf238941c6b08 (patch) | |
| tree | 697a429f5ac903d766c2faaf5044fa7a16eeea77 /runtime/doc | |
| parent | f4921e2b7deb4812414998a521c33f920f571c20 (diff) | |
| download | rneovim-be1fbe38b31b6046d396407c4efbf238941c6b08.tar.gz rneovim-be1fbe38b31b6046d396407c4efbf238941c6b08.tar.bz2 rneovim-be1fbe38b31b6046d396407c4efbf238941c6b08.zip | |
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).
Diffstat (limited to 'runtime/doc')
| -rw-r--r-- | runtime/doc/lua.txt | 35 | ||||
| -rw-r--r-- | runtime/doc/news.txt | 1 |
2 files changed, 36 insertions, 0 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index ef2125841d..300d809854 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -4624,6 +4624,41 @@ vim.text.hexencode({str}) *vim.text.hexencode()* Return: ~ (`string`) Hex encoded string +vim.text.indent({size}, {text}, {opts}) *vim.text.indent()* + Sets the indent (i.e. the common leading whitespace) of non-empty lines in + `text` to `size` spaces/tabs. + + Indent is calculated by number of consecutive indent chars. + • The first indented, non-empty line decides the indent char (space/tab): + • `SPC SPC TAB …` = two-space indent. + • `TAB SPC …` = one-tab indent. + • Set `opts.expandtab` to treat tabs as spaces. + + To "dedent" (remove the common indent), pass `size=0`: >lua + vim.print(vim.text.indent(0, ' a\n b\n')) +< + + To adjust relative-to an existing indent, call indent() twice: >lua + local indented, old_indent = vim.text.indent(0, ' a\n b\n') + indented = vim.text.indent(old_indent + 2, indented) + vim.print(indented) +< + + To ignore the final, blank line when calculating the indent, use gsub() + before calling indent(): >lua + local text = ' a\n b\n ' + vim.print(vim.text.indent(0, (text:gsub('\n[\t ]+\n?$', '\n')))) +< + + Parameters: ~ + • {size} (`integer`) Number of spaces. + • {text} (`string`) Text to indent. + • {opts} (`{ expandtab?: number }?`) + + Return (multiple): ~ + (`string`) Indented text. + (`integer`) Indent size before modification. + ============================================================================== Lua module: tohtml *vim.tohtml* diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 17de99730a..0d47c7adb9 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -321,6 +321,7 @@ LUA • |vim.fs.relpath()| gets relative path compared to base path. • |vim.fs.dir()| and |vim.fs.find()| now follow symbolic links by default, the behavior can be turn off using the new `follow` option. +• |vim.text.indent()| indents/dedents text. OPTIONS |