Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Indicate modified buffers #1835

Merged
merged 25 commits into from Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b65ecd6
Outlined new options
chomosuke Dec 17, 2022
b14b135
Merge branch 'master' into feat-modified-buffers
chomosuke Dec 17, 2022
ece4c37
highlight_modified is highlight_opened_files
chomosuke Dec 18, 2022
a34c907
prototype with autocmd
chomosuke Dec 18, 2022
84e4ff5
Merge branch 'master' into feat-modified-buffers
chomosuke Dec 19, 2022
e501f16
moved modified into glyphs
chomosuke Dec 19, 2022
00d3739
show_on_dirs and show_on_open_dirs
chomosuke Dec 21, 2022
2ad6125
icon placement before & after
chomosuke Dec 21, 2022
b3ed7c9
_get_filename_offset
chomosuke Dec 21, 2022
b4ef5e0
fixed :wq doesn't update modified indicator
chomosuke Dec 22, 2022
e95d3a5
highlight_modified, signcolumn modified_placement
chomosuke Dec 23, 2022
9b04ff5
updated doc to match the reality of no multi char for glyphs.modified
chomosuke Dec 23, 2022
8bf609c
fixed git signcolumn doesn't show
chomosuke Dec 23, 2022
c7b20ca
fixed highlight_modified gets replaced by highlight_opened_files
chomosuke Dec 23, 2022
621d634
fixed renderer.icons.show.modified = false crash
chomosuke Dec 23, 2022
2bbccb1
updated doc to reflect empty icon not breaking rendering
chomosuke Dec 23, 2022
845069d
removed debounce_delay to implement in a later PR
chomosuke Dec 27, 2022
efb3805
doc nit: order placement
alex-courtis Dec 30, 2022
ad6cb10
change modified dirs default to be consistent with git
chomosuke Dec 31, 2022
061a154
illegal git & modified placement changed to default
chomosuke Dec 31, 2022
38846ab
don't assume icon exist
chomosuke Dec 31, 2022
9e15b32
nit remove comment
alex-courtis Dec 31, 2022
e5c104d
Merge branch 'master' into feat-modified-buffers
chomosuke Dec 31, 2022
4554681
Noted in doc that glyphs can't have more than 2 characters if in sign…
chomosuke Dec 31, 2022
413dc30
Don't sign_define if placement isn't signcolumn
chomosuke Dec 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 54 additions & 2 deletions doc/nvim-tree-lua.txt
Expand Up @@ -222,6 +222,7 @@ Subsequent calls to setup will replace the previous configuration.
highlight_git = false,
full_name = false,
highlight_opened_files = "none",
highlight_modified = "none",
root_folder_label = ":~:s?$?/..?",
indent_width = 2,
indent_markers = {
Expand All @@ -238,18 +239,21 @@ Subsequent calls to setup will replace the previous configuration.
icons = {
webdev_colors = true,
git_placement = "before",
modified_placement = "after",
padding = " ",
symlink_arrow = " ➛ ",
show = {
file = true,
folder = true,
folder_arrow = true,
git = true,
modified = true,
},
glyphs = {
default = "",
symlink = "",
bookmark = "",
modified = "●",
folder = {
arrow_closed = "",
arrow_open = "",
Expand Down Expand Up @@ -324,6 +328,11 @@ Subsequent calls to setup will replace the previous configuration.
show_on_open_dirs = true,
timeout = 400,
},
modified = {
alex-courtis marked this conversation as resolved.
Show resolved Hide resolved
enable = false,
show_on_dirs = true,
show_on_open_dirs = true,
},
actions = {
use_system_clipboard = true,
change_dir = {
Expand Down Expand Up @@ -542,6 +551,7 @@ Configuration options for the system open command.

*nvim-tree.diagnostics*
Show LSP and COC diagnostics in the signcolumn
Note that the modified sign will take precedence over the diagnostics signs.
alex-courtis marked this conversation as resolved.
Show resolved Hide resolved

`NOTE`: it will use the default diagnostic color groups to highlight the signs.
If you wish to customize, you can override these groups:
Expand Down Expand Up @@ -617,6 +627,26 @@ Git integration with icons and colors.
milliseconds but a few seconds), it will not render anything until the git
process returned the data.

*nvim-tree.modified*
Indicate which file have unsaved modification.

*nvim-tree.modified.enable*
Enable / disable the feature.
Type: `boolean`, Default: `false`

*nvim-tree.modified.show_on_dirs*
Show modified indication on directory whose children are modified.
Type: `boolean`, Default: `true`

*nvim-tree.modified.show_on_open_dirs*
Show modified indication on open directories.
Only relevant when |modified.show_on_dirs| is `true`.
Type: `boolean`, Default: `true`

You will still need to set |renderer.icons.show.modified| `= true` or
|renderer.highlight_modified| `= true` to be able to see things in the
tree.

*nvim-tree.filesystem_watchers*
Will use file system watcher (libuv fs_event) to watch the filesystem for
changes.
Expand Down Expand Up @@ -773,6 +803,13 @@ UI rendering setup
Value can be `"none"`, `"icon"`, `"name"` or `"all"`.
Type: `string`, Default: `"none"`

*nvim-tree.renderer.highlight_modified*
Highlight icons and/or names for modified files using `NvimTreeModified`
highlight groups.
Value can be `"none"`, `"icon"`, `"name"` or `"all"`
alex-courtis marked this conversation as resolved.
Show resolved Hide resolved
This can be used with or without the icons.
Type: `string`, Default `"none"`

*nvim-tree.renderer.root_folder_label* (previously `renderer.root_folder_modifier`)
In what format to show root folder. See `:help filename-modifiers` for
available `string` options.
Expand Down Expand Up @@ -815,12 +852,17 @@ UI rendering setup
Place where the git icons will be rendered.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Note that the diagnostic signs will take precedence over the git signs.
Note that the diagnostic signs and the modified sign will take precedence over the git signs.
Type: `string`, Default: `before`

*nvim-tree.renderer.icons.modified_placement*
Place where the modified icon will be rendered.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `after`

*nvim-tree.renderer.icons.padding*
Inserted between icon and filename.
Use with caution, it could break rendering if you set an empty string depending on your font.
Type: `string`, Default: `" "`

*nvim-tree.renderer.icons.symlink_arrow*
Expand Down Expand Up @@ -848,6 +890,11 @@ UI rendering setup
Requires |git.enable| `= true`
Type: `boolean`, Default: `true`

*nvim-tree.renderer.icons.show.modified*
Show a modified icon, see |renderer.icons.modified_placement|
alex-courtis marked this conversation as resolved.
Show resolved Hide resolved
Requires |modified.enable| `= true`
Type: `boolean`, Default: `true`

*nvim-tree.renderer.icons.glyphs*
Configuration options for icon glyphs.

Expand All @@ -859,6 +906,10 @@ UI rendering setup
Glyph for symlinks to files.
Type: `string`, Default: `""`

*nvim-tree.renderer.icons.glyphs.modified*
Icon to display for modified files.
Type: `string`, Default: `"●"`

*nvim-tree.renderer.icons.glyphs.folder*
Glyphs for directories.
Type: `table`, Default:
Expand Down Expand Up @@ -1465,6 +1516,7 @@ NvimTreeEmptyFolderName (Directory)
NvimTreeOpenedFolderName (Directory)
NvimTreeExecFile
NvimTreeOpenedFile
NvimTreeModifiedFile
NvimTreeSpecialFile
NvimTreeImageFile
NvimTreeIndentMarker
Expand Down
20 changes: 20 additions & 0 deletions lua/nvim-tree.lua
Expand Up @@ -12,6 +12,7 @@ local copy_paste = require "nvim-tree.actions.fs.copy-paste"
local collapse_all = require "nvim-tree.actions.tree-modifiers.collapse-all"
local git = require "nvim-tree.git"
local filters = require "nvim-tree.explorer.filters"
local modified = require "nvim-tree.modified"

local _config = {}

Expand Down Expand Up @@ -476,6 +477,15 @@ local function setup_autocommands(opts)
end,
})
end

if opts.modified.enable then
create_nvim_tree_autocmd({ "BufModifiedSet", "BufWritePost" }, {
callback = function()
modified.reload()
reloaders.reload_explorer()
end,
})
end
end

local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
Expand Down Expand Up @@ -531,6 +541,7 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
highlight_git = false,
full_name = false,
highlight_opened_files = "none",
highlight_modified = "none",
root_folder_label = ":~:s?$?/..?",
indent_width = 2,
indent_markers = {
Expand All @@ -547,18 +558,21 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
icons = {
webdev_colors = true,
git_placement = "before",
modified_placement = "after",
padding = " ",
symlink_arrow = " ➛ ",
show = {
file = true,
folder = true,
folder_arrow = true,
git = true,
modified = true,
},
glyphs = {
default = "",
symlink = "",
bookmark = "",
modified = "●",
folder = {
arrow_closed = "",
arrow_open = "",
Expand Down Expand Up @@ -633,6 +647,11 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
show_on_open_dirs = true,
timeout = 400,
},
modified = {
enable = false,
show_on_dirs = true,
show_on_open_dirs = true,
},
actions = {
use_system_clipboard = true,
change_dir = {
Expand Down Expand Up @@ -809,6 +828,7 @@ function M.setup(conf)
require("nvim-tree.renderer").setup(opts)
require("nvim-tree.live-filter").setup(opts)
require("nvim-tree.marks").setup(opts)
require("nvim-tree.modified").setup(opts)
if M.config.renderer.icons.show.file and pcall(require, "nvim-web-devicons") then
require("nvim-web-devicons").setup()
end
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/colors.lua
Expand Up @@ -40,6 +40,7 @@ local function get_hl_groups()
SpecialFile = { gui = "bold,underline", fg = colors.yellow },
ImageFile = { gui = "bold", fg = colors.purple },
OpenedFile = { gui = "bold", fg = colors.green },
ModifiedFile = { fg = colors.green },

GitDirty = { fg = colors.dark_red },
GitDeleted = { fg = colors.dark_red },
Expand Down
34 changes: 34 additions & 0 deletions lua/nvim-tree/modified.lua
@@ -0,0 +1,34 @@
local M = {}

---@type table<string, boolean> record of which file is modified
M._record = {}

---refresh M.record
function M.reload()
M._record = {}
local bufs = vim.fn.getbufinfo { bufmodified = true, buflisted = true }
for _, buf in pairs(bufs) do
local path = buf.name
M._record[path] = true
while path ~= vim.fn.getcwd() and path ~= "/" do
path = vim.fn.fnamemodify(path, ":h")
M._record[path] = true
end
end
end

---@param node table
---@return boolean
function M.is_modified(node)
return M.config.enable
and M._record[node.absolute_path]
and (not node.nodes or M.config.show_on_dirs)
and (not node.open or M.config.show_on_open_dirs)
end

---@param opts table
function M.setup(opts)
M.config = opts.modified
end

return M