feat: nvim lsp integration (#260)

This commit is contained in:
Kiyan 2021-04-08 23:30:35 +02:00 committed by GitHub
parent 50d31fb7f3
commit 82b20f5b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 2 deletions

View File

@ -36,6 +36,7 @@ let g:nvim_tree_disable_netrw = 0 "1 by default, disables netrw
let g:nvim_tree_hijack_netrw = 0 "1 by default, prevents netrw from automatically opening when opening directories (but lets you keep its other utilities) let g:nvim_tree_hijack_netrw = 0 "1 by default, prevents netrw from automatically opening when opening directories (but lets you keep its other utilities)
let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names let g:nvim_tree_add_trailing = 1 "0 by default, append a trailing slash to folder names
let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree let g:nvim_tree_group_empty = 1 " 0 by default, compact folders that only contain a single folder into one node in the file tree
let g:nvim_tree_lsp_diagnostics = 1 "0 by default, will show lsp diagnostics in the tree. See :help nvim_tree_lsp_diagnostics
let g:nvim_tree_show_icons = { let g:nvim_tree_show_icons = {
\ 'git': 1, \ 'git': 1,
\ 'folders': 0, \ 'folders': 0,
@ -173,6 +174,7 @@ This plugin is very fast because it uses the `libuv` `scandir` and `scandir_next
- Change directory with `.` - Change directory with `.`
- Add / Rename / delete files - Add / Rename / delete files
- Git integration (icons and file highlight) - Git integration (icons and file highlight)
- Lsp diagnostics integration (file highlight)
- Indent markers - Indent markers
- Mouse support - Mouse support
- It's fast - It's fast

View File

@ -220,6 +220,12 @@ Can be 0 or 1. When 1, appends a trailing slash to folder names.
Can be 0 or 1. When 1, folders that contain only one folder are grouped Can be 0 or 1. When 1, folders that contain only one folder are grouped
together. 0 by default. together. 0 by default.
|g:nvim_tree_lsp_diagnostics| *g:nvim_tree_lsp_diagnostics*
Can be 0 or 1. When 1, will show nvim-lsp diagnostics in the tree
by highlighting filenames with `NvimTreeLspDiagnostics`.
Code will be executed on `BufWritePost`. 0 by default.
============================================================================== ==============================================================================
INFORMATIONS *nvim-tree-info* INFORMATIONS *nvim-tree-info*
@ -360,6 +366,7 @@ NvimTreeSpecialFile
NvimTreeImageFile NvimTreeImageFile
NvimTreeMarkdownFile NvimTreeMarkdownFile
NvimTreeIndentMarker NvimTreeIndentMarker
NvimTreeLspDiagnostics
NvimTreeLicenseIcon NvimTreeLicenseIcon
NvimTreeYamlIcon NvimTreeYamlIcon

View File

@ -34,6 +34,7 @@ local function get_hl_groups()
Symlink = { gui = 'bold', fg = colors.cyan }, Symlink = { gui = 'bold', fg = colors.cyan },
FolderIcon = { fg = '#8094b4' }, FolderIcon = { fg = '#8094b4' },
RootFolder = { fg = colors.purple }, RootFolder = { fg = colors.purple },
LspDiagnostics = { gui = 'underline' },
ExecFile = { gui = 'bold', fg = colors.green }, ExecFile = { gui = 'bold', fg = colors.green },
SpecialFile = { gui = 'bold,underline', fg = colors.yellow }, SpecialFile = { gui = 'bold,underline', fg = colors.yellow },
@ -74,8 +75,9 @@ function M.setup()
end end
local higlight_groups = get_hl_groups() local higlight_groups = get_hl_groups()
for k, d in pairs(higlight_groups) do for k, d in pairs(higlight_groups) do
local gui = d.gui or 'NONE' local gui = d.gui and ' gui='..d.gui or ''
api.nvim_command('hi def NvimTree'..k..' gui='..gui..' guifg='..d.fg) local fg = d.fg and ' guifg='..d.fg or ''
api.nvim_command('hi def NvimTree'..k..gui..fg)
end end
local links = get_links() local links = get_links()

View File

@ -0,0 +1,49 @@
local a = vim.api
local utils = require'nvim-tree.utils'
local get_diagnostics = vim.lsp.diagnostic.get_all
local M = {}
local function get_severity(diagnostics)
for _, v in ipairs(diagnostics) do
if v.severity > 0 then
return v.severity
end
end
return 0
end
local function highlight_node(node, linenr)
local buf = require'nvim-tree.lib'.Tree.bufnr
if not vim.fn.bufexists(buf) or not vim.fn.bufloaded(buf) then return end
local line = a.nvim_buf_get_lines(buf, linenr, linenr+1, false)[1]
local starts_at = vim.fn.stridx(line, node.name)
a.nvim_buf_add_highlight(buf, -1, 'NvimTreeLspDiagnostics', linenr, starts_at, -1)
end
function M.update()
local buffer_severity = {}
for buf, diagnostics in pairs(get_diagnostics()) do
local bufname = a.nvim_buf_get_name(buf)
if not buffer_severity[bufname] then
local severity = get_severity(diagnostics)
buffer_severity[bufname] = severity
end
end
vim.defer_fn(function()
local nodes = require'nvim-tree.lib'.Tree.entries
for bufname, severity in pairs(buffer_severity) do
if severity > 0 then
local node, line = utils.find_node(nodes, function(node)
return node.absolute_path == bufname
end)
if node then highlight_node(node, line) end
end
end
end, 100)
end
return M

View File

@ -64,4 +64,22 @@ function M.path_remove_trailing(path)
end end
M.path_separator = path_separator M.path_separator = path_separator
-- get the node from the tree that matches the predicate
-- @param nodes list of node
-- @param fn function(node): boolean
function M.find_node(nodes, fn)
local i = 1
for _, node in ipairs(nodes) do
if fn(node) then return node, i end
if node.open and #node.entries > 0 then
local n, idx = M.find_node(node.entries, fn)
i = i + idx
if n then return n, i end
end
i = i + 1
end
return nil, i
end
return M return M

View File

@ -13,6 +13,9 @@ augroup NvimTree
silent! autocmd! FileExplorer * silent! autocmd! FileExplorer *
endif endif
au BufWritePost * lua require'nvim-tree'.refresh() au BufWritePost * lua require'nvim-tree'.refresh()
if get(g:, 'nvim_tree_lsp_diagnostics', 0) == 1
au BufWritePost * lua require'nvim-tree.diagnostics'.update()
endif
au BufEnter * lua require'nvim-tree'.buf_enter() au BufEnter * lua require'nvim-tree'.buf_enter()
if get(g:, 'nvim_tree_auto_close') == 1 if get(g:, 'nvim_tree_auto_close') == 1
au WinClosed * lua require'nvim-tree'.on_leave() au WinClosed * lua require'nvim-tree'.on_leave()