nvim: add psoxizsh.lsp.core
Adds back some of the functionality of the late lsp.server, such that we can paper over the exact implementation details of _how_ we're enabling the language server. In practice, this will allow us to create a bunch of preset PsoxizshLspServer types, which users can then opt into enabling in their plugin/* configs
This commit is contained in:
parent
f4a32dc0ba
commit
b22285ef0f
|
@ -0,0 +1,95 @@
|
|||
|
||||
---@class PsoxizshLspServer
|
||||
---@field id string The canonical name of the server
|
||||
---@field specs LazySpec Any other LazySpecs that will be included with ours
|
||||
---@field opts PsoxizshLspServerConfig This spec's config
|
||||
---@field callback PsoxizshLspPresetCallback Converts self.opts into a LazySpec
|
||||
---@field get fun(self: PsoxizshLspServer): LazySpec Return the materialized LazySpec for this server
|
||||
---@field with fun(self: PsoxizshLspServer, opts: PsoxizshLspServerConfig): LazySpec Return the materialized LazySpec for this server, with the given opts merged in
|
||||
---@operator call(PsoxizshLspServerConfig|nil): LazySpec
|
||||
|
||||
---@class PsoxizshLspServerConfig
|
||||
---@field settings table The lspconfig settings for this server
|
||||
|
||||
---@alias PsoxizshLspPresetCallback fun(string, PsoxizshLspServerConfig): LazySpec
|
||||
|
||||
---@class psoxizsh.lsp.core
|
||||
local M = {}
|
||||
|
||||
local S = {}
|
||||
|
||||
---Merge the provided config into the normal neovim/nvim-lspconfig structure
|
||||
---@param server string
|
||||
---@param config table
|
||||
---@return table
|
||||
function M.make_server(server, config)
|
||||
return { servers = { [server] = config } }
|
||||
end
|
||||
|
||||
---Create a LazyPlugin spec override for neovim/nvim-lspconfig for the given
|
||||
---server and config
|
||||
---@param server string
|
||||
---@param opts PsoxizshLspServerConfig
|
||||
---@return LazySpec
|
||||
function M.lspconfig_server(server, opts)
|
||||
return {
|
||||
'neovim/nvim-lspconfig', opts = M.make_server(server, opts.settings)
|
||||
}
|
||||
end
|
||||
|
||||
---Create a new LSP server spec
|
||||
---@param id string
|
||||
---@param inherit LazySpec
|
||||
---@param cb PsoxizshLspPresetCallback
|
||||
---@return PsoxizshLspServer
|
||||
function M.preset(id, inherit, cb)
|
||||
return S.new(id, inherit, cb)
|
||||
end
|
||||
|
||||
---Create a new PsoxizshLspServer
|
||||
---@param id string
|
||||
---@param specs LazySpec
|
||||
---@param cb PsoxizshLspPresetCallback
|
||||
---@return PsoxizshLspServer
|
||||
function S.new(id, specs, cb)
|
||||
local this = vim.deepcopy(S)
|
||||
|
||||
this.id = id
|
||||
this.specs = specs
|
||||
this.callback = cb
|
||||
this.opts = { settings = {} }
|
||||
|
||||
setmetatable(this, {
|
||||
__call = function(self, opts)
|
||||
local hasopts = type(opts) == 'table' and next(opts) ~= 'nil' or type(opts) ~= 'nil'
|
||||
|
||||
return hasopts and self:with(opts) or self:get()
|
||||
end
|
||||
})
|
||||
|
||||
return this
|
||||
end
|
||||
|
||||
---Get the LazyVim plugin spec(s) for this LspServer
|
||||
---@param self PsoxizshLspServer
|
||||
---@return LazySpec
|
||||
function S.get(self)
|
||||
local unpack = table.unpack or unpack
|
||||
local specs = type(self.specs) == 'string' and {{ import = self.specs }} or self.specs
|
||||
local lsp = self.callback(self.id, self.opts)
|
||||
|
||||
return { unpack(specs), lsp }
|
||||
end
|
||||
|
||||
---Get the LazyVim plugin spec(s) for this LspServer
|
||||
---@param self PsoxizshLspServer
|
||||
---@param opts PsoxizshLspServerConfig
|
||||
---@return LazySpec
|
||||
function S.with(self, opts)
|
||||
local this = vim.deepcopy(self)
|
||||
this.opts = vim.tbl_deep_extend('force', this.opts, opts)
|
||||
|
||||
return this:get()
|
||||
end
|
||||
|
||||
return M
|
Loading…
Reference in New Issue