feat: lsp

This commit is contained in:
2025-10-23 20:16:45 +03:00
parent bb12a11cc0
commit 173ff5e47a
8 changed files with 471 additions and 30 deletions

View File

@@ -75,3 +75,5 @@ map('n', '[d', function()
vim.diagnostic.jump({ count = -1, float = true })
end)
map('n', '<leader>q', vim.diagnostic.setloclist)
map('n', '<leader>d', vim.diagnostic.open_float)
map('n', '<leader>s', vim.lsp.buf.signature_help)

View File

@@ -71,7 +71,7 @@ vim.opt.mouse = 'a' -- Enable mouse support
vim.opt.ignorecase = true
vim.opt.smartcase = true -- Override ignorecase if search contains upper case chars
vim.opt.inccommand = 'split' -- Live substitution preview
vim.opt.completeopt = { 'menu,menuone,noselect' }
vim.opt.completeopt = { 'fuzzy,menuone,popup,preview,noselect' }
-- Splits
vim.opt.splitright = true
@@ -87,22 +87,3 @@ vim.opt.timeout = true
vim.opt.ttimeout = true
vim.opt.timeoutlen = 500
vim.opt.ttimeoutlen = 10
vim.keymap.set('n', '<leader>d', vim.diagnostic.open_float, { noremap = true, silent = true })
vim.diagnostic.config({
update_in_insert = false,
virtual_text = {
prefix = '', -- remove annoying ▎ etc
format = function(diagnostic)
if diagnostic.source then
return string.format('[%s] %s', diagnostic.source, diagnostic.message)
end
return diagnostic.message
end,
},
float = {
border = 'rounded',
source = true, -- show source in floating window too
},
severity_sort = true,
})

View File

@@ -19,6 +19,13 @@ return {
TabLine = { fg = c.muted, bg = c.none },
TabLineSel = { fg = c.text, bg = c.none, bold = true },
TabLineFill = { fg = c.outline_light, bg = c.none },
Pmenu = { fg = c.text, bg = c.surface },
PmenuSel = { fg = c.text, bg = c.accent_light },
QuickFixLine = { fg = c.accent, bg = c.none, bold = true },
-- PmenuSbar = { bg = c.surface },
-- PmenuThumb = { bg = c.outline },
-- PmenuBorder = { fg = c.outline },
}
end,
})

View File

@@ -13,8 +13,8 @@ local language_specs = {
'gitignore',
'dockerfile',
'diff',
{ 'json', nil, nil, 'prettier' },
{ 'jsonc', nil, nil, 'prettier' },
{ 'json', 'json-lsp' },
{ 'jsonc', 'json-lsp' },
-- Shell / scripting
{ 'bash', 'bash-language-server', 'shellcheck', 'shfmt' },
@@ -30,12 +30,12 @@ local language_specs = {
-- { 'python', 'pyright', 'ruff', 'ruff' }, -- install ensurepip
-- Web stack
{ 'html', nil, nil, 'prettier' },
{ 'css', nil, nil, 'prettier' },
{ 'javascript', 'vtsls', 'eslint_d', 'prettier' },
{ 'typescript', 'vtsls', 'eslint_d', 'prettier' },
{ nil, 'vtsls', 'eslint_d', 'prettier', filetype = 'javascriptreact' },
{ 'tsx', 'vtsls', 'eslint_d', 'prettier', filetype = 'typescriptreact' },
{ 'html', 'html-lsp' },
{ 'css', 'css-lsp' },
{ 'javascript', { 'vtsls', 'eslint-lsp' }, nil, 'prettier' },
{ 'typescript', { 'vtsls', 'eslint-lsp' }, nil, 'prettier' },
{ nil, { 'vtsls', 'eslint-lsp' }, nil, 'prettier', filetype = 'javascriptreact' },
{ 'tsx', { 'vtsls', 'eslint-lsp' }, nil, 'prettier', filetype = 'typescriptreact' },
}
local function normalize(spec)
@@ -71,7 +71,13 @@ local function collect(specs)
table.insert(ts, s.treesitter)
end
if s.lsp then
table.insert(lsps, s.lsp)
if type(s.lsp) == 'table' then
for _, v in ipairs(s.lsp) do
table.insert(lsps, v)
end
else
table.insert(lsps, s.lsp)
end
end
if s.linter then
linters[s.filetype] = { s.linter }
@@ -187,9 +193,124 @@ vim.api.nvim_create_user_command('TreesitterInstallAll', function()
end
end
end, { desc = 'Install all Treesitter parsers defined in ensure_installed' })
vim.api.nvim_create_user_command('General', function()
print(vim.inspect(general))
end, {})
local special_sources = {
lua_ls = 'lua',
eslint = 'eslint',
}
vim.diagnostic.config({
underline = true,
severity_sort = true,
update_in_insert = true,
virtual_text = {
format = function(diagnostic)
local src = diagnostic.source and (special_sources[diagnostic.source] or diagnostic.source)
if src then
return string.format('%s: %s', src, diagnostic.message)
end
return diagnostic.message
end,
},
float = {
border = 'rounded',
header = '',
format = function(diagnostic)
local src = diagnostic.source and (special_sources[diagnostic.source] or diagnostic.source)
if src then
return string.format('%s: %s', src, diagnostic.message)
end
return diagnostic.message
end,
},
})
-- Override the virtual text diagnostic handler so that the most severe diagnostic is shown first.
local show_handler = vim.diagnostic.handlers.virtual_text.show
assert(show_handler)
local hide_handler = vim.diagnostic.handlers.virtual_text.hide
vim.diagnostic.handlers.virtual_text = {
show = function(ns, bufnr, diagnostics, opts)
table.sort(diagnostics, function(diag1, diag2)
return diag1.severity > diag2.severity
end)
return show_handler(ns, bufnr, diagnostics, opts)
end,
hide = hide_handler,
}
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('minimal_lsp', { clear = true }),
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)
if not client then
return
end
-- Enable native completion
if client:supports_method('textDocument/completion') then
vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true })
end
end,
})
---------------------------------------------------------------------
-- Optional: annotate completion items with their kind
---------------------------------------------------------------------
vim.lsp.handlers['textDocument/completion'] = function(err, result, ctx, config)
if err or not result then
return
end
for _, item in ipairs(result.items or result) do
if item.kind then
local kind = vim.lsp.protocol.CompletionItemKind[item.kind] or ''
item.menu = '[' .. kind .. ']'
end
end
return vim.lsp.completion._on_completion_result(err, result, ctx, config)
end
---------------------------------------------------------------------
-- Plugins
---------------------------------------------------------------------
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('lsp_attach_timing', { clear = true }),
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)
if client then
vim.notify(
string.format('LSP attached: %s (buf: %d)', client.name, ev.buf),
vim.log.levels.INFO
)
end
end,
})
local function enable_lsp_with_timing(lsp_name)
local start_time = vim.uv.hrtime()
vim.lsp.enable(lsp_name)
-- Track when the server actually attaches
local group = vim.api.nvim_create_augroup('lsp_timing_' .. lsp_name, { clear = true })
vim.api.nvim_create_autocmd('LspAttach', {
group = group,
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)
if client and client.name == lsp_name then
local elapsed = (vim.uv.hrtime() - start_time) / 1e6 -- Convert to milliseconds
vim.notify(string.format('%s attached in %.2f ms', lsp_name, elapsed), vim.log.levels.INFO)
vim.api.nvim_del_augroup_by_id(group)
end
end,
})
end
return {
{ 'windwp/nvim-ts-autotag', config = true },
{ 'windwp/nvim-autopairs', event = 'InsertEnter', config = true },
@@ -210,7 +331,8 @@ return {
table.insert(lsp_configs, lsp_name)
-- Native enable call (Neovim ≥ 0.11)
vim.lsp.enable(lsp_name)
-- vim.lsp.enable(lsp_name)
enable_lsp_with_timing(lsp_name)
else
vim.notify('Unknown LSP: ' .. lsp, vim.log.levels.WARN)
end

33
lua/utils.lua Normal file
View File

@@ -0,0 +1,33 @@
local M = {}
--- Appends `new_names` to `root_files` if `field` is found in any such file in any ancestor of `fname`.
---
--- NOTE: this does a "breadth-first" search, so is broken for multi-project workspaces:
--- https://github.com/neovim/nvim-lspconfig/issues/3818#issuecomment-2848836794
---
--- @param root_files string[] List of root-marker files to append to.
--- @param new_names string[] Potential root-marker filenames (e.g. `{ 'package.json', 'package.json5' }`) to inspect for the given `field`.
--- @param field string Field to search for in the given `new_names` files.
--- @param fname string Full path of the current buffer name to start searching upwards from.
function M.root_markers_with_field(root_files, new_names, field, fname)
local path = vim.fn.fnamemodify(fname, ':h')
local found = vim.fs.find(new_names, { path = path, upward = true })
for _, f in ipairs(found or {}) do
-- Match the given `field`.
for line in io.lines(f) do
if line:find(field) then
root_files[#root_files + 1] = vim.fs.basename(f)
break
end
end
end
return root_files
end
function M.insert_package_json(root_files, field, fname)
return M.root_markers_with_field(root_files, { 'package.json', 'package.json5' }, field, fname)
end
return M