parent
6b12b11411
commit
324a4f8ffc
@ -13,6 +13,10 @@ from UltiSnips.indent_util import IndentUtil
|
|||||||
from UltiSnips.text import escape
|
from UltiSnips.text import escape
|
||||||
from UltiSnips.text_objects import SnippetInstance
|
from UltiSnips.text_objects import SnippetInstance
|
||||||
|
|
||||||
|
__WHITESPACE_SPLIT = re.compile(r"\s")
|
||||||
|
def split_at_whitespace(string):
|
||||||
|
"""Like string.split(), but keeps empty words as empty words."""
|
||||||
|
return re.split(__WHITESPACE_SPLIT, string)
|
||||||
|
|
||||||
def _words_for_line(trigger, before, num_words=None):
|
def _words_for_line(trigger, before, num_words=None):
|
||||||
"""Gets the final 'num_words' words from 'before'.
|
"""Gets the final 'num_words' words from 'before'.
|
||||||
@ -20,13 +24,10 @@ def _words_for_line(trigger, before, num_words=None):
|
|||||||
If num_words is None, then use the number of words in 'trigger'.
|
If num_words is None, then use the number of words in 'trigger'.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if not len(before):
|
|
||||||
return ''
|
|
||||||
|
|
||||||
if num_words is None:
|
if num_words is None:
|
||||||
num_words = len(trigger.split())
|
num_words = len(split_at_whitespace(trigger))
|
||||||
|
|
||||||
word_list = before.split()
|
word_list = split_at_whitespace(before)
|
||||||
if len(word_list) <= num_words:
|
if len(word_list) <= num_words:
|
||||||
return before.strip()
|
return before.strip()
|
||||||
else:
|
else:
|
||||||
@ -143,22 +144,18 @@ class SnippetDefinition(object):
|
|||||||
"""The matched context."""
|
"""The matched context."""
|
||||||
return self._context
|
return self._context
|
||||||
|
|
||||||
def matches(self, trigger):
|
def matches(self, before):
|
||||||
"""Returns True if this snippet matches 'trigger'."""
|
"""Returns True if this snippet matches 'before'."""
|
||||||
# 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).
|
||||||
self._matched = ''
|
self._matched = ''
|
||||||
|
|
||||||
# Don't expand on whitespace
|
words = _words_for_line(self._trigger, before)
|
||||||
if trigger and trigger.rstrip() != trigger:
|
|
||||||
return False
|
|
||||||
|
|
||||||
words = _words_for_line(self._trigger, trigger)
|
|
||||||
|
|
||||||
if 'r' in self._opts:
|
if 'r' in self._opts:
|
||||||
match = self._re_match(trigger)
|
match = self._re_match(before)
|
||||||
elif 'w' in self._opts:
|
elif 'w' in self._opts:
|
||||||
words_len = len(self._trigger)
|
words_len = len(self._trigger)
|
||||||
words_prefix = words[:-words_len]
|
words_prefix = words[:-words_len]
|
||||||
@ -182,7 +179,7 @@ class SnippetDefinition(object):
|
|||||||
|
|
||||||
# Ensure the match was on a word boundry if needed
|
# Ensure the match was on a word boundry if needed
|
||||||
if 'b' in self._opts and match:
|
if 'b' in self._opts and match:
|
||||||
text_before = trigger.rstrip()[:-len(self._matched)]
|
text_before = before.rstrip()[:-len(self._matched)]
|
||||||
if text_before.strip(' \t') != '':
|
if text_before.strip(' \t') != '':
|
||||||
self._matched = ''
|
self._matched = ''
|
||||||
return False
|
return False
|
||||||
@ -194,21 +191,21 @@ class SnippetDefinition(object):
|
|||||||
|
|
||||||
return match
|
return match
|
||||||
|
|
||||||
def could_match(self, trigger):
|
def could_match(self, before):
|
||||||
"""Return True if this snippet could match the (partial) 'trigger'."""
|
"""Return True if this snippet could match the (partial) 'before'."""
|
||||||
self._matched = ''
|
self._matched = ''
|
||||||
|
|
||||||
# List all on whitespace.
|
# List all on whitespace.
|
||||||
if trigger and trigger[-1] in (' ', '\t'):
|
if before and before[-1] in (' ', '\t'):
|
||||||
trigger = ''
|
before = ''
|
||||||
if trigger and trigger.rstrip() is not trigger:
|
if before and before.rstrip() is not before:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
words = _words_for_line(self._trigger, trigger)
|
words = _words_for_line(self._trigger, before)
|
||||||
|
|
||||||
if 'r' in self._opts:
|
if 'r' in self._opts:
|
||||||
# Test for full match only
|
# Test for full match only
|
||||||
match = self._re_match(trigger)
|
match = self._re_match(before)
|
||||||
elif 'w' in self._opts:
|
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.
|
||||||
qwords = escape(words, r'\"')
|
qwords = escape(words, r'\"')
|
||||||
@ -234,7 +231,7 @@ class SnippetDefinition(object):
|
|||||||
|
|
||||||
# Ensure the match was on a word boundry if needed
|
# Ensure the match was on a word boundry if needed
|
||||||
if 'b' in self._opts and match:
|
if 'b' in self._opts and match:
|
||||||
text_before = trigger.rstrip()[:-len(self._matched)]
|
text_before = before.rstrip()[:-len(self._matched)]
|
||||||
if text_before.strip(' \t') != '':
|
if text_before.strip(' \t') != '':
|
||||||
self._matched = ''
|
self._matched = ''
|
||||||
return False
|
return False
|
||||||
|
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
"""Implements a container for parsed snippets."""
|
"""Implements a container for parsed snippets."""
|
||||||
|
|
||||||
|
|
||||||
class SnippetDictionary(object):
|
class SnippetDictionary(object):
|
||||||
|
|
||||||
"""See module docstring."""
|
"""See module docstring."""
|
||||||
|
@ -592,8 +592,6 @@ class SnippetManager(object):
|
|||||||
def _try_expand(self):
|
def _try_expand(self):
|
||||||
"""Try to expand a snippet in the current place."""
|
"""Try to expand a snippet in the current place."""
|
||||||
before = _vim.buf.line_till_cursor
|
before = _vim.buf.line_till_cursor
|
||||||
if not before:
|
|
||||||
return False
|
|
||||||
snippets = self._snips(before, False)
|
snippets = self._snips(before, False)
|
||||||
if snippets:
|
if snippets:
|
||||||
# prefer snippets with context if any
|
# prefer snippets with context if any
|
||||||
|
@ -209,6 +209,12 @@ class SnippetOptions_Regex_Expand(_VimTest):
|
|||||||
wanted = 'Expand me!'
|
wanted = 'Expand me!'
|
||||||
|
|
||||||
|
|
||||||
|
class SnippetOptions_Regex_WithSpace(_VimTest):
|
||||||
|
snippets = ('test ', 'Expand me!', '', 'r')
|
||||||
|
keys = 'test ' + EX
|
||||||
|
wanted = 'Expand me!'
|
||||||
|
|
||||||
|
|
||||||
class SnippetOptions_Regex_Multiple(_VimTest):
|
class SnippetOptions_Regex_Multiple(_VimTest):
|
||||||
snippets = ('(test *)+', 'Expand me!', '', 'r')
|
snippets = ('(test *)+', 'Expand me!', '', 'r')
|
||||||
keys = 'test test test' + EX
|
keys = 'test test test' + EX
|
||||||
|
Loading…
x
Reference in New Issue
Block a user