fixed up ptk completer a bit

This commit is contained in:
Anthony Scopatz 2017-02-27 01:07:24 -05:00
parent 8920cc3d4e
commit b46020f1d4
2 changed files with 23 additions and 2 deletions

14
news/ptkcomp.rst Normal file
View file

@ -0,0 +1,14 @@
**Added:** None
**Changed:** None
**Deprecated:** None
**Removed:** None
**Fixed:**
* PTK completions will now correctly deduplicate autosuggest completions
and display completions values based on the cursor position.
**Security:** None

View file

@ -48,6 +48,7 @@ class PromptToolkitCompleter(Completer):
endidx + expand_offset, endidx + expand_offset,
self.ctx) self.ctx)
# completions from auto suggest # completions from auto suggest
sug_comp = None
if env.get('AUTO_SUGGEST'): if env.get('AUTO_SUGGEST'):
sug_comp = self.suggestion_completion(document, line) sug_comp = self.suggestion_completion(document, line)
if sug_comp is None: if sug_comp is None:
@ -55,7 +56,9 @@ class PromptToolkitCompleter(Completer):
elif len(completions) == 0: elif len(completions) == 0:
completions = (sug_comp,) completions = (sug_comp,)
else: else:
completions = (sug_comp,) + completions completions = set(completions)
completions.discard(sug_comp)
completions = (sug_comp,) + tuple(sorted(completions))
# reserve space, if needed. # reserve space, if needed.
if len(completions) <= 1: if len(completions) <= 1:
pass pass
@ -69,9 +72,13 @@ class PromptToolkitCompleter(Completer):
break break
c_prefix = c_prefix[:-1] c_prefix = c_prefix[:-1]
# yield completions # yield completions
if sug_comp is None:
pre = min(document.cursor_position_col - begidx, len(c_prefix))
else:
pre = len(c_prefix)
for comp in completions: for comp in completions:
# do not display quote # do not display quote
disp = comp.strip('\'"')[len(c_prefix):] disp = comp[pre:].strip('\'"')
yield Completion(comp, -l, display=disp) yield Completion(comp, -l, display=disp)
def suggestion_completion(self, document, line): def suggestion_completion(self, document, line):