forked from liuchengxu/vim-clap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiler.vim
328 lines (286 loc) · 8.82 KB
/
filer.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
" Author: liuchengxu <[email protected]>
" Description: Ivy-like file explorer.
scriptencoding utf-8
let s:save_cpo = &cpoptions
set cpoptions&vim
let s:filer = {}
let s:PATH_SEPERATOR = has('win32') && !(exists('+shellslash') && &shellslash) ? '\' : '/'
let s:DIRECTORY_IS_EMPTY = (g:clap_enable_icon ? ' ' : '').'Directory is empty'
function! clap#provider#filer#hi_empty_dir() abort
syntax match ClapEmptyDirectory /^.*Directory is empty/
hi default link ClapEmptyDirectory WarningMsg
endfunction
function! s:handle_error(error) abort
let s:filer_error_cache[a:error.dir] = a:error.message
call g:clap.preview.show([a:error.message])
endfunction
function! s:handle_response(result, error) abort
if a:error isnot v:null
call s:handle_error(a:error)
return
endif
if a:result.total == 0
let s:filer_empty_cache[a:result.dir] = s:DIRECTORY_IS_EMPTY
call g:clap.display.set_lines([s:DIRECTORY_IS_EMPTY])
else
let s:filer_cache[a:result.dir] = a:result.entries
call g:clap.display.set_lines(a:result.entries)
endif
call clap#sign#reset_to_first_line()
call clap#state#refresh_matches_count(string(a:result.total))
call g:clap#display_win.shrink_if_undersize()
endfunction
function! s:set_prompt() abort
if strlen(s:current_dir) < s:winwidth * 3 / 4
call clap#spinner#set(s:current_dir)
else
let parent = fnamemodify(s:current_dir, ':p:h')
let last = fnamemodify(s:current_dir, ':p:t')
let short_dir = pathshorten(parent).s:PATH_SEPERATOR.last
if strlen(short_dir) < s:winwidth * 3 / 4
call clap#spinner#set(short_dir)
else
call clap#spinner#set(pathshorten(s:current_dir))
endif
endif
endfunction
if has('win32')
function! s:is_root_directory(dir) abort
return a:dir =~? '^\([a-z]:\|\(\\\\\|\/\/\)[^\\\/]\+\(\\\|\/\/\)[^\\\/]\+\)\(\\\|\/\)\+$'
endfunction
else
function! s:is_root_directory(dir) abort
return a:dir ==# s:PATH_SEPERATOR
endfunction
endif
function! s:goto_parent() abort
" The root directory
if s:is_root_directory(s:current_dir)
return
endif
if s:current_dir[-1:] ==# s:PATH_SEPERATOR
let parent_dir = fnamemodify(s:current_dir, ':h:h')
else
let parent_dir = fnamemodify(s:current_dir, ':h')
endif
if s:is_root_directory(parent_dir)
let s:current_dir = parent_dir
else
let s:current_dir = parent_dir.s:PATH_SEPERATOR
endif
call s:set_prompt()
call s:filter_or_send_message()
endfunction
function! s:filter_or_send_message() abort
call g:clap.preview.hide()
if has_key(s:filer_cache, s:current_dir)
call s:do_filter()
else
call clap#client#call('filer', function('s:handle_response'), {'cwd': s:current_dir})
endif
endfunction
if has('nvim')
function! s:bs_action() abort
call clap#highlight#clear()
let input = g:clap.input.get()
if input ==# ''
call s:goto_parent()
else
call g:clap.input.set(input[:-2])
call s:filter_or_send_message()
endif
return ''
endfunction
else
function! s:bs_action(before_bs) abort
call clap#highlight#clear()
if a:before_bs ==# ''
call s:goto_parent()
else
call s:filter_or_send_message()
endif
return ''
endfunction
endif
function! s:do_filter() abort
let query = g:clap.input.get()
let candidates = s:filer_cache[s:current_dir]
if query ==# ''
call g:clap.display.set_lines(candidates)
call g:clap#display_win.shrink_if_undersize()
else
call clap#filter#on_typed(function('clap#filter#sync'), query, candidates)
endif
endfunction
function! s:reset_to(new_dir) abort
let s:current_dir = a:new_dir
call s:set_prompt()
call clap#highlight#clear()
call g:clap.input.set('')
call s:filter_or_send_message()
endfunction
function! s:get_current_entry() abort
let curline = g:clap.display.getcurline()
if g:clap_enable_icon
let curline = curline[4:]
endif
return s:smart_concatenate(s:current_dir, curline)
endfunction
function! s:try_go_to_dir_is_ok() abort
let input = g:clap.input.get()
if input[-1:] ==# s:PATH_SEPERATOR
if isdirectory(expand(input))
call s:reset_to(expand(input))
return v:true
endif
endif
return v:false
endfunction
function! s:tab_action() abort
if s:try_go_to_dir_is_ok()
return
endif
if exists('g:__clap_has_no_matches') && g:__clap_has_no_matches
return
endif
if has_key(s:filer_error_cache, s:current_dir)
call g:clap.display.set_lines([s:filer_error_cache[s:current_dir]])
return
endif
if has_key(s:filer_empty_cache, s:current_dir)
if g:clap.display.get_lines() != [s:DIRECTORY_IS_EMPTY]
call g:clap.display.set_lines([s:DIRECTORY_IS_EMPTY])
endif
return
endif
let current_entry = s:get_current_entry()
if filereadable(current_entry)
call clap#preview#file(current_entry)
return ''
else
call g:clap.preview.hide()
endif
call s:reset_to(current_entry)
call clap#sign#ensure_exists()
return ''
endfunction
function! s:smart_concatenate(cur_dir, curline) abort
if a:cur_dir[-1:] ==# s:PATH_SEPERATOR
return a:cur_dir.a:curline
else
return a:cur_dir.s:PATH_SEPERATOR.a:curline
endif
endfunction
function! s:filer_sink(selected) abort
let curline = g:clap_enable_icon ? a:selected[4:] : a:selected
execute 'edit' s:smart_concatenate(s:current_dir, curline)
endfunction
function! s:filer_on_typed() abort
" <Tab> and <Backspace> also trigger the CursorMoved event.
" s:filter_or_send_message() is already handled in tab and bs action,
" on_typed handler only needs to take care of the filtering.
if exists('s:filer_cache') && has_key(s:filer_cache, s:current_dir)
let cur_input = g:clap.input.get()
if cur_input ==# s:last_input
return
endif
let s:last_input = cur_input
call clap#highlight#clear()
call s:do_filter()
endif
return ''
endfunction
" Deprecated now.
function! s:sync_on_move_impl() abort
let current_entry = s:get_current_entry()
if filereadable(current_entry)
call clap#preview#file(current_entry)
else
call g:clap.preview.hide()
endif
endfunction
function! s:filer_handle_on_move_response(result, error) abort
if a:error isnot v:null
call s:handle_error(a:error)
return
endif
if empty(a:result.lines)
call g:clap.preview.show(['Empty entries'])
else
call g:clap.preview.show(a:result.lines)
if has_key(a:result, 'is_dir')
call g:clap.preview.set_syntax('clap_filer')
call clap#preview#clear_header_highlight()
else
if has_key(a:result, 'fname')
call g:clap.preview.set_syntax(clap#ext#into_filetype(a:result.fname))
endif
call clap#preview#highlight_header()
endif
endif
endfunction
function! s:filer.on_move_async() abort
call clap#client#call_on_move('filer/on_move', function('s:filer_handle_on_move_response'), {'cwd': s:current_dir})
endfunction
function! s:filer_on_no_matches(input) abort
execute 'edit' s:smart_concatenate(s:current_dir, a:input)
endfunction
if has('win32')
function! s:normalize_path_sep(path) abort
return substitute(a:path, '[/\\]',s:PATH_SEPERATOR, 'g')
endfunction
else
function! s:normalize_path_sep(path) abort
return a:path
endfunction
endif
function! s:set_initial_current_dir() abort
if empty(g:clap.provider.args)
let s:current_dir = getcwd()
if s:current_dir[-1:] !=# s:PATH_SEPERATOR
let s:current_dir = s:current_dir.s:PATH_SEPERATOR
endif
return
endif
let maybe_dir = g:clap.provider.args[0]
" %:p:h, % is actually g:clap.start.bufnr
if maybe_dir =~# '^%.\+'
let m = matchstr(maybe_dir, '^%\zs\(.*\)')
let target_dir = fnamemodify(bufname(g:clap.start.bufnr), m)
elseif isdirectory(expand(maybe_dir))
let target_dir = maybe_dir
else
let s:current_dir = getcwd()
if s:current_dir[-1:] !=# s:PATH_SEPERATOR
let s:current_dir = s:current_dir.s:PATH_SEPERATOR
endif
return
endif
let target_dir = s:normalize_path_sep(expand(target_dir))
if target_dir[-1:] ==# s:PATH_SEPERATOR
let s:current_dir = target_dir
else
let s:current_dir = target_dir.s:PATH_SEPERATOR
endif
endfunction
function! s:start_rpc_service() abort
let s:filer_cache = {}
let s:filer_error_cache = {}
let s:filer_empty_cache = {}
let s:last_input = ''
let s:winwidth = winwidth(g:clap.display.winid)
call s:set_initial_current_dir()
call s:set_prompt()
call clap#client#call_on_init('filer/on_init', function('s:handle_response'), {'cwd': s:current_dir})
endfunction
let s:filer.init = function('s:start_rpc_service')
let s:filer.sink = function('s:filer_sink')
let s:filer.syntax = 'clap_filer'
let s:filer.on_typed = function('s:filer_on_typed')
let s:filer.bs_action = function('s:bs_action')
let s:filer.tab_action = function('s:tab_action')
let s:filer.source_type = g:__t_rpc
let s:filer.on_no_matches = function('s:filer_on_no_matches')
let g:clap#provider#filer# = s:filer
let &cpoptions = s:save_cpo
unlet s:save_cpo