Skip to content

Commit

Permalink
feat: added lspconfig integration to fix its workspace management
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 5, 2024
1 parent 4c29893 commit 3e301ee
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
for editing your **Neovim** config by lazily updating your
workspace libraries.

## 🚀 Features
## Features

- much faster auto-completion, since only the modules you `require`
in open Neovim files will be loaded.
Expand Down Expand Up @@ -121,13 +121,36 @@ Default settings:
local defaults = {
runtime = vim.env.VIMRUNTIME --[[@as string]],
library = {}, ---@type lazydev.Library.spec[]
-- add the cmp source for completion of:
-- `require "modname"`
-- `---@module "modname"`
cmp = true,
integrations = {
-- Fixes lspconfig's workspace management for LuaLS
-- Only create a new workspace if the buffer is not part
-- of an existing workspace or one of its libraries
lspconfig = true,
-- add the cmp source for completion of:
-- `require "modname"`
-- `---@module "modname"`
cmp = true,
-- same, but for Coq
coq = false,
},
---@type boolean|(fun(root:string):boolean?)
enabled = function(root_dir)
return vim.g.lazydev_enabled == nil and true or vim.g.lazydev_enabled
end,
}
```

## 🚀 Usage

Just install the plugin and start editing your Lua files.

If you don't use [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig),
then you can use `require('lazydev').find_workspace(buf?)` to check if the buffer
is part of an existing workspace or its libraries.

The `:LazyDev` command:

- `:LazyDev` or `:LazyDev debug` will show a notification with the **lazydev**
settings for the current buffer.
- `:LazyDev lsp`: will show a notification with the settings for
**any** attached LSP servers. Not limited to **LuaLS**.
24 changes: 18 additions & 6 deletions lua/lazydev/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ local M = {}
local defaults = {
runtime = vim.env.VIMRUNTIME --[[@as string]],
library = {}, ---@type lazydev.Library.spec[]
integrations = {
-- Fixes lspconfig workspace management for LuaLS
-- Only create a new workspace if the buffer is not part
-- of an existing workspace or one of its libraries
lspconfig = true,
-- add the cmp source for completion of:
-- `require "modname"`
-- `---@module "modname"`
cmp = true,
-- same, but for Coq
coq = false,
},
---@type boolean|(fun(root:string):boolean?)
enabled = function(root_dir)
return vim.g.lazydev_enabled == nil and true or vim.g.lazydev_enabled
end,
-- add the cmp source for completion of:
-- `require "modname"`
-- `---@module "modname"`
cmp = true,
debug = false,
}

Expand Down Expand Up @@ -47,6 +55,7 @@ function M.setup(opts)
return
end

---@type lazydev.Config
options = vim.tbl_deep_extend("force", {}, options or defaults, opts or {})

M.libs, M.words, M.mods = {}, {}, {}
Expand Down Expand Up @@ -87,8 +96,11 @@ function M.setup(opts)

vim.schedule(function()
require("lazydev.buf").setup()
require("lazydev.cmp").setup()
require("lazydev.coq").setup()
for name, enabled in pairs(options.integrations) do
if enabled then
require("lazydev.integrations." .. name).setup()
end
end
end)
return options
end
Expand Down
12 changes: 12 additions & 0 deletions lua/lazydev/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,16 @@ function M.setup(opts)
require("lazydev.config").setup(opts)
end

--- Checks if the current buffer is in a workspace:
--- * part of the workspace root
--- * part of the workspace libraries
--- Returns the workspace root if found
---@param buf? integer
function M.find_workspace(buf)
local fname = vim.api.nvim_buf_get_name(buf or 0)
local Workspace = require("lazydev.workspace")
local ws = Workspace.find({ path = fname })
return ws and ws.root or nil
end

return M
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions lua/lazydev/integrations/lspconfig.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
local M = {}

function M.setup()
local ok, Manager = pcall(require, "lspconfig.manager")
if not ok then
return
end

local try_add = Manager.try_add

--- @param buf integer
--- @param project_root? string
function Manager:try_add(buf, project_root)
local is_lua_ls = false
for _, ids in pairs(self._clients) do
for _, client_id in ipairs(ids) do
local client = vim.lsp.get_client_by_id(client_id)
if client and client.name == "lua_ls" then
is_lua_ls = true
break
end
end
end
if is_lua_ls and not project_root then
local root = require("lazydev").find_workspace(buf)
if root then
return self:add(root, false, buf)
end
end
return try_add(self, buf, project_root)
end
end

return M

0 comments on commit 3e301ee

Please sign in to comment.