From b92df50cc1855b55b36e221b2bed7d0e5e250c28 Mon Sep 17 00:00:00 2001 From: Sidney Liebrand Date: Sun, 10 Dec 2023 15:49:22 +0100 Subject: [PATCH 1/4] Feature: implement hidden file/directory toggling (#132) --- lua/carbon/entry.lua | 7 +------ lua/carbon/init.lua | 9 +++++++++ lua/carbon/settings.lua | 1 + lua/carbon/view.lua | 19 +++++++++++++++---- lua/carbon/watcher.lua | 3 +-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lua/carbon/entry.lua b/lua/carbon/entry.lua index b7a6b10..4258eb9 100644 --- a/lua/carbon/entry.lua +++ b/lua/carbon/entry.lua @@ -149,12 +149,7 @@ function entry:get_children() end for name in iterator do - local absolute_path = self.path .. '/' .. name - local relative_path = vim.fn.fnamemodify(absolute_path, ':.') - - if not util.is_excluded(relative_path) then - entries[#entries + 1] = entry.new(absolute_path, self) - end + entries[#entries + 1] = entry.new(self.path .. '/' .. name, self) end table.sort(entries) diff --git a/lua/carbon/init.lua b/lua/carbon/init.lua index 61333de..0de6de2 100644 --- a/lua/carbon/init.lua +++ b/lua/carbon/init.lua @@ -121,6 +121,15 @@ function carbon.session_load_post(event) end end +function carbon.toggle_hidden() + view.execute(function(ctx) + ctx.view.show_hidden = not ctx.view.show_hidden + + ctx.view:update() + ctx.view:render() + end) +end + function carbon.toggle_recursive() view.execute(function(ctx) if ctx.cursor.line.entry.is_directory then diff --git a/lua/carbon/settings.lua b/lua/carbon/settings.lua index 6958504..3cd677c 100644 --- a/lua/carbon/settings.lua +++ b/lua/carbon/settings.lua @@ -63,6 +63,7 @@ local defaults = { create = { 'c', '%' }, delete = 'd', close_parent = '-', + toggle_hidden = '*', toggle_recursive = '!', }, highlights = { diff --git a/lua/carbon/view.lua b/lua/carbon/view.lua index fee61e5..8691322 100644 --- a/lua/carbon/view.lua +++ b/lua/carbon/view.lua @@ -94,6 +94,7 @@ function view.get(path) index = index, initial = resolved, states = {}, + show_hidden = false, root = entry.new(resolved), }, view) @@ -545,6 +546,16 @@ function view:current_lines() return self.cached_lines end +function view:entry_children(target) + if self.show_hidden then + return target:children() + else + return vim.tbl_filter(function(child) + return not util.is_excluded(vim.fn.fnamemodify(child.path, ':.')) + end, target:children()) + end +end + function view:lines(input_target, lines, depth) lines = lines or {} depth = depth or 0 @@ -572,7 +583,7 @@ function view:lines(input_target, lines, depth) watcher.register(self.root.path) end - for _, child in ipairs(target:children()) do + for _, child in ipairs(self:entry_children(target)) do local tmp = child local hls = {} local path = {} @@ -585,20 +596,20 @@ function view:lines(input_target, lines, depth) if settings.compress then while tmp.is_directory - and #tmp:children() == 1 + and #self:entry_children(tmp) == 1 and self:get_path_attr(tmp.path, 'compressible') do watcher.register(tmp.path) path[#path + 1] = tmp - tmp = tmp:children()[1] + tmp = self:entry_children(tmp)[1] end end if tmp.is_directory then watcher.register(tmp.path) - is_empty = #tmp:children() == 0 + is_empty = #self:entry_children(tmp) == 0 path_suffix = '/' if not is_empty and self:get_path_attr(tmp.path, 'open') then diff --git a/lua/carbon/watcher.lua b/lua/carbon/watcher.lua index c46c73c..628fc53 100644 --- a/lua/carbon/watcher.lua +++ b/lua/carbon/watcher.lua @@ -1,4 +1,3 @@ -local util = require('carbon.util') local watcher = {} watcher.listeners = {} @@ -25,7 +24,7 @@ function watcher.release(path) end function watcher.register(path) - if not watcher.listeners[path] and not util.is_excluded(path) then + if not watcher.listeners[path] then watcher.listeners[path] = vim.loop.new_fs_event() watcher.listeners[path]:start( From b38727faa4e531572aff27a1763ae18d7c1f184a Mon Sep 17 00:00:00 2001 From: Sidney Liebrand Date: Sun, 10 Dec 2023 15:51:32 +0100 Subject: [PATCH 2/4] Document hidden file/directory toggling --- doc/carbon.txt | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/doc/carbon.txt b/doc/carbon.txt index 319268a..47654dd 100644 --- a/doc/carbon.txt +++ b/doc/carbon.txt @@ -392,6 +392,14 @@ PLUGS *carbon-plug Closes the parent directory of the entry the cursor is on. Accepts a [count] to allow closing multiple parents at once. + `------------------------------------------------------------------------------` + (carbon-toggle-hidden) *carbon-plug-toggle-hidden* + + Implementation: `require('carbon').toggle_hidden()` + Mapping: |carbon-setting-actions-toggle-hidden| + + Toggles visibility of hidden files and directories in the current view. + `------------------------------------------------------------------------------` (carbon-toggle-recursive) *carbon-plug-toggle-recursive* @@ -647,6 +655,13 @@ CARBON *carbon-carbo Finally, if the Carbon buffer is the only active buffer in the only active window, nothing happens. + `------------------------------------------------------------------------------` + toggle_hidden *carbon-carbon-toggle-hidden* + + Signature: `require('carbon').toggle_hidden()` + + Toggles hidden files and directories in the current Carbon view. + `------------------------------------------------------------------------------` toggle_recursive *carbon-carbon-toggle-recursive* @@ -1199,8 +1214,11 @@ ENTRY *carbon-entr sorted in case-insensitive alphabetical order and all directories will come before any files. - Paths matching any of the patterns defined in |carbon-setting-exclude| will - be excluded from the returned table. + Paths matching |carbon-setting-exclude| are not filtered here. This is done at + render time to allow hidden files to be toggled. See: + + - |carbon-view-entry-children| + - |carbon-plug-toggle-hidden| ================================================================================ VIEW *carbon-view* @@ -1246,7 +1264,8 @@ VIEW *carbon-vie `index = ,` `initial = ,` `states = >>,` - `root = ` + `show_hidden = ,` + `root = ,` `}` `------------------------------------------------------------------------------` @@ -1429,10 +1448,22 @@ VIEW *carbon-vie |carbon-setting-highlights-CarbonFlash|. The delay and duration of the highlight are controlled by |carbon-setting-flash|. + `------------------------------------------------------------------------------` + entry_children *carbon-view-entry-children* + + Signature: `view:entry_children(`{entry}`)` + + The variable `view` in this section refers to a |carbon-view-instance|. + + The {entry} argument must be an entry object as returned by |carbon-entry-new|. + Returns filtered child entries of given {entry} based on `settings.exclude` + when `view.show_hidden` is `false`. Otherwise returns all child entries + without filtering. + `------------------------------------------------------------------------------` lines *carbon-view-lines* - Signature: `view:lines(`{target}[, {lines}[, {depth}]]`)` + Signature: `view:lines(`{entry}[, {lines}[, {depth}]]`)` The variable `view` in this section refers to a |carbon-view-instance|. @@ -2119,6 +2150,7 @@ SETTINGS *carbon-setting ` move = `|carbon-setting-actions-move|`,` ` delete = `|carbon-setting-actions-delete|`,` ` close_parent = `|carbon-setting-actions-close-parent|`,` + `toggle_hidden =` |carbon-setting-actions-toggle-hidden|`,` ` toggle_recursive = `|carbon-setting-actions-toggle-recursive|`,` ` },` @@ -2388,6 +2420,7 @@ SETTINGS *carbon-setting ` create = { 'c', '%' },` *carbon-setting-actions-create* ` delete = 'd',` *carbon-setting-actions-delete* ` close_parent = '-',` *carbon-setting-actions-close-parent* + ` toggle_hidden = '*',` *carbon-setting-actions-toggle-hidden* ` toggle_recursive = '!',` *carbon-setting-actions-toggle-recursive* `}` @@ -2414,6 +2447,7 @@ SETTINGS *carbon-setting `create` => |carbon-plug-create| `delete` => |carbon-plug-delete| `close_parent` => |carbon-plug-close-parent| + `toggle_hidden` => |carbon-plug-toggle-hidden| `toggle_recursive` => |carbon-plug-toggle-recursive| `------------------------------------------------------------------------------` From 12f6729fa7c3e2260ffdbab6d4ef5f9a130dc609 Mon Sep 17 00:00:00 2001 From: Sidney Liebrand Date: Sun, 10 Dec 2023 16:00:48 +0100 Subject: [PATCH 3/4] Test hidden file/directory toggling --- test/specs/carbon_spec.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/specs/carbon_spec.lua b/test/specs/carbon_spec.lua index 7b3a6fa..42daf69 100644 --- a/test/specs/carbon_spec.lua +++ b/test/specs/carbon_spec.lua @@ -179,6 +179,25 @@ describe('carbon', function() end) end) + describe('toggle_hidden', function() + it('toggles hidden files and directories', function() + local visible_before = #vim.api.nvim_buf_get_lines(0, 0, -1, true) + + carbon.toggle_hidden() + + local visible_after = #vim.api.nvim_buf_get_lines(0, 0, -1, true) + + assert.not_equal(visible_before, visible_after) + assert.is_true(visible_before < visible_after) + + carbon.toggle_hidden() + + local visible_after_revert = #vim.api.nvim_buf_get_lines(0, 0, -1, true) + + assert.equal(visible_before, visible_after_revert) + end) + end) + describe('toggle_recursive', function() it('toggles recursively opened directory', function() local assets_entry = helpers.entry('doc/assets') From 08a2f33303ab0e85f21da3a5e4110f1663f4fced Mon Sep 17 00:00:00 2001 From: Sidney Liebrand Date: Sun, 10 Dec 2023 16:02:53 +0100 Subject: [PATCH 4/4] Remove unused module require --- lua/carbon/entry.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/carbon/entry.lua b/lua/carbon/entry.lua index 4258eb9..ee8b72a 100644 --- a/lua/carbon/entry.lua +++ b/lua/carbon/entry.lua @@ -1,4 +1,3 @@ -local util = require('carbon.util') local watcher = require('carbon.watcher') local entry = {}