Skip to content

Commit ee7752b

Browse files
committed
feat(integration): General vim input helper
1 parent deef7e4 commit ee7752b

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

src/sphinxnotes/snippet/integration/binding.vim

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,8 @@ function! g:SphinxNotesSnippetListAndUrl()
3838
call g:SphinxNotesSnippetList(function('s:CallUrl'), 'ds')
3939
endfunction
4040

41-
function! g:SphinxNotesSnippetInput(id, item)
42-
let content = system(join([s:snippet, 'get', '--' . a:item, a:id, '2>/dev/null'], ' '))
43-
let content = substitute(content, '\n\+$', '', '') " skip trailing \n
44-
if a:item == 'docname'
45-
" Create doc reference.
46-
let content = ':doc:`/' . content . '`'
47-
endif
48-
execute 'normal! i' . content
49-
endfunction
50-
51-
function! g:SphinxNotesSnippetListAndInputDocname()
52-
function! s:InputDocname(selection)
53-
call g:SphinxNotesSnippetInput(s:SplitID(a:selection), 'docname')
54-
endfunction
55-
call g:SphinxNotesSnippetList(function('s:InputDocname'), 'd')
56-
endfunction
57-
5841
nmap <C-k>e :call g:SphinxNotesSnippetListAndEdit()<CR>
5942
nmap <C-k>u :call g:SphinxNotesSnippetListAndUrl()<CR>
60-
nmap <C-k>d :call g:SphinxNotesSnippetListAndInputDocname()<CR>
61-
" FIXME: can't return to insert mode even use a/startinsert!/C-o
62-
imap <C-k>d <C-o>:call g:SphinxNotesSnippetListAndInputDocname()<CR>
43+
nmap <C-k>i :call g:SphinxNotesSnippetInputSnippetAttr()<CR>
6344
6445
" vim: set shiftwidth=2:

src/sphinxnotes/snippet/integration/plugin.vim

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,94 @@
88
" NOTE: junegunn/fzf.vim is required
99

1010
let s:snippet = 'snippet'
11+
let s:width = 0.9
12+
let s:height = 0.6
1113

14+
" TODO: remove
1215
function! s:SplitID(row)
1316
return split(a:row, ' ')[0]
1417
endfunction
1518

16-
" TODO: extra opts
19+
" Use fzf to list all snippets, callback with arguments (selection).
1720
function! g:SphinxNotesSnippetList(callback, tags)
18-
let l:width = 0.9
1921
let cmd = [s:snippet, 'list',
2022
\ '--tags', a:tags,
21-
\ '--width', float2nr(&columns * l:width) - 2,
23+
\ '--width', float2nr(&columns * s:width) - 2,
2224
\ ]
2325
" https://github.com/junegunn/fzf/blob/master/README-VIM.md#fzfrun
2426
call fzf#run({
2527
\ 'source': join(cmd, ' '),
2628
\ 'sink': a:callback,
2729
\ 'options': ['--with-nth', '2..', '--no-hscroll', '--header-lines', '1'],
28-
\ 'window': {'width': l:width, 'height': 0.6},
30+
\ 'window': {'width': s:width, 'height': s:height},
2931
\ })
3032
endfunction
3133

32-
" vim: set shiftwidth=2:
34+
" Return the attribute value of specific snippet.
35+
function! g:SphinxNotesSnippetGet(id, attr)
36+
let cmd = [s:snippet, 'get', a:id, '--' . a:attr]
37+
return systemlist(join(cmd, ' '))
38+
endfunction
39+
40+
" Use fzf to list all attr of specific snippet,
41+
" callback with arguments (attr_name, attr_value).
42+
function! g:SphinxNotesSnippetListSnippetAttrs(id, callback)
43+
" Display attr -> Identify attr (also used as CLI option)
44+
let attrs = {
45+
\ 'Source': 'src',
46+
\ 'URL': 'url',
47+
\ 'Docname': 'docname',
48+
\ 'Dependent files': 'deps',
49+
\ 'Text': 'text',
50+
\ 'Title': 'title',
51+
\ }
52+
let delim = ' '
53+
let table = ['OPTION' . delim . 'ATTRIBUTE']
54+
for name in keys(attrs)
55+
call add(table, attrs[name] . delim . name)
56+
endfor
57+
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+
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
65+
endfunction
66+
67+
let preview_cmd = [s:snippet, 'get', a:id, '--$(echo {} | cut -d " " -f1)']
68+
let info_cmd = ['echo', 'Index ID:', a:id]
69+
call fzf#run({
70+
\ 'source': table,
71+
\ 'sink': function('s:SphinxNotesSnippetListSnippetAttrs_CB'),
72+
\ 'options': [
73+
\ '--header-lines', '1',
74+
\ '--with-nth', '2..',
75+
\ '--preview', join(preview_cmd, ' '),
76+
\ '--preview-window', ',wrap',
77+
\ '--info-command', join(info_cmd, ' '),
78+
\ ],
79+
\ 'window': {'width': s:width, 'height': s:height},
80+
\ })
81+
endfunction
82+
83+
function! g:SphinxNotesSnippetInputSnippetAttr(id)
84+
function! s:SphinxNotesSnippetInputSnippetAttr_CB(attr, val)
85+
if a:attr == 'docname'
86+
" Create doc reference.
87+
let content = ':doc:`/' . val[0] . '`'
88+
elif a:attr == 'title'
89+
" Create local section reference.
90+
let content = '`' . val[0] . '`_'
91+
else:
92+
let content = join(val, '<CR>')
93+
endif
94+
95+
execute 'normal! i' . content
96+
endfunction
97+
98+
call g:SphinxNotesSnippetListSnippetAttrs(a:id, function('s:SphinxNotesSnippetInputSnippetAttr_CB'))
99+
endfunction
100+
101+
" vim: set shiftwidth=2:

0 commit comments

Comments
 (0)