add syntax highlighting

This commit is contained in:
kyazdani42 2020-02-14 16:51:33 +01:00
parent 4761e3f2a5
commit 083d2b556d
4 changed files with 90 additions and 3 deletions

View File

@ -16,7 +16,7 @@
- [x] add / rename / delete file in directory
- [ ] update tree when altering the FS
- [ ] syntax highlighting
- [x] syntax highlighting
- [ ] simple git integration (color of file changing when staged/changed)
- [ ] quickly find file in the directory structure
@ -25,3 +25,4 @@
- [ ] handle permissions properly (TODO: display error on Read access denied)
- [ ] buffer / window should always stay on the left and never change size (open a file with only the tree open to reproduce this bug)
- [ ] buffer / window should not disappear when only the tree is opened

View File

@ -23,7 +23,9 @@ end
local function dev_icons(pathname, isdir, open)
if isdir == true then return default_icons(pathname, isdir, open) end
return api.nvim_call_function('WebDevIconsGetFileTypeSymbol', { pathname, isdir }) .. " "
local icon = api.nvim_call_function('WebDevIconsGetFileTypeSymbol', { pathname, isdir })
if icon == "" then return "" end
return icon .. " "
end
local function get_icon_func_gen()
@ -51,6 +53,67 @@ local function format_tree(tree)
return dirs
end
local HIGHLIGHT_GROUPS = {
['^.*%.md$'] = 'MarkdownFile';
['^LICENSE$'] = 'LicenseFile';
['^.*%.vim$'] = 'VimFile';
['^.*%.c$'] = 'CFile';
['^.*%.cpp$'] = 'CFile';
['^.*%.cxx$'] = 'CFile';
['^.*%.h$'] = 'CFile';
['^.*%.hpp$'] = 'CFile';
['^.*%.py$'] = 'PythonFile';
['^.*%.lua$'] = 'LuaFile';
['^.*%.rs$'] = 'RustFile';
['^.*%.sh$'] = 'ShellFile';
['^.*%.zsh$'] = 'ShellFile';
['^.*%.csh$'] = 'ShellFile';
['^.*%.rs$'] = 'RustFile';
['^.*%.js$'] = 'JavascriptFile';
['^.*%.json$'] = 'JsonFile';
['^.*%.toml$'] = 'TomlFile';
['^.*%.yml$'] = 'YamlFile';
['^.*%.gitignore$'] = 'GitignoreFile';
['^.*%.ts$'] = 'TypescriptFile';
['^.*%.jsx$'] = 'ReactFile';
['^.*%.tsx$'] = 'ReactFile';
['^.*%.html?$'] = 'HtmlFile';
['^.*%.png$'] = 'ImageFile';
['^.*%.jpe?g$'] = 'ImageFile';
}
local function highlight_line(buffer)
local function highlight(group, line, from, to)
vim.api.nvim_buf_add_highlight(buffer, -1, group, line, from, to)
end
return function(line, node)
local text_start = node.depth * 2 + 4
if node.dir then
if node.name ~= '..' then
highlight('LuaTreeFolderIcon', line, 0, text_start)
highlight('LuaTreeFolderName', line, text_start, -1)
else
highlight('LuaTreeFolderName', line, 0, -1)
end
else
for k, v in pairs(HIGHLIGHT_GROUPS) do
if string.match(node.name, k) ~= nil then
highlight('LuaTree' .. v, line, 0, text_start)
break
end
end
end
end
end
local function highlight_buffer(buffer, tree)
local highlight = highlight_line(buffer)
for i, node in pairs(tree) do
highlight(i - 1, node)
end
end
return {
format_tree = format_tree;
highlight_buffer = highlight_buffer;
}

View File

@ -1,5 +1,6 @@
local lib_file = require 'lib/file'
local format = require 'lib/format'.format_tree
local highlight = require 'lib/format'.highlight_buffer
local api = vim.api
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end
@ -143,6 +144,7 @@ local function update_view(update_cursor)
api.nvim_buf_set_option(buf, 'modifiable', true)
api.nvim_buf_set_lines(buf, 0, -1, false, format(Tree))
highlight(buf, Tree)
api.nvim_buf_set_option(buf, 'modifiable', false)
if update_cursor == true then

View File

@ -6,10 +6,31 @@ set cpo&vim
hi def link LuaTreePopup Normal
hi def LuaTreeEndOfBuffer guifg=bg
execute 'hi def LuaTreeFolderName guifg='.g:terminal_color_4.' gui=bold'
execute 'hi def LuaTreeFolderIcon guifg='.g:terminal_color_11
execute 'hi def LuaTreeLicenseFile guifg='.g:terminal_color_3
execute 'hi def LuaTreeMarkdownFile guifg='.g:terminal_color_5
execute 'hi def LuaTreeImageFile guifg='.g:terminal_color_5
execute 'hi def LuaTreeYamlFile guifg='.g:terminal_color_3
execute 'hi def LuaTreeTomlFile guifg='.g:terminal_color_3
execute 'hi def LuaTreeGitignoreFile guifg='.g:terminal_color_3
execute 'hi def LuaTreeJsonFile guifg='.g:terminal_color_3
hi def LuaTreeLuaFile guifg=#2947b1
execute 'hi def LuaTreePythonFile guifg='.g:terminal_color_2
execute 'hi def LuaTreeShellFile guifg='.g:terminal_color_2
execute 'hi def LuaTreeJavascriptFile guifg='.g:terminal_color_3
execute 'hi def LuaTreeCFile guifg='.g:terminal_color_4
execute 'hi def LuaTreeReactFile guifg='.g:terminal_color_14
execute 'hi def LuaTreeHtmlFile guifg='.g:terminal_color_11
execute 'hi def LuaTreeRustFile guifg='.g:terminal_color_11
execute 'hi def LuaTreeVimFile guifg='.g:terminal_color_10
execute 'hi def LuaTreeTypescriptFile guifg='.g:terminal_color_12
command! LuaTree lua require'tree'.toggle()
let &cpo = s:save_cpo
unlet s:save_cpo
let g:loaded_tree = 1