Skip to content

Commit

Permalink
Merge pull request #58 from kevinjqiu/issue-57-vendor-modified-pt
Browse files Browse the repository at this point in the history
fixes #57 vendor modified pt
  • Loading branch information
kevinjqiu committed Aug 1, 2016
2 parents dfe70f4 + fa009fc commit 51636cf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include README.rst
include LICENSE
include requirements.txt
68 changes: 67 additions & 1 deletion cdbcli/completer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
import functools

from prompt_toolkit.contrib.completers import WordCompleter
from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
from .grammar import grammar
from .commands import COMMANDS, get_all_dbs, is_view

# {{{ See https://github.com/jonathanslenders/python-prompt-toolkit/pull/344
# I modified this class to support context-aware auto-complete word list
# fetching. Once the above PR is merged and new version of prompt-toolkit
# is released, this class can be removed.
from six import string_types
from prompt_toolkit.completion import Completer, Completion


class WordCompleter(Completer): # pragma: nocover
"""
Simple autocompletion on a list of words.
:param words: List of words, or a callable that produces a list of words.
:param ignore_case: If True, case-insensitive completion.
:param meta_dict: Optional dict mapping words to their meta-information.
:param WORD: When True, use WORD characters.
:param sentence: When True, don't complete by comparing the word before the
cursor, but by comparing all the text before the cursor. In this case,
the list of words is just a list of strings, where each string can
contain spaces. (Can not be used together with the WORD option.)
:param match_middle: When True, match not only the start, but also in the
middle of the word.
"""
def __init__(self, words, ignore_case=False, meta_dict=None, WORD=False,
sentence=False, match_middle=False):
assert not (WORD and sentence)

if not callable(words):
assert all(isinstance(w, string_types) for w in words)
self.fetch_words = lambda: list(words) # noqa
else:
self.fetch_words = words
self.ignore_case = ignore_case
self.meta_dict = meta_dict or {}
self.WORD = WORD
self.sentence = sentence
self.match_middle = match_middle

@property
def words(self):
return self.fetch_words()

def get_completions(self, document, complete_event):
# Get word/text before cursor.
if self.sentence:
word_before_cursor = document.text_before_cursor
else:
word_before_cursor = document.get_word_before_cursor(WORD=self.WORD)

if self.ignore_case:
word_before_cursor = word_before_cursor.lower()

def word_matches(word):
""" True when the word before the cursor matches. """
if self.ignore_case:
word = word.lower()

if self.match_middle:
return word_before_cursor in word
else:
return word.startswith(word_before_cursor)

for a in self.words:
if word_matches(a):
display_meta = self.meta_dict.get(a, '')
yield Completion(a, -len(word_before_cursor), display_meta=display_meta)
# }}}


def fetch_db_names(environment, couch_server):
try:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
click==6.6
git+https://github.com/kevinjqiu/python-prompt-toolkit.git@dynamic-word-completer
prompt-toolkit==1.0.3
CouchDB==1.0.1
Pygments==2.1.3
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

packages=setuptools.find_packages(),

install_requires=[],
install_requires=[
req.strip() for req in open('requirements.txt').readlines() if req
],

classifiers=[
'Development Status :: 2 - Pre-Alpha',
Expand Down

0 comments on commit 51636cf

Please sign in to comment.