Skip to content

Commit

Permalink
feat: added files option #39 (#41)
Browse files Browse the repository at this point in the history
* Implement files option #39

* Remove debug statements and add readme example
  • Loading branch information
al1-ce authored Jul 4, 2024
1 parent 78d8a11 commit 380e80d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions doc/lazydev.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lua/lazydev/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions lua/lazydev/config.lua
Original file line number Diff line number Diff line change
@@ -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]],
Expand All @@ -29,6 +29,7 @@ local defaults = {
M.libs = {} ---@type lazydev.Library[]
M.words = {} ---@type table<string, string[]>
M.mods = {} ---@type table<string, string[]>
M.files = {} ---@type table<string, string[]>

---@type lazydev.Config
local options
Expand Down Expand Up @@ -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

Expand All @@ -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(...)
Expand Down

0 comments on commit 380e80d

Please sign in to comment.