From f7c4a493e63a62730626d7e5a005c1eadf8e5ffd Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Wed, 7 Jun 2017 22:19:51 -0400 Subject: [PATCH] Use completion base text for issue search --- autoload/rhubarb.vim | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/autoload/rhubarb.vim b/autoload/rhubarb.vim index 0b31323..5cb140d 100644 --- a/autoload/rhubarb.vim +++ b/autoload/rhubarb.vim @@ -173,23 +173,42 @@ function! rhubarb#repo_request(...) abort return rhubarb#request('repos/%s' . (a:0 && a:1 !=# '' ? '/' . a:1 : ''), a:0 > 1 ? a:2 : {}) endfunction +function! s:url_encode(str) abort + return substitute(a:str, '[?@=&<>%#/:+[:space:]]', '\=submatch(0)==" "?"+":printf("%%%02X", char2nr(submatch(0)))', 'g') +endfunction + +function! rhubarb#repo_search(type, q) abort + return rhubarb#request('search/'.a:type.'?per_page=100&q=repo:%s'.s:url_encode(' '.a:q)) +endfunction + " Section: Issues +let s:reference = '\<\%(\c\%(clos\|resolv\|referenc\)e[sd]\=\|\cfix\%(e[sd]\)\=\)\>' function! rhubarb#omnifunc(findstart,base) abort if a:findstart - let existing = matchstr(getline('.')[0:col('.')-1],'#\d*$\|@[[:alnum:]-]*$') + let existing = matchstr(getline('.')[0:col('.')-1],s:reference.'\s\+\zs[^#/,.;]*$\|[#@[:alnum:]-]*$') return col('.')-1-strlen(existing) endif try if a:base =~# '^@' return map(rhubarb#repo_request('collaborators'), '"@".v:val.login') else - let prefix = (a:base =~# '^#' ? '#' : s:repo_homepage().'/issues/') - let issues = rhubarb#repo_request('issues') - if type(issues) == type({}) - call s:throw(get(issues, 'message', 'unknown error')) + if a:base =~# '^#' + let prefix = '#' + let query = '' + else + let prefix = s:repo_homepage().'/issues/' + let query = a:base endif - return map(issues, '{"word": prefix.v:val.number, "menu": v:val.title, "info": substitute(v:val.body,"\\r","","g")}') + let response = rhubarb#repo_search('issues', 'state:open '.query) + if type(response) != type({}) + call s:throw('unknown error') + elseif has_key(response, 'message') + call s:throw(response[message]) + else + let issues = get(response, 'items', []) + endif + return map(issues, '{"word": prefix.v:val.number, "abbr": "#".v:val.number, "menu": v:val.title, "info": substitute(v:val.body,"\\r","","g")}') endif catch /^\%(fugitive\|rhubarb\):/ echoerr v:errmsg