Skip to content

Commit b29c0a5

Browse files
committed
code factorization
1 parent a85ce87 commit b29c0a5

File tree

1 file changed

+20
-26
lines changed

1 file changed

+20
-26
lines changed

plugin/ranger.vim

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,39 @@ if !exists('s:choice_file_path')
4444
let s:choice_file_path = '/tmp/chosenfile'
4545
endif
4646

47+
function! EditFiles(cmd)
48+
try
49+
if filereadable(s:choice_file_path)
50+
for f in readfile(s:choice_file_path)
51+
exec a:cmd . split(f, getcwd() . '/')[0]
52+
endfor
53+
call delete(s:choice_file_path)
54+
endif
55+
endtry
56+
endfunction
57+
58+
function! GetRangerCmd(path)
59+
return s:ranger_command . ' --choosefiles=' . s:choice_file_path .
60+
\ (isdirectory(a:path) ? ' "' : ' --selectfile="') . expand(a:path) . '"'
61+
endfunction
62+
4763
if has('nvim')
4864
function! OpenRangerIn(path, edit_cmd)
49-
let currentPath = expand(a:path)
5065
let rangerCallback = { 'name': 'ranger', 'edit_cmd': a:edit_cmd }
5166
function! rangerCallback.on_exit(job_id, code, event)
5267
if a:code == 0
5368
silent! Bclose!
5469
endif
55-
try
56-
if filereadable(s:choice_file_path)
57-
for f in readfile(s:choice_file_path)
58-
exec self.edit_cmd . split(f, getcwd() . '/')[0]
59-
endfor
60-
call delete(s:choice_file_path)
61-
endif
62-
endtry
70+
call EditFiles(self.edit_cmd)
6371
endfunction
6472
enew
65-
if isdirectory(currentPath)
66-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"', rangerCallback)
67-
else
68-
call termopen(s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"', rangerCallback)
69-
endif
73+
call termopen(GetRangerCmd(a:path), rangerCallback)
7074
startinsert
7175
endfunction
7276
else
7377
function! OpenRangerIn(path, edit_cmd)
74-
let currentPath = expand(a:path)
75-
if isdirectory(currentPath)
76-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' "' . currentPath . '"'
77-
else
78-
silent exec '!' . s:ranger_command . ' --choosefiles=' . s:choice_file_path . ' --selectfile="' . currentPath . '"'
79-
endif
80-
if filereadable(s:choice_file_path)
81-
for f in readfile(s:choice_file_path)
82-
exec a:edit_cmd . split(f, getcwd() . '/')[0]
83-
endfor
84-
call delete(s:choice_file_path)
85-
endif
78+
silent exec '!' . GetRangerCmd(a:path)
79+
call EditFiles(a:edit_cmd)
8680
redraw!
8781
" reset the filetype to fix the issue that happens
8882
" when opening ranger on VimEnter (with `vim .`)

0 commit comments

Comments
 (0)