vim: make gf
work for lua modules
This commit is contained in:
parent
d45715e545
commit
0bccfe6ad7
2 changed files with 60 additions and 0 deletions
|
@ -23,6 +23,19 @@ M.setup = function()
|
|||
pattern = '*',
|
||||
callback = function(_) vim.opt.cursorlineopt = 'both' end
|
||||
})
|
||||
|
||||
local lua_go_to_file = function()
|
||||
require('fschauen.util').edit_lua_module(vim.fn.expand('<cfile>'))
|
||||
end
|
||||
|
||||
vim.api.nvim_create_autocmd('FileType', {
|
||||
desc = 'Make `gf` work for lua modules.',
|
||||
group = group,
|
||||
pattern = 'lua',
|
||||
callback = function (_)
|
||||
vim.keymap.set('n', 'gf', lua_go_to_file, { buffer = 0 })
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
47
config/nvim/lua/fschauen/util.lua
Normal file
47
config/nvim/lua/fschauen/util.lua
Normal file
|
@ -0,0 +1,47 @@
|
|||
local M = {}
|
||||
|
||||
M.file_exists = function(path)
|
||||
local stat = vim.loop.fs_stat(path)
|
||||
return stat and stat.type == 'file'
|
||||
end
|
||||
|
||||
M.edit_file = function(path)
|
||||
if not pcall(vim.api.nvim_command, string.format('edit %s', path)) then
|
||||
vim.notify('Could not open ' .. path, vim.log.levels.ERROR)
|
||||
end
|
||||
end
|
||||
|
||||
local find_module_source = function(modname)
|
||||
modname = modname:gsub('^%.+', ''):gsub('/', '.')
|
||||
local base = 'lua/' .. modname:gsub('%.', '/')
|
||||
local candidates = { base .. '.lua', base .. '/init.lua' }
|
||||
|
||||
local results = {}
|
||||
for _, directory in ipairs(vim.opt.runtimepath:get()) do
|
||||
for _, candidate in ipairs(candidates) do
|
||||
local path = directory .. '/' .. candidate
|
||||
if M.file_exists(path) then
|
||||
table.insert(results, path)
|
||||
end
|
||||
end
|
||||
end
|
||||
return results
|
||||
end
|
||||
|
||||
M.edit_lua_module = function(modname)
|
||||
local sources = find_module_source(modname)
|
||||
if #sources == 0 then
|
||||
vim.notify('Not found: ' .. modname, vim.log.levels.WARN)
|
||||
elseif #sources == 1 then
|
||||
M.edit_file(sources[1])
|
||||
else
|
||||
vim.ui.select(sources, { prompt = 'Which one?' }, function(choice)
|
||||
if choice then
|
||||
M.edit_file(choice)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
|
Loading…
Add table
Reference in a new issue