Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit bdef92a

Browse files
committed
fix: errors on not loaded buffer
1 parent ac7ec68 commit bdef92a

2 files changed

Lines changed: 75 additions & 16 deletions

File tree

lua/diagnostic.lua

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ local util = require 'diagnostic.util'
33
local M = {}
44

55
-- TODO change this to use vim.lsp.util.diagnostics_by_buf
6-
M.bufferDiagnostic = {}
76
local diagnosticTable = {}
87

8+
99
local remove_diagnostics = function(diagnostics)
1010
-- Remove Index
1111
local remove = {}
@@ -38,7 +38,7 @@ function M.modifyCallback()
3838
if not result then
3939
return
4040
end
41-
local uri = result.uri
41+
uri = result.uri
4242
local bufnr = vim.uri_to_bufnr(uri)
4343
if not bufnr then
4444
vim.lsp.err_message("LSP.publishDiagnostics: Couldn't find buffer for ", uri)
@@ -48,7 +48,7 @@ function M.modifyCallback()
4848
if vim.api.nvim_get_var('diagnostic_level') ~= nil then
4949
result.diagnostics = remove_diagnostics(result.diagnostics)
5050
end
51-
M.bufferDiagnostic[bufnr] = result
51+
vim.lsp.util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
5252
if vim.api.nvim_get_var('diagnostic_insert_delay') == 1 then
5353
if vim.api.nvim_get_mode()['mode'] == "i" or vim.api.nvim_get_mode()['mode'] == "ic" then
5454
return
@@ -60,33 +60,31 @@ function M.modifyCallback()
6060
end
6161

6262
function M.diagnostics_loclist(local_result)
63-
if local_result and local_result.diagnostics then
64-
for _, v in ipairs(local_result.diagnostics) do
65-
v.uri = v.uri or local_result.uri
63+
if local_result then
64+
for _, v in ipairs(local_result) do
65+
v.uri = v.uri or uri
6666
end
6767
end
68-
vim.lsp.util.set_loclist(vim.lsp.util.locations_to_items(local_result.diagnostics))
68+
vim.lsp.util.set_loclist(util.locations_to_items(local_result))
6969
end
7070

7171
function M.publish_diagnostics(bufnr)
7272
if vim.fn.getcmdwintype() == ':' then return end
73+
vim.lsp.util.buf_clear_diagnostics(bufnr)
7374
if #vim.lsp.buf_get_clients() == 0 then return end
74-
local result = M.bufferDiagnostic[bufnr]
75-
if result == nil then return end
75+
local diagnostics = vim.lsp.util.diagnostics_by_buf[bufnr]
76+
if diagnostics == nil then return end
7677
vim.fn.setloclist(0, {}, 'r')
77-
vim.lsp.util.buf_clear_diagnostics(bufnr)
78-
vim.lsp.util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
79-
util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
8078
if vim.api.nvim_get_var('diagnostic_enable_underline') == 1 then
81-
vim.lsp.util.buf_diagnostics_underline(bufnr, result.diagnostics)
79+
vim.lsp.util.buf_diagnostics_underline(bufnr, diagnostics)
8280
end
8381
if vim.api.nvim_get_var('diagnostic_show_sign') == 1 then
84-
util.buf_diagnostics_signs(bufnr, result.diagnostics)
82+
util.buf_diagnostics_signs(bufnr, diagnostics)
8583
end
8684
if vim.api.nvim_get_var('diagnostic_enable_virtual_text') == 1 then
87-
util.buf_diagnostics_virtual_text(bufnr, result.diagnostics)
85+
util.buf_diagnostics_virtual_text(bufnr, diagnostics)
8886
end
89-
M.diagnostics_loclist(result)
87+
M.diagnostics_loclist(diagnostics)
9088
M.trigger_diagnostics_changed()
9189
end
9290

lua/diagnostic/util.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,67 @@ function M.buf_diagnostics_virtual_text(bufnr, diagnostics)
102102
end
103103
end
104104

105+
local function sort_by_key(fn)
106+
return function(a,b)
107+
local ka, kb = fn(a), fn(b)
108+
assert(#ka == #kb)
109+
for i = 1, #ka do
110+
if ka[i] ~= kb[i] then
111+
return ka[i] < kb[i]
112+
end
113+
end
114+
-- every value must have been equal here, which means it's not less than.
115+
return false
116+
end
117+
end
118+
119+
local position_sort = sort_by_key(function(v)
120+
return {v.start.line, v.start.character}
121+
end)
122+
123+
124+
function M.locations_to_items(locations)
125+
local items = {}
126+
local grouped = setmetatable({}, {
127+
__index = function(t, k)
128+
local v = {}
129+
rawset(t, k, v)
130+
return v
131+
end;
132+
})
133+
local fname = api.nvim_buf_get_name(0)
134+
for _, d in ipairs(locations) do
135+
local range = d.range or d.targetSelectionRange
136+
table.insert(grouped[fname], {start = range.start})
137+
end
138+
139+
140+
local keys = vim.tbl_keys(grouped)
141+
table.sort(keys)
142+
local rows = grouped[fname]
143+
144+
table.sort(rows, position_sort)
145+
local bufnr = vim.fn.bufnr()
146+
for _, temp in ipairs(rows) do
147+
local pos = temp.start
148+
local row = pos.line
149+
local line = api.nvim_buf_get_lines(0, row, row+1, true)[1]
150+
local col
151+
if pos.character > #line then
152+
col = #line
153+
else
154+
col = vim.str_byteindex(line, pos.character)
155+
end
156+
table.insert(items, {
157+
bufnr = bufnr,
158+
lnum = row + 1,
159+
col = col + 1;
160+
text = line
161+
})
162+
end
163+
return items
164+
end
165+
105166
function M.buf_diagnostics_signs(bufnr, diagnostics)
106167
for _, diagnostic in ipairs(diagnostics) do
107168
local diagnostic_severity_map = {

0 commit comments

Comments
 (0)