Skip to content
This repository was archived by the owner on Sep 12, 2022. It is now read-only.

Commit f2566c0

Browse files
committed
Do expand_node_rec recursively.
1 parent 07b7867 commit f2566c0

File tree

3 files changed

+65
-48
lines changed

3 files changed

+65
-48
lines changed

doc/vim-ntranger.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ za Toggle expand current directory under cursor. See |za|.
117117

118118

119119
*NETRToggleExpandRec* *g:NETRToggleExpandRec*
120-
zA Recursively toggle expand current directory under cursor. See |zA|.
120+
zA Recursively toggle expand current directory under cursor. See |zA|,
121+
The maximum number of expansion is |foldnestmax|.
121122

122123
*NETRVimCD* *g:NETRVimCD*
123124
L Changing vim's pwd to the directory of the entry under cursor

pythonx/netranger/netranger.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,25 @@ def preview_off(self):
934934
self._close_last_previewee()
935935
self.redraw_if_winwidth_changed()
936936

937-
def toggle_expand(self, rec=False):
937+
def _expand_node(self, node, level):
938+
if not node.is_DIR:
939+
return []
940+
941+
try:
942+
node.expanded = True
943+
self._expanded_nodes.add(node)
944+
if level == 1:
945+
return self.create_nodes(node.fullpath, node.level + 1)
946+
else:
947+
res = []
948+
for n in self.create_nodes(node.fullpath, node.level + 1):
949+
res += [n] + self._expand_node(n, level - 1)
950+
return res
951+
except PermissionError:
952+
Vim.ErrorMsg(f'Permission Denied. Not Expanding: {node.name}')
953+
return []
954+
955+
def toggle_expand(self, maxlevel=1):
938956
"""Create subnodes for the target directory.
939957
940958
Also record the mtime of the target directory so that we can
@@ -951,34 +969,8 @@ def toggle_expand(self, rec=False):
951969
del self.nodes[self.clineno + 1:end_ind]
952970
cur_node.expanded = False
953971
else:
954-
try:
955-
new_nodes = self.create_nodes(self.cur_node.fullpath,
956-
cur_node.level + 1)
957-
cur_node.expanded = True
958-
self._expanded_nodes.add(cur_node)
959-
except PermissionError:
960-
Vim.ErrorMsg(
961-
f'Permission Denied. Not Expanding: {cur_node.name}')
962-
return
963-
964-
if rec:
965-
ind = 0
966-
while ind < len(new_nodes):
967-
iter_node = new_nodes[ind]
968-
if iter_node.is_DIR:
969-
try:
970-
new_nodes[ind + 1:ind + 1] = self.create_nodes(
971-
iter_node.fullpath, iter_node.level + 1)
972-
iter_node.expanded = True
973-
self._expanded_nodes.add(iter_node)
974-
except PermissionError:
975-
Vim.ErrorMsg(
976-
f'Permission Denied. Not Expanding: {iter_node.name}'
977-
)
978-
ind += 1
979-
980-
if len(new_nodes) > 0:
981-
self.nodes[self.clineno + 1:self.clineno + 1] = new_nodes
972+
self.nodes[self.clineno + 1:self.clineno + 1] = self._expand_node(
973+
self.cur_node, maxlevel)
982974
self._redraw()
983975

984976
def edit(self):
@@ -1634,7 +1626,8 @@ def NETRToggleExpand(self):
16341626

16351627
def NETRToggleExpandRec(self):
16361628
""" Expand the current node recursively. """
1637-
self.cur_buf.toggle_expand(rec=True)
1629+
self.cur_buf.toggle_expand(
1630+
maxlevel=Vim.current.window.options['foldnestmax'])
16381631

16391632
def NETRNew(self):
16401633
""" Show the NewUI. """

test/test.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,23 @@ def wait_for_fs_free(self):
7474

7575
def editable_win_width(self):
7676
# This function takes gutter into consideration.
77-
ve = nvim.options['virtualedit']
78-
nvim.options['virtualedit'] = 'all'
77+
ve = self.set_vim_option('virtualedit', 'all')
7978
nvim.command('noautocmd norm! g$')
8079
res = nvim.call('virtcol', '.')
8180
nvim.command('noautocmd norm! g0')
82-
nvim.options['virtualedit'] = ve
81+
self.set_vim_option('virtualedit', ve)
8382
return res
8483

84+
def set_vim_option(self, opt, value):
85+
ori = nvim.options[opt]
86+
nvim.options[opt] = value
87+
return ori
88+
89+
def set_vim_window_option(self, opt, value):
90+
ori = nvim.current.window.options[opt]
91+
nvim.current.window.options[opt] = value
92+
return ori
93+
8594
def unlock_fs(self):
8695
# nvim.input is asynchronous, we need make sure there's enough time for it
8796
# to take effects.
@@ -222,20 +231,6 @@ def test_NETRGoPrevSibling(self):
222231
nvim.input('{')
223232
self.assert_content('dir', hi='dir')
224233

225-
def test_NETRToggleExpandRec(self):
226-
nvim.input('zA')
227-
self.assert_content('dir', ind=0, hi='dir')
228-
self.assert_content('subdir', level=1, ind=1, hi='dir')
229-
self.assert_content('subsubdir', level=2, ind=2, hi='dir')
230-
self.assert_content('placeholder', level=3, ind=3, hi='file')
231-
self.assert_content('subdir2', level=1, ind=4, hi='dir')
232-
self.assert_content('placeholder', level=2, ind=5, hi='file')
233-
self.assert_content('a', level=1, ind=6, hi='file')
234-
self.assert_content('dir2', level=0, ind=7, hi='dir')
235-
nvim.input('zA')
236-
self.assert_content('dir', ind=0, hi='dir')
237-
self.assert_content('dir2', ind=1, hi='dir')
238-
239234
def test_NETRVimCD(self):
240235
nvim.input('L')
241236
self.assertEqual('dir', os.path.basename(nvim.call('getcwd')))
@@ -374,6 +369,20 @@ def test_NETRToggleExpand(self):
374369
self.assert_content('dir', ind=0, hi='dir')
375370
self.assert_content('dir2', ind=1, hi='dir')
376371

372+
def test_NETRToggleExpandRec(self):
373+
nvim.input('zA')
374+
self.assert_content('dir', ind=0, hi='dir')
375+
self.assert_content('subdir', level=1, ind=1, hi='dir')
376+
self.assert_content('subsubdir', level=2, ind=2, hi='dir')
377+
self.assert_content('placeholder', level=3, ind=3, hi='file')
378+
self.assert_content('subdir2', level=1, ind=4, hi='dir')
379+
self.assert_content('placeholder', level=2, ind=5, hi='file')
380+
self.assert_content('a', level=1, ind=6, hi='file')
381+
self.assert_content('dir2', level=0, ind=7, hi='dir')
382+
nvim.input('zA')
383+
self.assert_content('dir', ind=0, hi='dir')
384+
self.assert_content('dir2', ind=1, hi='dir')
385+
377386
def test_NETRToggleShowHidden(self):
378387
nvim.input('zh')
379388
self.assert_content('.a', ind=2, hi='file')
@@ -646,6 +655,20 @@ def test_opt_Autochdir(self):
646655

647656
nvim.vars['NETRAutochdir'] = default_value
648657

658+
def test_NETRToggleExpandRec(self):
659+
# Set foldnestmax=1 should behave exactly the same as NETRToggleExpand
660+
fdn = self.set_vim_window_option('foldnestmax', 1)
661+
nvim.input('zA')
662+
self.assert_content('dir', ind=0, hi='dir')
663+
self.assert_content('subdir', level=1, ind=1, hi='dir')
664+
self.assert_content('subdir2', ind=2, level=1, hi='dir')
665+
self.assert_content('a', ind=3, level=1, hi='file')
666+
self.assert_content('dir2', ind=4, hi='dir')
667+
nvim.input('zA')
668+
self.assert_content('dir', ind=0, hi='dir')
669+
self.assert_content('dir2', ind=1, hi='dir')
670+
self.set_vim_window_option('foldnestmax', fdn)
671+
649672

650673
class TestBuilitInFunctionsRemote(NetrangerRemoteTest):
651674
def test_NETREdit_remote(self):

0 commit comments

Comments
 (0)