Skip to content

Commit fa15108

Browse files
refactor to s:_set
1 parent 04f88bf commit fa15108

File tree

1 file changed

+45
-66
lines changed
  • autoload/vital/__vital__/Experimental/UI

1 file changed

+45
-66
lines changed

autoload/vital/__vital__/Experimental/UI/Popup.vim

Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,15 @@ endfunction
1414
" 'h': 5,
1515
" 'x': 1,
1616
" 'y': 1,
17+
" 'pos': 'topleft|topright|bottomleft|bottomright|topcenter|bottomcenter',
1718
" }
1819
function! s:create(opt) abort
1920
let data = {}
20-
call s:_set_w(data, get(a:opt, 'w', 5))
21-
call s:_set_h(data, get(a:opt, 'h', 5))
22-
call s:_set_x(data, get(a:opt, 'x', 1))
23-
call s:_set_y(data, get(a:opt, 'y', 1))
24-
call s:_set_pos(data, get(a:opt, 'pos', 'topleft'))
25-
call s:_set_contents(data, get(a:opt, 'contents', []))
26-
27-
if data['pos'] ==# 'topleft'
28-
let data['sx'] = data['x']
29-
let data['sy'] = data['y']
30-
elseif data['pos'] ==# 'topright'
31-
let data['sx'] = data['x'] + data['w'] + 1
32-
let data['sy'] = data['y']
33-
elseif data['pos'] ==# 'bottomleft'
34-
let data['sx'] = data['x']
35-
let data['sy'] = data['y'] - data['h'] + 1
36-
elseif data['pos'] ==# 'bottomright'
37-
let data['sx'] = data['x'] + data['w'] + 1
38-
let data['sy'] = data['y'] - data['h'] + 1
39-
elseif data['pos'] ==# 'topcenter'
40-
let data['sx'] = data['x'] + float2nr(data['x'] / 2) + 1
41-
let data['sy'] = data['y'] - data['h']
42-
elseif data['pos'] ==# 'bottomcenter'
43-
let data['sx'] = data['x'] + float2nr(data['x'] / 2) + 1
44-
let data['sy'] = data['y'] + 1
45-
else
46-
throw 'vital: Experimental.UI.Popup: Invalid pos'
47-
endif
21+
call s:_set(data, a:opt)
4822

4923
if s:_has_nvim
5024
let buf = nvim_create_buf(0, 1)
51-
call nvim_buf_set_lines(buf, 0, -1, 1, s:_get_contents(data))
25+
call nvim_buf_set_lines(buf, 0, -1, 1, data['contents'])
5226
let opt = {
5327
\ 'relative': 'editor',
5428
\ 'style': 'minimal',
@@ -61,7 +35,7 @@ function! s:create(opt) abort
6135
let id = nvim_open_win(buf, 1, opt)
6236
else
6337
" neovim doesn't support scrollbar so don't enable it
64-
let id = popup_create(s:_get_contents(data), {
38+
let id = popup_create(data['contents'], {
6539
\ 'width': data['w'],
6640
\ 'height': data['h'],
6741
\ 'minwidth': data['w'],
@@ -77,42 +51,6 @@ function! s:create(opt) abort
7751
return id
7852
endfunction
7953

80-
function! s:_set_contents(data, contents) abort
81-
let a:data['contents'] = a:contents
82-
endfunction
83-
84-
function! s:_get_contents(data) abort
85-
return get(a:data, 'contents', [])
86-
endfunction
87-
88-
function! s:_set_w(data, w) abort
89-
let a:data['w'] = a:w
90-
endfunction
91-
92-
function! s:_set_h(data, h) abort
93-
let a:data['h'] = a:h
94-
endfunction
95-
96-
function! s:_set_y(data, y) abort
97-
if s:_has_nvim
98-
let a:data['y'] = a:y - 1
99-
else
100-
let a:data['y'] = a:y
101-
endif
102-
endfunction
103-
104-
function! s:_set_x(data, x) abort
105-
if s:_has_nvim
106-
let a:data['x'] = a:x - 1
107-
else
108-
let a:data['x'] = a:x
109-
endif
110-
endfunction
111-
112-
function! s:_set_pos(data, pos) abort
113-
let a:data['pos'] = a:pos
114-
endfunction
115-
11654
function! s:close(id) abort
11755
if has_key(s:_popups, a:id)
11856
if s:_has_nvim
@@ -124,6 +62,47 @@ function! s:close(id) abort
12462
endif
12563
endfunction
12664

65+
function! s:_set(data, opt) abort
66+
" try to set values from a:opt, if not set from a:data, if not default
67+
let a:data['w'] = get(a:opt, 'w', get(a:data, 'w', 5))
68+
let a:data['h'] = get(a:opt, 'h', get(a:data, 'h', 5))
69+
70+
if s:_has_nvim
71+
" a:opt[x/y] need to - 1
72+
" a:data['x/y'] already normalized
73+
let a:data['x'] = has_key(a:opt, 'x') ? a:opt['x'] - 1 : get(a:data, 'x', 0)
74+
let a:data['y'] = has_key(a:opt, 'y') ? a:opt['y'] - 1 : get(a:data, 'y', 0)
75+
else
76+
let a:data['x'] = get(a:opt, 'x', get(a:data, 'x', 1))
77+
let a:data['y'] = get(a:opt, 'y', get(a:data, 'y', 1))
78+
endif
79+
80+
let a:data['contents'] = get(a:opt, 'contents', get(a:data, 'contents', []))
81+
let a:data['pos'] = get(a:opt, 'pos', get(a:data, 'pos', 'topleft'))
82+
83+
if a:data['pos'] ==# 'topleft'
84+
let a:data['sx'] = a:data['x']
85+
let a:data['sy'] = a:data['y']
86+
elseif a:data['pos'] ==# 'topright'
87+
let a:data['sx'] = a:data['x'] + a:data['w'] + 1
88+
let a:data['sy'] = a:data['y']
89+
elseif a:data['pos'] ==# 'bottomleft'
90+
let a:data['sx'] = a:data['x']
91+
let a:data['sy'] = a:data['y'] - a:data['h'] + 1
92+
elseif a:data['pos'] ==# 'bottomright'
93+
let a:data['sx'] = a:data['x'] + a:data['w'] + 1
94+
let a:data['sy'] = a:data['y'] - a:data['h'] + 1
95+
elseif a:data['pos'] ==# 'topcenter'
96+
let a:data['sx'] = a:data['x'] + float2nr(a:data['x'] / 2) + 1
97+
let a:data['sy'] = a:data['y'] - a:data['h']
98+
elseif a:data['pos'] ==# 'bottomcenter'
99+
let a:data['sx'] = a:data['x'] + float2nr(a:data['x'] / 2) + 1
100+
let a:data['sy'] = a:data['y'] + 1
101+
else
102+
throw 'vital: Experimental.UI.Popup: Invalid pos'
103+
endif
104+
endfunction
105+
127106
let &cpo = s:save_cpo
128107
unlet s:save_cpo
129108
"vim: sts=2 sw=2 smarttab et ai textwidth=0 fdm=marker

0 commit comments

Comments
 (0)