Skip to content

Commit a45b88b

Browse files
committed
refactor(integration): Refactor vim plugin
1 parent 242da8c commit a45b88b

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

src/sphinxnotes/snippet/integration/binding.nvim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
" TODO: Support vim?
99

1010
function! g:SphinxNotesSnippetListAndView()
11-
function! s:CallView(selection)
12-
call g:SphinxNotesSnippetView(s:SplitID(a:selection))
11+
function! ListAndView_CB(id)
12+
call g:SphinxNotesSnippetView(a:id)
1313
endfunction
14-
call g:SphinxNotesSnippetList(function('s:CallView'), '*')
14+
call g:SphinxNotesSnippetList('"*"', function('ListAndView_CB'))
1515
endfunction
1616

1717
" https://github.com/anhmv/vim-float-window/blob/master/plugin/float-window.vim

src/sphinxnotes/snippet/integration/binding.vim

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
" :Version: 20211114
77
"
88

9-
function! g:SphinxNotesSnippetEdit(id)
10-
let file = system(join([s:snippet, 'get', '--file', a:id, '2>/dev/null'], ' '))
11-
let line = system(join([s:snippet, 'get', '--line-start', a:id, '2>/dev/null'], ' '))
9+
function g:SphinxNotesSnippetEdit(id)
10+
let file = g:SphinxNotesSnippetGet(a:id, 'file')[0]
11+
let line = g:SphinxNotesSnippetGet(a:id, 'line-start')[0]
1212
if &modified
1313
execute 'vsplit ' . file
1414
else
@@ -17,25 +17,25 @@ function! g:SphinxNotesSnippetEdit(id)
1717
execute line
1818
endfunction
1919

20-
function! g:SphinxNotesSnippetListAndEdit()
21-
function! s:CallEdit(selection)
22-
call g:SphinxNotesSnippetEdit(s:SplitID(a:selection))
20+
function g:SphinxNotesSnippetListAndEdit()
21+
function! ListAndEdit_CB(id)
22+
call g:SphinxNotesSnippetEdit(a:id)
2323
endfunction
24-
call g:SphinxNotesSnippetList(function('s:CallEdit'), 'ds')
24+
call g:SphinxNotesSnippetList('ds', function('ListAndEdit_CB'))
2525
endfunction
2626

27-
function! g:SphinxNotesSnippetUrl(id)
28-
let url_list = systemlist(join([s:snippet, 'get', '--url', a:id, '2>/dev/null'], ' '))
27+
function g:SphinxNotesSnippetUrl(id)
28+
let url_list = g:SphinxNotesSnippetGet(a:id, 'url')
2929
for url in url_list
3030
echo system('xdg-open ' . shellescape(url))
3131
endfor
3232
endfunction
3333

34-
function! g:SphinxNotesSnippetListAndUrl()
35-
function! s:CallUrl(selection)
36-
call g:SphinxNotesSnippetUrl(s:SplitID(a:selection))
34+
function g:SphinxNotesSnippetListAndUrl()
35+
function! ListAndUrl_CB(id)
36+
call g:SphinxNotesSnippetUrl(a:id)
3737
endfunction
38-
call g:SphinxNotesSnippetList(function('s:CallUrl'), 'ds')
38+
call g:SphinxNotesSnippetList('ds', function('ListAndUrl_CB'))
3939
endfunction
4040

4141
nmap <C-k>e :call g:SphinxNotesSnippetListAndEdit()<CR>

src/sphinxnotes/snippet/integration/plugin.vim

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,39 @@ let s:snippet = 'snippet'
1111
let s:width = 0.9
1212
let s:height = 0.6
1313

14-
" TODO: remove
15-
function! s:SplitID(row)
16-
return split(a:row, ' ')[0]
17-
endfunction
18-
19-
" Use fzf to list all snippets, callback with arguments (selection).
20-
function! g:SphinxNotesSnippetList(callback, tags)
14+
" Use fzf to list all snippets, callback with argument id.
15+
function g:SphinxNotesSnippetList(tags, callback)
2116
let cmd = [s:snippet, 'list',
2217
\ '--tags', a:tags,
2318
\ '--width', float2nr(&columns * s:width) - 2,
2419
\ ]
20+
21+
" Use closure keyword so that inner function can access outer one's
22+
" localvars (l:) and arguments (a:).
23+
" https://vi.stackexchange.com/a/21807
24+
function! List_CB(selection) closure
25+
let id = split(a:selection, ' ')[0]
26+
call a:callback(id)
27+
endfunction
28+
2529
" https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun
2630
call fzf#run({
2731
\ 'source': join(cmd, ' '),
28-
\ 'sink': a:callback,
32+
\ 'sink': function('List_CB'),
2933
\ 'options': ['--with-nth', '2..', '--no-hscroll', '--header-lines', '1'],
3034
\ 'window': {'width': s:width, 'height': s:height},
3135
\ })
3236
endfunction
3337

3438
" Return the attribute value of specific snippet.
35-
function! g:SphinxNotesSnippetGet(id, attr)
39+
function g:SphinxNotesSnippetGet(id, attr)
3640
let cmd = [s:snippet, 'get', a:id, '--' . a:attr]
3741
return systemlist(join(cmd, ' '))
3842
endfunction
3943

4044
" Use fzf to list all attr of specific snippet,
4145
" callback with arguments (attr_name, attr_value).
42-
function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
46+
function g:SphinxNotesSnippetListSnippetAttrs(id, callback)
4347
" Display attr -> Identify attr (also used as CLI option)
4448
let attrs = {
4549
\ 'Source': 'src',
@@ -55,20 +59,17 @@ function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
5559
call add(table, attrs[name] . delim . name)
5660
endfor
5761

58-
" Local scope -> script scope, so vars can be access from inner function.
59-
let s:id_for_list_snippet_attrs = a:id
60-
let s:cb_for_list_snippet_attrs = a:callback
61-
function! s:SphinxNotesSnippetListSnippetAttrs_CB(selection)
62+
function! ListSnippetAttrs_CB(selection) closure
6263
let opt = split(a:selection, ' ')[0]
63-
let val = g:SphinxNotesSnippetGet(s:id_for_list_snippet_attrs, opt)
64-
call s:cb_for_list_snippet_attrs(opt, val) " finally call user's cb
64+
let val = g:SphinxNotesSnippetGet(a:id, opt)
65+
call a:callback(opt, val) " finally call user's cb
6566
endfunction
6667

6768
let preview_cmd = [s:snippet, 'get', a:id, '--$(echo {} | cut -d " " -f1)']
6869
let info_cmd = ['echo', 'Index ID:', a:id]
6970
call fzf#run({
7071
\ 'source': table,
71-
\ 'sink': function('s:SphinxNotesSnippetListSnippetAttrs_CB'),
72+
\ 'sink': function('ListSnippetAttrs_CB'),
7273
\ 'options': [
7374
\ '--header-lines', '1',
7475
\ '--with-nth', '2..',
@@ -80,8 +81,8 @@ function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
8081
\ })
8182
endfunction
8283

83-
function! g:SphinxNotesSnippetInput(id)
84-
function! s:SphinxNotesSnippetInput_CB(attr, val)
84+
function g:SphinxNotesSnippetInput(id)
85+
function! Input_CB(attr, val) " TODO: became g:func.
8586
if a:attr == 'docname'
8687
" Create doc reference.
8788
let content = ':doc:`/' . a:val[0] . '`'
@@ -91,20 +92,18 @@ function! g:SphinxNotesSnippetInput(id)
9192
else
9293
let content = join(a:val, '<CR>')
9394
endif
94-
9595
execute 'normal! i' . content
9696
endfunction
9797

98-
call g:SphinxNotesSnippetListSnippetAttrs(a:id, function('s:SphinxNotesSnippetInput_CB'))
98+
call g:SphinxNotesSnippetListSnippetAttrs(a:id, function('Input_CB'))
9999
endfunction
100100

101-
function! g:SphinxNotesSnippetListAndInput()
102-
function! s:SphinxNotesSnippetListAndInput_CB(selection)
103-
let id = s:SplitID(a:selection)
104-
call g:SphinxNotesSnippetInput(id)
101+
function g:SphinxNotesSnippetListAndInput()
102+
function! ListAndInput_CB(id)
103+
call g:SphinxNotesSnippetInput(a:id)
105104
endfunction
106105

107-
call g:SphinxNotesSnippetList(function('s:SphinxNotesSnippetListAndInput_CB'), '"*"')
106+
call g:SphinxNotesSnippetList('"*"', function('ListAndInput_CB'))
108107
endfunction
109108

110109
" vim: set shiftwidth=2:

0 commit comments

Comments
 (0)