Skip to content

Instantly share code, notes, and snippets.

@balta2ar
Created May 7, 2016 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balta2ar/f452af034da2d4027ae36c92d8cf9ba9 to your computer and use it in GitHub Desktop.
Save balta2ar/f452af034da2d4027ae36c92d8cf9ba9 to your computer and use it in GitHub Desktop.
RT completer based on deoplete
"""
This is RT source plugin for deoplete. It completes RequestTicket numbers.
"""
from .base import Base
import re
from time import strftime
from pprint import pformat
CANDIDATES_FILENAME = '/tmp/rt.candidates.txt'
RT_PATTERN = r'RT:?\w*$'
RX_RT = re.compile(RT_PATTERN, re.IGNORECASE)
def log(msg):
#return
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')
# Taken from: https://github.com/amjith/fuzzyfinder
def fuzzyfinder(text, collection):
suggestions = []
text = str(text) if not isinstance(text, str) else text
pat = '.*?'.join(map(re.escape, text))
regex = re.compile(pat, re.IGNORECASE)
for item in collection:
r = regex.search(item)
if r:
suggestions.append((len(r.group()), r.start(), item))
return [z for _, _, z in sorted(suggestions)]
def read_candidates(filename):
with open(filename) as file_object:
lines = file_object.readlines()
candidates = [str(line.strip()) for line in lines]
return candidates
class Source(Base):
def __init__(self, vim):
Base.__init__(self, vim)
self.name = 'request_tracker'
#self.kind = 'keyword'
self.mark = '[RT]'
self.min_pattern_length = 2
self.matchers = []
self.sorters = []
self.max_menu_width = 120
self.max_abbr_width = 120
self.input_pattern = RT_PATTERN
def get_complete_position(self, context):
log('GET POS: ' + pformat(context))
match = RX_RT.search(context['input'])
pos = match.start() if match else -1
log('GET POS RESULT: ' + pformat(pos))
return pos
def gather_candidates(self, context):
log('GATHER: ' + pformat(context))
# Cut off and remember RT prefix
complete_str = context['complete_str']
if complete_str.startswith('RT:'):
prefix = 'RT:'
complete_str = complete_str[3:]
elif complete_str.startswith('RT'):
prefix = 'RT'
complete_str = complete_str[2:]
log('COMPLETE STR: %s' % complete_str)
# Read candidates, cut off http part and fuzzy match by long description
candidates_from_file = read_candidates(CANDIDATES_FILENAME)
candidates_without_http = []
for candidate in candidates_from_file:
pos = candidate.find(' http')
candidates_without_http.append(candidate[:pos] if pos != -1 else candidate)
filtered_candidates = fuzzyfinder(complete_str, candidates_without_http)
result = []
for x in filtered_candidates:
short = prefix + x[:6]
long = prefix + x
item = {'word': short, 'abbr': long}
result.append(item)
log('GATHER CAND: ' + str(result))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment