1
0
mirror of https://github.com/amix/vimrc synced 2025-06-23 15:04:59 +08:00

Updated plugins

This commit is contained in:
Amir Salihefendic
2019-11-16 16:28:42 +01:00
parent 96e10ed101
commit 72bdaba47e
204 changed files with 5936 additions and 1666 deletions

View File

@ -187,7 +187,7 @@ function! gitgutter#diff#handler(bufnr, diff) abort
call gitgutter#sign#clear_signs(a:bufnr)
else
if g:gitgutter_signs || g:gitgutter_highlight_lines
if g:gitgutter_signs || g:gitgutter_highlight_lines || g:gitgutter_highlight_linenrs
call gitgutter#sign#update_signs(a:bufnr, modified_lines)
endif
endif
@ -385,6 +385,10 @@ function! s:write_buffer(bufnr, file)
call map(bufcontents, 'v:val."\r"')
endif
if getbufvar(a:bufnr, '&endofline')
call add(bufcontents, '')
endif
let fenc = getbufvar(a:bufnr, '&fileencoding')
if fenc !=# &encoding
call map(bufcontents, 'iconv(v:val, &encoding, "'.fenc.'")')
@ -394,7 +398,7 @@ function! s:write_buffer(bufnr, file)
let bufcontents[0]=''.bufcontents[0]
endif
call writefile(bufcontents, a:file)
call writefile(bufcontents, a:file, 'b')
endfunction

View File

@ -0,0 +1,225 @@
" This is the minimum number of characters required between regions of change
" in a line. It's somewhat arbitrary: higher values mean less visual busyness;
" lower values mean more detail.
let s:gap_between_regions = 5
" Calculates the changed portions of lines.
"
" Based on:
"
" - diff-highlight (included with git)
" https://github.com/git/git/blob/master/contrib/diff-highlight/DiffHighlight.pm
"
" - Diff Strategies, Neil Fraser
" https://neil.fraser.name/writing/diff/
" Returns a list of intra-line changed regions.
" Each element is a list:
"
" [
" line number (1-based),
" type ('+' or '-'),
" start column (1-based, inclusive),
" stop column (1-based, inclusive),
" ]
"
" Args:
" hunk_body - list of lines
function! gitgutter#diff_highlight#process(hunk_body)
" Check whether we have the same number of lines added as removed.
let [removed, added] = [0, 0]
for line in a:hunk_body
if line[0] == '-'
let removed += 1
elseif line[0] == '+'
let added += 1
endif
endfor
if removed != added
return []
endif
let regions = []
for i in range(removed)
" pair lines by position
let rline = a:hunk_body[i]
let aline = a:hunk_body[i + removed]
call s:diff(rline, aline, i, i+removed, 0, 0, regions, 1)
endfor
return regions
endfunction
function! s:diff(rline, aline, rlinenr, alinenr, rprefix, aprefix, regions, whole_line)
" diff marker does not count as a difference in prefix
let start = a:whole_line ? 1 : 0
let prefix = s:common_prefix(a:rline[start:], a:aline[start:])
if a:whole_line
let prefix += 1
endif
let [rsuffix, asuffix] = s:common_suffix(a:rline, a:aline, prefix+1)
" region of change (common prefix and suffix removed)
let rtext = a:rline[prefix+1:rsuffix-1]
let atext = a:aline[prefix+1:asuffix-1]
" singular insertion
if empty(rtext)
if !a:whole_line || len(atext) != len(a:aline) " not whole line
call add(a:regions, [a:alinenr+1, '+', a:aprefix+prefix+1+1, a:aprefix+asuffix+1-1])
endif
return
endif
" singular deletion
if empty(atext)
if !a:whole_line || len(rtext) != len(a:rline) " not whole line
call add(a:regions, [a:rlinenr+1, '-', a:rprefix+prefix+1+1, a:rprefix+rsuffix+1-1])
endif
return
endif
" two insertions
let j = stridx(atext, rtext)
if j != -1
call add(a:regions, [a:alinenr+1, '+', a:aprefix+prefix+1+1, a:aprefix+prefix+j+1])
call add(a:regions, [a:alinenr+1, '+', a:aprefix+prefix+1+1+j+len(rtext), a:aprefix+asuffix+1-1])
return
endif
" two deletions
let j = stridx(rtext, atext)
if j != -1
call add(a:regions, [a:rlinenr+1, '-', a:rprefix+prefix+1+1, a:rprefix+prefix+j+1])
call add(a:regions, [a:rlinenr+1, '-', a:rprefix+prefix+1+1+j+len(atext), a:rprefix+rsuffix+1-1])
return
endif
" two edits
let lcs = s:lcs(rtext, atext)
" TODO do we need to ensure we don't get more than 2 elements when splitting?
if len(lcs) > s:gap_between_regions
let redits = s:split(rtext, lcs)
let aedits = s:split(atext, lcs)
call s:diff(redits[0], aedits[0], a:rlinenr, a:alinenr, a:rprefix+prefix+1, a:aprefix+prefix+1, a:regions, 0)
call s:diff(redits[1], aedits[1], a:rlinenr, a:alinenr, a:rprefix+prefix+1+len(redits[0])+len(lcs), a:aprefix+prefix+1+len(aedits[0])+len(lcs), a:regions, 0)
return
endif
" fall back to highlighting entire changed area
" if a change (but not the whole line)
if !a:whole_line || ((prefix != 0 || rsuffix != len(a:rline)) && prefix+1 < rsuffix)
call add(a:regions, [a:rlinenr+1, '-', a:rprefix+prefix+1+1, a:rprefix+rsuffix+1-1])
endif
" if a change (but not the whole line)
if !a:whole_line || ((prefix != 0 || asuffix != len(a:aline)) && prefix+1 < asuffix)
call add(a:regions, [a:alinenr+1, '+', a:aprefix+prefix+1+1, a:aprefix+asuffix+1-1])
endif
endfunction
function! s:lcs(s1, s2)
if empty(a:s1) || empty(a:s2)
return ''
endif
let matrix = map(repeat([repeat([0], len(a:s2)+1)], len(a:s1)+1), 'copy(v:val)')
let maxlength = 0
let endindex = len(a:s1)
for i in range(1, len(a:s1))
for j in range(1, len(a:s2))
if a:s1[i-1] ==# a:s2[j-1]
let matrix[i][j] = 1 + matrix[i-1][j-1]
if matrix[i][j] > maxlength
let maxlength = matrix[i][j]
let endindex = i - 1
endif
endif
endfor
endfor
return a:s1[endindex - maxlength + 1 : endindex]
endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#lcs(s1, s2)
return s:lcs(a:s1, a:s2)
endfunction
endif
" Returns 0-based index of last character of common prefix
" If there is no common prefix, returns -1.
"
" a, b - strings
"
function! s:common_prefix(a, b)
let len = min([len(a:a), len(a:b)])
if len == 0
return -1
endif
for i in range(len)
if a:a[i:i] != a:b[i:i]
return i - 1
endif
endfor
return i
endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_prefix(a, b)
return s:common_prefix(a:a, a:b)
endfunction
endif
" Returns 0-based indices of start of common suffix
"
" a, b - strings
" start - 0-based index to start from
function! s:common_suffix(a, b, start)
let [sa, sb] = [len(a:a), len(a:b)]
while sa >= a:start && sb >= a:start
if a:a[sa] ==# a:b[sb]
let sa -= 1
let sb -= 1
else
break
endif
endwhile
return [sa+1, sb+1]
endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#common_suffix(a, b, start)
return s:common_suffix(a:a, a:b, a:start)
endfunction
endif
" Split a string on another string.
" Assumes 1 occurrence of the delimiter.
function! s:split(str, delimiter)
let i = stridx(a:str, a:delimiter)
if i == 0
return ['', a:str[len(a:delimiter):]]
endif
return [a:str[:i-1], a:str[i+len(a:delimiter):]]
endfunction
if $VIM_GITGUTTER_TEST
function! gitgutter#diff_highlight#split(str, delimiter)
return s:split(a:str, a:delimiter)
endfunction
endif

View File

@ -31,6 +31,37 @@ function! gitgutter#fold#level(lnum)
endfunction
function! gitgutter#fold#foldtext()
if !gitgutter#fold#is_changed()
return foldtext()
endif
return substitute(foldtext(), ':', ' (*):', '')
endfunction
" Returns 1 if any of the folded lines have been changed
" (added, removed, or modified), 0 otherwise.
function! gitgutter#fold#is_changed()
for hunk in gitgutter#hunk#hunks(bufnr(''))
let hunk_begin = hunk[2]
let hunk_end = hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
if hunk_end < v:foldstart
continue
endif
if hunk_begin > v:foldend
break
endif
return 1
endfor
return 0
endfunction
" A line in a hunk has a fold level of 0.
" A line within 3 lines of a hunk has a fold level of 1.
" All other lines have a fold level of 2.

View File

@ -30,6 +30,7 @@ function! gitgutter#highlight#line_toggle() abort
endif
endfunction
function! gitgutter#highlight#linenr_disable() abort
let g:gitgutter_highlight_linenrs = 0
call s:define_sign_linenr_highlights()
@ -42,12 +43,12 @@ function! gitgutter#highlight#linenr_disable() abort
endfunction
function! gitgutter#highlight#linenr_enable() abort
let old_highlight_lines = g:gitgutter_highlight_linenrs
let old_highlight_linenrs = g:gitgutter_highlight_linenrs
let g:gitgutter_highlight_linenrs = 1
call s:define_sign_linenr_highlights()
if !old_highlight_lines && !g:gitgutter_signs
if !old_highlight_linenrs && !g:gitgutter_signs
call gitgutter#all(1)
endif
@ -102,6 +103,10 @@ function! gitgutter#highlight#define_highlights() abort
highlight default link GitGutterChangeLineNr CursorLineNr
highlight default link GitGutterDeleteLineNr CursorLineNr
highlight default link GitGutterChangeDeleteLineNr CursorLineNr
" Highlights used intra line.
highlight GitGutterAddIntraLine gui=reverse cterm=reverse
highlight GitGutterDeleteIntraLine gui=reverse cterm=reverse
endfunction
function! gitgutter#highlight#define_signs() abort
@ -171,12 +176,12 @@ function! s:define_sign_linenr_highlights() abort
if has('nvim-0.3.2')
try
if g:gitgutter_highlight_linenrs
sign define GitGutterLineAdded numhl=GitGutterAddLineNr
sign define GitGutterLineModified numhl=GitGutterChangeLineNr
sign define GitGutterLineRemoved numhl=GitGutterDeleteLineNr
sign define GitGutterLineRemovedFirstLine numhl=GitGutterDeleteLineNr
sign define GitGutterLineRemovedAboveAndBelow numhl=GitGutterDeleteLineNr
sign define GitGutterLineModifiedRemoved numhl=GitGutterChangeDeleteLineNr
sign define GitGutterLineAdded numhl=GitGutterAddLineNr
sign define GitGutterLineModified numhl=GitGutterChangeLineNr
sign define GitGutterLineRemoved numhl=GitGutterDeleteLineNr
sign define GitGutterLineRemovedFirstLine numhl=GitGutterDeleteLineNr
sign define GitGutterLineRemovedAboveAndBelow numhl=GitGutterDeleteLineNr
sign define GitGutterLineModifiedRemoved numhl=GitGutterChangeDeleteLineNr
else
sign define GitGutterLineAdded numhl=
sign define GitGutterLineModified numhl=

View File

@ -1,3 +1,5 @@
let s:winid = 0
function! gitgutter#hunk#set_hunks(bufnr, hunks) abort
call gitgutter#utility#setbufvar(a:bufnr, 'hunks', a:hunks)
call s:reset_summary(a:bufnr)
@ -175,24 +177,24 @@ function! gitgutter#hunk#stage(...) abort
else
call s:hunk_op(function('s:stage'))
endif
silent! call repeat#set("\<Plug>GitGutterStageHunk", -1)
silent! call repeat#set("\<Plug>(GitGutterStageHunk)", -1)
endfunction
function! gitgutter#hunk#undo() abort
call s:hunk_op(function('s:undo'))
silent! call repeat#set("\<Plug>GitGutterUndoHunk", -1)
silent! call repeat#set("\<Plug>(GitGutterUndoHunk)", -1)
endfunction
function! gitgutter#hunk#preview() abort
call s:hunk_op(function('s:preview'))
silent! call repeat#set("\<Plug>GitGutterPreviewHunk", -1)
silent! call repeat#set("\<Plug>(GitGutterPreviewHunk)", -1)
endfunction
function! s:hunk_op(op, ...)
let bufnr = bufnr('')
if &previewwindow
if s:in_hunk_preview_window()
if string(a:op) =~ '_stage'
" combine hunk-body in preview window with updated hunk-header
let hunk_body = getline(1, '$')
@ -214,8 +216,8 @@ function! s:hunk_op(op, ...)
let hunk_diff = join(hunk_header + hunk_body, "\n")."\n"
wincmd p
pclose
call s:goto_original_window()
call s:close_hunk_preview_window()
call s:stage(hunk_diff)
endif
@ -225,7 +227,7 @@ function! s:hunk_op(op, ...)
if gitgutter#utility#is_active(bufnr)
" Get a (synchronous) diff.
let [async, g:gitgutter_async] = [g:gitgutter_async, 0]
let diff = gitgutter#diff#run_diff(bufnr, 'index', 1)
let diff = gitgutter#diff#run_diff(bufnr, g:gitgutter_diff_relative_to, 1)
let g:gitgutter_async = async
call gitgutter#hunk#set_hunks(bufnr, gitgutter#diff#parse_diff(diff))
@ -276,7 +278,7 @@ endfunction
function! s:undo(hunk_diff)
" Apply reverse patch to buffer.
let hunk = gitgutter#diff#parse_hunk(split(a:hunk_diff, '\n')[4])
let lines = map(split(a:hunk_diff, '\n')[5:], 'v:val[1:]')
let lines = map(split(a:hunk_diff, '\r\?\n')[5:], 'v:val[1:]')
let lnum = hunk[2]
let added_only = hunk[1] == 0 && hunk[3] > 0
let removed_only = hunk[1] > 0 && hunk[3] == 0
@ -293,33 +295,16 @@ endfunction
function! s:preview(hunk_diff)
let lines = split(a:hunk_diff, '\n')
let lines = split(a:hunk_diff, '\r\?\n')
let header = lines[0:4]
let body = lines[5:]
let body_length = len(body)
let previewheight = min([body_length, &previewheight])
silent! wincmd P
if !&previewwindow
noautocmd execute g:gitgutter_preview_win_location previewheight 'new'
set previewwindow
else
execute 'resize' previewheight
call s:open_hunk_preview_window()
call s:populate_hunk_preview_window(header, body)
call s:enable_staging_from_hunk_preview_window()
if &previewwindow
call s:goto_original_window()
endif
let b:hunk_header = header
setlocal noreadonly modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
execute "%delete_"
call setline(1, body)
normal! gg
cnoreabbrev <buffer> <expr> w getcmdtype() == ':' && getcmdline() == 'w' ? 'GitGutterStageHunk' : 'w'
" Staging hunk from the preview window closes the window anyway.
cnoreabbrev <buffer> <expr> wq getcmdtype() == ':' && getcmdline() == 'wq' ? 'GitGutterStageHunk' : 'wq'
noautocmd wincmd p
endfunction
@ -390,3 +375,150 @@ function! s:line_adjustment_for_current_hunk() abort
return adj
endfunction
function! s:in_hunk_preview_window()
if g:gitgutter_preview_win_floating
return win_id2win(s:winid) == winnr()
else
return &previewwindow
endif
endfunction
" Floating window: does not move cursor to floating window.
" Preview window: moves cursor to preview window.
function! s:open_hunk_preview_window()
if g:gitgutter_preview_win_floating
if exists('*nvim_open_win')
call s:close_hunk_preview_window()
let buf = nvim_create_buf(v:false, v:false)
" Set default width and height for now.
let s:winid = nvim_open_win(buf, v:false, {
\ 'relative': 'cursor',
\ 'row': 1,
\ 'col': 0,
\ 'width': 42,
\ 'height': &previewheight,
\ 'style': 'minimal'
\ })
call nvim_buf_set_option(buf, 'filetype', 'diff')
call nvim_buf_set_option(buf, 'buftype', 'acwrite')
call nvim_buf_set_option(buf, 'bufhidden', 'delete')
call nvim_buf_set_option(buf, 'swapfile', v:false)
call nvim_buf_set_name(buf, 'gitgutter://hunk-preview')
" Assumes cursor is in original window.
autocmd CursorMoved <buffer> ++once call s:close_hunk_preview_window()
return
endif
if exists('*popup_create')
let s:winid = popup_create('', {
\ 'line': 'cursor+1',
\ 'col': 'cursor',
\ 'moved': 'any',
\ })
call setbufvar(winbufnr(s:winid), '&filetype', 'diff')
return
endif
endif
silent! wincmd P
if !&previewwindow
noautocmd execute g:gitgutter_preview_win_location &previewheight 'new gitgutter://hunk-preview'
let s:winid = win_getid()
set previewwindow
setlocal filetype=diff buftype=acwrite bufhidden=delete
" Reset some defaults in case someone else has changed them.
setlocal noreadonly modifiable noswapfile
endif
endfunction
" Floating window: does not care where cursor is.
" Preview window: assumes cursor is in preview window.
function! s:populate_hunk_preview_window(header, body)
let body_length = len(a:body)
let height = min([body_length, &previewheight])
if g:gitgutter_preview_win_floating
if exists('*nvim_open_win')
" Assumes cursor is not in previewing window.
call nvim_buf_set_var(winbufnr(s:winid), 'hunk_header', a:header)
let width = max(map(copy(a:body), 'strdisplaywidth(v:val)'))
call nvim_win_set_width(s:winid, width)
call nvim_win_set_height(s:winid, height)
call nvim_buf_set_lines(winbufnr(s:winid), 0, -1, v:false, [])
call nvim_buf_set_lines(winbufnr(s:winid), 0, -1, v:false, a:body)
call nvim_buf_set_option(winbufnr(s:winid), 'modified', v:false)
let ns_id = nvim_create_namespace('GitGutter')
call nvim_buf_clear_namespace(winbufnr(s:winid), ns_id, 0, -1)
for region in gitgutter#diff_highlight#process(a:body)
let group = region[1] == '+' ? 'GitGutterAddIntraLine' : 'GitGutterDeleteIntraLine'
call nvim_buf_add_highlight(winbufnr(s:winid), ns_id, group, region[0]-1, region[2]-1, region[3])
endfor
call nvim_win_set_cursor(s:winid, [1,0])
endif
if exists('*popup_create')
call popup_settext(s:winid, a:body)
for region in gitgutter#diff_highlight#process(a:body)
let group = region[1] == '+' ? 'GitGutterAddIntraLine' : 'GitGutterDeleteIntraLine'
call win_execute(s:winid, "call matchaddpos('".group."', [[".region[0].", ".region[2].", ".(region[3]-region[2]+1)."]])")
endfor
endif
else
let b:hunk_header = a:header
execute 'resize' height
%delete _
call setline(1, a:body)
setlocal nomodified
call clearmatches()
for region in gitgutter#diff_highlight#process(a:body)
let group = region[1] == '+' ? 'GitGutterAddIntraLine' : 'GitGutterDeleteIntraLine'
call matchaddpos(group, [[region[0], region[2], region[3]-region[2]+1]])
endfor
1
endif
endfunction
function! s:enable_staging_from_hunk_preview_window()
augroup gitgutter_hunk_preview
autocmd!
execute 'autocmd BufWriteCmd <buffer='.winbufnr(s:winid).'> GitGutterStageHunk'
augroup END
endfunction
function! s:goto_original_window()
noautocmd wincmd p
endfunction
function! s:close_hunk_preview_window()
call setbufvar(winbufnr(s:winid), '&modified', 0)
if g:gitgutter_preview_win_floating
if win_id2win(s:winid) > 0
execute win_id2win(s:winid).'wincmd c'
endif
else
pclose
endif
let s:winid = 0
endfunction

View File

@ -11,7 +11,7 @@ function! gitgutter#sign#enable() abort
let g:gitgutter_signs = 1
call gitgutter#highlight#define_sign_text_highlights()
if !old_signs && !g:gitgutter_highlight_lines
if !old_signs && !g:gitgutter_highlight_lines && !g:gitgutter_highlight_linenrs
call gitgutter#all(1)
endif
endfunction
@ -20,7 +20,7 @@ function! gitgutter#sign#disable() abort
let g:gitgutter_signs = 0
call gitgutter#highlight#define_sign_text_highlights()
if !g:gitgutter_highlight_lines
if !g:gitgutter_highlight_lines && !g:gitgutter_highlight_linenrs
call gitgutter#sign#clear_signs(bufnr(''))
endif
endfunction