Skip to content

Commit 04407c3

Browse files
committed
Rewrite: rewrite the whole api
- The tree is created asynchronously - More solid logic for opening and closing the tree - smart rendering - smart updates
1 parent e1fbabf commit 04407c3

File tree

14 files changed

+543
-1008
lines changed

14 files changed

+543
-1008
lines changed

lua/lib/colors.lua

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
local api = vim.api
2-
local get_colors = require 'lib/config'.get_colors
32

4-
local colors = get_colors()
3+
local function get_color_from_hl(hl_name, fallback)
4+
local id = vim.api.nvim_get_hl_id_by_name(hl_name)
5+
if not id then return fallback end
56

6-
local M = {}
7+
local hl = vim.api.nvim_get_hl_by_id(id, true)
8+
if not hl or not hl.foreground then return fallback end
9+
10+
return hl.foreground
11+
end
12+
13+
local function get_colors()
14+
return {
15+
red = vim.g.terminal_color_1 or get_color_from_hl('Keyword', 'Red'),
16+
green = vim.g.terminal_color_2 or get_color_from_hl('Character', 'Green'),
17+
yellow = vim.g.terminal_color_3 or get_color_from_hl('PreProc', 'Yellow'),
18+
blue = vim.g.terminal_color_4 or get_color_from_hl('Include', 'Blue'),
19+
purple = vim.g.terminal_color_5 or get_color_from_hl('Define', 'Purple'),
20+
cyan = vim.g.terminal_color_6 or get_color_from_hl('Conditional', 'Cyan'),
21+
dark_red = vim.g.terminal_color_9 or get_color_from_hl('Keyword', 'DarkRed'),
22+
orange = vim.g.terminal_color_11 or get_color_from_hl('Number', 'Orange'),
23+
}
24+
end
25+
26+
local function get_hl_groups()
27+
local colors = get_colors()
728

8-
local function create_hl()
929
return {
1030
Symlink = { gui = 'bold', fg = colors.cyan },
1131
FolderName = { gui = 'bold', fg = colors.blue },
@@ -22,6 +42,7 @@ local function create_hl()
2242
JsonIcon = { fg = colors.yellow },
2343

2444
LuaIcon = { fg = '#42a5f5' },
45+
GoIcon = { fg = '#7Fd5EA' },
2546
PythonIcon = { fg = colors.green },
2647
ShellIcon = { fg = colors.green },
2748
JavascriptIcon = { fg = colors.yellow },
@@ -40,25 +61,52 @@ local function create_hl()
4061
}
4162
end
4263

43-
local HIGHLIGHTS = create_hl()
64+
local function get_links()
65+
return {
66+
Normal = 'Normal',
67+
EndOfBuffer = 'EndOfBuffer',
68+
CursorLine = 'CursorLine',
69+
VertSplit = 'VertSplit',
70+
CursorColumn = 'CursorColumn'
71+
}
72+
end
73+
74+
local M = {}
4475

45-
local LINKS = {
46-
Normal = 'Normal',
47-
EndOfBuffer = 'EndOfBuffer',
48-
CursorLine = 'CursorLine',
49-
VertSplit = 'VertSplit',
50-
CursorColumn = 'CursorColumn'
76+
M.ft_to_hl_group = {
77+
['LICENSE'] = 'LicenseIcon';
78+
['vim'] = 'VimIcon';
79+
['c'] = 'CIcon';
80+
['cpp'] = 'CIcon';
81+
['python'] = 'PythonIcon';
82+
['lua'] = 'LuaIcon';
83+
['rust'] = 'RustIcon';
84+
['sh'] = 'ShellIcon';
85+
['bash'] = 'ShellIcon';
86+
['zsh'] = 'ShellIcon';
87+
['markdown'] = 'MarkdownIcon';
88+
['json'] = 'JsonIcon';
89+
['toml'] = 'TomlIcon';
90+
['go'] = 'GoIcon';
91+
['yaml'] = 'YamlIcon';
92+
['conf'] = 'GitignoreIcon';
93+
['javascript'] = 'JavascriptIcon';
94+
['typescript'] = 'TypescriptIcon';
95+
['jsx'] = 'ReactIcon';
96+
['tsx'] = 'ReactIcon';
97+
['html'] = 'HtmlIcon';
98+
['htm'] = 'HtmlIcon';
5199
}
52100

53-
function M.init_colors()
54-
colors = get_colors()
55-
HIGHLIGHTS = create_hl()
56-
for k, d in pairs(HIGHLIGHTS) do
101+
function M.setup()
102+
local higlight_groups = get_hl_groups()
103+
for k, d in pairs(higlight_groups) do
57104
local gui = d.gui or 'NONE'
58105
api.nvim_command('hi def LuaTree'..k..' gui='..gui..' guifg='..d.fg)
59106
end
60107

61-
for k, d in pairs(LINKS) do
108+
local links = get_links()
109+
for k, d in pairs(links) do
62110
api.nvim_command('hi def link LuaTree'..k..' '..d)
63111
end
64112
end

lua/lib/config.lua

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,28 @@
1-
local api = vim.api
2-
31
local M = {}
42

5-
local function get(var, fallback)
6-
if api.nvim_call_function('exists', { var }) == 1 then
7-
return api.nvim_get_var(var)
8-
else
9-
return fallback
10-
end
11-
end
12-
13-
local function get_color_from_hl(hl_name, fallback)
14-
local id = api.nvim_get_hl_id_by_name(hl_name)
15-
if not id then return fallback end
3+
function M.get_icon_state()
4+
local has_devicons = vim.fn.exists("*WebDevIconsGetFileTypeSymbol") == 1
5+
local show_icons = vim.g.lua_tree_show_icons or { git = 1, folders = 1, files = 1 }
166

17-
local hl = api.nvim_get_hl_by_id(id, true)
18-
if not hl or not hl.foreground then return fallback end
19-
20-
return hl.foreground
7+
return {
8+
show_file_icon = has_devicons and show_icons.files == 1,
9+
show_folder_icon = show_icons.folders == 1,
10+
show_git_icon = show_icons.git == 1
11+
}
2112
end
2213

23-
local HAS_DEV_ICONS = api.nvim_call_function('exists', { "*WebDevIconsGetFileTypeSymbol" }) == 1
24-
25-
local show_icons = get('lua_tree_show_icons', { git = 1, folders = 1, files = 1 })
26-
27-
M.SHOW_FILE_ICON = HAS_DEV_ICONS and show_icons.files == 1
28-
M.SHOW_FOLDER_ICON = show_icons.folders == 1
29-
M.SHOW_GIT_ICON = show_icons.git == 1
30-
31-
function M.get_colors()
14+
function M.get_bindings()
15+
local keybindings = vim.g.lua_tree_bindings or {}
3216
return {
33-
red = get('terminal_color_1', get_color_from_hl('Keyword', 'Red')),
34-
green = get('terminal_color_2', get_color_from_hl('Character', 'Green')),
35-
yellow = get('terminal_color_3', get_color_from_hl('PreProc', 'Yellow')),
36-
blue = get('terminal_color_4', get_color_from_hl('Include', 'Blue')),
37-
purple = get('terminal_color_5', get_color_from_hl('Define', 'Purple')),
38-
cyan = get('terminal_color_6', get_color_from_hl('Conditional', 'Cyan')),
39-
orange = get('terminal_color_11', get_color_from_hl('Number', 'Orange')),
40-
dark_red = get('terminal_color_9', get_color_from_hl('Keyword', 'DarkRed')),
17+
edit = keybindings.edit or '<CR>',
18+
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
19+
edit_split = keybindings.edit_split or '<C-x>',
20+
edit_tab = keybindings.edit_tab or '<C-t>',
21+
cd = keybindings.cd or '<C-]>',
22+
create = keybindings.create or 'a',
23+
remove = keybindings.remove or 'd',
24+
rename = keybindings.rename or 'r',
4125
}
4226
end
4327

44-
local keybindings = get('lua_tree_bindings', {});
45-
46-
M.bindings = {
47-
edit = keybindings.edit or '<CR>',
48-
edit_vsplit = keybindings.edit_vsplit or '<C-v>',
49-
edit_split = keybindings.edit_split or '<C-x>',
50-
edit_tab = keybindings.edit_tab or '<C-t>',
51-
cd = keybindings.cd or '.',
52-
create = keybindings.create or 'a',
53-
remove = keybindings.remove or 'd',
54-
rename = keybindings.rename or 'r',
55-
}
56-
5728
return M

lua/lib/format.lua

Lines changed: 0 additions & 183 deletions
This file was deleted.

0 commit comments

Comments
 (0)