Skip to content

Instantly share code, notes, and snippets.

@aquach
Last active February 19, 2016 18:43
Show Gist options
  • Save aquach/774b0a51e47301ff365d to your computer and use it in GitHub Desktop.
Save aquach/774b0a51e47301ff365d to your computer and use it in GitHub Desktop.
Unite.vim sorter for burke/matcher (provides Command-T fuzzy matching for Unite)
" Paste this into your .vimrc.
" It's very janky but hopefully easy to tweak for your own purposes.
" If you want to make this into a real plugin, be my guest.
" To use, put this in your .vimrc:
" call unite#custom#source('<source name>', 'sorters', 'sorter_burkematcher')
python << endpython
import vim
import subprocess
def rank_files():
candidates = vim.eval('a:candidates')
input_str = vim.eval('a:context.input')
candidate_filenames = '\n'.join(c['word'] for c in candidates)
p = subprocess.Popen([ 'matcher', '--limit', '100', input_str ], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(candidate_filenames)
ranked_filenames = p.communicate()[0].split("\n")
p.stdin.close()
return dict([ (v, i) for i, v in enumerate(ranked_filenames) ])
def score(ranks_by_filename):
worst_rank = len(ranks_by_filename)
c = vim.eval('candidate')
cost = ranks_by_filename.get(c['word'], worst_rank)
vim.command('let candidate.filter__cost = %d' % cost)
endpython
let sorter_burkematcher = { 'name': 'sorter_burkematcher' }
function! sorter_burkematcher.filter(candidates, context)
if a:context.input == '' || !has('float') || empty(a:candidates)
return a:candidates
endif
python ranks = rank_files()
for candidate in a:candidates
python score(ranks)
endfor
return unite#util#sort_by(a:candidates, 'v:val.filter__cost')
endfunction
call unite#define_filter(sorter_burkematcher)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment