Skip to content

Commit 158f106

Browse files
authored
feat(common): add symbols to git status component, closes #148 (#173)
1 parent 537f679 commit 158f106

File tree

7 files changed

+158
-25
lines changed

7 files changed

+158
-25
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ luac.out
4141

4242
# Vim tag files
4343
tags
44+
45+
#example
46+
ignored

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Neo-tree is a Neovim plugin to browse the file system and other tree like
44
structures in whatever style suits you, including sidebars, floating windows,
55
netrw split style, or all of them at once!
66

7-
![Neo-tree file system](https://github.com/nvim-neo-tree/resources/raw/main/images/Neo-tree-filesystem.png)
7+
![Neo-tree file system](https://github.com/nvim-neo-tree/resources/raw/main/images/Neo-tree-2.0-with-git-symbols.png)
88

99
### Breaking Changes BAD :bomb: :imp:
1010

@@ -82,7 +82,19 @@ use {
8282
use_git_status_colors = true,
8383
},
8484
git_status = {
85-
highlight = "NeoTreeDimText", -- if you remove this the status will be colorful
85+
symbols = {
86+
-- Change type
87+
added = "",
88+
deleted = "",
89+
modified = "",
90+
renamed = "",
91+
-- Status type
92+
untracked = "",
93+
ignored = "",
94+
unstaged = "",
95+
staged = "",
96+
conflict = "",
97+
}
8698
},
8799
},
88100
filesystem = {

doc/neo-tree.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,50 @@ config when calling the setup function. To just disable colors on file or
429429
directory names, you can set `use_git_status_colors = false` in the `name`
430430
component of your renderer(s).
431431

432+
Starting with 2.0, this will display symbols by default. The default symbols
433+
will require a nerd font to be installed. To change these symbols, you can set
434+
the following properties:
435+
>
436+
require("neo-tree").setup({
437+
default_component_configs = {
438+
symbols = {
439+
-- Change type
440+
added = "✚",
441+
deleted = "✖",
442+
modified = "",
443+
renamed = "",
444+
-- Status type
445+
untracked = "",
446+
ignored = "",
447+
unstaged = "",
448+
staged = "",
449+
conflict = "",
450+
}
451+
}
452+
})
453+
<
454+
To change the color of these symbols, you can edit the corresponding highlight
455+
groups:
456+
457+
NeoTreeGitAdded
458+
NeoTreeGitConflict
459+
NeoTreeGitDeleted
460+
NeoTreeGitIgnored
461+
NeoTreeGitModified
462+
NeoTreeGitUntracked
463+
464+
To revert to the previous behavior of passing the git status through as-is
465+
with codes like `[M ]` for changed/unstaged, and `[ M]` for changed/staged,
466+
you can set the `symbols` property to nil or false:
467+
>
468+
require("neo-tree").setup({
469+
default_component_configs = {
470+
git_status = {
471+
symbols = false
472+
}
473+
}
474+
})
475+
<
432476
See also: |neo-tree-git-status-source|
433477

434478

@@ -529,6 +573,7 @@ NeoTreeFilterTerm The filter term, as displayed in the root node.
529573
NeoTreeFloatBorder The border for pop-up windows.
530574
NeoTreeGitAdded File name when the git status is added.
531575
NeoTreeGitConflict File name when the git status is conflict.
576+
NeoTreeGitDeleted File name when the git status is deleted.
532577
NeoTreeGitIgnored File name when the git status is ignored.
533578
NeoTreeGitModified File name when the git status is modified.
534579
NeoTreeGitUntracked File name when the git status is untracked.

lua/neo-tree/defaults.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ local config = {
6868
use_git_status_colors = true,
6969
},
7070
git_status = {
71-
highlight = "NeoTreeDimText",
71+
symbols = {
72+
-- Change type
73+
added = "",
74+
deleted = "",
75+
modified = "",
76+
renamed = "",
77+
-- Status type
78+
untracked = "",
79+
ignored = "",
80+
unstaged = "",
81+
staged = "",
82+
conflict = "",
83+
}
7284
},
7385
},
7486
filesystem = {

lua/neo-tree/sources/common/components.lua

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
-- highlight: The highlight group to apply to this text.
1212

1313
local highlights = require("neo-tree.ui.highlights")
14+
local utils = require("neo-tree.utils")
1415

1516
local M = {}
1617

@@ -86,25 +87,83 @@ M.git_status = function(config, node, state)
8687
end
8788
end
8889

89-
local highlight = highlights.FILE_NAME
90+
local symbols = config.symbols or {}
91+
local change_symbol
92+
local change_highlt = highlights.FILE_NAME
93+
local status_symbol = symbols.unstaged
94+
local status_highlt = highlights.GIT_CONFLICT
95+
96+
if git_status:sub(2, 2) == " " then
97+
status_symbol = symbols.staged
98+
status_highlt = highlights.GIT_ADDED
99+
end
100+
90101
if git_status:match("?$") then
91-
highlight = highlights.GIT_UNTRACKED
92-
elseif git_status:match("U") then
93-
highlight = highlights.GIT_CONFLICT
102+
status_symbol = nil
103+
status_highlt = highlights.GIT_UNTRACKED
104+
change_symbol = symbols.untracked
105+
change_highlt = highlights.GIT_UNTRACKED
106+
-- all variations of merge conflicts
107+
elseif git_status == "DD" then
108+
status_symbol = symbols.conflict
109+
status_highlt = highlights.GIT_CONFLICT
110+
change_symbol = symbols.deleted
111+
change_highlt = highlights.GIT_CONFLICT
112+
elseif git_status == "UU" then
113+
status_symbol = symbols.conflict
114+
status_highlt = highlights.GIT_CONFLICT
115+
change_symbol = symbols.modified
116+
change_highlt = highlights.GIT_CONFLICT
94117
elseif git_status == "AA" then
95-
highlight = highlights.GIT_CONFLICT
118+
status_symbol = symbols.conflict
119+
status_highlt = highlights.GIT_CONFLICT
120+
change_symbol = symbols.added
121+
change_highlt = highlights.GIT_CONFLICT
122+
elseif git_status:match("U") then
123+
status_symbol = symbols.conflict
124+
status_highlt = highlights.GIT_CONFLICT
125+
if git_status:match("A") then
126+
change_symbol = symbols.added
127+
elseif git_status:match("D") then
128+
change_symbol = symbols.deleted
129+
end
130+
change_highlt = highlights.GIT_CONFLICT
131+
-- end merge conflict section
96132
elseif git_status:match("M") then
97-
highlight = highlights.GIT_MODIFIED
98-
elseif git_status:match("[ACRT]") then
99-
highlight = highlights.GIT_ADDED
133+
change_symbol = symbols.modified
134+
change_highlt = highlights.GIT_MODIFIED
135+
elseif git_status:match("R") then
136+
change_symbol = symbols.renamed
137+
change_highlt = highlights.GIT_RENAMED
138+
elseif git_status:match("[ACT]") then
139+
change_symbol = symbols.added
140+
change_highlt = highlights.GIT_ADDED
100141
elseif git_status:match("!") then
101-
highlight = highlights.GIT_IGNORED
142+
status_symbol = nil
143+
change_symbol = symbols.ignored
144+
change_highlt = highlights.GIT_IGNORED
145+
end
146+
147+
if change_symbol then
148+
local components = {}
149+
components[1] = {
150+
text = " " .. change_symbol,
151+
highlight = change_highlt,
152+
}
153+
if status_symbol then
154+
components[2] = {
155+
text = " " .. status_symbol,
156+
highlight = status_highlt,
157+
}
158+
end
159+
return components
160+
else
161+
return {
162+
text = " [" .. git_status .. "]",
163+
highlight = config.highlight or change_highlt,
164+
}
102165
end
103166

104-
return {
105-
text = " [" .. git_status .. "]",
106-
highlight = config.highlight or highlight,
107-
}
108167
end
109168

110169
M.filtered_by = function(config, node, state)

lua/neo-tree/ui/highlights.lua

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ M.FILTER_TERM = "NeoTreeFilterTerm"
1414
M.FLOAT_BORDER = "NeoTreeFloatBorder"
1515
M.GIT_ADDED = "NeoTreeGitAdded"
1616
M.GIT_CONFLICT = "NeoTreeGitConflict"
17+
M.GIT_DELETED = "NeoTreeGitDeleted"
1718
M.GIT_IGNORED = "NeoTreeGitIgnored"
1819
M.GIT_MODIFIED = "NeoTreeGitModified"
20+
M.GIT_RENAMED = "NeoTreeGitRenamed"
1921
M.GIT_UNTRACKED = "NeoTreeGitUntracked"
2022
M.HIDDEN_BY_NAME = "NeoTreeHiddenByName"
2123
M.INDENT_MARKER = "NeoTreeIndentMarker"
@@ -41,6 +43,8 @@ end
4143
--is not defined and it is not linked to another group.
4244
---@param foreground string The foreground color to use, in hex, if the highlight group
4345
--is not defined and it is not linked to another group.
46+
---@gui string The gui to use, if the highlight group is not defined and it is not linked
47+
--to another group.
4448
---@return table table The highlight group values.
4549
local function create_highlight_group(hl_group_name, link_to_if_exists, background, foreground, gui)
4650
local success, hl_group = pcall(vim.api.nvim_get_hl_by_name, hl_group_name, true)
@@ -99,23 +103,22 @@ M.setup = function()
99103

100104
create_highlight_group(M.TITLE_BAR, {}, float_border_hl.foreground, nil)
101105

102-
create_highlight_group(M.GIT_ADDED, { "GitGutterAdd", "GitSignsAdd" }, nil, "5faf5f")
103-
104-
create_highlight_group(M.GIT_CONFLICT, { "GitGutterDelete", "GitSignsDelete" }, nil, "ff5900")
105-
106-
local modified = create_highlight_group(
106+
local added = create_highlight_group(M.GIT_ADDED, { "GitGutterAdd", "GitSignsAdd" }, nil, "5faf5f")
107+
create_highlight_group(M.GIT_DELETED, { "GitGutterDelete", "GitSignsDelete" }, nil, "ff5900")
108+
create_highlight_group(
107109
M.GIT_MODIFIED,
108110
{ "GitGutterChange", "GitSignsChange" },
109111
nil,
110112
"d7af5f"
111113
)
112-
113-
create_highlight_group(M.GIT_UNTRACKED, {}, nil, modified.foreground, "italic")
114+
local conflict = create_highlight_group(M.GIT_CONFLICT, {}, nil, "ff8700", "italic,bold")
115+
create_highlight_group(M.GIT_IGNORED, { M.DOTFILE }, nil, nil)
116+
create_highlight_group(M.GIT_RENAMED, { M.GIT_MODIFIED }, nil, nil)
117+
create_highlight_group(M.GIT_UNTRACKED, {}, nil, conflict.foreground, "italic")
114118

115119
create_highlight_group(M.BUFFER_NUMBER, { "SpecialChar" })
116120
create_highlight_group(M.DIM_TEXT, {}, nil, "505050")
117121
create_highlight_group(M.DOTFILE, {}, nil, "626262")
118-
create_highlight_group(M.GIT_IGNORED, { M.DOTFILE }, nil, nil)
119122
create_highlight_group(M.HIDDEN_BY_NAME, { M.DOTFILE }, nil, nil)
120123
create_highlight_group(M.CURSOR_LINE, { "CursorLine" }, nil, nil, "bold")
121124
create_highlight_group(M.DIRECTORY_NAME, {}, "NONE", "NONE")

lua/neo-tree/ui/renderer.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ local remove_filtered = function(source_items, filtered_items)
8080
for _, child in ipairs(source_items) do
8181
local fby = child.filtered_by
8282
if type(fby) == "table" then
83-
log.info("filtered_by is a table", fby)
8483
if filtered_items.visible and not fby.never_show then
8584
table.insert(filtered, child)
8685
end

0 commit comments

Comments
 (0)