forked from nvimdev/lspsaga.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightbulb.lua
More file actions
208 lines (185 loc) · 5.32 KB
/
Copy pathlightbulb.lua
File metadata and controls
208 lines (185 loc) · 5.32 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
local api, lsp, fn = vim.api, vim.lsp, vim.fn
---@diagnostic disable-next-line: deprecated
local uv = vim.version().minor >= 10 and vim.uv or vim.loop
local config = require('lspsaga').config
local util = require('lspsaga.util')
local nvim_buf_set_extmark = api.nvim_buf_set_extmark
local inrender_row = -1
local inrender_buf = nil
local function get_name()
return 'SagaLightBulb'
end
local namespace = api.nvim_create_namespace(get_name())
local defined = false
if not defined then
fn.sign_define(get_name(), { text = config.ui.code_action, texthl = get_name() })
defined = true
end
local function update_lightbulb(bufnr, row)
if not bufnr or not api.nvim_buf_is_valid(bufnr) then
return
end
api.nvim_buf_clear_namespace(bufnr, namespace, 0, -1)
local name = get_name()
pcall(fn.sign_unplace, name, { id = inrender_row, buffer = bufnr })
if not row then
return
end
if config.lightbulb.sign then
fn.sign_place(
row + 1,
name,
name,
bufnr,
{ lnum = row + 1, priority = config.lightbulb.sign_priority }
)
end
if config.lightbulb.virtual_text then
nvim_buf_set_extmark(bufnr, namespace, row, -1, {
virt_text = { { config.ui.code_action, name } },
virt_text_pos = 'overlay',
hl_mode = 'combine',
})
end
inrender_row = row + 1
inrender_buf = bufnr
end
local function severity_vim_to_lsp(severity)
if type(severity) == 'string' then
severity = vim.diagnostic.severity[severity]
end
return severity
end
--- @param diagnostic vim.Diagnostic
--- @return lsp.DiagnosticTag[]?
local function tags_vim_to_lsp(diagnostic)
if not diagnostic._tags then
return
end
local tags = {} --- @type lsp.DiagnosticTag[]
if diagnostic._tags.unnecessary then
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Unnecessary
end
if diagnostic._tags.deprecated then
tags[#tags + 1] = vim.lsp.protocol.DiagnosticTag.Deprecated
end
return tags
end
local function diagnostic_vim_to_lsp(diagnostics)
---@param diagnostic vim.Diagnostic
---@return lsp.Diagnostic
return vim.tbl_map(function(diagnostic)
local user_data = diagnostic.user_data or {}
if user_data.lsp and not vim.tbl_isempty(user_data.lsp) and user_data.lsp.range then
return user_data.lsp
end
return {
range = {
start = {
line = diagnostic.lnum + 1,
character = diagnostic.col,
},
['end'] = {
line = diagnostic.end_lnum + 1,
character = diagnostic.end_col,
},
},
severity = severity_vim_to_lsp(diagnostic.severity),
message = diagnostic.message,
source = diagnostic.source,
code = diagnostic.code,
tags = tags_vim_to_lsp(diagnostic),
}
end, diagnostics)
end
local function render(bufnr)
local row = api.nvim_win_get_cursor(0)[1] - 1
local params = lsp.util.make_range_params(0, util.get_offset_encoding({ bufnr = bufnr }))
params.context = {
diagnostics = diagnostic_vim_to_lsp(vim.diagnostic.get(bufnr, { lnum = row })),
}
lsp.buf_request(bufnr, 'textDocument/codeAction', params, function(_, result, _)
if api.nvim_get_current_buf() ~= bufnr then
return
end
if result and #result > 0 then
update_lightbulb(bufnr, row)
else
update_lightbulb(bufnr, nil)
end
end)
end
local timer = assert(uv.new_timer())
local function update(buf)
timer:stop()
update_lightbulb(inrender_buf)
timer:start(config.lightbulb.debounce, 0, function()
timer:stop()
vim.schedule(function()
if api.nvim_buf_is_valid(buf) and api.nvim_get_current_buf() == buf then
render(buf)
end
end)
end)
end
local function lb_autocmd()
local name = 'SagaLightBulb'
local g = api.nvim_create_augroup(name, { clear = true })
api.nvim_create_autocmd('LspAttach', {
group = g,
callback = function(opt)
local client = lsp.get_client_by_id(opt.data.client_id)
if not client then
return
end
if not util.client_supports_method(client, 'textDocument/codeAction') then
return
end
if vim.tbl_contains(config.lightbulb.ignore.clients, client.name) then
return
end
if vim.tbl_contains(config.lightbulb.ignore.ft, vim.bo.filetype) then
return
end
local buf = opt.buf
local group_name = name .. tostring(buf)
local ok = pcall(api.nvim_get_autocmds, { group = group_name })
if ok then
return
end
local group = api.nvim_create_augroup(group_name, { clear = true })
api.nvim_create_autocmd('CursorMoved', {
group = group,
buffer = buf,
callback = function(args)
update(args.buf)
end,
})
if not config.lightbulb.enable_in_insert then
api.nvim_create_autocmd('InsertEnter', {
group = group,
buffer = buf,
callback = function(args)
update_lightbulb(args.buf, nil)
end,
})
end
api.nvim_create_autocmd('BufLeave', {
group = group,
buffer = buf,
callback = function(args)
update_lightbulb(args.buf, nil)
end,
})
end,
})
api.nvim_create_autocmd('LspDetach', {
group = g,
callback = function(args)
pcall(api.nvim_del_augroup_by_name, 'SagaLightBulb' .. tostring(args.buf))
end,
})
end
return {
lb_autocmd = lb_autocmd,
}