Skip to content

Commit 685c5da

Browse files
authored
Merge pull request #288 from lambdalisue/fix-refresh-directory
Ignore and remove expanded directory on 'reload' action if the directory has removed outside of fern
2 parents 4ec2a38 + a8266bd commit 685c5da

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

autoload/fern/comparator/default.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ function! s:compare(n1, n2) abort
2626
endif
2727
endfor
2828
" Shorter first
29-
let r = s:comp(l1, l2)
30-
return r is# 0 ? s:comp(!a:n1.status, !a:n2.status) : r
31-
endfunction
32-
33-
function! s:comp(i1, i2) abort
34-
return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
29+
let r = fern#util#compare(l1, l2)
30+
if r isnot# 0
31+
return r
32+
endif
33+
" Leaf first
34+
return fern#util#compare(!a:n1.status, !a:n2.status)
3535
endfunction

autoload/fern/internal/node.vim

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,20 @@ function! fern#internal#node#reload(node, nodes, provider, comparator, token) ab
173173
let inner = s:Promise.resolve(copy(a:nodes))
174174
\.then(s:AsyncLambda.filter_f({ v -> K(v) == k }))
175175
\.then(s:AsyncLambda.filter_f({ v -> v.status is# s:STATUS_EXPANDED }))
176-
let descendants = inner
177-
\.then({v -> copy(v)})
178176
\.then(s:AsyncLambda.map_f({ v ->
179-
\ fern#internal#node#children(v, a:provider, a:token, { 'cache': 0 }).then({ children ->
180-
\ s:Lambda.if(v.status is# s:STATUS_EXPANDED, { -> children }, { -> []})
177+
\ fern#internal#node#children(v, a:provider, a:token, {
178+
\ 'cache': 0,
179+
\ }).then({ children ->
180+
\ s:Lambda.if(v.status is# s:STATUS_EXPANDED, { -> [v] + children }, { -> [v]})
181+
\ }).catch({ error ->
182+
\ s:Lambda.pass([], fern#logger#warn(error))
181183
\ })
182184
\ }))
183185
\.then({ v -> s:Promise.all(v) })
184186
\.then(s:AsyncLambda.reduce_f({ a, v -> a + v }, []))
187+
\.then({ v -> s:sort(v, { a, b -> fern#util#compare(b.status, a.status) }) })
185188
let l:Done = fern#internal#node#process(a:node)
186-
return s:Promise.all([outer, inner, descendants])
189+
return s:Promise.all([outer, inner])
187190
\.finally({ -> Profile('all') })
188191
\.then(s:AsyncLambda.reduce_f({ a, v -> a + v }, []))
189192
\.finally({ -> Profile('reduce') })

autoload/fern/util.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
let s:Promise = vital#fern#import('Async.Promise')
22

3+
function! fern#util#compare(i1, i2) abort
4+
return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
5+
endfunction
6+
37
function! fern#util#sleep(ms) abort
48
return s:Promise.new({ resolve -> timer_start(a:ms, { -> resolve() }) })
59
endfunction

doc/fern-develop.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@ fern#hook#emit({name}[, {args}...])
681681
call fern#hook#emit('t', 'Hello', 'World')
682682
" -> ['Hello', 'World']
683683
<
684+
*fern#util#compare()*
685+
fern#util#compare({i1}, {i2})
686+
Compare {i1} and {i2} and return -1, 0, or 1.
687+
This is equivalent to the following code
688+
>
689+
i1 == i2 ? 0 : i1 > i2 ? 1 : -1
690+
<
691+
684692
*fern#util#sleep()*
685693
fern#util#sleep({ms})
686694
Return a promise which will be resolved after {ms} milliseconds.

test/fern/internal/node.vimspec

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,57 @@ Describe fern#internal#node
237237
Assert Equals(r[6]._uri, '/shallow/gamma')
238238
Assert Equals(r[7]._uri, '/leaf')
239239
End
240+
241+
It keeps status of nodes
242+
let [r, e] = Promise.wait(
243+
\ fern#internal#node#reload(node, nodes, provider, Comparator, token),
244+
\ { 'timeout': TIMEOUT },
245+
\)
246+
Assert Equals(e, v:null)
247+
Assert Equals(len(r), 8)
248+
Assert Equals(r[0].status, g:fern#STATUS_EXPANDED)
249+
Assert Equals(r[1].status, g:fern#STATUS_COLLAPSED)
250+
Assert Equals(r[2].status, g:fern#STATUS_COLLAPSED)
251+
Assert Equals(r[3].status, g:fern#STATUS_EXPANDED)
252+
Assert Equals(r[4].status, g:fern#STATUS_COLLAPSED)
253+
Assert Equals(r[5].status, g:fern#STATUS_COLLAPSED)
254+
Assert Equals(r[6].status, g:fern#STATUS_NONE)
255+
Assert Equals(r[7].status, g:fern#STATUS_NONE)
256+
End
257+
258+
It resolves to a list of nodes (root)
259+
let [r, e] = Promise.wait(
260+
\ fern#internal#node#reload(nodes[0], nodes, provider, Comparator, token),
261+
\ { 'timeout': TIMEOUT },
262+
\)
263+
Assert Equals(e, v:null)
264+
Assert Equals(len(r), 8)
265+
Assert Equals(r[0]._uri, '/')
266+
Assert Equals(r[1]._uri, '/deep')
267+
Assert Equals(r[2]._uri, '/heavy')
268+
Assert Equals(r[3]._uri, '/shallow')
269+
Assert Equals(r[4]._uri, '/shallow/alpha')
270+
Assert Equals(r[5]._uri, '/shallow/beta')
271+
Assert Equals(r[6]._uri, '/shallow/gamma')
272+
Assert Equals(r[7]._uri, '/leaf')
273+
End
274+
275+
It keeps status of nodes (root)
276+
let [r, e] = Promise.wait(
277+
\ fern#internal#node#reload(nodes[0], nodes, provider, Comparator, token),
278+
\ { 'timeout': TIMEOUT },
279+
\)
280+
Assert Equals(e, v:null)
281+
Assert Equals(len(r), 8)
282+
Assert Equals(r[0].status, g:fern#STATUS_EXPANDED)
283+
Assert Equals(r[1].status, g:fern#STATUS_COLLAPSED)
284+
Assert Equals(r[2].status, g:fern#STATUS_COLLAPSED)
285+
Assert Equals(r[3].status, g:fern#STATUS_EXPANDED)
286+
Assert Equals(r[4].status, g:fern#STATUS_COLLAPSED)
287+
Assert Equals(r[5].status, g:fern#STATUS_COLLAPSED)
288+
Assert Equals(r[6].status, g:fern#STATUS_NONE)
289+
Assert Equals(r[7].status, g:fern#STATUS_NONE)
290+
End
240291
End
241292

242293
Describe #reveal()

0 commit comments

Comments
 (0)