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

@@ -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