diff --git a/README.md b/README.md index 592b768e..86069bd2 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/lua/lib/format.lua b/lua/lib/format.lua index d9c1e323..8ed7f663 100644 --- a/lua/lib/format.lua +++ b/lua/lib/format.lua @@ -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; } diff --git a/lua/tree.lua b/lua/tree.lua index 9ad2a682..815befb9 100644 --- a/lua/tree.lua +++ b/lua/tree.lua @@ -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 diff --git a/plugin/tree.vim b/plugin/tree.vim index 06c8cc0b..cab011c7 100644 --- a/plugin/tree.vim +++ b/plugin/tree.vim @@ -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 -