Skip to content

Commit 01b7fc0

Browse files
add vital.vim's FloatingWindow
1 parent 678c059 commit 01b7fc0

File tree

6 files changed

+755
-0
lines changed

6 files changed

+755
-0
lines changed

autoload/vital/_lsp.vim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let s:_plugin_name = expand('<sfile>:t:r')
2+
3+
function! vital#{s:_plugin_name}#new() abort
4+
return vital#{s:_plugin_name[1:]}#new()
5+
endfunction
6+
7+
function! vital#{s:_plugin_name}#function(funcname) abort
8+
silent! return function(a:funcname)
9+
endfunction
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
" ___vital___
2+
" NOTE: lines between '" ___vital___' is generated by :Vitalize.
3+
" Do not modify the code nor insert new lines before '" ___vital___'
4+
function! s:_SID() abort
5+
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
6+
endfunction
7+
execute join(['function! vital#_lsp#VS#Vim#Syntax#Markdown#import() abort', printf("return map({'apply': ''}, \"vital#_lsp#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
8+
delfunction s:_SID
9+
" ___vital___
10+
function! s:apply(text, ...) abort
11+
if !exists('b:___VS_Vim_Syntax_Markdown')
12+
runtime! syntax/markdown.vim
13+
let b:___VS_Vim_Syntax_Markdown = {}
14+
endif
15+
16+
try
17+
for [l:mark, l:filetype] in items(s:_get_filetype_map(a:text, get(a:000, 0, {})))
18+
let l:group = substitute(toupper(l:mark), '\.', '_', 'g')
19+
if has_key(b:___VS_Vim_Syntax_Markdown, l:group)
20+
continue
21+
endif
22+
let b:___VS_Vim_Syntax_Markdown[l:group] = v:true
23+
24+
try
25+
if exists('b:current_syntax')
26+
unlet b:current_syntax
27+
endif
28+
execute printf('syntax include @%s syntax/%s.vim', l:group, l:filetype)
29+
execute printf('syntax region %s matchgroup=Conceal start=/%s/rs=e matchgroup=Conceal end=/%s/re=s contains=@%s containedin=ALL keepend concealends',
30+
\ l:group,
31+
\ printf('^\s*```\s*%s\s*', l:mark),
32+
\ '\s*```\s*$',
33+
\ l:group
34+
\ )
35+
catch /.*/
36+
echomsg printf('[VS.Vim.Syntax.Markdown] The `%s` is not valid filetype! You can add `"let g:markdown_fenced_languages = ["FILETYPE=%s"]`.', l:mark, l:mark)
37+
endtry
38+
endfor
39+
catch /.*/
40+
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint })
41+
endtry
42+
endfunction
43+
44+
"
45+
" _get_filetype_map
46+
"
47+
function! s:_get_filetype_map(text, filetype_map) abort
48+
let l:filetype_map = {}
49+
for l:mark in s:_find_marks(a:text)
50+
let l:filetype_map[l:mark] = s:_get_filetype_from_mark(l:mark, a:filetype_map)
51+
endfor
52+
return l:filetype_map
53+
endfunction
54+
55+
"
56+
" _find_marks
57+
"
58+
function! s:_find_marks(text) abort
59+
let l:marks = {}
60+
61+
" find from buffer contents.
62+
let l:pos = 0
63+
while 1
64+
let l:match = matchlist(a:text, '```\s*\(\w\+\)', l:pos, 1)
65+
if empty(l:match)
66+
break
67+
endif
68+
let l:marks[l:match[1]] = v:true
69+
let l:pos = matchend(a:text, '```\s*\(\w\+\)', l:pos, 1)
70+
endwhile
71+
72+
return keys(l:marks)
73+
endfunction
74+
75+
"
76+
" _get_filetype_from_mark
77+
"
78+
function! s:_get_filetype_from_mark(mark, filetype_map) abort
79+
for l:config in get(g:, 'markdown_fenced_languages', [])
80+
if l:config !~# '='
81+
if l:config ==# a:mark
82+
return a:mark
83+
endif
84+
else
85+
let l:config = split(l:config, '=')
86+
if l:config[1] ==# a:mark
87+
return l:config[0]
88+
endif
89+
endif
90+
endfor
91+
return get(a:filetype_map, a:mark, a:mark)
92+
endfunction
93+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
" ___vital___
2+
" NOTE: lines between '" ___vital___' is generated by :Vitalize.
3+
" Do not modify the code nor insert new lines before '" ___vital___'
4+
function! s:_SID() abort
5+
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
6+
endfunction
7+
execute join(['function! vital#_lsp#VS#Vim#Window#import() abort', printf("return map({'do': '', 'screenpos': ''}, \"vital#_lsp#function('<SNR>%s_' . v:key)\")", s:_SID()), 'endfunction'], "\n")
8+
delfunction s:_SID
9+
" ___vital___
10+
let s:Do = { -> {} }
11+
12+
"
13+
" do
14+
"
15+
function! s:do(winid, func) abort
16+
let l:curr_winid = win_getid()
17+
if l:curr_winid == a:winid
18+
call a:func()
19+
return
20+
endif
21+
22+
if exists('*win_execute')
23+
let s:Do = a:func
24+
try
25+
noautocmd keepalt keepjumps call win_execute(a:winid, 'call s:Do()')
26+
catch /.*/
27+
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint })
28+
endtry
29+
unlet s:Do
30+
return
31+
endif
32+
33+
noautocmd keepalt keepjumps call win_gotoid(a:winid)
34+
try
35+
call a:func()
36+
catch /.*/
37+
echomsg string({ 'exception': v:exception, 'throwpoint': v:throwpoint })
38+
endtry
39+
noautocmd keepalt keepjumps call win_gotoid(l:curr_winid)
40+
endfunction
41+
42+
"
43+
" screenpos
44+
"
45+
function! s:screenpos(pos) abort
46+
let l:pos = getpos('.')
47+
let l:scroll_x = (l:pos[2] + l:pos[3]) - wincol()
48+
let l:scroll_y = l:pos[1] - winline()
49+
let l:winpos = win_screenpos(win_getid())
50+
return [l:winpos[0] + (a:pos[0] - l:scroll_y) - 2, l:winpos[1] + (a:pos[1] - l:scroll_x) - 2]
51+
endfunction
52+

0 commit comments

Comments
 (0)