diff --git a/README.md b/README.md index af978dc..f5f61a4 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,9 @@ Examples: -- Load the wezterm types when the `wezterm` module is required -- Needs `justinsgithub/wezterm-types` to be installed { path = "wezterm-types", mods = { "wezterm" } }, + -- Load the xmake types when opening file named `xmake.lua` + -- Needs `LelouchHe/xmake-luals-addon` to be installed + { path = "xmake-luals-addon/library", files = { "xmake.lua" } }, }, -- always enable unless `vim.g.lazydev_enabled = false` -- This is the default diff --git a/doc/lazydev.nvim.txt b/doc/lazydev.nvim.txt index 475cce7..b86a878 100644 --- a/doc/lazydev.nvim.txt +++ b/doc/lazydev.nvim.txt @@ -116,6 +116,9 @@ Examples: -- Load the wezterm types when the `wezterm` module is required -- Needs `justinsgithub/wezterm-types` to be installed { path = "wezterm-types", mods = { "wezterm" } }, + -- Load the xmake types when opening file named `xmake.lua` + -- Needs `LelouchHe/xmake-luals-addon` to be installed + { path = "xmake-luals-addon/library", files = { "xmake.lua" } }, }, -- always enable unless `vim.g.lazydev_enabled = false` -- This is the default diff --git a/lua/lazydev/buf.lua b/lua/lazydev/buf.lua index 47abb0e..67ca5b1 100644 --- a/lua/lazydev/buf.lua +++ b/lua/lazydev/buf.lua @@ -14,7 +14,7 @@ M.modules = {} function M.setup() for _, lib in ipairs(Config.libs) do - if #lib.words == 0 and #lib.mods == 0 then + if #lib.words == 0 and #lib.mods == 0 and #lib.files == 0 then Workspace.global():add(lib.path) end end @@ -79,6 +79,7 @@ function M.on_attach(client, buf) }) -- Trigger initial scan M.on_lines(buf, 0, vim.api.nvim_buf_line_count(buf)) + M.on_file(buf) M.update() end @@ -149,6 +150,16 @@ function M.on_mod(buf, modname) end end +---@param buf number +function M.on_file(buf) + -- Check for words + for file, paths in pairs(Config.files) do + if file == vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ":p:t") then + Workspace.find({ buf = buf }):add(paths) + end + end +end + --- Update LuaLS settings with the current library function M.update() if package.loaded["neodev"] then diff --git a/lua/lazydev/config.lua b/lua/lazydev/config.lua index dac6abd..8c92950 100644 --- a/lua/lazydev/config.lua +++ b/lua/lazydev/config.lua @@ -1,8 +1,8 @@ ---@class lazydev.Config.mod: lazydev.Config local M = {} ----@alias lazydev.Library {path:string, words:string[], mods:string[]} ----@alias lazydev.Library.spec string|{path:string, words?:string[], mods?:string[]} +---@alias lazydev.Library {path:string, words:string[], mods:string[], files:string[]} +---@alias lazydev.Library.spec string|{path:string, words?:string[], mods?:string[], files?:string[]} ---@class lazydev.Config local defaults = { runtime = vim.env.VIMRUNTIME --[[@as string]], @@ -29,6 +29,7 @@ local defaults = { M.libs = {} ---@type lazydev.Library[] M.words = {} ---@type table M.mods = {} ---@type table +M.files = {} ---@type table ---@type lazydev.Config local options @@ -58,18 +59,20 @@ function M.setup(opts) ---@type lazydev.Config options = vim.tbl_deep_extend("force", {}, options or defaults, opts or {}) - M.libs, M.words, M.mods = {}, {}, {} + M.libs, M.words, M.mods, M.files = {}, {}, {}, {} local runtime = require("lazydev.util").norm(options.runtime) table.insert(M.libs, { path = vim.uv.fs_stat(runtime) and runtime or vim.env.VIMRUNTIME, words = {}, mods = {}, + files = {}, }) for _, lib in pairs(M.library) do table.insert(M.libs, { path = type(lib) == "table" and lib.path or lib, words = type(lib) == "table" and lib.words or {}, mods = type(lib) == "table" and lib.mods or {}, + files = type(lib) == "table" and lib.files or {}, }) end @@ -82,6 +85,10 @@ function M.setup(opts) M.mods[mod] = M.mods[mod] or {} table.insert(M.mods[mod], lib.path) end + for _, file in ipairs(lib.files) do + M.files[file] = M.files[file] or {} + table.insert(M.files[file], lib.path) + end end vim.api.nvim_create_user_command("LazyDev", function(...)