refactor!: rename vim.pretty_print => vim.print

Problem:
The function name `vim.pretty_print`:
1. is verbose, which partially defeats its purpose as sugar
2. does not draw from existing precedent or any sort of convention
   (except external projects like penlight or python?), which reduces
   discoverability, and degrades signaling about best practices.

Solution:
- Rename to `vim.print`.
- Change the behavior so that
  1. strings are printed without quotes
  2. each arg is printed on its own line
  3. tables are indented with 2 instead of 4 spaces
- Example:
  :lua ='a', 'b', 42, {a=3}
  a
  b
  42
  {
    a = 3
  }

Comparison of alternatives:
- `vim.print`:
  - pro: consistent with Lua's `print()`
  - pro: aligns with potential `nvim_print` API function which will
    replace nvim_echo, nvim_notify, etc.
  - con: behaves differently than Lua's `print()`, slightly misleading?
- `vim.echo`:
  - pro: `:echo` has similar "pretty print" behavior.
  - con: inconsistent with Lua idioms.
- `vim.p`:
  - pro: very short, fits with `vim.o`, etc.
  - con: not as discoverable as "echo"
  - con: less opportunity for `local p = vim.p` because of potential shadowing.
This commit is contained in:
Justin M. Keyes
2023-03-07 16:04:57 +01:00
parent 5aec611469
commit 673d2b52fa
11 changed files with 103 additions and 58 deletions

View File

@@ -778,22 +778,37 @@ do
end
end
---Prints given arguments in human-readable format.
---Example:
---<pre>lua
--- -- Print highlight group Normal and store it's contents in a variable.
--- local hl_normal = vim.pretty_print(vim.api.nvim_get_hl_by_name("Normal", true))
---</pre>
---@see |vim.inspect()|
---@return any # given arguments.
---@private
function vim.pretty_print(...)
local objects = {}
for i = 1, select('#', ...) do
local v = select(i, ...)
table.insert(objects, vim.inspect(v))
vim.deprecate('vim.pretty_print', 'vim.print', '0.10')
return vim.print(...)
end
--- "Pretty prints" the given arguments and returns them unmodified.
---
--- Example:
--- <pre>lua
--- local hl_normal = vim.print(vim.api.nvim_get_hl_by_name('Normal', true))
--- </pre>
---
--- @see |vim.inspect()|
--- @return any # given arguments.
function vim.print(...)
if vim.in_fast_event() then
print(...)
return ...
end
for i = 1, select('#', ...) do
local o = select(i, ...)
if type(o) == 'string' then
vim.api.nvim_out_write(o)
else
vim.api.nvim_out_write(vim.inspect(o, { newline = '\n', indent = ' ' }))
end
vim.api.nvim_out_write('\n')
end
print(table.concat(objects, ' '))
return ...
end