Skip to content

Commit

Permalink
Merge pull request #135 from SidOfc/feature/toggle-hidden-entries
Browse files Browse the repository at this point in the history
feature/toggle hidden entries
  • Loading branch information
SidOfc authored Dec 10, 2023
2 parents 92d9ed9 + 08a2f33 commit b2bf49c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 17 deletions.
42 changes: 38 additions & 4 deletions doc/carbon.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

`------------------------------------------------------------------------------`
<Plug>(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.

`------------------------------------------------------------------------------`
<Plug>(carbon-toggle-recursive) *carbon-plug-toggle-recursive*

Expand Down Expand Up @@ -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*

Expand Down Expand Up @@ -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*
Expand Down Expand Up @@ -1246,7 +1264,8 @@ VIEW *carbon-vie
`index = <number>,`
`initial = <string>,`
`states = <table<string, table<string>>>,`
`root = <entry>`
`show_hidden = <boolean>,`
`root = <entry>,`
`}`

`------------------------------------------------------------------------------`
Expand Down Expand Up @@ -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|.

Expand Down Expand Up @@ -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|`,`
` },`

Expand Down Expand Up @@ -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*
`}`

Expand All @@ -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|

`------------------------------------------------------------------------------`
Expand Down
8 changes: 1 addition & 7 deletions lua/carbon/entry.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local util = require('carbon.util')
local watcher = require('carbon.watcher')
local entry = {}

Expand Down Expand Up @@ -149,12 +148,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)
Expand Down
9 changes: 9 additions & 0 deletions lua/carbon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/carbon/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ local defaults = {
create = { 'c', '%' },
delete = 'd',
close_parent = '-',
toggle_hidden = '*',
toggle_recursive = '!',
},
highlights = {
Expand Down
19 changes: 15 additions & 4 deletions lua/carbon/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function view.get(path)
index = index,
initial = resolved,
states = {},
show_hidden = false,
root = entry.new(resolved),
}, view)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions lua/carbon/watcher.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local util = require('carbon.util')
local watcher = {}

watcher.listeners = {}
Expand All @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions test/specs/carbon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit b2bf49c

Please sign in to comment.