-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlsp_ui.lua
More file actions
277 lines (256 loc) · 9.27 KB
/
lsp_ui.lua
File metadata and controls
277 lines (256 loc) · 9.27 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local M = {}
local lspkind_symbol_map = {
Text = "",
Method = "",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
-- remove obsolete TS* highlight groups https://github.com/nvim-treesitter/nvim-treesitter/pull/3656
M.symbol_map = {
Text = { icon = lspkind_symbol_map.Text },
Method = { icon = lspkind_symbol_map.Method, hl = "@method" },
Function = { icon = lspkind_symbol_map.Function, hl = "@function" },
Constructor = { icon = lspkind_symbol_map.Constructor, hl = "@constructor" },
Field = { icon = lspkind_symbol_map.Field, hl = "@field" },
Variable = { icon = lspkind_symbol_map.Variable, hl = "@constant" },
Class = { icon = lspkind_symbol_map.Class, hl = "@type" },
Interface = { icon = lspkind_symbol_map.Interface, hl = "@type" },
Module = { icon = lspkind_symbol_map.Module, hl = "@namespace" },
Property = { icon = lspkind_symbol_map.Property, hl = "@method" },
Unit = { icon = lspkind_symbol_map.Unit },
Value = { icon = lspkind_symbol_map.Value },
Enum = { icon = lspkind_symbol_map.Enum, hl = "TSType" },
Keyword = { icon = lspkind_symbol_map.Keyword },
Snippet = { icon = lspkind_symbol_map.Snippet },
Color = { icon = lspkind_symbol_map.Color },
File = { icon = lspkind_symbol_map.File, hl = "@text.uri" },
Reference = { icon = lspkind_symbol_map.Reference },
Folder = { icon = lspkind_symbol_map.Folder },
EnumMember = { icon = lspkind_symbol_map.EnumMember, hl = "@field" },
Constant = { icon = lspkind_symbol_map.Constant, hl = "@constant" },
Struct = { icon = lspkind_symbol_map.Struct, hl = "@type" },
Event = { icon = lspkind_symbol_map.Event, hl = "@type" },
Operator = { icon = lspkind_symbol_map.Operator, hl = "@operator" },
TypeParameter = { icon = "", hl = "@parameter" },
---------------------------------------------------------
Namespace = { icon = "", hl = "@namespace" },
Package = { icon = "", hl = "@namespace" },
String = { icon = "", hl = "@string" },
Number = { icon = "", hl = "@number" },
Boolean = { icon = "", hl = "@boolean" },
Array = { icon = "", hl = "@constant" },
Object = { icon = "", hl = "@type" },
Key = { icon = "", hl = "@type" },
Null = { icon = "", hl = "@type" },
Component = { icon = "", hl = "@function" },
Fragment = { icon = "", hl = "@constant" },
}
M.window = {
winhighlight = "Normal:Normal,FloatBorder:Normal,CursorLine:Visual,Search:None",
}
M.hover_actions = {
border = {
{ "┌", "FloatBorder" },
{ "─", "FloatBorder" },
{ "┐", "FloatBorder" },
{ "│", "FloatBorder" },
{ "┘", "FloatBorder" },
{ "─", "FloatBorder" },
{ "└", "FloatBorder" },
{ "│", "FloatBorder" },
},
style = "fillchars",
-- Maximal width of the hover window. Nil means no max.
max_width = 80,
-- Maximal height of the hover window. Nil means no max.
max_height = 20,
-- whether the hover action window gets automatically focused
-- default: false
auto_focus = false,
}
M.signs = {
closed = "",
opened = "",
}
M.diagnostics = {
icons = {
hint = "",
info = "",
warning = "",
error = "",
},
}
-- LSP 相关美化参考 https://github.com/NvChad/NvChad
local function lspSymbol(name, icon)
local hl = "DiagnosticSign" .. name
vim.fn.sign_define(hl, { text = icon, numhl = hl, texthl = hl })
end
local function lspDiagnosticConf(lsp_ui)
local diagnostics_conf = {
virtual_text = true,
signs = true,
underline = true,
update_in_insert = false,
severity_sort = false,
}
lspSymbol("Error", lsp_ui.diagnostics.icons.error)
lspSymbol("Info", lsp_ui.diagnostics.icons.info)
lspSymbol("Hint", lsp_ui.diagnostics.icons.hint)
lspSymbol("Warn", lsp_ui.diagnostics.icons.warning)
if vim.fn.has("nvim-0.10") == 1 then
diagnostics_conf.signs = {
text = {
[vim.diagnostic.severity.ERROR] = lsp_ui.diagnostics.icons.error,
[vim.diagnostic.severity.WARN] = lsp_ui.diagnostics.icons.warning,
[vim.diagnostic.severity.HINT] = lsp_ui.diagnostics.icons.hint,
[vim.diagnostic.severity.INFO] = lsp_ui.diagnostics.icons.info,
},
}
end
vim.diagnostic.config(diagnostics_conf)
end
-- 文档格式化
local function markdown_format(input)
if input then
input = string.gsub(input, '%[([%a%$_]?[%.%w%(%)*"+,\\_%[%]%s :%-@<>]*)%]%(file:/[^%)]+%)', function(i1)
return "`" .. i1 .. "`"
end)
input = string.gsub(input, '%[([%a%$_]?[%.%w%(%)*"+,\\_%[%]%s :%-@<>]*)%]%(jdt://[^%)]+%)', function(i1)
return "`" .. i1 .. "`"
end)
end
return input
end
local function lspDocUI(lsp_ui)
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, lsp_ui.hover_actions)
local function split_lines(value)
value = string.gsub(value, "\r\n?", "\n")
return vim.split(value, "\n", { plain = true })
end
local function convert_input_to_markdown_lines(input, contents)
contents = contents or {}
-- MarkedString variation 1
if type(input) == "string" then
input = markdown_format(input)
vim.list_extend(contents, split_lines(input))
else
assert(type(input) == "table", "Expected a table for Hover.contents")
-- MarkupContent
if input.kind then
-- The kind can be either plaintext or markdown.
-- If it's plaintext, then wrap it in a <text></text> block
-- Some servers send input.value as empty, so let's ignore this :(
local value = input.value or ""
if input.kind == "plaintext" then
-- wrap this in a <text></text> block so that stylize_markdown
-- can properly process it as plaintext
value = string.format("<text>\n%s\n</text>", value)
end
-- assert(type(value) == 'string')
vim.list_extend(contents, split_lines(value))
-- MarkupString variation 2
elseif input.language then
-- Some servers send input.value as empty, so let's ignore this :(
-- assert(type(input.value) == 'string')
table.insert(contents, "```" .. input.language)
vim.list_extend(contents, split_lines(input.value or ""))
table.insert(contents, "```")
-- By deduction, this must be MarkedString[]
else
-- Use our existing logic to handle MarkedString
for _, marked_string in ipairs(input) do
convert_input_to_markdown_lines(marked_string, contents)
end
end
end
if (contents[1] == "" or contents[1] == nil) and #contents == 1 then
return {}
end
return contents
end
local function jhover(_, result, ctx, config)
config = config or {}
config.focus_id = ctx.method
if vim.api.nvim_get_current_buf() ~= ctx.bufnr then
-- Ignore result since buffer changed. This happens for slow language servers.
return
end
if not (result and result.contents) then
if config.silent ~= true then
vim.notify("No information available")
end
return
end
local markdown_lines = convert_input_to_markdown_lines(result.contents)
markdown_lines = vim.lsp.util.trim_empty_lines(markdown_lines)
if vim.tbl_isempty(markdown_lines) then
if config.silent ~= true then
vim.notify("No information available")
end
return
end
local bufnr, winnr = vim.lsp.util.open_floating_preview(markdown_lines, "markdown", config)
vim.api.nvim_win_set_option(winnr, "winhighlight", lsp_ui.window.winhighlight)
return bufnr, winnr
end
-- https://github.com/neovim/neovim/pull/25073 美化 hover
if vim.fn.has("nvim-0.10") == 0 then
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(jhover, lsp_ui.hover_actions)
else
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, lsp_ui.hover_actions)
end
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(function(a, result, ctx, b)
local bufnr, winnr = vim.lsp.handlers.signature_help(a, result, ctx, b)
vim.api.nvim_win_set_option(winnr, "winhighlight", lsp_ui.window.winhighlight)
return bufnr, winnr
end, lsp_ui.hover_actions)
end
local function cmpDocUI()
local source = require("cmp_nvim_lsp.source")
source.resolve = function(self, completion_item, callback)
-- client is stopped.
if self.client.is_stopped() then
return callback()
end
-- client has no completion capability.
if not self:_get(self.client.server_capabilities, { "completionProvider", "resolveProvider" }) then
return callback()
end
self:_request("completionItem/resolve", completion_item, function(_, response)
-- jdtls 文档格式化
if self.client.name == "jdtls" and response and response.documentation then
response.documentation.value = markdown_format(response.documentation.value)
end
-- print(vim.inspect(response))
callback(response or completion_item)
end)
end
end
M.init = function()
local lsp_ui = M
lspDiagnosticConf(lsp_ui)
lspDocUI(lsp_ui)
cmpDocUI()
end
return M