Skip to content

Commit

Permalink
feat: use a new diagnostic module for nvim 0.6
Browse files Browse the repository at this point in the history
- Close #6 Thanks to @alexlafroscia for the initial work
  • Loading branch information
spywhere committed Jan 9, 2022
1 parent 2340ac4 commit 78a8f68
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
16 changes: 8 additions & 8 deletions autoload/lightline/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,42 @@ function! lightline#lsp#hints() abort
if !lightline#lsp#linted()
return ''
endif
let l:counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Hint]])')
let l:counts = luaeval("require('lightline-lsp')._get_diagnostic_count('hint')")
return l:counts == 0 ? '' : printf(s:indicator_hints . '%d', counts)
endfunction

function! lightline#lsp#infos() abort
if !lightline#lsp#linted()
return ''
endif
let l:counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Information]])')
let l:counts = luaeval("require('lightline-lsp')._get_diagnostic_count('info')")
return l:counts == 0 ? '' : printf(s:indicator_infos . '%d', counts)
endfunction

function! lightline#lsp#warnings() abort
if !lightline#lsp#linted()
return ''
endif
let l:counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Warning]])')
let l:counts = luaeval("require('lightline-lsp')._get_diagnostic_count('warn')")
return l:counts == 0 ? '' : printf(s:indicator_warnings . '%d', counts)
endfunction

function! lightline#lsp#errors() abort
if !lightline#lsp#linted()
return ''
endif
let l:counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Error]])')
let l:counts = luaeval("require('lightline-lsp')._get_diagnostic_count('error')")
return l:counts == 0 ? '' : printf(s:indicator_errors . '%d', counts)
endfunction

function! lightline#lsp#ok() abort
if !lightline#lsp#linted()
return ''
endif
let l:hint_counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Hint]])')
let l:info_counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Information]])')
let l:warn_counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Warning]])')
let l:error_counts = luaeval('vim.lsp.diagnostic.get_count('.bufnr().', [[Error]])')
let l:hint_counts = luaeval("require('lightline-lsp')._get_diagnostic_count('hint')")
let l:info_counts = luaeval("require('lightline-lsp')._get_diagnostic_count('info')")
let l:warn_counts = luaeval("require('lightline-lsp')._get_diagnostic_count('warn')")
let l:error_counts = luaeval("require('lightline-lsp')._get_diagnostic_count('error')")
let l:counts = l:hint_counts+l:info_counts+l:warn_counts+l:error_counts
return l:counts == 0 ? s:indicator_ok : ''
endfunction
Expand Down
49 changes: 49 additions & 0 deletions lua/lightline-lsp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local M = {}

local function is_lsp_attached()
return next(vim.lsp.buf_get_clients(0))
end

local function get_count(buffer_number, diagnostic_type)
if vim.diagnostic == nil then
-- starting nvim-0.5
local severity = ({
hint = 'Hint',
info = 'Information',
warn = 'Warn',
error = 'Error'
})[diagnostic_type]

return vim.lsp.diagnostic.get_count(buffer_number, severity)
else
-- starting nvim-0.6
local severity = ({
hint = vim.diagnostic.severity.HINT,
info = vim.diagnostic.severity.INFO,
warn = vim.diagnostic.severity.WARN,
error = vim.diagnostic.severity.ERROR
})[diagnostic_type]

return vim.tbl_count(vim.diagnostic.get(
buffer_number,
{
severity = severity
}
))
end
end

M._get_diagnostic_count = function (diagnostic_type)
if not is_lsp_attached() then
return 0
end

local active_clients = vim.lsp.get_active_clients()
if vim.tbl_isempty(active_clients) then
return 0
end

return get_count(vim.api.nvim_get_current_buf(), diagnostic_type)
end

return M

0 comments on commit 78a8f68

Please sign in to comment.