|
| 1 | +let s:Promise = vital#fern#import('Async.Promise') |
| 2 | +let s:ESCAPE_PATTERN = '^$~.*[]\' |
| 3 | + |
| 4 | +function! fern#internal#replacer#start(factory, ...) abort |
| 5 | + let options = extend({ |
| 6 | + \ 'bufname': printf('fern-replacer:%s', sha256(localtime()))[:7], |
| 7 | + \ 'opener': 'vsplit', |
| 8 | + \ 'cursor': [1, 1], |
| 9 | + \ 'is_drawer': v:false, |
| 10 | + \ 'modifiers': [], |
| 11 | + \}, a:0 ? a:1 : {}, |
| 12 | + \) |
| 13 | + return s:Promise.new(funcref('s:executor', [a:factory, options])) |
| 14 | +endfunction |
| 15 | + |
| 16 | +function! s:executor(factory, options, resolve, reject) abort |
| 17 | + call fern#internal#buffer#open(a:options.bufname, { |
| 18 | + \ 'opener': a:options.opener, |
| 19 | + \ 'locator': a:options.is_drawer, |
| 20 | + \ 'keepalt': !a:options.is_drawer && g:fern#keepalt_on_edit, |
| 21 | + \ 'keepjumps': !a:options.is_drawer && g:fern#keepjumps_on_edit, |
| 22 | + \ 'mods': 'noautocmd', |
| 23 | + \}) |
| 24 | + |
| 25 | + setlocal buftype=acwrite bufhidden=wipe |
| 26 | + setlocal noswapfile nobuflisted |
| 27 | + setlocal nowrap |
| 28 | + setlocal filetype=fern-replacer |
| 29 | + |
| 30 | + let b:fern_replacer_resolve = a:resolve |
| 31 | + let b:fern_replacer_factory = a:factory |
| 32 | + let b:fern_replacer_candidates = a:factory() |
| 33 | + let b:fern_replacer_modifiers = a:options.modifiers |
| 34 | + |
| 35 | + augroup fern_replacer_internal |
| 36 | + autocmd! * <buffer> |
| 37 | + autocmd BufReadCmd <buffer> call s:BufReadCmd() |
| 38 | + autocmd BufWriteCmd <buffer> call s:BufWriteCmd() |
| 39 | + autocmd ColorScheme <buffer> call s:highlight() |
| 40 | + augroup END |
| 41 | + |
| 42 | + call s:highlight() |
| 43 | + call s:syntax() |
| 44 | + |
| 45 | + " Do NOT allow to add/remove lines |
| 46 | + nnoremap <buffer><silent> <Plug>(fern-replacer-p) :<C-u>call <SID>map_paste(0)<CR> |
| 47 | + nnoremap <buffer><silent> <Plug>(fern-replacer-P) :<C-u>call <SID>map_paste(-1)<CR> |
| 48 | + nnoremap <buffer><silent> <Plug>(fern-replacer-warn) :<C-u>call <SID>map_warn()<CR> |
| 49 | + inoremap <buffer><silent><expr> <Plug>(fern-replacer-warn) <SID>map_warn() |
| 50 | + nnoremap <buffer><silent> dd 0D |
| 51 | + nmap <buffer> p <Plug>(fern-replacer-p) |
| 52 | + nmap <buffer> P <Plug>(fern-replacer-P) |
| 53 | + nmap <buffer> o <Plug>(fern-replacer-warn) |
| 54 | + nmap <buffer> O <Plug>(fern-replacer-warn) |
| 55 | + imap <buffer> <C-m> <Plug>(fern-replacer-warn) |
| 56 | + imap <buffer> <Return> <Plug>(fern-replacer-warn) |
| 57 | + edit |
| 58 | + call cursor(a:options.cursor) |
| 59 | +endfunction |
| 60 | + |
| 61 | +function! s:map_warn() abort |
| 62 | + echohl WarningMsg |
| 63 | + echo 'Newline is prohibited in the replacer buffer' |
| 64 | + echohl None |
| 65 | + return '' |
| 66 | +endfunction |
| 67 | + |
| 68 | +function! s:map_paste(offset) abort |
| 69 | + let line = getline('.') |
| 70 | + let v = substitute(getreg(), '\r\?\n', '', 'g') |
| 71 | + let c = col('.') + a:offset - 1 |
| 72 | + let l = line[:c] |
| 73 | + let r = line[c + 1:] |
| 74 | + call setline(line('.'), l . v . r) |
| 75 | +endfunction |
| 76 | + |
| 77 | +function! s:BufReadCmd() abort |
| 78 | + let b:fern_replacer_candidates = b:fern_replacer_factory() |
| 79 | + call s:syntax() |
| 80 | + call setline(1, b:fern_replacer_candidates) |
| 81 | +endfunction |
| 82 | + |
| 83 | +function! s:BufWriteCmd() abort |
| 84 | + if !&modifiable |
| 85 | + return |
| 86 | + endif |
| 87 | + let candidates = b:fern_replacer_candidates |
| 88 | + let result = [] |
| 89 | + for index in range(len(candidates)) |
| 90 | + let src = candidates[index] |
| 91 | + let dst = getline(index + 1) |
| 92 | + if empty(dst) || dst ==# src |
| 93 | + continue |
| 94 | + endif |
| 95 | + call add(result, [src, dst]) |
| 96 | + endfor |
| 97 | + try |
| 98 | + for Modifier in b:fern_replacer_modifiers |
| 99 | + let result = Modifier(result) |
| 100 | + endfor |
| 101 | + let Resolve = b:fern_replacer_resolve |
| 102 | + set nomodified |
| 103 | + close |
| 104 | + call Resolve(result) |
| 105 | + catch |
| 106 | + echohl ErrorMsg |
| 107 | + echo '[fern] Please fix the following error first to continue or cancel with ":q!"' |
| 108 | + echo printf('[fern] %s', substitute(v:exception, '^Vim(.*):', '', '')) |
| 109 | + echohl None |
| 110 | + endtry |
| 111 | +endfunction |
| 112 | + |
| 113 | +function! s:syntax() abort |
| 114 | + syntax clear |
| 115 | + syntax match FernReplacerModified '^.\+$' |
| 116 | + |
| 117 | + for index in range(len(b:fern_replacer_candidates)) |
| 118 | + let candidate = b:fern_replacer_candidates[index] |
| 119 | + execute printf( |
| 120 | + \ 'syntax match FernReplacerOriginal ''^\%%%dl%s$''', |
| 121 | + \ index + 1, |
| 122 | + \ escape(candidate, s:ESCAPE_PATTERN), |
| 123 | + \) |
| 124 | + endfor |
| 125 | +endfunction |
| 126 | + |
| 127 | +function! s:highlight() abort |
| 128 | + highlight default link FernReplacerOriginal Normal |
| 129 | + highlight default link FernReplacerModified Special |
| 130 | +endfunction |
0 commit comments