Skip to content

Instantly share code, notes, and snippets.

@acomagu
Created September 6, 2020 06:12
Show Gist options
  • Select an option

  • Save acomagu/5ae06fcfa2bdf6b2f0a2267690989dd9 to your computer and use it in GitHub Desktop.

Select an option

Save acomagu/5ae06fcfa2bdf6b2f0a2267690989dd9 to your computer and use it in GitHub Desktop.
供養; もっとよさげなやつ -> https://qiita.com/orokasan/items/81031d08844a04cff81d
lua << EOF
local function sort_by_key(fn)
return function(a,b)
local ka, kb = fn(a), fn(b)
assert(#ka == #kb)
for i = 1, #ka do
if ka[i] ~= kb[i] then
return ka[i] < kb[i]
end
end
-- every value must have been equal here, which means it's not less than.
return false
end
end
local pos_sort = sort_by_key(function(v)
return {v.start.line, v.start.character}
end)
function l2i(locations)
local items = {}
local grouped = setmetatable({}, {
__index = function(t, k)
local v = {}
rawset(t, k, v)
return v
end;
})
for _, d in ipairs(locations) do
local start = d.range.start
local fname = assert(vim.uri_to_fname(d.uri))
table.insert(grouped[fname], {
start = start,
message = d.message;
})
end
local keys = vim.tbl_keys(grouped)
table.sort(keys)
for _, fname in ipairs(keys) do
local rows = grouped[fname]
table.sort(rows, pos_sort)
local i = 0
for line in io.lines(fname) do
for _, pos in ipairs(rows) do
local row = pos["start"].line
if i == row then
local col
if pos["start"].character > #line then
col = #line
else
col = vim.str_byteindex(line, pos["start"].character)
end
table.insert(items, {
filename = fname,
lnum = row + 1,
col = col + 1;
text = pos["message"];
})
end
end
i = i + 1
end
end
return items
end
do
local method = 'textDocument/publishDiagnostics'
local default_callback = vim.lsp.callbacks[method]
vim.lsp.callbacks[method] = function(err, method, result, client_id)
default_callback(err, method, result, client_id)
if result and result.diagnostics then
for _, v in ipairs(result.diagnostics) do
v.uri = v.uri or result.uri
end
vim.fn.setqflist({}, ' ', {
title = 'Language Server';
items = l2i(result.diagnostics);
})
end
end
end
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment