I broke up with neovim....vim is my best friend now

This commit is contained in:
LinlyBoi
2023-04-30 08:14:07 +03:00
parent 0d185449c5
commit 4a4a6b1e81
5245 changed files with 468325 additions and 25 deletions

View File

@@ -0,0 +1,88 @@
let s:cache = {}
function! vsnip#source#snipmate#refresh(path) abort
if has_key(s:cache, a:path)
unlet s:cache[a:path]
endif
endfunction
function! vsnip#source#snipmate#find(bufnr) abort
let filetypes = vsnip#source#filetypes(a:bufnr)
return s:find(filetypes, a:bufnr)
endfunction
function! s:find(filetypes, bufnr) abort
let sources = []
for path in s:get_source_paths(a:filetypes, a:bufnr)
if !has_key(s:cache, path)
let s:cache[path] = s:create(path, a:bufnr)
endif
call add(sources, s:cache[path])
endfor
return sources
endfunction
function! s:get_source_paths(filetypes, bufnr) abort
let paths = []
for dir in s:get_source_dirs(a:bufnr)
for filetype in a:filetypes
let path = resolve(expand(printf('%s/%s.snippets', dir, filetype)))
if has_key(s:cache, path) || filereadable(path)
call add(paths, path)
endif
endfor
endfor
return paths
endfunction
function! s:get_source_dirs(bufnr) abort
let dirs = []
let buf_dir = getbufvar(a:bufnr, 'vsnip_snippet_dir', '')
if buf_dir !=# ''
let dirs += [buf_dir]
endif
let dirs += getbufvar(a:bufnr, 'vsnip_snippet_dirs', [])
let dirs += [g:vsnip_snippet_dir]
let dirs += g:vsnip_snippet_dirs
return dirs
endfunction
function! s:create(path, bufnr) abort
let file = readfile(a:path)
let file = type(file) == v:t_list ? file : [file]
call map(file, { _, f -> iconv(f, 'utf-8', &encoding) })
let source = []
let i = -1
while i + 1 < len(file)
let [i, line] = [i + 1, file[i + 1]]
if line =~# '^\(#\|\s*$\)'
" Comment, or blank line before snippets
elseif line =~# '^extends\s\+\S'
let filetypes = map(split(line[7:], ','), 'trim(v:val)')
let source += flatten(s:find(filetypes, a:bufnr))
elseif line =~# '^snippet\s\+\S' && i + 1 < len(file)
let matched = matchlist(line, '^snippet\s\+\(\S\+\)\s*\(.*\)')
let [prefix, description] = [matched[1], matched[2]]
let body = []
let indent = matchstr(file[i + 1], '^\s\+')
while i + 1 < len(file) && file[i + 1] =~# '^\(' . indent . '\|\s*$\)'
let [i, line] = [i + 1, file[i + 1]]
call add(body, line[strlen(indent):])
endwhile
let [prefixes, prefixes_alias] = vsnip#source#resolve_prefix(prefix)
call add(source, {
\ 'label': prefix,
\ 'prefix': prefixes,
\ 'prefix_alias': prefixes_alias,
\ 'body': body,
\ 'description': description
\ })
else
echohl ErrorMsg
echomsg printf('[vsnip] Parsing error occurred on: %s#L%s', a:path, i + 1)
echohl None
break
endif
endwhile
return sort(source, { a, b -> strlen(b.prefix[0]) - strlen(a.prefix[0]) })
endfunction

View File

@@ -0,0 +1,69 @@
let s:cache = {}
"
" vsnip#source#user_snippet#find.
"
function! vsnip#source#user_snippet#find(bufnr) abort
let l:sources = []
for l:path in s:get_source_paths(a:bufnr)
if !has_key(s:cache, l:path)
let s:cache[l:path] = vsnip#source#create(l:path)
endif
call add(l:sources, s:cache[l:path])
endfor
return l:sources
endfunction
"
" vsnip#source#user_snippet#refresh.
"
function! vsnip#source#user_snippet#refresh(path) abort
if has_key(s:cache, a:path)
unlet s:cache[a:path]
endif
endfunction
function! s:get_source_dirs(bufnr) abort
let l:dirs = []
let l:buf_dir = getbufvar(a:bufnr, 'vsnip_snippet_dir', v:null)
if l:buf_dir isnot v:null
let l:dirs += [l:buf_dir]
endif
let l:dirs += getbufvar(a:bufnr, 'vsnip_snippet_dirs', [])
let l:dirs += [g:vsnip_snippet_dir]
let l:dirs += g:vsnip_snippet_dirs
return l:dirs
endfunction
"
" get_source_paths.
"
function! s:get_source_paths(bufnr) abort
let l:filetypes = vsnip#source#filetypes(a:bufnr)
let l:paths = []
for l:dir in s:get_source_dirs(a:bufnr)
for l:filetype in l:filetypes
let l:path = resolve(expand(printf('%s/%s.json', l:dir, l:filetype)))
if has_key(s:cache, l:path) || filereadable(l:path)
call add(l:paths, l:path)
endif
endfor
endfor
return l:paths
endfunction
"
" vsnip#source#user_snippet#dirs
"
fun! vsnip#source#user_snippet#dirs(...) abort
return s:get_source_dirs(a:0 ? a:1 : bufnr(''))
endfun
"
" vsnip#source#user_snippet#paths
"
fun! vsnip#source#user_snippet#paths(...) abort
return s:get_source_paths(a:0 ? a:1 : bufnr(''))
endfun

View File

@@ -0,0 +1,104 @@
let s:snippets = {}
let s:runtimepaths = {}
"
" vsnip#source#vscode#refresh.
"
function! vsnip#source#vscode#refresh(path) abort
if has_key(s:snippets, a:path)
unlet s:snippets[a:path]
for [l:rtp, l:v] in items(s:runtimepaths)
if stridx(l:rtp, a:path) == 0
unlet s:runtimepaths[l:rtp]
endif
endfor
endif
endfunction
"
" vsnip#source#vscode#find.
"
function! vsnip#source#vscode#find(bufnr) abort
return s:find(map(vsnip#source#filetypes(a:bufnr), 's:get_language(v:val)'))
endfunction
"
" find.
"
function! s:find(languages) abort
" Load `package.json#contributes.snippets` if does not exists it's cache.
let l:rtp_list = exists('*nvim_list_runtime_paths') ? nvim_list_runtime_paths() : split(&runtimepath, ',')
for l:rtp in l:rtp_list
if has_key(s:runtimepaths, l:rtp)
continue
endif
let s:runtimepaths[l:rtp] = v:true
try
let l:package_json = resolve(expand(l:rtp . '/package.json'))
if !filereadable(l:package_json)
continue
endif
let l:package_json = readfile(l:package_json)
let l:package_json = type(l:package_json) == type([]) ? join(l:package_json, "\n") : l:package_json
let l:package_json = iconv(l:package_json, 'utf-8', &encoding)
let l:package_json = json_decode(l:package_json)
" if package.json has not `contributes.snippets` fields, skip it.
if !has_key(l:package_json, 'contributes')
\ || !has_key(l:package_json.contributes, 'snippets')
continue
endif
" Create source if does not exists it's cache.
for l:snippet in l:package_json.contributes.snippets
let l:path = resolve(expand(l:rtp . '/' . l:snippet.path))
let l:languages = type(l:snippet.language) == type([]) ? l:snippet.language : [l:snippet.language]
" if already cached `snippets.json`, add new language.
if has_key(s:snippets, l:path)
for l:language in l:languages
if index(s:snippets[l:path].languages, l:language) == -1
call add(s:snippets[l:path].languages, l:language)
endif
endfor
continue
endif
" register new snippet.
let s:snippets[l:path] = {
\ 'languages': l:languages,
\ }
endfor
catch /.*/
endtry
endfor
" filter by language.
let l:sources = []
for l:language in a:languages
for [l:path, l:snippet] in items(s:snippets)
if index(l:snippet.languages, l:language) >= 0
if !has_key(l:snippet, 'source')
let l:snippet.source = vsnip#source#create(l:path)
end
call add(l:sources, l:snippet.source)
endif
endfor
endfor
return l:sources
endfunction
"
" get_language.
"
function! s:get_language(filetype) abort
return get({
\ 'javascript.jsx': 'javascriptreact',
\ 'typescript.tsx': 'typescriptreact',
\ 'sh': 'shellscript',
\ 'cs': 'csharp',
\ }, a:filetype, a:filetype)
endfunction