Skip to content

Commit

Permalink
Improve find plugin (SpaceVim#3227)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdjeg authored Nov 16, 2019
1 parent 9678f3f commit 387288f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 79 deletions.
2 changes: 2 additions & 0 deletions autoload/SpaceVim/layers/core/statusline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ function! SpaceVim#layers#core#statusline#get(...) abort
elseif &filetype ==# 'defx'
return '%#SpaceVim_statusline_ia#' . s:winnr(1) . '%#SpaceVim_statusline_ia_SpaceVim_statusline_b#' . s:lsep
\ . '%#SpaceVim_statusline_b# defx %#SpaceVim_statusline_b_SpaceVim_statusline_c#' . s:lsep . ' '
elseif &filetype ==# 'SpaceVimFindArgv'
return '%#SpaceVim_statusline_a_bold# Find %#SpaceVim_statusline_a_SpaceVim_statusline_b#' . s:lsep
elseif &filetype ==# 'gista-list'
return '%#SpaceVim_statusline_ia#'
\ . s:winnr(1) . '%#SpaceVim_statusline_ia_SpaceVim_statusline_b#'
Expand Down
140 changes: 62 additions & 78 deletions autoload/SpaceVim/plugins/find.vim
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ let s:JOB = SpaceVim#api#import('job')
" let s:LIST = SpaceVim#api#import('data#list')
"}}}

let s:MPT._prompt.mpt = ' find '

let s:options_en = {
let s:support_tools = ['find', 'fd']
let s:current_tool = 'find'
let s:MPT._prompt.mpt = ' ' . s:current_tool . ' '
let s:options = {}
let s:second_option = {}
let s:options.find = {
\ '-amin' : 'File was last accessed n minutes ago.',
\ '-anewer' : 'File was last accessed more recently than file was modified.',
\ '-atime' : 'File was last accessed n*24 hours ago.',
Expand Down Expand Up @@ -71,63 +75,21 @@ let s:options_en = {
\ '-xdev' : '将范围局限在先行的文件系统中',
\ '-xtype' : '此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查'
\ }

let s:options = {
\ '-amin' : '查找在指定时间曾被存取过的文件或目录,单位以分钟计算',
\ '-anewer' : '查找其存取时间较指定文件或目录的存取时间更接近现在的文件或目录',
\ '-atime' : '查找在指定时间曾被存取过的文件或目录,单位以24小时计算',
\ '-cmin' : '查找在指定时间之时被更改过的文件或目录',
\ '-cnewer' : '查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录',
\ '-ctime' : '查找在指定时间之时被更改的文件或目录,单位以24小时计算',
\ '-daystart' : '从本日开始计算时间',
\ '-depth' : '从指定目录下最深层的子目录开始查找',
\ '-exec' : '假设find指令的回传值为True,就执行该指令',
\ '-false' : '将find指令的回传值皆设为False',
\ '-fls' : '此参数的效果和指定“-ls”参数类似,但会把结果保存为指定的列表文件',
\ '-follow' : '排除符号连接',
\ '-fprint' : '此参数的效果和指定“-print”参数类似,但会把结果保存成指定的列表文件',
\ '-fprintf' : '此参数的效果和指定“-printf”参数类似,但会把结果保存成指定的列表文件',
\ '-fstype' : '只寻找该文件系统类型下的文件或目录',
\ '-gid' : '查找符合指定之群组识别码的文件或目录',
\ '-group' : '查找符合指定之群组名称的文件或目录',
\ '-help' : '——help:在线帮助',
\ '-ilname' : '此参数的效果和指定“-lname”参数类似,但忽略字符大小写的差别',
\ '-iname' : '指定字符串作为寻找符号连接的范本样式',
\ '-inum' : '查找符合指定的inode编号的文件或目录',
\ '-ipath' : '此参数的效果和指定“-path”参数类似,但忽略字符大小写的差别',
\ '-iregex' : '此参数的效果和指定“-regexe”参数类似,但忽略字符大小写的差别',
\ '-links' : '查找符合指定的硬连接数目的文件或目录',
\ '-ls' : '假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出',
\ '-maxdepth' : '设置最大目录层级',
\ '-mindepth' : 'Do not apply any tests or actions at levels less than n.',
\ '-mmin' : '查找在指定时间曾被更改过的文件或目录,单位以分钟计算',
\ '-mount' : '此参数的效果和指定“-xdev”相同',
\ '-mtime' : '查找在指定时间曾被更改过的文件或目录,单位以24小时计算',
\ '-name' : '指定字符串作为寻找文件或目录的范本样式',
\ '-newer' : '查找其更改时间较指定文件或目录的更改时间更接近现在的文件或目录',
\ '-nogroup' : '找出不属于本地主机群组识别码的文件或目录',
\ '-noleaf' : '不去考虑目录至少需拥有两个硬连接存在',
\ '-nouser' : '找出不属于本地主机用户识别码的文件或目录',
\ '-ok' : '此参数的效果和指定“-exec”类似,但在执行指令之前会先询问用户,若回答“y”或“Y”,则放弃执行命令',
\ '-path' : '指定字符串作为寻找目录的范本样式',
\ '-perm' : '查找符合指定的权限数值的文件或目录',
\ '-print' : '假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为每列一个名称,每个名称前皆有“./”字符串',
\ '-print0' : '假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式为全部的名称皆在同一行',
\ '-printf' : '假设find指令的回传值为Ture,就将文件或目录名称列出到标准输出。格式可以自行指定',
\ '-prune' : '不寻找字符串作为寻找文件或目录的范本样式',
\ '-regex' : '指定字符串作为寻找文件或目录的范本样式',
\ '-size' : '查找符合指定的文件大小的文件',
\ '-true' : '将find指令的回传值皆设为True',
\ '-type' : '只寻找符合指定的文件类型的文件',
\ '-uid' : '查找符合指定的用户识别码的文件或目录',
\ '-used' : '查找文件或目录被更改之后在指定时间曾被存取过的文件或目录,单位以日计算',
\ '-user' : '查找符和指定的拥有者名称的文件或目录',
\ '-version' : '显示版本信息',
\ '-xdev' : '将范围局限在先行的文件系统中',
\ '-xtype' : '此参数的效果和指定“-type”参数类似,差别在于它针对符号连接检查'
let s:options.fd = {
\ '-H' : 'Search hidden files and directories',
\ '-I' : 'Do not respect .(git|fd)ignore files',
\ '-s' : 'Case-sensitive serch',
\ '-i' : 'Case-insensitive serch',
\ '-t' : 'Filter by type: file (f), directory (d), symlink (l), executable (x), empty (e)',
\ '-g' : 'Glob-based search',
\ '-d' : 'Set maximum search depth (default: none)',
\ '-L' : 'Follow symbolic links',
\ '-0' : 'Separate results by the null character',
\ '-F' : 'Treat the pattern as a literal string',
\ '-e' : 'Filter by file extension',
\ }

let s:second_option_en = {
let s:second_option.find = {
\ '-type' :
\ {
\ 'f' : 'regular file',
Expand All @@ -140,27 +102,29 @@ let s:second_option_en = {
\ },
\ }

let s:second_option = {
\ '-type' :
let s:second_option.fd = {
\ '-t' :
\ {
\ 'f' : '普通文件',
\ 'l' : '符号连接',
\ 'd' : '目录',
\ 'c' : '字符设备',
\ 'b' : '块设备',
\ 's' : '套接字',
\ 'p' : 'Fifo',
\ 'f' : 'regular file',
\ 'l' : 'symbolic link',
\ 'd' : 'directory',
\ 'x' : 'executable',
\ 'e' : 'empty',
\ },
\ }

let s:finded_files = []
function! s:start_find() abort
let cmd = 'find -not -iwholename "*.git*" ' . s:MPT._prompt.begin . s:MPT._prompt.cursor . s:MPT._prompt.end
if s:current_tool ==# 'find'
let cmd = 'find -not -iwholename "*.git*" ' . s:MPT._prompt.begin . s:MPT._prompt.cursor . s:MPT._prompt.end
elseif s:current_tool ==# 'fd'
let cmd = 'fd ' . s:MPT._prompt.begin . s:MPT._prompt.cursor . s:MPT._prompt.end
endif
call s:MPT._clear_prompt()
let s:MPT._quit = 1
let line = getline('.')
noautocmd q
redraw!
let s:finded_files = []
call s:JOB.start(cmd,
\ {
\ 'on_stdout' : function('s:find_on_stdout'),
Expand All @@ -174,7 +138,7 @@ function! s:find_on_stdout(id, data, event) abort
endfunction

function! s:find_on_exit(id, data, event) abort
let files = map(filter(s:finded_files, '!empty(v:val)'), "{'filename' : v:val}")
let files = map(filter(deepcopy(s:finded_files), '!empty(v:val)'), "{'filename' : v:val}")
if !empty(files)
call setqflist(files)
copen
Expand All @@ -195,8 +159,8 @@ function! s:next_item() abort
else
normal! j
endif
let argv = matchstr(getline('.'), '^-[a-zA-Z0-9]')
let s:MPT._prompt.begin = substitute(s:MPT._prompt.begin, '-[a-zA-Z]*$', argv, 'g')
let argv = matchstr(getline('.'), '[-a-zA-Z0-9]*')
let s:MPT._prompt.begin = substitute(s:MPT._prompt.begin, '[-a-zA-Z0-9]*$', argv, 'g')
redraw
call s:MPT._build_prompt()
endfunction
Expand All @@ -207,8 +171,8 @@ function! s:previous_item() abort
else
normal! k
endif
let argv = matchstr(getline('.'), '^-[a-zA-Z0-9]')
let s:MPT._prompt.begin = substitute(s:MPT._prompt.begin, '-[a-zA-Z]*$', argv, 'g')
let argv = matchstr(getline('.'), '[-a-zA-Z0-9]*')
let s:MPT._prompt.begin = substitute(s:MPT._prompt.begin, '[-a-zA-Z0-9]*$', argv, 'g')
redraw
call s:MPT._build_prompt()
endfunction
Expand All @@ -221,6 +185,7 @@ function! SpaceVim#plugins#find#open() abort
\ "\<C-j>" : function('s:next_item'),
\ "\<S-tab>" : function('s:previous_item'),
\ "\<C-k>" : function('s:previous_item'),
\ "\<C-e>" : function('s:switch_tool'),
\ }
noautocmd rightbelow split __spacevim_find_argv__
let s:find_argvs_buffer_id = bufnr('%')
Expand All @@ -229,22 +194,41 @@ function! SpaceVim#plugins#find#open() abort
call s:MPT.open()
endfunction

function! s:switch_tool() abort
normal! "_dG
if s:current_tool ==# 'find'
let s:current_tool = 'fd'
else
let s:current_tool = 'find'
endif
let s:MPT._prompt.begin = ''
let s:MPT._prompt.cursor = ''
let s:MPT._prompt.end = ''
let s:MPT._prompt.mpt = ' ' . s:current_tool . ' '
redraw
call s:MPT._build_prompt()
endfunction

function! s:enter() abort

endfunction

function! s:handle_command_line(cmd) abort
normal! "_dG
if empty(a:cmd)
if empty(s:MPT._prompt.begin)
redraw
call s:MPT._build_prompt()
return
endif
let argv = split(a:cmd)[-1]
if a:cmd[-1:] ==# ' ' && argv ==# '-type'
let argv = split(s:MPT._prompt.begin)[-1]
if s:MPT._prompt.begin[-1:] ==# ' ' && has_key(s:second_option[s:current_tool], argv)
let line = []
for item in items(s:second_option_en['-type'])
for item in items(s:second_option[s:current_tool][argv])
call add(line, ' ' . item[0] . repeat(' ', 8 - len(item[0])) . item[1])
endfor
call setline(1, line)
elseif argv =~# '^-[a-zA-Z0-1]*'
let argvs = filter(deepcopy(s:options_en), 'v:key =~ argv')
let argvs = filter(deepcopy(s:options[s:current_tool]), 'v:key =~ argv')
let line = []
for item in items(argvs)
call add(line, item[0] . repeat(' ', 15 - len(item[0])) . item[1])
Expand Down
2 changes: 1 addition & 1 deletion syntax/SpaceVimFindArgv.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ endif
let b:current_syntax = 'SpaceVimFindArgv'
syntax case ignore

syn match CMDFindArgvOpt /-[a-zA-Z]*\ /
syn match CMDFindArgvOpt /-[a-zA-Z0-9]*\ /
syn match CMDFindSecArgvOpt /^\s\+[a-z]\s\+/
syn match CMDFindArgvDesc /\(-[a-zA-Z0-1]*\ \)\@<=.*/
syn match CMDFindSecArgvDesc /\(^\s\+[a-z]\s\+\)\@<=.*/
Expand Down

0 comments on commit 387288f

Please sign in to comment.