Skip to content

Commit

Permalink
feat(workspace): find a workspace for a buffer / path
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 5, 2024
1 parent be8332e commit 4c29893
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lua/lazydev/buf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function M.on_line(buf, line)
-- Check for words
for word, paths in pairs(Config.words) do
if line:find(word) then
Workspace.find(buf):add(paths)
Workspace.find({ buf = buf }):add(paths)
end
end
-- Check for modules
Expand All @@ -113,7 +113,7 @@ end
---@param buf number
---@param modname string
function M.on_mod(buf, modname)
local ws = Workspace.find(buf)
local ws = Workspace.find({ buf = buf })

-- Check for configured modules
if Config.mods[modname] then
Expand Down
2 changes: 1 addition & 1 deletion lua/lazydev/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local M = {}
M.commands = {
debug = function()
local buf = vim.api.nvim_get_current_buf()
local ws = Workspace.find(buf)
local ws = Workspace.find({ buf = buf })
if not ws then
return Util.warn("No **LuaLS** workspace found.\nUse `:LazyDev lsp` to see settings of attached LSP clients.")
end
Expand Down
38 changes: 32 additions & 6 deletions lua/lazydev/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,38 @@ function M.single(client)
return M.get(client, M.SINGLE)
end

function M.find(buf)
local client = Util.get_clients({
name = "lua_ls",
bufnr = buf,
})[1]
return client and M.get(client.id, M.get_root(client, buf))
---@param opts {buf?:number, path?:string}
function M.find(opts)
if opts.buf then
local client = Util.get_clients({
name = "lua_ls",
bufnr = opts.buf,
})[1]
return client and M.get(client.id, M.get_root(client, opts.buf))
elseif opts.path then
for _, ws in pairs(M.workspaces) do
if not M.is_special(ws.root) and ws:has(opts.path) then
return ws
end
end
end
end

---@param path string
---@param opts? {library?:boolean}
function M:has(path, opts)
opts = opts or {}
path = Util.norm(path)
local dirs = { self.root } ---@type string[]
if opts.library ~= false then
vim.list_extend(dirs, self.library)
vim.list_extend(dirs, M.global().library)
end
for _, dir in ipairs(dirs) do
if (path .. "/"):sub(1, #dir + 1) == dir .. "/" then
return true
end
end
end

---@param client vim.lsp.Client
Expand Down

0 comments on commit 4c29893

Please sign in to comment.