Skip to content
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

[Do not merge] add vital's experimental popup #1032

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions autoload/vital/_popup.vim
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
225 changes: 225 additions & 0 deletions autoload/vital/_popup/Experimental/UI/Popup.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
" ___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#_popup#Experimental#UI#Popup#import() abort', printf("return map({'options': '', 'bufnr': '', 'hide': '', 'create': '', 'show': '', 'is_supported': '', 'close': '', 'winid': '', 'contents': ''}, \"vital#_popup#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
delfunction s:_SID
" ___vital___
let s:save_cpo = &cpo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[vint] reported by reviewdog 🐶
Use the full option name cpoptions instead of cpo (see :help option-summary)

set cpo&vim
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[vint] reported by reviewdog 🐶
Use the full option name cpoptions instead of cpo (see :help option-summary)


let s:_has_nvim = has('nvim')
let s:_popups = {}
let s:_id = 0

function! s:is_supported() abort
return has('nvim') && exists('*nvim_open_win') || (!has('nvim') && exists('*popup_create') && has('patch-8.2.0286'))
endfunction

" opt = {
" 'contents': ['This', 'is', 'a', 'line'],
" 'w': 5,
" 'h': 5,
" 'x': 1,
" 'y': 1,
" 'pos': 'topleft|topright|bottomleft|bottomright|topcenter|bottomcenter',
" }
function! s:create(opt) abort
let id = s:_nextid()
Copy link

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:id (see Anti-pattern of vimrc (Scope of identifier))

let data = { 'winid': 0 }
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))


call s:_set(data, a:opt)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))


if s:_has_nvim
let data['bufnr'] = nvim_create_buf(0, 1)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

call nvim_buf_set_lines(data['bufnr'], 0, -1, 1, data['contents'])
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

let data['winid'] = s:_nvim_open_win(data)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

else
" neovim doesn't support scrollbar so don't enable it
let data['winid'] = popup_create(data['contents'], s:_get_vim_options(data))
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

let data['bufnr'] = winbufnr(data['winid'])
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

endif
let s:_popups[id] = data
Copy link

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:id (see Anti-pattern of vimrc (Scope of identifier))

Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

call s:_notify(id, {}, 'create')
Copy link

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:id (see Anti-pattern of vimrc (Scope of identifier))

call s:_notify(id, {}, 'show')
Copy link

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:id (see Anti-pattern of vimrc (Scope of identifier))

return id
Copy link

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:id (see Anti-pattern of vimrc (Scope of identifier))

endfunction

function! s:close(id) abort
let data = s:_popups[a:id]
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

if s:_has_nvim
call nvim_win_close(data['winid'], 1)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

let data['winid'] = 0
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

else
call popup_close(data['winid'])
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

endif
call s:_notify(a:id, {}, 'close')
call remove(s:_popups, a:id)
endfunction

function! s:hide(id) abort
let data = s:_popups[a:id]
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

if s:_has_nvim
call nvim_win_close(data['winid'], 1)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

let data['winid'] = 0
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

else
call popup_hide(data['winid'])
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

endif
call s:_notify(a:id, {}, 'hide')
endfunction

function! s:show(id) abort
let data = s:_popups[a:id]
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

if s:_has_nvim
let data['winid'] = s:_nvim_open_win(data)
Copy link

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:data (see Anti-pattern of vimrc (Scope of identifier))

else
call popup_show(data['winid'])
endif
call s:_notify(a:id, {}, 'show')
endfunction

function! s:contents(id, contents) abort
let data = s:_popups[a:id]
let data['contents'] = a:contents
if s:_has_nvim
call nvim_buf_set_lines(data['bufnr'], 0, -1, 1, a:contents)
else
call popup_settext(data['winid'], a:contents)
endif
endfunction

function! s:options(id, opt) abort
let data = s:_popups[a:id]
call s:_set(data, a:opt)
if s:_has_nvim
call nvim_win_set_config(data['winid'], s:_get_nvim_options(data))
else
call popup_setoptions(data['winid'], s:_get_vim_options(data))
endif
call s:_notify(a:id, {}, 'options')
endfunction

function! s:winid(id) abort
" can return 0 when hidden for neovim
return s:_popups[a:id]['winid']
endfunction

function! s:bufnr(id) abort
return s:_popups[a:id]['bufnr']
endfunction

function! s:_get_vim_options(data) abort
return {
\ 'width': a:data['w'],
\ 'height': a:data['h'],
\ 'minwidth': a:data['w'],
\ 'minheight': a:data['h'],
\ 'maxwidth': a:data['w'],
\ 'maxheight': a:data['h'],
\ 'col': a:data['sx'],
\ 'line': a:data['sy'],
\ 'scrollbar': 0,
\ 'wrap': 0,
\ }
endfunction

function! s:_get_nvim_options(data) abort
return {
\ 'relative': 'editor',
\ 'style': 'minimal',
\ 'width': a:data['w'],
\ 'height': a:data['h'],
\ 'col': a:data['sx'],
\ 'row': a:data['sy'],
\ 'focusable': 0,
\ }
endfunction

function! s:_nvim_open_win(data) abort
return nvim_open_win(a:data['bufnr'], 0, s:_get_nvim_options(a:data))
endfunction

function! s:_user_to_editor_xy(val) abort
if s:_has_nvim
return a:val - 1
else
return a:val
endif
endfunction

function! s:_editor_to_user_xy(val) abort
if s:_has_nvim
return a:val + 1
else
return a:val
endif
endfunction

function! s:_set(data, opt) abort
" copy all callbacks to data
for [key, Cb] in items(a:opt)
if stridx(key, 'on_') == 0
let a:data[key] = Cb
endif
endfor

" try to set values from a:opt, if not set from a:data, if not default
let a:data['w'] = get(a:opt, 'w', get(a:data, 'w', 5))
let a:data['h'] = get(a:opt, 'h', get(a:data, 'h', 5))

if s:_has_nvim
" a:opt[x/y] need to normalize
" a:data['x/y'] already normalized
let a:data['x'] = has_key(a:opt, 'x') ? s:_user_to_editor_xy(a:opt['x']) : get(a:data, 'x', 0)
let a:data['y'] = has_key(a:opt, 'y') ? s:_user_to_editor_xy(a:opt['y']) : get(a:data, 'y', 0)
else
let a:data['x'] = get(a:opt, 'x', get(a:data, 'x', 1))
let a:data['y'] = get(a:opt, 'y', get(a:data, 'y', 1))
endif

let a:data['contents'] = get(a:opt, 'contents', get(a:data, 'contents', []))
let a:data['pos'] = get(a:opt, 'pos', get(a:data, 'pos', 'topleft'))

if a:data['pos'] ==# 'topleft'
let a:data['sx'] = a:data['x']
let a:data['sy'] = a:data['y']
elseif a:data['pos'] ==# 'topright'
let a:data['sx'] = a:data['x'] + a:data['w'] + 1
let a:data['sy'] = a:data['y']
elseif a:data['pos'] ==# 'bottomleft'
let a:data['sx'] = a:data['x']
let a:data['sy'] = a:data['y'] - a:data['h'] + 1
elseif a:data['pos'] ==# 'bottomright'
let a:data['sx'] = a:data['x'] + a:data['w'] + 1
let a:data['sy'] = a:data['y'] - a:data['h'] + 1
elseif a:data['pos'] ==# 'topcenter'
let a:data['sx'] = a:data['x'] + float2nr(a:data['x'] / 2) + 1
let a:data['sy'] = a:data['y'] - a:data['h']
elseif a:data['pos'] ==# 'bottomcenter'
let a:data['sx'] = a:data['x'] + float2nr(a:data['x'] / 2) + 1
let a:data['sy'] = a:data['y'] + 1
else
throw 'vital: Experimental.UI.Popup: Invalid pos'
endif
endfunction

function! s:_nextid() abort
let s:_id += 1
return s:_id
endfunction

function! s:_notify(id, data, event) abort
if has_key(s:_popups[a:id], 'on_event')
call s:_popups[a:id]['on_event'](a:id, a:data, a:event)
endif
let eventhandler = 'on_' . a:event
if has_key(s:_popups[a:id], eventhandler)
call s:_popups[a:id][eventhandler](a:id, a:data, a:event)
endif
endfunction

let &cpo = s:save_cpo
unlet s:save_cpo
"vim: sts=2 sw=2 smarttab et ai textwidth=0 fdm=marker
Loading