Skip to content

Commit

Permalink
Add commands from nvim_get_commands
Browse files Browse the repository at this point in the history
This adds commands from nvim_get_commands and attempts to filter out non-human-friendly descriptions.

Contributes #259
  • Loading branch information
ibash committed Jan 29, 2024
1 parent 234e2ef commit 0327d07
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lua/legendary/data/builtins.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
local M = {}
-- commented out ones need a way to get input after selecting

-- get global user-commands
M.get_commands = function()
local global_commands = vim.api.nvim_get_commands({})
local entries = {}

for _, v in pairs(global_commands) do
local description = v["definition"]
if not M.is_description(description) then
-- description cannot be the empty string or the command doesn't show
description = " "
end

table.insert(entries, {v["name"], description = description})
end

return entries
end

-- The commands from nvim_get_commands and nvim_buf_get_commands have a definition that is sometimes a human-friendly
-- description, but most often just vim code. We attempt to filter the non-human-friendly descriptions here. This list
-- was made empirically.
M.is_description = function(description)
return string.len(description) > 0
and not string.find(description, "^exe ")
and not string.find(description, "^:exe ")
and not string.find(description, "^call ")
and not string.find(description, "^:call ")
and not string.find(description, "^echoerr ")
and not string.find(description, "^lua require")
end

M.builtin_keymaps = {
{ '<C-o><C-o>', description = 'Reopen last opened file' },
{ 'gx', description = 'Open with external app' },
Expand Down
4 changes: 4 additions & 0 deletions lua/legendary/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ local function lazy_load_stuff()
-- this somewhat large data file if not needed
local Builtins = require('legendary.data.builtins')

State.items:add(vim.tbl_map(function(keymap)
return Command:parse(keymap, true)
end, Builtins.get_commands()))

State.items:add(vim.tbl_map(function(keymap)
return Keymap:parse(keymap, true)
end, Builtins.builtin_keymaps))
Expand Down

0 comments on commit 0327d07

Please sign in to comment.