blob: c69a385fac1776b1a96bf85de082a83833566232 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# Copyright (C) 2011-2019 Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
import re
from .completer import Completer
class StringlistCompleter(Completer):
"""Completer for a fixed list of strings."""
def __init__(self, resultlist, ignorecase=True, match_anywhere=False):
"""
:param resultlist: strings used for completion
:type resultlist: list of str
:param liberal: match case insensitive and not prefix-only
:type liberal: bool
"""
self.resultlist = resultlist
self.flags = re.IGNORECASE if ignorecase else 0
self.match_anywhere = match_anywhere
super().__init__()
def complete(self, original, pos):
pref = original[:pos]
re_prefix = '.*' if self.match_anywhere else ''
def match(s, m):
r = '{}{}.*'.format(re_prefix, re.escape(m))
return re.match(r, s, flags=self.flags) is not None
return [(a, len(a)) for a in self.resultlist if match(a, pref)]
|