Created
April 14, 2016 08:39
-
-
Save balta2ar/7955094dbf88c4cf0a386f18d03ef447 to your computer and use it in GitHub Desktop.
RT completer based on YouCompleteMe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals | |
from __future__ import print_function | |
from __future__ import division | |
from __future__ import absolute_import | |
from future import standard_library | |
standard_library.install_aliases() | |
from builtins import * # noqa | |
from ycmd.completers.general_completer import GeneralCompleter | |
from ycmd import responses | |
import re | |
from time import strftime | |
def log(msg): | |
timestamp = strftime("%Y-%m-%d %H:%M:%S") | |
with open('/tmp/rt.completer.log', 'a+') as file_object: | |
file_object.write('%s ' % timestamp + msg + '\n') | |
#log('MODULE IMPORT') | |
CANDIDATES_FILENAME = '/tmp/rt.candidates.txt' | |
class RTCompleter( GeneralCompleter ): | |
""" | |
Completer for YouCompleteMe (ycmd) that completes RequestTracker ticket | |
numbers by their titles. | |
""" | |
def __init__( self, user_options ): | |
super( RTCompleter, self ).__init__( user_options ) | |
log('RTCompleter: init') | |
self._candidates = None | |
self._filtered_candidates = None | |
self._read_candidates() | |
def _read_candidates(self): | |
with open(CANDIDATES_FILENAME) as file_object: | |
lines = file_object.readlines() | |
self._candidates = [str(line.strip()) for line in lines] | |
def ShouldUseNow( self, request_data ): | |
log('ShouldUseNow: init') | |
query = request_data['query'] | |
return query.startswith('RT:') or query.startswith('RT') | |
def ComputeCandidates( self, request_data ): | |
log('ComputeCandidates: init') | |
log(str(request_data._cached_computed)) | |
if not self.ShouldUseNow( request_data ): | |
return [] | |
self._read_candidates() | |
query = request_data['query'] | |
if query.startswith('RT'): | |
query = query[2:] | |
log('ComputeCandidates: query >%s<' % query) | |
candidates = self._candidates | |
#make_rx = lambda x: re.compile('.*' + re.sub(r'(.)', r'\1.*', x), re.IGNORECASE) | |
make_rx = lambda x: re.compile('.*%s.*' % x.lower(), re.IGNORECASE) | |
subwords = [make_rx(x) for x in query.split('_')] | |
results = [x for x in candidates if self._match_candidate(x.lower(), subwords)] | |
log('CANDIDATES: %s' % results) | |
return [responses.BuildCompletionData('RT:' + x[:6], menu_text=x) | |
for x in results] | |
def _match_candidate(self, candidate, subwords): | |
for subword in subwords: | |
if not subword.match(candidate): | |
return False | |
return True | |
def OnBufferVisit( self, request_data ): | |
log('OnBufferVisit: init') | |
self._read_candidates() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment