-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzz-lsp.lua
More file actions
219 lines (189 loc) · 5.91 KB
/
zz-lsp.lua
File metadata and controls
219 lines (189 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
-- ============================================================================
-- LSP Configuration - All LSP setup in one place
-- ============================================================================
-- ============================================================================
-- 1. DIAGNOSTIC CONFIGURATION
-- ============================================================================
local signs_text = {
[vim.diagnostic.severity.ERROR] = "■",
[vim.diagnostic.severity.WARN] = "▲",
[vim.diagnostic.severity.HINT] = "✦",
[vim.diagnostic.severity.INFO] = "●",
}
vim.diagnostic.config({
virtual_text = false,
signs = { text = signs_text },
update_in_insert = true,
underline = true,
severity_sort = true,
float = {
focusable = false,
style = "minimal",
border = "single",
source = "always",
},
})
-- ============================================================================
-- 2. DIAGNOSTIC DISPLAY & HOVER LOGIC
-- ============================================================================
-- State management
local state = {
skip_diagnostic = false,
timer = nil,
}
-- Config
local config = {
float_opts = {
focusable = false,
close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
border = "rounded",
source = "always",
prefix = " ",
scope = "cursor",
},
hover_delay = 1000,
}
-- Cache blink.cmp require at module level (not in hot path)
local blink_cmp = require("blink.cmp")
local function is_completion_visible()
return blink_cmp.is_visible()
end
local function display_diagnostics()
if not is_completion_visible() then
vim.diagnostic.open_float(nil, config.float_opts)
end
end
-- Auto-show diagnostics on cursor hold
vim.api.nvim_create_autocmd({ "CursorHold" }, {
pattern = "*",
callback = function()
if not state.skip_diagnostic then
display_diagnostics()
end
end,
})
-- Toggle hover (temporarily suppress diagnostic float)
local function toggle_hover()
state.skip_diagnostic = true
vim.lsp.buf.hover()
if state.timer then
state.timer:stop()
end
state.timer = vim.defer_fn(function()
state.skip_diagnostic = false
end, config.hover_delay)
end
-- ============================================================================
-- 3. LSP KEYMAPS
-- ============================================================================
local function setup_keymaps(bufnr)
local maps = {
{ "n", "gD", vim.lsp.buf.declaration },
{ "n", "gd", require("telescope.builtin").lsp_definitions },
{ "n", "K", toggle_hover },
{ "n", "gi", require("telescope.builtin").lsp_implementations },
{ "n", "<Space>rn", vim.lsp.buf.rename },
{ "n", "gr", require("telescope.builtin").lsp_references },
{ "n", "<Space>f", vim.lsp.buf.code_action },
{ "n", "<Space>l", require("telescope.builtin").diagnostics },
}
for _, map in ipairs(maps) do
vim.keymap.set(map[1], map[2], map[3], { buffer = bufnr })
end
end
-- ============================================================================
-- 4. AUTO-FORMAT ON SAVE
-- ============================================================================
local skip_format = {
c = true,
-- cpp = true
}
local function setup_format(client, bufnr)
if not client:supports_method("textDocument/formatting") then
return
end
if skip_format[vim.bo.filetype] then
return
end
vim.api.nvim_create_autocmd("BufWritePre", {
buffer = bufnr,
callback = function()
vim.lsp.buf.format({
bufnr = bufnr,
id = client.id,
timeout_ms = 2000,
})
end,
})
end
-- ============================================================================
-- 5. LSP ATTACH HANDLER
-- ============================================================================
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then
return
end
setup_keymaps(args.buf)
setup_format(client, args.buf)
end,
})
-- ============================================================================
-- 6. INLAY HINTS
-- ============================================================================
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
vim.api.nvim_set_hl(0, "LspInlayHint", { link = "Comment" })
-- ============================================================================
-- 7. SERVER-SPECIFIC CONFIGURATIONS
-- ============================================================================
-- Clangd
vim.lsp.config("clangd", {
cmd = {
"clangd",
"--background-index",
"--clang-tidy",
"--completion-style=detailed",
"--header-insertion=never",
"--pch-storage=memory",
"--offset-encoding=utf-16",
},
init_options = {
fallbackFlags = { "--std=gnu++23" },
},
})
-- Lua LS
vim.lsp.config("lua_ls", {
settings = {
Lua = {
hint = { enable = true },
runtime = {
version = "LuaJIT",
},
diagnostics = {
globals = { "vim" },
},
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
},
telemetry = {
enable = false,
},
},
},
})
-- ============================================================================
-- 8. ENABLE LSP SERVERS
-- ============================================================================
local lsp_servers = {
"clangd",
"lua_ls",
"ruff",
-- "pyright",
"ty",
"taplo",
"texlab",
"neocmake",
"rust_analyzer",
}
vim.lsp.enable(lsp_servers)