Skip to content

Commit

Permalink
feat: add lsp keys cond function from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
rafi committed May 26, 2024
1 parent 25ab45b commit c0fb9c4
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 117 deletions.
222 changes: 119 additions & 103 deletions lua/rafi/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,97 +23,105 @@ return {
'williamboman/mason-lspconfig.nvim',
},
---@class PluginLspOpts
opts = {
-- Options for vim.diagnostic.config()
---@type vim.diagnostic.Opts
diagnostics = {
underline = true,
update_in_insert = false,
virtual_text = {
spacing = 4,
source = 'if_many',
prefix = '',
-- this will set set the prefix to a function that returns the diagnostics icon based on the severity
-- this only works on a recent 0.10.0 build. Will be set to "●" when not supported
-- prefix = 'icons',
opts = function()
return {
-- Options for vim.diagnostic.config()
---@type vim.diagnostic.Opts
diagnostics = {
underline = true,
update_in_insert = false,
virtual_text = {
spacing = 4,
source = 'if_many',
prefix = '',
-- this will set set the prefix to a function that returns the diagnostics icon based on the severity
-- this only works on a recent 0.10.0 build. Will be set to "●" when not supported
-- prefix = 'icons',
},
severity_sort = true,
-- CUSTOM: Diagnostic popup
-- float = {
-- border = 'rounded',
-- source = true,
-- header = '',
-- prefix = '',
-- },
signs = {
text = {
[vim.diagnostic.severity.ERROR] = LazyVim.config.icons.diagnostics.Error,
[vim.diagnostic.severity.WARN] = LazyVim.config.icons.diagnostics.Warn,
[vim.diagnostic.severity.HINT] = LazyVim.config.icons.diagnostics.Hint,
[vim.diagnostic.severity.INFO] = LazyVim.config.icons.diagnostics.Info,
},
},
},
severity_sort = true,
float = {
border = 'rounded',
source = true,
header = '',
prefix = '',
-- Enable this to enable the builtin LSP inlay hints on Neovim >= 0.10.0
-- Be aware that you also will need to properly configure your LSP server
-- to provide the inlay hints.
inlay_hints = {
enabled = false,
},
signs = {
text = {
[vim.diagnostic.severity.ERROR] = LazyVim.config.icons.diagnostics.Error,
[vim.diagnostic.severity.WARN] = LazyVim.config.icons.diagnostics.Warn,
[vim.diagnostic.severity.HINT] = LazyVim.config.icons.diagnostics.Hint,
[vim.diagnostic.severity.INFO] = LazyVim.config.icons.diagnostics.Info,
},
-- Enable this to enable the builtin LSP code lenses on Neovim >= 0.10.0
-- Be aware that you also will need to properly configure your LSP server to
-- provide the code lenses.
codelens = {
enabled = false,
},
},
-- Enable this to enable the builtin LSP inlay hints on Neovim >= 0.10.0
-- Be aware that you also will need to properly configure your LSP server
-- to provide the inlay hints.
inlay_hints = {
enabled = false,
},
-- Enable this to enable the builtin LSP code lenses on Neovim >= 0.10.0
-- Be aware that you also will need to properly configure your LSP server to
-- provide the code lenses.
codelens = {
enabled = false,
},
-- Enable lsp cursor word highlighting
document_highlight = {
enabled = true,
},
-- Add any global capabilities here
capabilities = {},
-- Formatting options for vim.lsp.buf.format
format = {
formatting_options = nil,
timeout_ms = nil,
},
-- LSP Server Settings
---@type lspconfig.options
---@diagnostic disable: missing-fields
servers = {
lua_ls = {
settings = {
Lua = {
workspace = { checkThirdParty = false },
codeLens = { enable = true },
completion = { callSnippet = 'Replace' },
doc = {
privateName = { '^_' },
},
hint = {
enable = true,
setType = false,
paramType = true,
paramName = 'Disable',
semicolon = 'Disable',
arrayIndex = 'Disable',
-- Enable lsp cursor word highlighting
document_highlight = {
enabled = true,
},
-- Add any global capabilities here
capabilities = {},
-- Formatting options for vim.lsp.buf.format
format = {
formatting_options = nil,
timeout_ms = nil,
},
-- LSP Server Settings
---@type lspconfig.options
---@diagnostic disable: missing-fields
servers = {
lua_ls = {
-- mason = false, -- set to false if you don't want this server to be
-- installed with mason Use this to add any additional keymaps for
-- specific lsp servers.
---@type LazyKeysSpec[]
-- keys = {},
settings = {
Lua = {
workspace = { checkThirdParty = false },
codeLens = { enable = true },
completion = { callSnippet = 'Replace' },
doc = {
privateName = { '^_' },
},
hint = {
enable = true,
setType = false,
paramType = true,
paramName = 'Disable',
semicolon = 'Disable',
arrayIndex = 'Disable',
},
},
},
},
},
},
-- you can do any additional lsp server setup here
-- return true if you don't want this server to be setup with lspconfig
---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?>
setup = {
-- example to setup with typescript.nvim
-- tsserver = function(_, opts)
-- require('typescript').setup({ server = opts })
-- return true
-- end,
-- Specify * to use this function as a fallback for any server
-- ['*'] = function(server, opts) end,
},
},
-- you can do any additional lsp server setup here
-- return true if you don't want this server to be setup with lspconfig
---@type table<string, fun(server:string, opts:_.lspconfig.options):boolean?>
setup = {
-- example to setup with typescript.nvim
-- tsserver = function(_, opts)
-- require('typescript').setup({ server = opts })
-- return true
-- end,
-- Specify * to use this function as a fallback for any server
-- ['*'] = function(server, opts) end,
},
}
end,
---@param opts PluginLspOpts
config = function(_, opts)
if LazyVim.has('neoconf.nvim') then
Expand All @@ -128,17 +136,11 @@ return {
require('rafi.plugins.lsp.keymaps').on_attach(client, buffer)
end)

-- When LSP client stops, undo buffer keymaps and such.
vim.api.nvim_create_autocmd('LspDetach', {
callback = function(args)
local buffer = args.buf ---@type number
local client = vim.lsp.get_client_by_id(args.data.client_id)
require('rafi.plugins.lsp.keymaps').on_detach(client, buffer)
end,
})

LazyVim.lsp.setup()
LazyVim.lsp.on_dynamic_capability(require('rafi.plugins.lsp.keymaps').on_attach)
LazyVim.lsp.on_dynamic_capability(
require('rafi.plugins.lsp.keymaps').on_attach
)

LazyVim.lsp.words.setup(opts.document_highlight)

Expand All @@ -158,19 +160,33 @@ return {
if vim.fn.has('nvim-0.10') == 1 then
-- inlay hints
if opts.inlay_hints.enabled then
LazyVim.lsp.on_supports_method('textDocument/inlayHint', function(_, buffer)
LazyVim.toggle.inlay_hints(buffer, true)
end)
LazyVim.lsp.on_supports_method(
'textDocument/inlayHint',
function(_, buffer)
if
vim.api.nvim_buf_is_valid(buffer)
and vim.bo[buffer].buftype == ''
then
LazyVim.toggle.inlay_hints(buffer, true)
end
end
)
end
-- code lens
if opts.codelens.enabled and vim.lsp.codelens then
LazyVim.lsp.on_supports_method('textDocument/codeLens', function(_, buffer)
vim.lsp.codelens.refresh()
vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorHold', 'InsertLeave' }, {
buffer = buffer,
callback = vim.lsp.codelens.refresh,
})
end)
LazyVim.lsp.on_supports_method(
'textDocument/codeLens',
function(_, buffer)
vim.lsp.codelens.refresh()
vim.api.nvim_create_autocmd(
{ 'BufEnter', 'CursorHold', 'InsertLeave' },
{
buffer = buffer,
callback = vim.lsp.codelens.refresh,
}
)
end
)
end
end

Expand Down
33 changes: 19 additions & 14 deletions lua/rafi/plugins/lsp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ function M.get()
})
end, desc = 'Source Action', has = 'codeAction' },

{ ']]', function() LazyVim.lsp.words.jump(vim.v.count1) end, has = 'documentHighlight', desc = 'Next Reference' },
{ '[[', function() LazyVim.lsp.words.jump(-vim.v.count1) end, has = 'documentHighlight', desc = 'Next Reference' },
{ ']]', function() LazyVim.lsp.words.jump(vim.v.count1) end, has = 'documentHighlight',
desc = 'Next Reference', cond = function() return LazyVim.lsp.words.enabled end },
{ '[[', function() LazyVim.lsp.words.jump(-vim.v.count1) end, has = 'documentHighlight',
desc = 'Prev Reference', cond = function() return LazyVim.lsp.words.enabled end },
{ '<a-n>', function() LazyVim.lsp.words.jump(vim.v.count1, true) end, has = 'documentHighlight',
desc = 'Next Reference', cond = function() return LazyVim.lsp.words.enabled end },
{ '<a-p>', function() LazyVim.lsp.words.jump(-vim.v.count1, true) end, has = 'documentHighlight',
desc = 'Prev Reference', cond = function() return LazyVim.lsp.words.enabled end },

{ 'K', function()
-- Show hover documentation or folded lines.
Expand Down Expand Up @@ -91,7 +97,7 @@ function M.has(buffer, method)
return false
end

---@return (LazyKeys|{has?:string})[]
---@return LazyKeysLsp[]
function M.resolve(buffer)
local Keys = require('lazy.core.handler.keys')
if not Keys.resolve then
Expand All @@ -114,9 +120,18 @@ function M.on_attach(_, buffer)
local keymaps = M.resolve(buffer)

for _, keys in pairs(keymaps) do
if not keys.has or M.has(buffer, keys.has) then
local has = not keys.has or M.has(buffer, keys.has)
local cond = not (
keys.cond == false or (
(type(keys.cond) == 'function') and not keys.cond()
)
)

if has and cond then
local opts = Keys.opts(keys) --[[@as vim.keymap.set.Opts]]
---@diagnostic disable-next-line: inject-field
opts.cond = nil
---@diagnostic disable-next-line: inject-field
opts.has = nil
opts.silent = opts.silent ~= false
opts.buffer = buffer
Expand All @@ -125,16 +140,6 @@ function M.on_attach(_, buffer)
end
end

function M.on_detach(_, buffer)
local keymaps = M.resolve(buffer)
for _, keys in pairs(keymaps) do
if not keys.has or M.has(buffer, keys.has) then
local opts = { buffer = buffer }
vim.keymap.del(keys.mode or 'n', keys.lhs, opts)
end
end
end

-- Display a list of formatters and apply the selected one.
function M.formatter_select()
local buf = vim.api.nvim_get_current_buf()
Expand Down

0 comments on commit c0fb9c4

Please sign in to comment.