1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-03-22 17:05:42 +08:00

Improve the vim api (#3126)

This commit is contained in:
Wang Shidong 2019-10-06 22:10:48 +08:00 committed by GitHub
parent a5c99f70c0
commit 769cc8beff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,85 +11,106 @@ let s:self = {}
let s:CMP = SpaceVim#api#import('vim#compatible') let s:CMP = SpaceVim#api#import('vim#compatible')
function! s:self.jumps() abort function! s:self.jumps() abort
let result = [] let result = []
for jump in split(s:CMP.execute('jumps'), '\n')[1:] for jump in split(s:CMP.execute('jumps'), '\n')[1:]
let list = split(jump) let list = split(jump)
if len(list) < 4 if len(list) < 4
continue continue
endif endif
let [linenr, col, file_text] = [list[1], list[2]+1, join(list[3:])] let [linenr, col, file_text] = [list[1], list[2]+1, join(list[3:])]
let lines = getbufline(file_text, linenr) let lines = getbufline(file_text, linenr)
let path = file_text let path = file_text
let bufnr = bufnr(file_text) let bufnr = bufnr(file_text)
if empty(lines) if empty(lines)
if stridx(join(split(getline(linenr))), file_text) == 0 if stridx(join(split(getline(linenr))), file_text) == 0
let lines = [file_text] let lines = [file_text]
let path = bufname('%') let path = bufname('%')
let bufnr = bufnr('%') let bufnr = bufnr('%')
elseif filereadable(path) elseif filereadable(path)
let bufnr = 0 let bufnr = 0
let lines = ['buffer unloaded'] let lines = ['buffer unloaded']
else else
" Skip. " Skip.
continue continue
endif endif
endif endif
if getbufvar(bufnr, '&filetype') ==# 'unite' if getbufvar(bufnr, '&filetype') ==# 'unite'
" Skip unite buffer. " Skip unite buffer.
continue continue
endif endif
call add(result, [linenr, col, file_text, path, bufnr, lines]) call add(result, [linenr, col, file_text, path, bufnr, lines])
endfor endfor
return result return result
endfunction endfunction
function! s:self.parse_string(line) abort function! s:self.parse_string(line) abort
let expr = '`[^`]*`' let expr = '`[^`]*`'
let i = 0 let i = 0
let line = [] let line = []
while i < strlen(a:line) || i != -1 while i < strlen(a:line) || i != -1
let [rst, m, n] = matchstrpos(a:line, expr, i) let [rst, m, n] = matchstrpos(a:line, expr, i)
if m == -1 if m == -1
call add(line, a:line[ i : -1 ]) call add(line, a:line[ i : -1 ])
break break
else else
call add(line, a:line[ i : m-1]) call add(line, a:line[ i : m-1])
try try
let rst = eval(rst[1:-2]) let rst = eval(rst[1:-2])
catch catch
let rst = '' let rst = ''
endtry endtry
call add(line, rst) call add(line, rst)
endif endif
let i = n let i = n
endwhile endwhile
return join(line, '') return join(line, '')
endfunction endfunction
if exists('*nvim_win_set_cursor') if exists('*nvim_win_set_cursor')
function! s:self.win_set_cursor(win, pos) abort function! s:self.win_set_cursor(win, pos) abort
call nvim_win_set_cursor(a:win, a:pos) call nvim_win_set_cursor(a:win, a:pos)
endfunction endfunction
elseif exists('*win_execute')
function! s:self.win_set_cursor(win, pos) abort
" @fixme use g` to move to cursor line
" this seem to be a bug of vim
" https://github.com/vim/vim/issues/5022
call win_execute(a:win, ':call cursor(' . a:pos[0] . ', ' . a:pos[1] . ')')
" call win_execute(a:win, ':' . a:pos[0])
call win_execute(a:win, ':normal! g"')
endfunction
elseif has('lua')
function! s:self.win_set_cursor(win, pos) abort
lua local winindex = vim.eval("win_id2win(a:win) - 1")
lua local w = vim.window(winindex)
lua w.line = vim.eval("a:pos[0]")
lua w.col = vim.eval("a:pos[1]")
endfunction
else else
function! s:self.win_set_cursor(win, pos) abort function! s:self.win_set_cursor(win, pos) abort
endfunction endfunction
endif endif
if exists('*nvim_buf_line_count') if exists('*nvim_buf_line_count')
function! s:self.buf_line_count(buf) abort function! s:self.buf_line_count(buf) abort
return nvim_buf_line_count(a:buf) return nvim_buf_line_count(a:buf)
endfunction endfunction
elseif has('lua')
function! s:self.buf_line_count(buf) abort
" lua numbers are floats, so use float2nr
return float2nr(luaeval('#vim.buffer(vim.eval("a:buf"))'))
endfunction
else else
function! s:self.buf_line_count(buf) abort function! s:self.buf_line_count(buf) abort
endfunction endfunction
endif endif
function! SpaceVim#api#vim#get() abort function! SpaceVim#api#vim#get() abort
return deepcopy(s:self) return deepcopy(s:self)
endfunction endfunction