Skip to content

Instantly share code, notes, and snippets.

@junegunn
Last active May 10, 2023 15:50
Show Gist options
  • Save junegunn/5dff641d68d20ba309ce to your computer and use it in GitHub Desktop.
Save junegunn/5dff641d68d20ba309ce to your computer and use it in GitHub Desktop.
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 = "http://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
@junegunn
Copy link
Author

@mirzalazuardi Place the file in ~/.vim/plugin directory or simply paste the code onto your .vimrc.

@lalitmee
Copy link

@junegunn, when I tried using this, it was failing for me with some Open.uri from kernel is deprecated. I did a little bit of searching and fixed it by only using URI.open(URL) instead of open(URL).

Here is the updated code:

" ----------------------------------------------------------------------------
" 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  = "http://vimawesome.com/api/plugins?page=#{page}&query=#{query}"
      data = URI.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

@hungpham3112
Copy link

I'm using your script and gvim on Windows shows message:

[vim-plug] vim - Invalid argument: vim - (implicit vim-scripts expansion is deprecated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment