added basic regular expression matching.
This commit is contained in:
parent
b5c8a32762
commit
1167fd6729
@ -155,28 +155,39 @@ class Snippet(object):
|
|||||||
|
|
||||||
def _word_for_line(self, before):
|
def _word_for_line(self, before):
|
||||||
word = ''
|
word = ''
|
||||||
if len(before):
|
if len(before) and before.split():
|
||||||
word = before.split()[-1]
|
word = before.split()[-1]
|
||||||
return word
|
return word
|
||||||
|
|
||||||
|
def _re_match(self, trigger):
|
||||||
|
match = re.search(self._t, trigger)
|
||||||
|
if match:
|
||||||
|
if match.end() != len(trigger):
|
||||||
|
match = False
|
||||||
|
else:
|
||||||
|
self._matched = trigger[match.start():match.end()]
|
||||||
|
return match
|
||||||
|
|
||||||
def matches(self, trigger):
|
def matches(self, trigger):
|
||||||
# If user supplies both "w" and "i", it should perhaps be an
|
# If user supplies both "w" and "i", it should perhaps be an
|
||||||
# error, but if permitted it seems that "w" should take precedence
|
# error, but if permitted it seems that "w" should take precedence
|
||||||
# (since matching at word boundary and within a word == matching at word
|
# (since matching at word boundary and within a word == matching at word
|
||||||
# boundary).
|
# boundary).
|
||||||
word = self._word_for_line(trigger)
|
|
||||||
self._matched = ""
|
self._matched = ""
|
||||||
|
|
||||||
if trigger and trigger[-1] in string.whitespace:
|
if trigger and trigger[-1] in string.whitespace:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
word = self._word_for_line(trigger)
|
||||||
|
|
||||||
if "b" in self._opts:
|
if "b" in self._opts:
|
||||||
text_before = trigger.rstrip()[:-len(word)]
|
text_before = trigger.rstrip()[:-len(word)]
|
||||||
if text_before.strip(" \t") != '':
|
if text_before.strip(" \t") != '':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
if "w" in self._opts:
|
if "r" in self._opts:
|
||||||
|
match = self._re_match(trigger)
|
||||||
|
elif "w" in self._opts:
|
||||||
word_len = len(self._t)
|
word_len = len(self._t)
|
||||||
word_prefix = word[:-word_len]
|
word_prefix = word[:-word_len]
|
||||||
word_suffix = word[-word_len:]
|
word_suffix = word[-word_len:]
|
||||||
@ -206,7 +217,10 @@ class Snippet(object):
|
|||||||
if text_before.strip(" \t") != '':
|
if text_before.strip(" \t") != '':
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "w" in self._opts:
|
if "r" in self._opts:
|
||||||
|
# Test for full match only
|
||||||
|
match = self._re_match(trigger)
|
||||||
|
elif "w" in self._opts:
|
||||||
# Trim non-empty prefix up to word boundary, if present.
|
# Trim non-empty prefix up to word boundary, if present.
|
||||||
word_suffix = re.sub(r'^.+\b(.+)$', r'\1', word)
|
word_suffix = re.sub(r'^.+\b(.+)$', r'\1', word)
|
||||||
match = self._t.startswith(word_suffix)
|
match = self._t.startswith(word_suffix)
|
||||||
|
27
test.py
27
test.py
@ -1442,6 +1442,33 @@ class SnippetOptions_ExpandWordSnippets_ExpandSuffix3(
|
|||||||
keys = "[[test" + EX
|
keys = "[[test" + EX
|
||||||
wanted = "[[Expand me!"
|
wanted = "[[Expand me!"
|
||||||
|
|
||||||
|
#################
|
||||||
|
# REGEX MATCHES #
|
||||||
|
#################
|
||||||
|
class SnippetOptions_Regex_Expand(_VimTest):
|
||||||
|
snippets = ("(test)", "Expand me!", "", "r")
|
||||||
|
keys = "test" + EX
|
||||||
|
wanted = "Expand me!"
|
||||||
|
class SnippetOptions_Regex_Multiple(_VimTest):
|
||||||
|
snippets = ("(test *)+", "Expand me!", "", "r")
|
||||||
|
keys = "test test test" + EX
|
||||||
|
wanted = "Expand me!"
|
||||||
|
|
||||||
|
class _Regex_Self(_VimTest):
|
||||||
|
snippets = (r"((?<=\W)|^)(\.)", "self.", "", "r")
|
||||||
|
class SnippetOptions_Regex_Self_Start(_Regex_Self):
|
||||||
|
keys = "." + EX
|
||||||
|
wanted = "self."
|
||||||
|
class SnippetOptions_Regex_Self_Space(_Regex_Self):
|
||||||
|
keys = " ." + EX
|
||||||
|
wanted = " self."
|
||||||
|
class SnippetOptions_Regex_Self_TextAfter(_Regex_Self):
|
||||||
|
keys = " .a" + EX
|
||||||
|
wanted = " .a" + EX
|
||||||
|
class SnippetOptions_Regex_Self_TextBefore(_Regex_Self):
|
||||||
|
keys = "a." + EX
|
||||||
|
wanted = "a." + EX
|
||||||
|
|
||||||
|
|
||||||
######################
|
######################
|
||||||
# SELECTING MULTIPLE #
|
# SELECTING MULTIPLE #
|
||||||
|
Loading…
x
Reference in New Issue
Block a user