Last active
May 15, 2018 17:51
-
-
Save balta2ar/0e0216bee6f1a6c9e177f8abb60f7b69 to your computer and use it in GitHub Desktop.
Deoplete sample prefix source
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 .base import Base | |
def log(msg): | |
with open('/tmp/deoplete-jira.log', 'a') as file_: | |
file_.write('%s\n' % msg) | |
from jira_rt_completion_server.jira_completer import JiraCompleter | |
class Source(Base): | |
def __init__(self, vim): | |
Base.__init__(self, vim) | |
self._completer = JiraCompleter('~/.cache/jira/jira.candidates.tsv') | |
self.debug_enabled = True | |
self.name = 'jira' | |
self.mark = '[JIRA]' | |
# Use these options if you want to filter candidates yourself | |
self.is_volatile = True | |
self.matchers = [] # ['matcher_cpsm'] | |
self.sorters = [] | |
self.max_menu_width = 150 | |
self.max_abbr_width = 150 | |
self.input_pattern = self._completer.input_pattern | |
def get_complete_position(self, context): | |
return self._completer.get_complete_position(context) | |
def gather_candidates(self, context): | |
return self._completer.gather_candidates(context) | |
def on_post_filter(self, context): | |
return self._completer.on_post_filter(context) |
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
""" | |
This is JIRE source plugin for deoplete. It completes JIRE issue keys from | |
a cache file. | |
""" | |
import re | |
import cpsm_py | |
from cpsm_py import ctrlp_match | |
from os.path import expanduser, expandvars | |
from datetime import datetime | |
def log(msg): | |
return | |
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S,%f") | |
with open('/tmp/deoplete-jira.log', 'a') as file_: | |
file_.write('%s %s\n' % (timestamp, msg)) | |
class JiraCompleter(): | |
def __init__(self, source_filename): | |
log('Creating from file: %s' % source_filename) | |
self._source_filename = source_filename | |
self._max_key_len = 0 | |
self.input_pattern = r'JI:?\w*$' | |
self.input_prefix = r'JI:?' | |
def _load_lines(self): | |
# 0 - key | |
# 1 - subject | |
# 2 - status | |
# 3 - owner | |
# 4 - last updated | |
filename = expanduser(expandvars(self._source_filename)) | |
with open(filename) as file_: | |
lines = [line.strip().split('\t') for line in file_.readlines()] | |
log('_load_lines: #: %s' % len(lines)) | |
return lines | |
def gather_candidates(self, context): | |
log('gather_candidates: input: %s' % context['input']) | |
result = [] | |
lines = self._load_lines() | |
self._max_key_len = max(len(x[0]) for x in lines) | |
formatter = '%' + str(self._max_key_len) + 's %s' | |
log('gather_candidates: formatter: %s' % formatter) | |
for line in lines: | |
short = line[0] | |
long_ = line[1] | |
result.append({'word': short, 'abbr': formatter % (short, long_)}) | |
log('RESULT len: %s' % len(result)) | |
return result | |
def on_post_filter(self, context): | |
query = self._input_to_query(context['input']) | |
log('on_post_filter: input: %s, query: %s' % (context['input'], query)) | |
if not query: | |
return context['candidates'] | |
return self._filter_candidates_subject_only(context['candidates'], query) | |
def _input_to_query(self, input_): | |
m = re.search(self.input_prefix, input_) | |
return input_[m.end():] if m else '' | |
def get_complete_position(self, context): | |
pos = self._get_input_position(context['input']) | |
log('get_complete_position: input: %s, pos: %s' % (context['input'], pos)) | |
return pos | |
def _get_input_position(self, input_): | |
m = re.search(self.input_pattern, input_) | |
return m.start() if m else -1 | |
def _get_candidates(self): | |
lines = self._load_lines() | |
candidates = [{'word': line[0], 'abbr': line[1]} for line in lines] | |
return candidates | |
def _filter_candidates_subject_only(self, candidates, query): | |
""" | |
This version runs matches again issue subject only. If you want to | |
include issue key as well, use the version below. | |
""" | |
cut = self._max_key_len | |
log('filter_candidates: query: %s, cut: %s' % (query, cut)) | |
subject_to_candidate = {x['abbr'][cut+1:]: (x['word'], x['abbr']) for x in candidates} | |
lines = [x['abbr'][cut+1:] for x in candidates] | |
log('filter_candidates: lines: %s' % (lines,)) | |
result, _ranks = ctrlp_match(lines, query) | |
candidates = [{'word': subject_to_candidate[x][0], | |
'abbr': subject_to_candidate[x][1]} | |
for x in result] | |
return candidates | |
def make(): | |
completer = JiraCompleter('~/.cache/jira/jira.candidates.tsv') | |
return completer | |
def test(query=''): | |
from pprint import pprint | |
completer = JiraCompleter('~/.cache/jira/jira.candidates.tsv') | |
candidates = completer._get_candidates() | |
pprint(candidates) | |
print('-' * 50) | |
filtered = completer._filter_candidates_subject_only(candidates, query) | |
pprint(filtered) | |
print('INPUT', completer._input_to_query('JItest')) | |
print('INPUT', completer._input_to_query('JI?test')) | |
print('INPUT', completer._input_to_query('JFF')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment