Skip to content

Commit 80b5d66

Browse files
committed
updated lsp to default to mason
1 parent 4564d70 commit 80b5d66

File tree

3 files changed

+139
-198
lines changed

3 files changed

+139
-198
lines changed

init.lua

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,109 @@
1-
-- ~/.config/nvim/init.lua
21
require 'core.options' -- Load general options
32
require 'core.keymaps' -- Load general keymaps
43
require 'core.snippets' -- Custom code snippets
54

6-
-- Install package manager (lazy.nvim)
7-
local lazypath = vim.fn.stdpath('data') .. '/lazy/lazy.nvim'
5+
-- Install package manager
6+
local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
87
if not vim.loop.fs_stat(lazypath) then
9-
vim.fn.system({
10-
'git', 'clone', '--filter=blob:none',
8+
vim.fn.system {
9+
'git',
10+
'clone',
11+
'--filter=blob:none',
1112
'https://github.com/folke/lazy.nvim.git',
12-
'--branch=stable', lazypath,
13-
})
13+
'--branch=stable', -- latest stable release
14+
lazypath,
15+
}
1416
end
1517
vim.opt.rtp:prepend(lazypath)
16-
17-
-- Filetypes
1818
vim.filetype.add({
19-
extension = { templ = 'templ' },
19+
extension = {
20+
templ = "templ",
21+
}
2022
})
2123

22-
-- Theme selection (robust against unknown NVIM_THEME)
24+
-- Import color theme based on environment variable NVIM_THEME
2325
local default_color_scheme = 'quantum'
24-
local env_var_nvim_theme = os.getenv('NVIM_THEME') or default_color_scheme
26+
local env_var_nvim_theme = os.getenv 'NVIM_THEME' or default_color_scheme
27+
28+
-- Define a table of theme modules
2529
local themes = {
2630
quantum = 'plugins.themes.quantum',
27-
nord = 'plugins.themes.nord',
31+
nord = 'plugins.themes.nord',
2832
onedark = 'plugins.themes.onedark',
2933
}
30-
local theme_module = themes[env_var_nvim_theme] or themes[default_color_scheme]
3134

32-
-- Plugins
35+
-- Setup plugins
3336
require('lazy').setup({
34-
require(theme_module),
37+
require(themes[env_var_nvim_theme]),
3538
require 'core.ui',
3639

37-
-- Load mason early so tools are ready for LSP configs
38-
require 'plugins.mason',
39-
40-
-- Core dev UX
41-
require 'plugins.treesitter',
42-
require 'plugins.telescope',
43-
require 'plugins.lualine',
40+
require 'plugins.aerial',
41+
require 'plugins.autoformat',
42+
require 'plugins.flash',
43+
require 'plugins.autocompletion',
4444
require 'plugins.bufferline',
45+
require 'plugins.comment',
46+
require 'plugins.conform',
47+
require 'plugins.database',
48+
require 'plugins.debug',
49+
require 'plugins.gitsigns',
50+
require 'plugins.harpoon',
51+
require 'plugins.lazygit',
52+
require 'plugins.lsp',
53+
require 'plugins.lualine',
54+
require 'plugins.none-ls',
4555
require 'plugins.indent-blankline',
4656
require 'plugins.neo-tree',
57+
require 'plugins.misc',
58+
require 'plugins.mason',
59+
require 'plugins.snack',
60+
require 'plugins.telescope',
4761
require 'plugins.toggleterm',
62+
require 'plugins.treesitter',
4863
require 'plugins.vim-tmux-navigator',
4964
require 'plugins.zellij',
50-
require 'plugins.flash',
51-
require 'plugins.comment',
52-
require 'plugins.harpoon',
53-
require 'plugins.gitsigns',
54-
require 'plugins.lazygit',
55-
require 'plugins.aerial',
56-
require 'plugins.misc',
5765

58-
-- LSP & companions
59-
require 'plugins.autocompletion',
60-
require 'plugins.lsp',
61-
require 'plugins.none-ls', -- none-ls/null-ls sources & setup
62-
require 'plugins.autoformat', -- your autoformat-on-save/idle logic
63-
64-
-- Optional: pick one formatter stack. If you keep Conform,
65-
-- ensure it doesn't also format Go on save to avoid double-format.
66-
require 'plugins.conform',
67-
68-
-- Debugging / DB (as you had)
69-
require 'plugins.debug',
70-
require 'plugins.database',
7166
}, {
7267
ui = {
68+
-- If you have a Nerd Font, set icons to an empty table which will use the
69+
-- default lazy.nvim defined Nerd Font icons otherwise define a unicode icons table
7370
icons = vim.g.have_nerd_font and {} or {
74-
cmd = '', config = '🛠', event = '📅', ft = '📂', init = '',
75-
keys = '🗝', plugin = '🔌', runtime = '💻', require = '🌙',
76-
source = '📄', start = '🚀', task = '📌', lazy = '💤 ',
71+
cmd = '',
72+
config = '🛠',
73+
event = '📅',
74+
ft = '📂',
75+
init = '',
76+
keys = '🗝',
77+
plugin = '🔌',
78+
runtime = '💻',
79+
require = '🌙',
80+
source = '📄',
81+
start = '🚀',
82+
task = '📌',
83+
lazy = '💤 ',
7784
},
7885
},
7986
})
8087

81-
-- (Optional) tiny helper if you ever want to source a session file
88+
-- Function to check if a file exists
8289
local function file_exists(file)
8390
local f = io.open(file, 'r')
84-
if f then f:close(); return true end
85-
return false
91+
if f then
92+
f:close()
93+
return true
94+
else
95+
return false
96+
end
8697
end
8798

88-
-- local session_file = '.session.vim'
89-
-- if file_exists(session_file) then vim.cmd('source ' .. session_file) end
99+
-- Path to the session file
100+
local session_file = '.session.vim'
90101

91-
-- vim: ts=2 sts=2 sw=2 et
102+
-- Check if the session file exists in the current directory
103+
-- if file_exists(session_file) then
104+
-- -- Source the session file
105+
-- vim.cmd('source ' .. session_file)
106+
-- end
92107

108+
-- The line beneath this is called `modeline`. See `:help modeline`
109+
-- vim: ts=2 sts=2 sw=2 et

lua/plugins/lsp.lua

Lines changed: 58 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2,162 +2,75 @@
22
return {
33
{
44
"neovim/nvim-lspconfig",
5-
lazy = false,
65
config = function()
7-
local lspconfig = require("lspconfig")
8-
local util = require("lspconfig.util")
9-
local configs = require("lspconfig.configs")
10-
11-
-- Recognize .templ files
12-
vim.filetype.add({ extension = { templ = "templ" } })
13-
14-
-- ==============================
15-
-- gopls
16-
-- ==============================
17-
lspconfig.gopls.setup({
18-
root_dir = function(fname)
19-
return util.root_pattern("go.work", "go.mod", ".git")(fname)
20-
or util.path.dirname(fname)
21-
end,
22-
handlers = {
23-
["textDocument/signatureHelp"] = function(err, result, ctx, config)
24-
if err and err.message and err.message:find("cannot get type") then
25-
return
26-
end
27-
return vim.lsp.handlers["textDocument/signatureHelp"](err, result, ctx, config)
28-
end,
29-
},
30-
settings = {
31-
gopls = {
32-
gofumpt = true,
33-
codelenses = {
34-
gc_details = false,
35-
generate = true,
36-
regenerate_cgo = true,
37-
run_govulncheck = true,
38-
test = true,
39-
tidy = true,
40-
upgrade_dependency = true,
41-
vendor = true,
42-
},
43-
hints = {
44-
assignVariableTypes = true,
45-
compositeLiteralFields = true,
46-
compositeLiteralTypes = true,
47-
constantValues = true,
48-
functionTypeParameters = true,
49-
parameterNames = true,
50-
rangeVariableTypes = true,
51-
},
52-
analyses = {
53-
fieldalignment = true,
54-
nilness = true,
55-
unusedparams = true,
56-
unusedwrite = true,
57-
useany = true,
58-
},
59-
usePlaceholders = true,
60-
completeUnimported = true,
61-
staticcheck = true,
62-
directoryFilters = {
63-
"-.git","-.vscode","-.idea","-.vscode-test","-node_modules",
64-
"-dist","-build","-out","-coverage","-tmp","-.cache",
65-
},
66-
semanticTokens = true,
67-
memoryMode = "DegradeClosed",
68-
symbolMatcher = "FastFuzzy",
69-
["ui.completion.experimentalPostfixCompletions"] = false,
70-
},
71-
},
72-
})
73-
74-
-- ==============================
75-
-- TypeScript / JavaScript (ts_ls OR tsserver fallback)
76-
-- ==============================
77-
local ts_server = lspconfig.ts_ls or lspconfig.tsserver
78-
if ts_server then
79-
ts_server.setup({})
80-
end
81-
82-
-- ==============================
83-
-- Astro (guard if missing)
84-
-- ==============================
85-
if lspconfig.astro then
86-
local function get_typescript_lib()
87-
local mason_ts = vim.fs.normalize(
88-
"~/.local/share/nvim/mason/packages/typescript-language-server/node_modules/typescript/lib"
89-
)
90-
if vim.fn.isdirectory(mason_ts) == 1 then return mason_ts end
91-
92-
local global_ts = (vim.fn.system("npm root -g"):gsub("\n", "")) .. "/typescript/lib"
93-
if vim.fn.isdirectory(global_ts) == 1 then return global_ts end
94-
95-
return vim.fs.normalize(
96-
"~/.local/share/nvim/mason/packages/astro-language-server/node_modules/typescript/lib"
97-
)
98-
end
99-
100-
lspconfig.astro.setup({
101-
init_options = { typescript = { tsdk = get_typescript_lib() } },
102-
})
103-
end
104-
105-
-- ==============================
106-
-- templ (register config if missing)
107-
-- ==============================
108-
if not configs.templ then
109-
configs.templ = {
110-
default_config = {
111-
cmd = { "templ", "lsp" },
112-
filetypes = { "templ" },
113-
root_dir = util.root_pattern("go.mod", ".git"),
114-
single_file_support = true,
115-
},
116-
}
117-
end
118-
lspconfig.templ.setup({})
119-
120-
-- ==============================
121-
-- Utilities
122-
-- ==============================
6+
---------------------------------------------------------------------------
7+
-- LSP client utilities
8+
---------------------------------------------------------------------------
1239
vim.api.nvim_create_user_command("LspClients", function()
10+
-- new API: vim.lsp.get_clients
12411
local clients = vim.lsp.get_clients()
125-
local counts = {}
126-
for _, c in ipairs(clients) do
127-
counts[c.name] = (counts[c.name] or 0) + 1
12+
local client_counts = {}
13+
14+
for _, client in ipairs(clients) do
15+
client_counts[client.name] = (client_counts[client.name] or 0) + 1
12816
end
17+
12918
print("=== Active LSP Clients ===")
130-
for name, n in pairs(counts) do
131-
local dup = n > 1 and " ⚠️ DUPLICATE" or ""
132-
print(string.format("%s: %d client(s)%s", name, n, dup))
19+
for name, count in pairs(client_counts) do
20+
local status = count > 1 and " ⚠️ DUPLICATE" or ""
21+
print(string.format("%s: %d client(s)%s", name, count, status))
22+
end
23+
24+
if next(client_counts) == nil then
25+
print("No active LSP clients")
13326
end
134-
if next(counts) == nil then print("No active LSP clients") end
135-
end, {})
27+
end, { desc = "Show active LSP clients and detect duplicates" })
13628

13729
vim.api.nvim_create_user_command("LspKillDuplicates", function()
30+
-- only worry about duplicate gopls, since that’s your main concern
13831
local gopls_clients = vim.lsp.get_clients({ name = "gopls" })
13932
if #gopls_clients <= 1 then
14033
print("No duplicate gopls clients found")
14134
return
14235
end
143-
local keep, kill = nil, {}
144-
for _, c in ipairs(gopls_clients) do
145-
local cnt = 0
146-
if c.config.settings and c.config.settings.gopls then
147-
for _ in pairs(c.config.settings.gopls) do cnt = cnt + 1 end
36+
37+
local client_to_keep = nil
38+
local clients_to_kill = {}
39+
40+
for _, client in ipairs(gopls_clients) do
41+
local settings_count = 0
42+
if client.config.settings and client.config.settings.gopls then
43+
for _ in pairs(client.config.settings.gopls) do
44+
settings_count = settings_count + 1
45+
end
14846
end
149-
if cnt > 0 and not keep then keep = c else table.insert(kill, c) end
47+
48+
if settings_count > 0 and not client_to_keep then
49+
client_to_keep = client
50+
else
51+
table.insert(clients_to_kill, client)
52+
end
53+
end
54+
55+
for _, client in ipairs(clients_to_kill) do
56+
print(string.format("Killing duplicate gopls client (id: %d)", client.id))
57+
client.stop(true)
15058
end
151-
for _, c in ipairs(kill) do
152-
print(("Killing duplicate gopls client (id: %d)"):format(c.id))
153-
c.stop(true)
59+
60+
if client_to_keep then
61+
print(string.format("Kept gopls client (id: %d) with settings", client_to_keep.id))
15462
end
155-
if keep then print(("Kept gopls client (id: %d) with settings"):format(keep.id)) end
156-
end, {})
63+
end, { desc = "Kill duplicate gopls clients" })
15764

65+
---------------------------------------------------------------------------
66+
-- Hover safety + keymaps
67+
---------------------------------------------------------------------------
15868
local function has_hover(bufnr)
159-
for _, c in pairs(vim.lsp.get_active_clients({ bufnr = bufnr })) do
160-
if c.server_capabilities and c.server_capabilities.hoverProvider then return true end
69+
local clients = vim.lsp.get_clients({ bufnr = bufnr })
70+
for _, c in pairs(clients) do
71+
if c.server_capabilities and c.server_capabilities.hoverProvider then
72+
return true
73+
end
16174
end
16275
return false
16376
end
@@ -172,11 +85,16 @@ return {
17285
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc })
17386
end
17487

88+
-- K = hover (safe)
17589
buf_map("n", "K", function()
176-
if not has_hover(bufnr) then return end
90+
if not has_hover(bufnr) then
91+
return
92+
end
17793
local ok, saga_hover = pcall(require, "lspsaga.hover")
17894
if ok and saga_hover and saga_hover.render_hover_doc then
179-
pcall(function() saga_hover:render_hover_doc() end)
95+
pcall(function()
96+
saga_hover:render_hover_doc()
97+
end)
18098
else
18199
pcall(vim.lsp.buf.hover)
182100
end

0 commit comments

Comments
 (0)