1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-13 02:05:40 +08:00

flygrep replace mode (#3090)

This commit is contained in:
Wang Shidong 2019-09-25 22:15:43 +08:00 committed by GitHub
parent 906d76d11f
commit e651113cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 7 deletions

View File

@ -52,7 +52,7 @@ function! s:update_history() abort
endif endif
call add(s:grep_history, s:grep_expr) call add(s:grep_history, s:grep_expr)
if !isdirectory(expand('~/.cache/SpaceVim')) if !isdirectory(expand('~/.cache/SpaceVim'))
call mkdir(expand('~/.cache/SpaceVim')) call mkdir(expand('~/.cache/SpaceVim'))
endif endif
call writefile([s:JSON.json_encode(s:grep_history)], expand('~/.cache/SpaceVim/flygrep_history')) call writefile([s:JSON.json_encode(s:grep_history)], expand('~/.cache/SpaceVim/flygrep_history'))
endfunction endfunction
@ -243,15 +243,62 @@ function! s:start_replace() abort
call matchdelete(s:hi_id) call matchdelete(s:hi_id)
catch catch
endtr endtr
if s:grepid != 0
call s:JOB.stop(s:grepid)
endif
let replace_text = s:current_grep_pattern let replace_text = s:current_grep_pattern
if !empty(replace_text) if !empty(replace_text)
call SpaceVim#plugins#iedit#start({'expr' : replace_text}, line('w0'), line('w$')) let rst = SpaceVim#plugins#iedit#start({'expr' : replace_text}, line('w0'), line('w$'))
endif endif
let s:hi_id = s:matchadd('FlyGrepPattern', s:expr_to_pattern(replace_text), 2) let s:hi_id = s:matchadd('FlyGrepPattern', s:expr_to_pattern(rst), 2)
redrawstatus redrawstatus
if rst !=# replace_text
call s:update_files(s:flygrep_result_to_files())
checktime
endif
endfunction endfunction
" }}} " }}}
function! s:flygrep_result_to_files() abort
let files = []
for line in getbufline(s:flygrep_buffer_id, 1, '$')
let filename = fnameescape(split(line, ':\d\+:')[0])
let linenr = matchstr(line, ':\d\+:')[1:-2]
let str = matchstr(line, '\(:\d\+:\d\+:\)\@<=.*')
call add(files, [filename, linenr, str])
endfor
return files
endfunction
function! s:update_files(files) abort
let fname = ''
let lines = {}
for file in a:files
if file[0] == fname
call extend(lines, {file[1] : file[2]})
else
if !empty(fname)
call s:update_file(fname, lines)
endif
let fname = file[0]
let lines = {}
call extend(lines, {file[1] : file[2]})
endif
endfor
if !empty(fname)
call s:update_file(fname, lines)
endif
endfunction
function! s:update_file(fname, lines) abort
let contents = readfile(a:fname, '')
for linenr in keys(a:lines)
let contents[linenr - 1] = a:lines[ linenr ]
endfor
call writefile(contents, a:fname, '')
endfunction
" API: MPT._prompt {{{ " API: MPT._prompt {{{
let s:MPT._prompt.mpt = g:spacevim_commandline_prompt . ' ' let s:MPT._prompt.mpt = g:spacevim_commandline_prompt . ' '
" }}} " }}}

View File

@ -118,7 +118,7 @@ function! SpaceVim#plugins#iedit#start(...) abort
if s:mode ==# 'n' && char == 27 if s:mode ==# 'n' && char == 27
let s:mode = '' let s:mode = ''
else else
call s:handle(s:mode, char) let symbol = s:handle(s:mode, char)
endif endif
endwhile endwhile
let s:stack = [] let s:stack = []
@ -135,14 +135,15 @@ function! SpaceVim#plugins#iedit#start(...) abort
endtry endtry
let s:hi_id = '' let s:hi_id = ''
let &l:cursorline = save_cl let &l:cursorline = save_cl
return symbol
endfunction endfunction
function! s:handle(mode, char) abort function! s:handle(mode, char) abort
if a:mode ==# 'n' if a:mode ==# 'n'
call s:handle_normal(a:char) return s:handle_normal(a:char)
elseif a:mode ==# 'i' elseif a:mode ==# 'i'
call s:handle_insert(a:char) return s:handle_insert(a:char)
endif endif
endfunction endfunction
@ -329,6 +330,7 @@ function! s:handle_normal(char) abort
call cursor(s:stack[s:index][0], s:stack[s:index][1] + len(s:cursor_stack[s:index].begin)) call cursor(s:stack[s:index][0], s:stack[s:index][1] + len(s:cursor_stack[s:index].begin))
endif endif
silent! call s:highlight_cursor() silent! call s:highlight_cursor()
return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end
endfunction endfunction
if exists('*timer_start') if exists('*timer_start')
@ -356,7 +358,7 @@ function! s:handle_insert(char) abort
silent! call s:highlight_cursor() silent! call s:highlight_cursor()
redraw! redraw!
redrawstatus! redrawstatus!
return return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end
elseif a:char ==# 23 elseif a:char ==# 23
" ctrl-w: delete word before cursor " ctrl-w: delete word before cursor
for i in range(len(s:cursor_stack)) for i in range(len(s:cursor_stack))
@ -447,6 +449,7 @@ function! s:handle_insert(char) abort
call s:replace_symbol() call s:replace_symbol()
endif endif
silent! call s:highlight_cursor() silent! call s:highlight_cursor()
return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end
endfunction endfunction
function! s:parse_symbol(begin, end, symbol, ...) abort function! s:parse_symbol(begin, end, symbol, ...) abort
let use_expr = get(a:000, 0, 0) let use_expr = get(a:000, 0, 0)