Skip to content

Commit

Permalink
Rewrite: rewrite the whole api
Browse files Browse the repository at this point in the history
- The tree is created asynchronously
- More solid logic for opening and closing the tree
- smart rendering
- smart updates
  • Loading branch information
kyazdani42 committed May 20, 2020
1 parent e1fbabf commit 04407c3
Show file tree
Hide file tree
Showing 14 changed files with 543 additions and 1,008 deletions.
80 changes: 64 additions & 16 deletions lua/lib/colors.lua
@@ -1,11 +1,31 @@
local api = vim.api
local get_colors = require 'lib/config'.get_colors

local colors = get_colors()
local function get_color_from_hl(hl_name, fallback)
local id = vim.api.nvim_get_hl_id_by_name(hl_name)
if not id then return fallback end

local M = {}
local hl = vim.api.nvim_get_hl_by_id(id, true)
if not hl or not hl.foreground then return fallback end

return hl.foreground
end

local function get_colors()
return {
red = vim.g.terminal_color_1 or get_color_from_hl('Keyword', 'Red'),
green = vim.g.terminal_color_2 or get_color_from_hl('Character', 'Green'),
yellow = vim.g.terminal_color_3 or get_color_from_hl('PreProc', 'Yellow'),
blue = vim.g.terminal_color_4 or get_color_from_hl('Include', 'Blue'),
purple = vim.g.terminal_color_5 or get_color_from_hl('Define', 'Purple'),
cyan = vim.g.terminal_color_6 or get_color_from_hl('Conditional', 'Cyan'),
dark_red = vim.g.terminal_color_9 or get_color_from_hl('Keyword', 'DarkRed'),
orange = vim.g.terminal_color_11 or get_color_from_hl('Number', 'Orange'),
}
end

local function get_hl_groups()
local colors = get_colors()

local function create_hl()
return {
Symlink = { gui = 'bold', fg = colors.cyan },
FolderName = { gui = 'bold', fg = colors.blue },
Expand All @@ -22,6 +42,7 @@ local function create_hl()
JsonIcon = { fg = colors.yellow },

LuaIcon = { fg = '#42a5f5' },
GoIcon = { fg = '#7Fd5EA' },
PythonIcon = { fg = colors.green },
ShellIcon = { fg = colors.green },
JavascriptIcon = { fg = colors.yellow },
Expand All @@ -40,25 +61,52 @@ local function create_hl()
}
end

local HIGHLIGHTS = create_hl()
local function get_links()
return {
Normal = 'Normal',
EndOfBuffer = 'EndOfBuffer',
CursorLine = 'CursorLine',
VertSplit = 'VertSplit',
CursorColumn = 'CursorColumn'
}
end

local M = {}

local LINKS = {
Normal = 'Normal',
EndOfBuffer = 'EndOfBuffer',
CursorLine = 'CursorLine',
VertSplit = 'VertSplit',
CursorColumn = 'CursorColumn'
M.ft_to_hl_group = {
['LICENSE'] = 'LicenseIcon';
['vim'] = 'VimIcon';
['c'] = 'CIcon';
['cpp'] = 'CIcon';
['python'] = 'PythonIcon';
['lua'] = 'LuaIcon';
['rust'] = 'RustIcon';
['sh'] = 'ShellIcon';
['bash'] = 'ShellIcon';
['zsh'] = 'ShellIcon';
['markdown'] = 'MarkdownIcon';
['json'] = 'JsonIcon';
['toml'] = 'TomlIcon';
['go'] = 'GoIcon';
['yaml'] = 'YamlIcon';
['conf'] = 'GitignoreIcon';
['javascript'] = 'JavascriptIcon';
['typescript'] = 'TypescriptIcon';
['jsx'] = 'ReactIcon';
['tsx'] = 'ReactIcon';
['html'] = 'HtmlIcon';
['htm'] = 'HtmlIcon';
}

function M.init_colors()
colors = get_colors()
HIGHLIGHTS = create_hl()
for k, d in pairs(HIGHLIGHTS) do
function M.setup()
local higlight_groups = get_hl_groups()
for k, d in pairs(higlight_groups) do
local gui = d.gui or 'NONE'
api.nvim_command('hi def LuaTree'..k..' gui='..gui..' guifg='..d.fg)
end

for k, d in pairs(LINKS) do
local links = get_links()
for k, d in pairs(links) do
api.nvim_command('hi def link LuaTree'..k..' '..d)
end
end
Expand Down
65 changes: 18 additions & 47 deletions lua/lib/config.lua
@@ -1,57 +1,28 @@
local api = vim.api

local M = {}

local function get(var, fallback)
if api.nvim_call_function('exists', { var }) == 1 then
return api.nvim_get_var(var)
else
return fallback
end
end

local function get_color_from_hl(hl_name, fallback)
local id = api.nvim_get_hl_id_by_name(hl_name)
if not id then return fallback end
function M.get_icon_state()
local has_devicons = vim.fn.exists("*WebDevIconsGetFileTypeSymbol") == 1
local show_icons = vim.g.lua_tree_show_icons or { git = 1, folders = 1, files = 1 }

local hl = api.nvim_get_hl_by_id(id, true)
if not hl or not hl.foreground then return fallback end

return hl.foreground
return {
show_file_icon = has_devicons and show_icons.files == 1,
show_folder_icon = show_icons.folders == 1,
show_git_icon = show_icons.git == 1
}
end

local HAS_DEV_ICONS = api.nvim_call_function('exists', { "*WebDevIconsGetFileTypeSymbol" }) == 1

local show_icons = get('lua_tree_show_icons', { git = 1, folders = 1, files = 1 })

M.SHOW_FILE_ICON = HAS_DEV_ICONS and show_icons.files == 1
M.SHOW_FOLDER_ICON = show_icons.folders == 1
M.SHOW_GIT_ICON = show_icons.git == 1

function M.get_colors()
function M.get_bindings()
local keybindings = vim.g.lua_tree_bindings or {}
return {
red = get('terminal_color_1', get_color_from_hl('Keyword', 'Red')),
green = get('terminal_color_2', get_color_from_hl('Character', 'Green')),
yellow = get('terminal_color_3', get_color_from_hl('PreProc', 'Yellow')),
blue = get('terminal_color_4', get_color_from_hl('Include', 'Blue')),
purple = get('terminal_color_5', get_color_from_hl('Define', 'Purple')),
cyan = get('terminal_color_6', get_color_from_hl('Conditional', 'Cyan')),
orange = get('terminal_color_11', get_color_from_hl('Number', 'Orange')),
dark_red = get('terminal_color_9', get_color_from_hl('Keyword', 'DarkRed')),
edit = keybindings.edit or '<CR>',
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
edit_split = keybindings.edit_split or '<C-x>',
edit_tab = keybindings.edit_tab or '<C-t>',
cd = keybindings.cd or '<C-]>',
create = keybindings.create or 'a',
remove = keybindings.remove or 'd',
rename = keybindings.rename or 'r',
}
end

local keybindings = get('lua_tree_bindings', {});

M.bindings = {
edit = keybindings.edit or '<CR>',
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
edit_split = keybindings.edit_split or '<C-x>',
edit_tab = keybindings.edit_tab or '<C-t>',
cd = keybindings.cd or '.',
create = keybindings.create or 'a',
remove = keybindings.remove or 'd',
rename = keybindings.rename or 'r',
}

return M
183 changes: 0 additions & 183 deletions lua/lib/format.lua

This file was deleted.

0 comments on commit 04407c3

Please sign in to comment.