-
Notifications
You must be signed in to change notification settings - Fork 309
add vim-vital-vs FloatingWindow #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
let s:_plugin_name = expand('<sfile>:t:r') | ||
|
||
function! vital#{s:_plugin_name}#new() abort | ||
return vital#{s:_plugin_name[1:]}#new() | ||
endfunction | ||
|
||
function! vital#{s:_plugin_name}#function(funcname) abort | ||
silent! return function(a:funcname) | ||
endfunction |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
" ___vital___ | ||
" NOTE: lines between '" ___vital___' is generated by :Vitalize. | ||
" Do not modify the code nor insert new lines before '" ___vital___' | ||
function! s:_SID() abort | ||
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$') | ||
endfunction | ||
execute join(['function! vital#_lsp#VS#Vim#Buffer#import() abort', printf("return map({'get_line_count': '', 'do': '', 'create': '', 'load': ''}, \"vital#_lsp#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") | ||
delfunction s:_SID | ||
" ___vital___ | ||
let s:Do = { -> {} } | ||
|
||
let g:___VS_Vim_Buffer_id = get(g:, '___VS_Vim_Buffer_id', 0) | ||
|
||
" | ||
" get_line_count | ||
" | ||
if exists('*nvim_buf_line_count') | ||
function! s:get_line_count(bufnr) abort | ||
return nvim_buf_line_count(a:bufnr) | ||
endfunction | ||
elseif has('patch-8.2.0019') | ||
function! s:get_line_count(bufnr) abort | ||
return getbufinfo(a:bufnr)[0].linecount | ||
endfunction | ||
else | ||
function! s:get_line_count(bufnr) abort | ||
if bufnr('%') == bufnr(a:bufnr) | ||
return line('$') | ||
endif | ||
return len(getbufline(a:bufnr, '^', '$')) | ||
endfunction | ||
endif | ||
|
||
" | ||
" create | ||
" | ||
function! s:create(...) abort | ||
let g:___VS_Vim_Buffer_id += 1 | ||
let l:bufnr = bufnr(printf('VS.Vim.Buffer: %s: %s', | ||
\ g:___VS_Vim_Buffer_id, | ||
\ get(a:000, 0, 'VS.Vim.Buffer.Default') | ||
\ ), v:true) | ||
call s:load(l:bufnr) | ||
return l:bufnr | ||
endfunction | ||
|
||
" | ||
" load | ||
" | ||
if exists('*bufload') | ||
function! s:load(bufnr_or_path) abort | ||
call bufload(bufnr(a:bufnr_or_path, v:true)) | ||
endfunction | ||
else | ||
function! s:load(bufnr_or_path) abort | ||
call s:do(bufnr(a:bufnr_or_path, v:true), { -> {} }) | ||
endfunction | ||
endif | ||
|
||
" | ||
" do | ||
" | ||
function! s:do(bufnr, func) abort | ||
let l:curr_bufnr = bufnr('%') | ||
if l:curr_bufnr == a:bufnr | ||
call a:func() | ||
return | ||
endif | ||
|
||
try | ||
execute printf('noautocmd keepalt keepjumps %sbuffer', a:bufnr) | ||
call a:func() | ||
catch /.*/ | ||
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint }) | ||
finally | ||
execute printf('noautocmd keepalt keepjumps %sbuffer', l:curr_bufnr) | ||
endtry | ||
endfunction | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
" ___vital___ | ||
" NOTE: lines between '" ___vital___' is generated by :Vitalize. | ||
" Do not modify the code nor insert new lines before '" ___vital___' | ||
function! s:_SID() abort | ||
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$') | ||
endfunction | ||
execute join(['function! vital#_lsp#VS#Vim#Window#import() abort', printf("return map({'info': '', 'do': '', 'find': '', 'scroll': '', 'screenpos': ''}, \"vital#_lsp#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n") | ||
delfunction s:_SID | ||
" ___vital___ | ||
let s:Do = { -> {} } | ||
|
||
" | ||
" do | ||
" | ||
function! s:do(winid, func) abort | ||
let l:curr_winid = win_getid() | ||
if l:curr_winid == a:winid | ||
call a:func() | ||
return | ||
endif | ||
|
||
if exists('*win_execute') | ||
let s:Do = a:func | ||
try | ||
noautocmd keepalt keepjumps call win_execute(a:winid, 'call s:Do()') | ||
catch /.*/ | ||
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint }) | ||
endtry | ||
unlet s:Do | ||
return | ||
endif | ||
|
||
noautocmd keepalt keepjumps call win_gotoid(a:winid) | ||
try | ||
call a:func() | ||
catch /.*/ | ||
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint }) | ||
endtry | ||
noautocmd keepalt keepjumps call win_gotoid(l:curr_winid) | ||
endfunction | ||
|
||
" | ||
" info | ||
" | ||
if has('nvim') | ||
function! s:info(win) abort | ||
let l:info = getwininfo(a:win)[0] | ||
return { | ||
\ 'width': l:info.width, | ||
\ 'height': l:info.height, | ||
\ 'topline': l:info.topline, | ||
\ } | ||
endfunction | ||
else | ||
function! s:info(win) abort | ||
if index(s:_get_visible_popup_winids(), a:win) >= 0 | ||
let l:info = popup_getpos(a:win) | ||
return { | ||
\ 'width': l:info.width, | ||
\ 'height': l:info.height, | ||
\ 'topline': l:info.firstline | ||
\ } | ||
endif | ||
|
||
let l:ctx = {} | ||
let l:ctx.info = {} | ||
function! l:ctx.callback() abort | ||
let self.info.width = winwidth(0) | ||
let self.info.height = winheight(0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [vint] reported by reviewdog 🐶 |
||
let self.info.topline = line('w0') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [vint] reported by reviewdog 🐶 |
||
endfunction | ||
call s:do(a:win, { -> l:ctx.callback() }) | ||
return l:ctx.info | ||
endfunction | ||
endif | ||
|
||
" | ||
" find | ||
" | ||
function! s:find(callback) abort | ||
let l:winids = [] | ||
let l:winids += map(range(1, tabpagewinnr(tabpagenr(), '$')), 'win_getid(v:val)') | ||
let l:winids += s:_get_visible_popup_winids() | ||
return filter(l:winids, 'a:callback(v:val)') | ||
endfunction | ||
|
||
" | ||
" scroll | ||
" | ||
function! s:scroll(winid, topline) abort | ||
let l:ctx = {} | ||
function! l:ctx.callback(winid, topline) abort | ||
let l:wininfo = s:info(a:winid) | ||
let l:topline = a:topline | ||
let l:topline = max([l:topline, 1]) | ||
let l:topline = min([l:topline, line('$') - l:wininfo.height + 1]) | ||
|
||
if l:topline == l:wininfo.topline | ||
return | ||
endif | ||
|
||
if index(s:_get_visible_popup_winids(), a:winid) >= 0 | ||
call popup_setoptions(a:winid, { | ||
\ 'firstline': l:topline, | ||
\ }) | ||
else | ||
let l:delta = l:topline - l:wininfo.topline | ||
let l:key = l:delta > 0 ? "\<C-e>" : "\<C-y>" | ||
execute printf('noautocmd silent normal! %s', repeat(l:key, abs(l:delta))) | ||
endif | ||
endfunction | ||
call s:do(a:winid, { -> l:ctx.callback(a:winid, a:topline) }) | ||
endfunction | ||
|
||
" | ||
" screenpos | ||
" | ||
" @param {[number, number]} pos - position on the current buffer. | ||
" | ||
function! s:screenpos(pos) abort | ||
let l:ui_x = wincol() - col('.') | ||
let l:view = winsaveview() | ||
let l:scroll_x = l:view.leftcol | ||
let l:scroll_y = l:view.topline - 1 | ||
let l:winpos = win_screenpos(win_getid()) | ||
let l:origin1 = [l:winpos[0] + (a:pos[0] - l:scroll_y) - 1, l:winpos[1] + (a:pos[1] + a:pos[2] + l:ui_x - l:scroll_x) - 1] | ||
return [l:origin1[0] - 1, l:origin1[1] - 1] | ||
endfunction | ||
|
||
" | ||
" _get_visible_popup_winids | ||
" | ||
function! s:_get_visible_popup_winids() abort | ||
if !exists('*popup_list') | ||
return [] | ||
endif | ||
return filter(popup_list(), 'popup_getpos(v:val).visible') | ||
endfunction | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[vint] reported by reviewdog 🐶
Make the scope explicit like
l:self
(see Anti-pattern of vimrc (Scope of identifier))