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

Commit

Permalink
refactor: use more built-in function, trimmed out the unnecessary part
Browse files Browse the repository at this point in the history
  • Loading branch information
haorenW1025 committed May 13, 2020
1 parent 17eeecd commit f88b35a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 186 deletions.
7 changes: 4 additions & 3 deletions lua/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local vim = vim
local util = require 'diagnostic.util'
local M = {}

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

Expand Down Expand Up @@ -71,12 +72,12 @@ function M.publish_diagnostics(bufnr)
local result = M.bufferDiagnostic[bufnr]
if result == nil then return end
vim.api.nvim_command('lexpr []')
util.buf_clear_diagnostics(bufnr)
vim.lsp.util.buf_clear_diagnostics(bufnr)
vim.lsp.util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
util.buf_diagnostics_save_positions(bufnr, result.diagnostics)
util.buf_diagnostics_underline(bufnr, result.diagnostics)
vim.lsp.util.buf_diagnostics_underline(bufnr, result.diagnostics)
if vim.api.nvim_get_var('diagnostic_show_sign') == 1 then
util.buf_diagnostics_signs(bufnr, result.diagnostics)
vim.lsp.util.buf_diagnostics_signs(bufnr, result.diagnostics)

This comment has been minimized.

Copy link
@ckipp01

ckipp01 May 14, 2020

Contributor

I think this doesn't quite behave as expected? I'll submit an issue and link to this.

end
if vim.api.nvim_get_var('diagnostic_enable_virtual_text') == 1 then
util.buf_diagnostics_virtual_text(bufnr, result.diagnostics)
Expand Down
189 changes: 7 additions & 182 deletions lua/diagnostic/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local protocol = require 'vim.lsp.protocol'
local vim = vim
local validate = vim.validate
local api = vim.api
-- TODO change all_buffer_diagnostics to built-in
local all_buffer_diagnostics = {}
local M ={}
local split = vim.split
Expand All @@ -24,140 +25,18 @@ local function highlight_range(bufnr, ns, hiname, start, finish)
end
end

-- Get the built-in namespace and highlight
local diagnostic_ns = api.nvim_create_namespace("vim_lsp_diagnostics")
local reference_ns = api.nvim_create_namespace("vim_lsp_references")
local sign_ns = 'vim_lsp_signs'
local underline_highlight_name = "LspDiagnosticsUnderline"
vim.cmd(string.format("highlight default %s gui=underline cterm=underline", underline_highlight_name))
for kind, _ in pairs(protocol.DiagnosticSeverity) do
if type(kind) == 'string' then
vim.cmd(string.format("highlight default link %s%s %s", underline_highlight_name, kind, underline_highlight_name))
end
end

local severity_highlights = {}

local default_severity_highlight = {
[protocol.DiagnosticSeverity.Error] = { guifg = "Red" };
[protocol.DiagnosticSeverity.Warning] = { guifg = "Orange" };
[protocol.DiagnosticSeverity.Information] = { guifg = "LightBlue" };
[protocol.DiagnosticSeverity.Hint] = { guifg = "LightGrey" };
local severity_highlights = {
[protocol.DiagnosticSeverity.Error] = "LspDiagnosticsError";
[protocol.DiagnosticSeverity.Warning] = "LspDiagnosticsWarning";
[protocol.DiagnosticSeverity.Information] = "LspDiagnosticsInformation";
[protocol.DiagnosticSeverity.Hint] = "LspDiagnosticsHint";
}

-- Initialize default severity highlights
for severity, hi_info in pairs(default_severity_highlight) do
local severity_name = protocol.DiagnosticSeverity[severity]
local highlight_name = "LspDiagnostics"..severity_name
-- Try to fill in the foreground color with a sane default.
local cmd_parts = {"highlight", "default", highlight_name}
for k, v in pairs(hi_info) do
table.insert(cmd_parts, k.."="..v)
end
api.nvim_command(table.concat(cmd_parts, ' '))
severity_highlights[severity] = highlight_name
end

function M.buf_clear_diagnostics(bufnr)
validate { bufnr = {bufnr, 'n', true} }
bufnr = bufnr == 0 and api.nvim_get_current_buf() or bufnr

-- clear sign group
vim.fn.sign_unplace(sign_ns, {buffer=bufnr})

-- clear virtual text namespace
api.nvim_buf_clear_namespace(bufnr, diagnostic_ns, 0, -1)
end


function M.open_floating_preview(contents, filetype, opts)
validate {
contents = { contents, 't' };
filetype = { filetype, 's', true };
opts = { opts, 't', true };
}
opts = opts or {}

-- Trim empty lines from the end.
contents = vim.lsp.util.trim_empty_lines(contents)

local width = opts.width
local height = opts.height or #contents
if not width then
width = 0
for i, line in ipairs(contents) do
-- Clean up the input and add left pad.
line = " "..line:gsub("\r", "")
-- TODO(ashkan) use nvim_strdisplaywidth if/when that is introduced.
local line_width = vim.fn.strdisplaywidth(line)
width = math.max(line_width, width)
contents[i] = line
end
-- Add right padding of 1 each.
width = width + 1
end

local floating_bufnr = api.nvim_create_buf(false, true)
if filetype then
api.nvim_buf_set_option(floating_bufnr, 'filetype', filetype)
end
local float_option = vim.lsp.util.make_floating_popup_options(width, height, opts)
local floating_winnr = api.nvim_open_win(floating_bufnr, false, float_option)
if filetype == 'markdown' then
api.nvim_win_set_option(floating_winnr, 'conceallevel', 2)
end
api.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents)
api.nvim_buf_set_option(floating_bufnr, 'modifiable', false)
-- Disable InsertCharPre
api.nvim_command("autocmd CursorMoved,BufHidden <buffer> ++once lua pcall(vim.api.nvim_win_close, "..floating_winnr..", true)")
return floating_bufnr, floating_winnr
end


function M.get_severity_highlight_name(severity)
return severity_highlights[severity]
end

function M.show_line_diagnostics()
local bufnr = api.nvim_get_current_buf()
local line = api.nvim_win_get_cursor(0)[1] - 1
-- local marks = api.nvim_buf_get_extmarks(bufnr, diagnostic_ns, {line, 0}, {line, -1}, {})
-- if #marks == 0 then
-- return
-- end
-- local buffer_diagnostics = all_buffer_diagnostics[bufnr]
local lines = {"Diagnostics:"}
local highlights = {{0, "Bold"}}

local buffer_diagnostics = all_buffer_diagnostics[bufnr]
if not buffer_diagnostics then return end
local line_diagnostics = buffer_diagnostics[line]
if not line_diagnostics then return end

for i, diagnostic in ipairs(line_diagnostics) do
-- for i, mark in ipairs(marks) do
-- local mark_id = mark[1]
-- local diagnostic = buffer_diagnostics[mark_id]

-- TODO(ashkan) make format configurable?
local prefix = string.format("%d. ", i)
local hiname = severity_highlights[diagnostic.severity]
local message_lines = split_lines(diagnostic.message)
table.insert(lines, prefix..message_lines[1])
table.insert(highlights, {#prefix + 1, hiname})
for j = 2, #message_lines do
table.insert(lines, message_lines[j])
table.insert(highlights, {0, hiname})
end
end
local popup_bufnr, winnr = M.open_floating_preview(lines, 'plaintext')
for i, hi in ipairs(highlights) do
local prefixlen, hiname = unpack(hi)
-- Start highlight after the prefix
api.nvim_buf_add_highlight(popup_bufnr, -1, hiname, i-1, prefixlen, -1)
end
return popup_bufnr, winnr
end

function M.buf_diagnostics_save_positions(bufnr, diagnostics)
validate {
bufnr = {bufnr, 'n', true};
Expand Down Expand Up @@ -190,47 +69,6 @@ function M.buf_diagnostics_save_positions(bufnr, diagnostics)
end
end

function M.buf_diagnostics_underline(bufnr, diagnostics)
for _, diagnostic in ipairs(diagnostics) do
local start = diagnostic.range["start"]
local finish = diagnostic.range["end"]

local hlmap = {
[protocol.DiagnosticSeverity.Error]='Error',
[protocol.DiagnosticSeverity.Warning]='Warning',
[protocol.DiagnosticSeverity.Information]='Information',
[protocol.DiagnosticSeverity.Hint]='Hint',
}

-- TODO care about encoding here since this is in byte index?
highlight_range(bufnr, diagnostic_ns,
underline_highlight_name..hlmap[diagnostic.severity],
{start.line, start.character},
{finish.line, finish.character}
)
end
end

function M.buf_clear_references(bufnr)
validate { bufnr = {bufnr, 'n', true} }
api.nvim_buf_clear_namespace(bufnr, reference_ns, 0, -1)
end

function M.buf_highlight_references(bufnr, references)
validate { bufnr = {bufnr, 'n', true} }
for _, reference in ipairs(references) do
local start_pos = {reference["range"]["start"]["line"], reference["range"]["start"]["character"]}
local end_pos = {reference["range"]["end"]["line"], reference["range"]["end"]["character"]}
local document_highlight_kind = {
[protocol.DocumentHighlightKind.Text] = "LspReferenceText";
[protocol.DocumentHighlightKind.Read] = "LspReferenceRead";
[protocol.DocumentHighlightKind.Write] = "LspReferenceWrite";
}
highlight_range(bufnr, reference_ns, document_highlight_kind[reference["kind"]], start_pos, end_pos)

end
end

function M.buf_diagnostics_virtual_text(bufnr, diagnostics)
local buffer_line_diagnostics = all_buffer_diagnostics[bufnr]
local prefix = api.nvim_get_var('diagnostic_virtual_text_prefix')
Expand Down Expand Up @@ -264,19 +102,6 @@ function M.buf_diagnostics_virtual_text(bufnr, diagnostics)
end
end

function M.buf_diagnostics_count(kind)
local bufnr = vim.api.nvim_get_current_buf()
local buffer_line_diagnostics = all_buffer_diagnostics[bufnr]
if not buffer_line_diagnostics then return end
local count = 0
for _, line_diags in pairs(buffer_line_diagnostics) do
for _, diag in ipairs(line_diags) do
if protocol.DiagnosticSeverity[kind] == diag.severity then count = count + 1 end
end
end
return count
end

function M.buf_diagnostics_signs(bufnr, diagnostics)
vim.fn.sign_define('LspDiagnosticsErrorSign', {text=vim.g['LspDiagnosticsErrorSign'] or 'E', texthl='LspDiagnosticsError', linehl='', numhl=''})
vim.fn.sign_define('LspDiagnosticsWarningSign', {text=vim.g['LspDiagnosticsWarningSign'] or 'W', texthl='LspDiagnosticsWarning', linehl='', numhl=''})
Expand Down
2 changes: 1 addition & 1 deletion lua/jumpLoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function M.openLineDiagnostics()
if api.nvim_get_var('diagnostic_auto_popup_while_jump') == 1 then
local timer = vim.loop.new_timer()
timer:start(100, 0, vim.schedule_wrap(function()
util.show_line_diagnostics()
vim.lsp.util.show_line_diagnostics()
timer:stop()
timer:close()
end))
Expand Down

0 comments on commit f88b35a

Please sign in to comment.