Skip to content

Instantly share code, notes, and snippets.

@ur4ltz
Forked from junegunn/vimawesome.vim
Last active November 14, 2019 00:12
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Plugin completion using VimAwesome API
" ----------------------------------------------------------------------------
" vimawesome.com
" ----------------------------------------------------------------------------
function! VimAwesomeComplete() abort
let prefix = matchstr(strpart(getline('.'), 0, col('.') - 1), '[.a-zA-Z0-9_/-]*$')
echohl WarningMsg
echo 'Downloading plugin list from VimAwesome'
echohl None
ruby << EOF
require 'json'
require 'open-uri'
query = VIM::evaluate('prefix').gsub('/', '%20')
items = 1.upto(max_pages = 3).map do |page|
Thread.new do
url = "https://vimawesome.com/api/plugins?page=#{page}&query=#{query}"
data = open(url).read
json = JSON.parse(data, symbolize_names: true)
json[:plugins].map do |info|
pair = info.values_at :github_owner, :github_repo_name
next if pair.any? { |e| e.nil? || e.empty? }
{word: pair.join('/'),
menu: info[:category].to_s,
info: info.values_at(:short_desc, :author).compact.join($/)}
end.compact
end
end.each(&:join).map(&:value).inject(:+)
VIM::command("let cands = #{JSON.dump items}")
EOF
if !empty(cands)
inoremap <buffer> <c-v> <c-n>
augroup _VimAwesomeComplete
autocmd!
autocmd CursorMovedI,InsertLeave * iunmap <buffer> <c-v>
\| autocmd! _VimAwesomeComplete
augroup END
call complete(col('.') - strchars(prefix), cands)
endif
return ''
endfunction
augroup VimAwesomeComplete
autocmd!
autocmd FileType vim inoremap <c-x><c-v> <c-r>=VimAwesomeComplete()<cr>
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment