Skip to content

Commit

Permalink
fix: too early setup on initial extension access (nvim-telescope#1601)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdschmidt93 authored Dec 27, 2021
1 parent 8843780 commit 3dafcb4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ telescope.register_extension({mod}) *telescope.register_extension()*

telescope.load_extension({name}) *telescope.load_extension()*
Load an extension.
- Notes:
- Loading triggers ext setup via the config passed in |telescope.setup|


Parameters: ~
Expand Down
24 changes: 13 additions & 11 deletions lua/telescope/_extensions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ extensions._loaded = {}
extensions._config = {}
extensions._health = {}

local load_extension = function(name)
local ok, ext = pcall(require, "telescope._extensions." .. name)
if not ok then
error("This extension doesn't exist or is not installed: " .. name .. "\n" .. ext)
end
return ext
end

extensions.manager = setmetatable({}, {
__index = function(t, k)
-- See if this extension exists.
local ok, ext = pcall(require, "telescope._extensions." .. k)
if not ok then
error("This extension doesn't exist or is not installed: " .. k .. "\n" .. ext)
end

if ext.setup then
ext.setup(extensions._config[k] or {}, require("telescope.config").values)
end

local ext = load_extension(k)
t[k] = ext.exports or {}
extensions._health[k] = ext.health

Expand Down Expand Up @@ -57,7 +56,10 @@ extensions.register = function(mod)
end

extensions.load = function(name)
return extensions.manager[name]
local ext = load_extension(name)
if ext.setup then
ext.setup(extensions._config[name] or {}, require("telescope.config").values)
end
end

extensions.set_config = function(extensions_config)
Expand Down
2 changes: 2 additions & 0 deletions lua/telescope/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ function telescope.register_extension(mod)
end

--- Load an extension.
--- - Notes:
--- - Loading triggers ext setup via the config passed in |telescope.setup|
---@param name string: Name of the extension
function telescope.load_extension(name)
return _extensions.load(name)
Expand Down

0 comments on commit 3dafcb4

Please sign in to comment.