mirror of
https://github.com/amix/vimrc
synced 2025-07-27 23:45:00 +08:00
Update Coc.nvim
This commit is contained in:
148
sources_non_forked/coc.nvim/autoload/coc/helper.vim
Normal file
148
sources_non_forked/coc.nvim/autoload/coc/helper.vim
Normal file
@ -0,0 +1,148 @@
|
||||
scriptencoding utf-8
|
||||
" Helper methods for viml
|
||||
|
||||
function! coc#helper#get_charactor(line, col) abort
|
||||
return strchars(strpart(a:line, 0, a:col - 1))
|
||||
endfunction
|
||||
|
||||
function! coc#helper#last_character(line) abort
|
||||
return strcharpart(a:line, strchars(a:line) - 1, 1)
|
||||
endfunction
|
||||
|
||||
function! coc#helper#obj_equal(one, two) abort
|
||||
for key in keys(a:one)
|
||||
if a:one[key] != a:two[key]
|
||||
return 0
|
||||
endif
|
||||
endfor
|
||||
return 1
|
||||
endfunction
|
||||
|
||||
" get change between two lines
|
||||
function! coc#helper#str_diff(curr, previous, col) abort
|
||||
let end = strpart(a:curr, a:col - 1)
|
||||
let start = strpart(a:curr, 0, a:col -1)
|
||||
let endOffset = 0
|
||||
let startOffset = 0
|
||||
let currLen = strchars(a:curr)
|
||||
let prevLen = strchars(a:previous)
|
||||
if len(end)
|
||||
let endLen = strchars(end)
|
||||
for i in range(min([prevLen, endLen]))
|
||||
if strcharpart(end, endLen - 1 - i, 1) ==# strcharpart(a:previous, prevLen -1 -i, 1)
|
||||
let endOffset = endOffset + 1
|
||||
else
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
let remain = endOffset == 0 ? a:previous : strcharpart(a:previous, 0, prevLen - endOffset)
|
||||
if len(remain)
|
||||
for i in range(min([strchars(remain), strchars(start)]))
|
||||
if strcharpart(remain, i, 1) ==# strcharpart(start, i ,1)
|
||||
let startOffset = startOffset + 1
|
||||
else
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
endif
|
||||
return {
|
||||
\ 'start': startOffset,
|
||||
\ 'end': prevLen - endOffset,
|
||||
\ 'text': strcharpart(a:curr, startOffset, currLen - startOffset - endOffset)
|
||||
\ }
|
||||
endfunction
|
||||
|
||||
function! coc#helper#str_apply(content, diff) abort
|
||||
let totalLen = strchars(a:content)
|
||||
let endLen = totalLen - a:diff['end']
|
||||
return strcharpart(a:content, 0, a:diff['start']).a:diff['text'].strcharpart(a:content, a:diff['end'], endLen)
|
||||
endfunction
|
||||
|
||||
" insert inserted to line at position, use ... when result is too long
|
||||
" line should only contains character has strwidth equals 1
|
||||
function! coc#helper#str_compose(line, position, inserted) abort
|
||||
let width = strwidth(a:line)
|
||||
let text = a:inserted
|
||||
let res = a:line
|
||||
let need_truncate = a:position + strwidth(text) + 1 > width
|
||||
if need_truncate
|
||||
let remain = width - a:position - 3
|
||||
if remain < 2
|
||||
" use text for full line, use first & end of a:line, ignore position
|
||||
let res = strcharpart(a:line, 0, 1)
|
||||
let w = strwidth(res)
|
||||
for i in range(strchars(text))
|
||||
let c = strcharpart(text, i, 1)
|
||||
let a = strwidth(c)
|
||||
if w + a <= width - 1
|
||||
let w = w + a
|
||||
let res = res.c
|
||||
endif
|
||||
endfor
|
||||
let res = res.strcharpart(a:line, w)
|
||||
else
|
||||
let res = strcharpart(a:line, 0, a:position)
|
||||
let w = strwidth(res)
|
||||
for i in range(strchars(text))
|
||||
let c = strcharpart(text, i, 1)
|
||||
let a = strwidth(c)
|
||||
if w + a <= width - 3
|
||||
let w = w + a
|
||||
let res = res.c
|
||||
endif
|
||||
endfor
|
||||
let res = res.'..'
|
||||
let w = w + 2
|
||||
let res = res.strcharpart(a:line, w)
|
||||
endif
|
||||
else
|
||||
let first = strcharpart(a:line, 0, a:position)
|
||||
let res = first.text.strcharpart(a:line, a:position + strwidth(text))
|
||||
endif
|
||||
return res
|
||||
endfunction
|
||||
|
||||
" Return new dict with keys removed
|
||||
function! coc#helper#dict_omit(dict, keys) abort
|
||||
let res = {}
|
||||
for key in keys(a:dict)
|
||||
if index(a:keys, key) == -1
|
||||
let res[key] = a:dict[key]
|
||||
endif
|
||||
endfor
|
||||
return res
|
||||
endfunction
|
||||
|
||||
" Return new dict with keys only
|
||||
function! coc#helper#dict_pick(dict, keys) abort
|
||||
let res = {}
|
||||
for key in keys(a:dict)
|
||||
if index(a:keys, key) != -1
|
||||
let res[key] = a:dict[key]
|
||||
endif
|
||||
endfor
|
||||
return res
|
||||
endfunction
|
||||
|
||||
" support for float values
|
||||
function! coc#helper#min(first, ...) abort
|
||||
let val = a:first
|
||||
for i in range(0, len(a:000) - 1)
|
||||
if a:000[i] < val
|
||||
let val = a:000[i]
|
||||
endif
|
||||
endfor
|
||||
return val
|
||||
endfunction
|
||||
|
||||
" support for float values
|
||||
function! coc#helper#max(first, ...) abort
|
||||
let val = a:first
|
||||
for i in range(0, len(a:000) - 1)
|
||||
if a:000[i] > val
|
||||
let val = a:000[i]
|
||||
endif
|
||||
endfor
|
||||
return val
|
||||
endfunction
|
@ -292,9 +292,8 @@ function! coc#notify#close(winid) abort
|
||||
endif
|
||||
call coc#window#set_var(a:winid, 'closing', 1)
|
||||
call s:cancel(a:winid)
|
||||
let winblend = coc#window#get_var(a:winid, 'winblend', 0)
|
||||
let curr = s:is_vim ? {'row': row} : {'winblend': winblend}
|
||||
let dest = s:is_vim ? {'row': row + 1} : {'winblend': winblend == 0 ? 0 : 60}
|
||||
let curr = s:is_vim ? {'row': row} : {'winblend': coc#window#get_var(a:winid, 'winblend', 30)}
|
||||
let dest = s:is_vim ? {'row': row + 1} : {'winblend': 60}
|
||||
call s:animate(a:winid, curr, dest, 0, 1)
|
||||
endfunction
|
||||
|
||||
|
@ -182,10 +182,14 @@ function! coc#util#jump(cmd, filepath, ...) abort
|
||||
else
|
||||
exec 'drop '.fnameescape(file)
|
||||
endif
|
||||
elseif a:cmd == 'edit' && bufloaded(file)
|
||||
exe 'b '.bufnr(file)
|
||||
elseif a:cmd == 'edit'
|
||||
if bufloaded(file)
|
||||
exe 'b '.bufnr(file)
|
||||
else
|
||||
exe a:cmd.' '.fnameescape(file)
|
||||
endif
|
||||
else
|
||||
call s:safer_open(a:cmd, file)
|
||||
exe a:cmd.' '.fnameescape(file)
|
||||
endif
|
||||
if !empty(get(a:, 1, []))
|
||||
let line = getline(a:1[0] + 1)
|
||||
@ -204,43 +208,6 @@ function! coc#util#jump(cmd, filepath, ...) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:safer_open(cmd, file) abort
|
||||
" How to support :pedit and :drop?
|
||||
let is_supported_cmd = index(["edit", "split", "vsplit", "tabe"], a:cmd) >= 0
|
||||
|
||||
" Special handling should be limited to URI.
|
||||
let looks_like_uri = match(a:file, "^.*://") >= 0
|
||||
|
||||
if looks_like_uri && is_supported_cmd && has('win32') && has('nvim')
|
||||
if bufloaded(a:file)
|
||||
let buf = bufnr(a:file)
|
||||
" Do not reload existing buffer
|
||||
let reload_buffer = v:false
|
||||
else
|
||||
" Workaround a bug for Win32 paths (works in Neovim only).
|
||||
"
|
||||
" reference:
|
||||
" - https://github.com/vim/vim/issues/541
|
||||
" - https://github.com/neoclide/coc-java/issues/82
|
||||
let buf = nvim_create_buf(v:true, v:false)
|
||||
" Ignore swap file error occuring when, for example, file is a URI.
|
||||
silent! call nvim_buf_set_name(buf, a:file)
|
||||
let reload_buffer = v:true
|
||||
endif
|
||||
if a:cmd != 'edit'
|
||||
" Open split, tab, etc. by a:cmd.
|
||||
exe a:cmd
|
||||
endif
|
||||
" Set filename and reload file (or URI) contents by :edit.
|
||||
exe 'keepjumps buffer ' . buf
|
||||
if reload_buffer
|
||||
exe 'keepjumps edit'
|
||||
endif
|
||||
else
|
||||
exe a:cmd.' '.fnameescape(a:file)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! coc#util#variables(bufnr) abort
|
||||
let info = getbufinfo(a:bufnr)
|
||||
let variables = empty(info) ? {} : copy(info[0]['variables'])
|
||||
|
Reference in New Issue
Block a user