Skip to content

Commit

Permalink
feat: make lazydev partially work on Neovim < 0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 25, 2024
1 parent 399299c commit 6c24937
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions lua/lazydev/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Workspace = require("lazydev.workspace")

local M = {}
M.attached = {} ---@type table<number,number>
M.did_global_handler = false

function M.assert(client)
assert(client and client.name == "lua_ls", "lazydev: Not a lua_ls client??")
Expand All @@ -21,46 +22,60 @@ function M.attach(client)
-- We need to make sure that each client has its own handlers table.
client.handlers = vim.tbl_extend("force", {}, client.handlers or {})

---@param params lsp.ConfigurationParams
client.handlers["workspace/configuration"] = function(err, params, ctx, cfg)
local c = vim.lsp.get_client_by_id(ctx.client_id)
assert(c == client, "lazydev: Invalid client " .. (c and c.name or "unknown"))
M.assert(c)
if not params.items or #params.items == 0 then
return {}
if vim.fn.has("nvim-0.10") == 0 then
if M.did_global_handler then
return
end

-- fallback scope
if #(client.workspace_folders or {}) > 0 and not params.items[1].scopeUri then
return {}
M.did_global_handler = true
local orig = vim.lsp.handlers["workspace/configuration"]
vim.lsp.handlers["workspace/configuration"] = function(err, params, ctx, cfg)
if M.attached[ctx.client_id] then
return M.on_workspace_configuration(err, params, ctx, cfg)
end
return orig(err, params, ctx, cfg)
end
else
client.handlers["workspace/configuration"] = M.on_workspace_configuration
end
end

local response = {}
for _, item in ipairs(params.items) do
if item.section then
local settings = client.settings
if item.section == "Lua" then
local ws = item.scopeUri and Workspace.get(client, vim.uri_to_fname(item.scopeUri))
or Workspace.single(client)
if ws:enabled() then
settings = ws.settings
end
end
---@param params lsp.ConfigurationParams
function M.on_workspace_configuration(err, params, ctx, cfg)
local client = vim.lsp.get_client_by_id(ctx.client_id)
M.assert(client)
if not client or not params.items or #params.items == 0 then
return {}
end

local keys = vim.split(item.section, ".", { plain = true }) --- @type string[]
local value = vim.tbl_get(settings or {}, unpack(keys))
-- For empty sections with no explicit '' key, return settings as is
if value == nil and item.section == "" then
value = settings
end
if value == nil then
value = vim.NIL
-- fallback scope
if #(client.workspace_folders or {}) > 0 and not params.items[1].scopeUri then
return {}
end

local response = {}
for _, item in ipairs(params.items) do
if item.section then
local settings = client.settings
if item.section == "Lua" then
local ws = item.scopeUri and Workspace.get(client, vim.uri_to_fname(item.scopeUri)) or Workspace.single(client)
if ws:enabled() then
settings = ws.settings
end
table.insert(response, value)
end

local keys = vim.split(item.section, ".", { plain = true }) --- @type string[]
local value = vim.tbl_get(settings or {}, unpack(keys))
-- For empty sections with no explicit '' key, return settings as is
if value == nil and item.section == "" then
value = settings
end
if value == nil then
value = vim.NIL
end
table.insert(response, value)
end
return response
end
return response
end

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

0 comments on commit 6c24937

Please sign in to comment.