Skip to content

Wrapped requires do not pass on type information #632

@akinsho

Description

@akinsho

Hi,

Not sure if this is a bug or feature request, but in the nvim community we are trying to improve the loading of plugins built with lua by lazy loading things. A example project doing this is https://github.com/tjdevries/lazy.nvim

When trying to implement lazy loading for lua modules (in the context of neovim plugins) wrapping the require into a setmetatable to delay requiring a module till it is indexed does not return the module type information when you hover over variables or try to autocomplete things.

e.g.

local lazy = {} --- Require on index. --- --- Will only require the module after the first index of a module. --- Only works for modules that export a table. lazy.require_on_index = function(require_path) return setmetatable({}, { __index = function(_, key) return require(require_path)[key] end, __newindex = function(_, key, value) require(require_path)[key] = value end, }) end return lazy -- In my module local lazy = require('module.lazy') local module = lazy.require_on_index('module') -- |has no type information just any|

I originally raised this in tjdevries/lazy-require.nvim#1 but I'm wondering if it is possible to pass on the type information somehow from the lazy require function so the lsp treats modules required with the lazy module as it would a module/variable required with the global require?

Could there be an extra annotation such as

---@import telescope.path local path = lazy_require('telescope.path') -- or even nice ---@require lazy.require_on_index = function(require_paath) end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions