|
| 1 | +let s:Promise = vital#fern#import('Async.Promise') |
| 2 | +let s:timer_diffupdate = 0 |
| 3 | + |
| 4 | +function! fern#mapping#diff#init(disable_default_mappings) abort |
| 5 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:select) :<C-u>call <SID>call('diff', 'select', v:false)<CR> |
| 6 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:split) :<C-u>call <SID>call('diff', 'split', v:false)<CR> |
| 7 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:vsplit) :<C-u>call <SID>call('diff', 'vsplit', v:false)<CR> |
| 8 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:tabedit) :<C-u>call <SID>call('diff', 'tabedit', v:false)<CR> |
| 9 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:above) :<C-u>call <SID>call('diff', 'leftabove split', v:false)<CR> |
| 10 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:left) :<C-u>call <SID>call('diff', 'leftabove vsplit', v:false)<CR> |
| 11 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:below) :<C-u>call <SID>call('diff', 'rightbelow split', v:false)<CR> |
| 12 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:right) :<C-u>call <SID>call('diff', 'rightbelow vsplit', v:false)<CR> |
| 13 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:top) :<C-u>call <SID>call('diff', 'topleft split', v:false)<CR> |
| 14 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:leftest) :<C-u>call <SID>call('diff', 'topleft vsplit', v:false)<CR> |
| 15 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:bottom) :<C-u>call <SID>call('diff', 'botright split', v:false)<CR> |
| 16 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:rightest) :<C-u>call <SID>call('diff', 'botright vsplit', v:false)<CR> |
| 17 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-error) :<C-u>call <SID>call('diff', 'edit', v:false)<CR> |
| 18 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-split) :<C-u>call <SID>call('diff', 'edit/split', v:false)<CR> |
| 19 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-vsplit) :<C-u>call <SID>call('diff', 'edit/vsplit', v:false)<CR> |
| 20 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-tabedit) :<C-u>call <SID>call('diff', 'edit/tabedit', v:false)<CR> |
| 21 | +
|
| 22 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:select:vert) :<C-u>call <SID>call('diff', 'select', v:true)<CR> |
| 23 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:split:vert) :<C-u>call <SID>call('diff', 'split', v:true)<CR> |
| 24 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:vsplit:vert) :<C-u>call <SID>call('diff', 'vsplit', v:true)<CR> |
| 25 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:tabedit:vert) :<C-u>call <SID>call('diff', 'tabedit', v:true)<CR> |
| 26 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:above:vert) :<C-u>call <SID>call('diff', 'leftabove split', v:true)<CR> |
| 27 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:left:vert) :<C-u>call <SID>call('diff', 'leftabove vsplit', v:true)<CR> |
| 28 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:below:vert) :<C-u>call <SID>call('diff', 'rightbelow split', v:true)<CR> |
| 29 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:right:vert) :<C-u>call <SID>call('diff', 'rightbelow vsplit', v:true)<CR> |
| 30 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:top:vert) :<C-u>call <SID>call('diff', 'topleft split', v:true)<CR> |
| 31 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:leftest:vert) :<C-u>call <SID>call('diff', 'topleft vsplit', v:true)<CR> |
| 32 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:bottom:vert) :<C-u>call <SID>call('diff', 'botright split', v:true)<CR> |
| 33 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:rightest:vert) :<C-u>call <SID>call('diff', 'botright vsplit', v:true)<CR> |
| 34 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-error:vert) :<C-u>call <SID>call('diff', 'edit', v:true)<CR> |
| 35 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-split:vert) :<C-u>call <SID>call('diff', 'edit/split', v:true)<CR> |
| 36 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-vsplit:vert) :<C-u>call <SID>call('diff', 'edit/vsplit', v:true)<CR> |
| 37 | + nnoremap <buffer><silent> <Plug>(fern-action-diff:edit-or-tabedit:vert) :<C-u>call <SID>call('diff', 'edit/tabedit', v:true)<CR> |
| 38 | +
|
| 39 | + " Smart map |
| 40 | + nmap <buffer><silent><expr> |
| 41 | + \ <Plug>(fern-action-diff:side) |
| 42 | + \ fern#smart#drawer( |
| 43 | + \ "\<Plug>(fern-action-diff:left)", |
| 44 | + \ "\<Plug>(fern-action-diff:right)", |
| 45 | + \ ) |
| 46 | + nmap <buffer><silent><expr> |
| 47 | + \ <Plug>(fern-action-diff:side:vert) |
| 48 | + \ fern#smart#drawer( |
| 49 | + \ "\<Plug>(fern-action-diff:left:vert)", |
| 50 | + \ "\<Plug>(fern-action-diff:right:vert)", |
| 51 | + \ ) |
| 52 | +
|
| 53 | + " Alias map |
| 54 | + nmap <buffer><silent> <Plug>(fern-action-diff:edit) <Plug>(fern-action-diff:edit-or-error) |
| 55 | + nmap <buffer><silent> <Plug>(fern-action-diff:edit:vert) <Plug>(fern-action-diff:edit-or-error:vert) |
| 56 | + nmap <buffer><silent> <Plug>(fern-action-diff) <Plug>(fern-action-diff:edit) |
| 57 | + nmap <buffer><silent> <Plug>(fern-action-diff:vert) <Plug>(fern-action-diff:edit:vert) |
| 58 | +endfunction |
| 59 | + |
| 60 | +function! s:call(name, ...) abort |
| 61 | + return call( |
| 62 | + \ 'fern#mapping#call', |
| 63 | + \ [funcref(printf('s:map_%s', a:name))] + a:000, |
| 64 | + \) |
| 65 | +endfunction |
| 66 | + |
| 67 | +function! s:map_diff(helper, opener, vert) abort |
| 68 | + let nodes = a:helper.sync.get_selected_nodes() |
| 69 | + let nodes = filter(copy(nodes), { -> v:val.bufname isnot# v:null }) |
| 70 | + if empty(nodes) |
| 71 | + return s:Promise.reject('no node found which has bufname') |
| 72 | + elseif len(nodes) < 2 |
| 73 | + return s:Promise.reject('at least two nodes are required to perform diff') |
| 74 | + endif |
| 75 | + try |
| 76 | + let is_drawer = a:helper.sync.is_drawer() |
| 77 | + let first = nodes[0] |
| 78 | + let nodes = nodes[1:] |
| 79 | + call fern#internal#buffer#open(first.bufname, { |
| 80 | + \ 'opener': a:opener, |
| 81 | + \ 'locator': is_drawer, |
| 82 | + \ 'keepalt': !is_drawer && g:fern#keepalt_on_edit, |
| 83 | + \ 'keepjumps': !is_drawer && g:fern#keepjumps_on_edit, |
| 84 | + \}) |
| 85 | + call s:diffthis() |
| 86 | + let winid = win_getid() |
| 87 | + for node in nodes |
| 88 | + noautocmd call win_gotoid(winid) |
| 89 | + call fern#internal#buffer#open(node.bufname, { |
| 90 | + \ 'opener': a:vert ? 'vsplit' : 'split', |
| 91 | + \ 'locator': is_drawer, |
| 92 | + \ 'keepalt': !is_drawer && g:fern#keepalt_on_edit, |
| 93 | + \ 'keepjumps': !is_drawer && g:fern#keepjumps_on_edit, |
| 94 | + \}) |
| 95 | + call s:diffthis() |
| 96 | + endfor |
| 97 | + call s:diffupdate() |
| 98 | + normal! zm |
| 99 | + " Fix <C-w><C-p> (#47) |
| 100 | + let winid_fern = win_getid() |
| 101 | + noautocmd call win_gotoid(winid) |
| 102 | + noautocmd call win_gotoid(winid_fern) |
| 103 | + return a:helper.async.update_marks([]) |
| 104 | + \.then({ -> a:helper.async.remark() }) |
| 105 | + catch |
| 106 | + return s:Promise.reject(v:exception) |
| 107 | + endtry |
| 108 | +endfunction |
| 109 | + |
| 110 | +function! s:diffthis() abort |
| 111 | + diffthis |
| 112 | + augroup fern_mapping_diff_internal |
| 113 | + autocmd! * <buffer> |
| 114 | + autocmd BufReadPost <buffer> |
| 115 | + \ if &diff && &foldmethod !=# 'diff' | |
| 116 | + \ setlocal foldmethod=diff | |
| 117 | + \ endif |
| 118 | + augroup END |
| 119 | +endfunction |
| 120 | + |
| 121 | +function! s:diffupdate() abort |
| 122 | + " NOTE: |
| 123 | + " 'diffupdate' does not work just after a buffer has opened |
| 124 | + " so use timer to delay the command. |
| 125 | + silent! call timer_stop(s:timer_diffupdate) |
| 126 | + let s:timer_diffupdate = timer_start(100, function('s:diffupdate_internal', [bufnr('%')])) |
| 127 | +endfunction |
| 128 | + |
| 129 | +function! s:diffupdate_internal(bufnr, ...) abort |
| 130 | + let winid = bufwinid(a:bufnr) |
| 131 | + if winid == -1 |
| 132 | + return |
| 133 | + endif |
| 134 | + let winid_saved = win_getid() |
| 135 | + try |
| 136 | + if winid != winid_saved |
| 137 | + call win_gotoid(winid) |
| 138 | + endif |
| 139 | + diffupdate |
| 140 | + syncbind |
| 141 | + finally |
| 142 | + call win_gotoid(winid_saved) |
| 143 | + endtry |
| 144 | +endfunction |
0 commit comments