@@ -4,6 +4,7 @@ local protocol = require 'vim.lsp.protocol'
44local vim = vim
55local validate = vim .validate
66local api = vim .api
7+ -- TODO change all_buffer_diagnostics to built-in
78local all_buffer_diagnostics = {}
89local M = {}
910local split = vim .split
@@ -24,140 +25,18 @@ local function highlight_range(bufnr, ns, hiname, start, finish)
2425 end
2526end
2627
28+ -- Get the built-in namespace and highlight
2729local diagnostic_ns = api .nvim_create_namespace (" vim_lsp_diagnostics" )
28- local reference_ns = api .nvim_create_namespace (" vim_lsp_references" )
2930local sign_ns = ' vim_lsp_signs'
3031local underline_highlight_name = " LspDiagnosticsUnderline"
31- vim .cmd (string.format (" highlight default %s gui=underline cterm=underline" , underline_highlight_name ))
32- for kind , _ in pairs (protocol .DiagnosticSeverity ) do
33- if type (kind ) == ' string' then
34- vim .cmd (string.format (" highlight default link %s%s %s" , underline_highlight_name , kind , underline_highlight_name ))
35- end
36- end
3732
38- local severity_highlights = {}
39-
40- local default_severity_highlight = {
41- [protocol .DiagnosticSeverity .Error ] = { guifg = " Red" };
42- [protocol .DiagnosticSeverity .Warning ] = { guifg = " Orange" };
43- [protocol .DiagnosticSeverity .Information ] = { guifg = " LightBlue" };
44- [protocol .DiagnosticSeverity .Hint ] = { guifg = " LightGrey" };
33+ local severity_highlights = {
34+ [protocol .DiagnosticSeverity .Error ] = " LspDiagnosticsError" ;
35+ [protocol .DiagnosticSeverity .Warning ] = " LspDiagnosticsWarning" ;
36+ [protocol .DiagnosticSeverity .Information ] = " LspDiagnosticsInformation" ;
37+ [protocol .DiagnosticSeverity .Hint ] = " LspDiagnosticsHint" ;
4538}
4639
47- -- Initialize default severity highlights
48- for severity , hi_info in pairs (default_severity_highlight ) do
49- local severity_name = protocol .DiagnosticSeverity [severity ]
50- local highlight_name = " LspDiagnostics" .. severity_name
51- -- Try to fill in the foreground color with a sane default.
52- local cmd_parts = {" highlight" , " default" , highlight_name }
53- for k , v in pairs (hi_info ) do
54- table.insert (cmd_parts , k .. " =" .. v )
55- end
56- api .nvim_command (table.concat (cmd_parts , ' ' ))
57- severity_highlights [severity ] = highlight_name
58- end
59-
60- function M .buf_clear_diagnostics (bufnr )
61- validate { bufnr = {bufnr , ' n' , true } }
62- bufnr = bufnr == 0 and api .nvim_get_current_buf () or bufnr
63-
64- -- clear sign group
65- vim .fn .sign_unplace (sign_ns , {buffer = bufnr })
66-
67- -- clear virtual text namespace
68- api .nvim_buf_clear_namespace (bufnr , diagnostic_ns , 0 , - 1 )
69- end
70-
71-
72- function M .open_floating_preview (contents , filetype , opts )
73- validate {
74- contents = { contents , ' t' };
75- filetype = { filetype , ' s' , true };
76- opts = { opts , ' t' , true };
77- }
78- opts = opts or {}
79-
80- -- Trim empty lines from the end.
81- contents = vim .lsp .util .trim_empty_lines (contents )
82-
83- local width = opts .width
84- local height = opts .height or # contents
85- if not width then
86- width = 0
87- for i , line in ipairs (contents ) do
88- -- Clean up the input and add left pad.
89- line = " " .. line :gsub (" \r " , " " )
90- -- TODO(ashkan) use nvim_strdisplaywidth if/when that is introduced.
91- local line_width = vim .fn .strdisplaywidth (line )
92- width = math.max (line_width , width )
93- contents [i ] = line
94- end
95- -- Add right padding of 1 each.
96- width = width + 1
97- end
98-
99- local floating_bufnr = api .nvim_create_buf (false , true )
100- if filetype then
101- api .nvim_buf_set_option (floating_bufnr , ' filetype' , filetype )
102- end
103- local float_option = vim .lsp .util .make_floating_popup_options (width , height , opts )
104- local floating_winnr = api .nvim_open_win (floating_bufnr , false , float_option )
105- if filetype == ' markdown' then
106- api .nvim_win_set_option (floating_winnr , ' conceallevel' , 2 )
107- end
108- api .nvim_buf_set_lines (floating_bufnr , 0 , - 1 , true , contents )
109- api .nvim_buf_set_option (floating_bufnr , ' modifiable' , false )
110- -- Disable InsertCharPre
111- api .nvim_command (" autocmd CursorMoved,BufHidden <buffer> ++once lua pcall(vim.api.nvim_win_close, " .. floating_winnr .. " , true)" )
112- return floating_bufnr , floating_winnr
113- end
114-
115-
116- function M .get_severity_highlight_name (severity )
117- return severity_highlights [severity ]
118- end
119-
120- function M .show_line_diagnostics ()
121- local bufnr = api .nvim_get_current_buf ()
122- local line = api .nvim_win_get_cursor (0 )[1 ] - 1
123- -- local marks = api.nvim_buf_get_extmarks(bufnr, diagnostic_ns, {line, 0}, {line, -1}, {})
124- -- if #marks == 0 then
125- -- return
126- -- end
127- -- local buffer_diagnostics = all_buffer_diagnostics[bufnr]
128- local lines = {" Diagnostics:" }
129- local highlights = {{0 , " Bold" }}
130-
131- local buffer_diagnostics = all_buffer_diagnostics [bufnr ]
132- if not buffer_diagnostics then return end
133- local line_diagnostics = buffer_diagnostics [line ]
134- if not line_diagnostics then return end
135-
136- for i , diagnostic in ipairs (line_diagnostics ) do
137- -- for i, mark in ipairs(marks) do
138- -- local mark_id = mark[1]
139- -- local diagnostic = buffer_diagnostics[mark_id]
140-
141- -- TODO(ashkan) make format configurable?
142- local prefix = string.format (" %d. " , i )
143- local hiname = severity_highlights [diagnostic .severity ]
144- local message_lines = split_lines (diagnostic .message )
145- table.insert (lines , prefix .. message_lines [1 ])
146- table.insert (highlights , {# prefix + 1 , hiname })
147- for j = 2 , # message_lines do
148- table.insert (lines , message_lines [j ])
149- table.insert (highlights , {0 , hiname })
150- end
151- end
152- local popup_bufnr , winnr = M .open_floating_preview (lines , ' plaintext' )
153- for i , hi in ipairs (highlights ) do
154- local prefixlen , hiname = unpack (hi )
155- -- Start highlight after the prefix
156- api .nvim_buf_add_highlight (popup_bufnr , - 1 , hiname , i - 1 , prefixlen , - 1 )
157- end
158- return popup_bufnr , winnr
159- end
160-
16140function M .buf_diagnostics_save_positions (bufnr , diagnostics )
16241 validate {
16342 bufnr = {bufnr , ' n' , true };
@@ -190,47 +69,6 @@ function M.buf_diagnostics_save_positions(bufnr, diagnostics)
19069 end
19170end
19271
193- function M .buf_diagnostics_underline (bufnr , diagnostics )
194- for _ , diagnostic in ipairs (diagnostics ) do
195- local start = diagnostic .range [" start" ]
196- local finish = diagnostic .range [" end" ]
197-
198- local hlmap = {
199- [protocol .DiagnosticSeverity .Error ]= ' Error' ,
200- [protocol .DiagnosticSeverity .Warning ]= ' Warning' ,
201- [protocol .DiagnosticSeverity .Information ]= ' Information' ,
202- [protocol .DiagnosticSeverity .Hint ]= ' Hint' ,
203- }
204-
205- -- TODO care about encoding here since this is in byte index?
206- highlight_range (bufnr , diagnostic_ns ,
207- underline_highlight_name .. hlmap [diagnostic .severity ],
208- {start .line , start .character },
209- {finish .line , finish .character }
210- )
211- end
212- end
213-
214- function M .buf_clear_references (bufnr )
215- validate { bufnr = {bufnr , ' n' , true } }
216- api .nvim_buf_clear_namespace (bufnr , reference_ns , 0 , - 1 )
217- end
218-
219- function M .buf_highlight_references (bufnr , references )
220- validate { bufnr = {bufnr , ' n' , true } }
221- for _ , reference in ipairs (references ) do
222- local start_pos = {reference [" range" ][" start" ][" line" ], reference [" range" ][" start" ][" character" ]}
223- local end_pos = {reference [" range" ][" end" ][" line" ], reference [" range" ][" end" ][" character" ]}
224- local document_highlight_kind = {
225- [protocol .DocumentHighlightKind .Text ] = " LspReferenceText" ;
226- [protocol .DocumentHighlightKind .Read ] = " LspReferenceRead" ;
227- [protocol .DocumentHighlightKind .Write ] = " LspReferenceWrite" ;
228- }
229- highlight_range (bufnr , reference_ns , document_highlight_kind [reference [" kind" ]], start_pos , end_pos )
230-
231- end
232- end
233-
23472function M .buf_diagnostics_virtual_text (bufnr , diagnostics )
23573 local buffer_line_diagnostics = all_buffer_diagnostics [bufnr ]
23674 local prefix = api .nvim_get_var (' diagnostic_virtual_text_prefix' )
@@ -264,19 +102,6 @@ function M.buf_diagnostics_virtual_text(bufnr, diagnostics)
264102 end
265103end
266104
267- function M .buf_diagnostics_count (kind )
268- local bufnr = vim .api .nvim_get_current_buf ()
269- local buffer_line_diagnostics = all_buffer_diagnostics [bufnr ]
270- if not buffer_line_diagnostics then return end
271- local count = 0
272- for _ , line_diags in pairs (buffer_line_diagnostics ) do
273- for _ , diag in ipairs (line_diags ) do
274- if protocol .DiagnosticSeverity [kind ] == diag .severity then count = count + 1 end
275- end
276- end
277- return count
278- end
279-
280105function M .buf_diagnostics_signs (bufnr , diagnostics )
281106 vim .fn .sign_define (' LspDiagnosticsErrorSign' , {text = vim .g [' LspDiagnosticsErrorSign' ] or ' E' , texthl = ' LspDiagnosticsError' , linehl = ' ' , numhl = ' ' })
282107 vim .fn .sign_define (' LspDiagnosticsWarningSign' , {text = vim .g [' LspDiagnosticsWarningSign' ] or ' W' , texthl = ' LspDiagnosticsWarning' , linehl = ' ' , numhl = ' ' })
0 commit comments