2022-12-14 10:46:54 +01:00
|
|
|
vim.api.nvim_create_user_command('Inspect', function(cmd)
|
|
|
|
|
if cmd.bang then
|
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.
2023-03-07 16:04:57 +01:00
|
|
|
vim.print(vim.inspect_pos())
|
2022-12-14 10:46:54 +01:00
|
|
|
else
|
|
|
|
|
vim.show_pos()
|
|
|
|
|
end
|
|
|
|
|
end, { desc = 'Inspect highlights and extmarks at the cursor', bang = true })
|
2023-03-02 18:03:11 +01:00
|
|
|
|
2023-03-17 12:41:57 +01:00
|
|
|
vim.api.nvim_create_user_command('InspectTree', function(cmd)
|
|
|
|
|
if cmd.mods ~= '' or cmd.count ~= 0 then
|
|
|
|
|
local count = cmd.count ~= 0 and cmd.count or ''
|
|
|
|
|
local new = cmd.mods ~= '' and 'new' or 'vnew'
|
|
|
|
|
|
|
|
|
|
vim.treesitter.inspect_tree({
|
|
|
|
|
command = ('%s %s%s'):format(cmd.mods, count, new),
|
|
|
|
|
})
|
|
|
|
|
else
|
|
|
|
|
vim.treesitter.inspect_tree()
|
|
|
|
|
end
|
|
|
|
|
end, { desc = 'Inspect treesitter language tree for buffer', count = true })
|
2023-04-30 15:53:02 +10:00
|
|
|
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 16:51:30 +02:00
|
|
|
-- TODO: use vim.region() when it lands... #13896 #16843
|
|
|
|
|
local function get_visual_selection()
|
|
|
|
|
local save_a = vim.fn.getreginfo('a')
|
2023-07-04 23:33:23 +02:00
|
|
|
vim.cmd([[norm! "ay]])
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 16:51:30 +02:00
|
|
|
local selection = vim.fn.getreg('a', 1)
|
|
|
|
|
vim.fn.setreg('a', save_a)
|
|
|
|
|
return selection
|
2023-04-30 15:53:02 +10:00
|
|
|
end
|
fix(gx): visual selection, expand env vars
---
Rejected experiment: move vim.ui.open() to vim.env.open()
Problem:
`vim.ui` is where user-interface "providers" live, which can be
overridden. It would also be useful to have a "providers" namespace for
platform-specific features such as "open", clipboard, python, and the other
providers listed in `:help providers`. We could overload `vim.ui` to
serve that purpose as the single "providers" namespace, but
`vim.ui.nodejs()` for example seems awkward.
Solution:
`vim.env` currently has too narrow of a purpose. Overload it to also be
a namespace for `vim.env.open`.
diff --git a/runtime/lua/vim/_meta.lua b/runtime/lua/vim/_meta.lua
index 913f1fe20348..17d05ff37595 100644
--- a/runtime/lua/vim/_meta.lua
+++ b/runtime/lua/vim/_meta.lua
@@ -37,8 +37,28 @@ local options_info = setmetatable({}, {
end,
})
-vim.env = setmetatable({}, {
- __index = function(_, k)
+vim.env = setmetatable({
+ open = setmetatable({}, {
+ __call = function(_, uri)
+ print('xxxxx'..uri)
+ return true
+ end,
+ __tostring = function()
+ local v = vim.fn.getenv('open')
+ if v == vim.NIL then
+ return nil
+ end
+ return v
+ end,
+ })
+ },
+ {
+ __index = function(t, k, ...)
+ if k == 'open' then
+ error()
+ -- vim.print({...})
+ -- return rawget(t, k)
+ end
local v = vim.fn.getenv(k)
if v == vim.NIL then
return nil
2023-07-02 16:51:30 +02:00
|
|
|
|
2023-07-04 23:33:23 +02:00
|
|
|
local gx_desc =
|
|
|
|
|
'Opens filepath or URI under cursor with the system handler (file explorer, web browser, …)'
|
|
|
|
|
local function do_open(uri)
|
|
|
|
|
local _, err = vim.ui.open(uri)
|
|
|
|
|
if err then
|
|
|
|
|
vim.notify(err, vim.log.levels.ERROR)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
vim.keymap.set({ 'n' }, 'gx', function()
|
|
|
|
|
do_open(vim.fn.expand('<cfile>'))
|
|
|
|
|
end, { desc = gx_desc })
|
|
|
|
|
vim.keymap.set({ 'x' }, 'gx', function()
|
|
|
|
|
do_open(get_visual_selection())
|
|
|
|
|
end, { desc = gx_desc })
|