From cd0b859b62030d88d14a65d452699ef676523bce Mon Sep 17 00:00:00 2001 From: Holger Rapp Date: Sun, 16 Aug 2009 16:34:54 +0200 Subject: [PATCH] Implemented i option (inword snippet expansion) along the line of the patch by jceb, also added his test cases --- UltiSnips/python.snippets | 2 +- doc/UltiSnips.txt | 3 +++ plugin/UltiSnips/__init__.py | 22 +++++++++++++++++++++- test.py | 24 ++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) diff --git a/UltiSnips/python.snippets b/UltiSnips/python.snippets index 30d4b0c..cc8f705 100644 --- a/UltiSnips/python.snippets +++ b/UltiSnips/python.snippets @@ -29,7 +29,7 @@ ${4/.+/(?0:\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s* ${2/object$|(.+)/(?1: $0.__init__\(self\)\n\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2: self._$2 = $2\n)/g} endsnippet -snippet . +snippet . "self." i self. endsnippet diff --git a/doc/UltiSnips.txt b/doc/UltiSnips.txt index cb53914..11a71cc 100644 --- a/doc/UltiSnips.txt +++ b/doc/UltiSnips.txt @@ -178,6 +178,9 @@ snippet on. The options currently supported are > of the cursor). Default is to expand snippets at every position, even mitten in the line. Most of my snippets have this option set, it keeps UltiSnips out of the way. + i Inword expansion - normally, triggers need whitespace before them to be + expanded. With this option, triggers are also expanded in the middle of + a word. 4.2 Plaintext snippets *UltiSnips-plaintext-snippets* ---------------------- diff --git a/plugin/UltiSnips/__init__.py b/plugin/UltiSnips/__init__.py index 473e9f2..3919609 100644 --- a/plugin/UltiSnips/__init__.py +++ b/plugin/UltiSnips/__init__.py @@ -11,12 +11,24 @@ from UltiSnips.Geometry import Position from UltiSnips.TextObjects import * from UltiSnips.Buffer import VimBuffer +# TODO: it doesn't make sense to have +# this as a baseclass of a dict, since we have +# to run through all snippets anyway class _SnippetDictionary(dict): def __init__(self, *args, **kwargs): dict.__init__(self, *args, **kwargs) self._extends = [] + def get_matching_snippets(self, trigger): + """Returns all snippets matching the given trigger.""" + rv = [] + for l in self.values(): + for snip in l: + if snip.matches(trigger): + rv.append(snip) + return rv + def extends(): def fget(self): return self._extends @@ -86,6 +98,11 @@ class Snippet(object): def __repr__(self): return "Snippet(%s,%s,%s)" % (self._t,self._d,self._opts) + def matches(self, trigger): + if "i" not in self._opts: + return trigger == self._t + return trigger.endswith(self._t) + def overwrites_previous(self): return "!" in self._opts overwrites_previous = property(overwrites_previous) @@ -489,6 +506,9 @@ class SnippetManager(object): return True raise + # Adjust before, maybe the trigger is not the complete word + text_before += word[:-len(snippet.trigger)] + self._expect_move_wo_change = True if self._cs: @@ -590,7 +610,7 @@ class SnippetManager(object): parent_results = reduce( lambda a,b: a+b, [ self._find_snippets(p, trigger) for p in snips.extends ], []) - return parent_results + snips.get(trigger, []) + return parent_results + snips.get_matching_snippets(trigger) UltiSnips_Manager = SnippetManager() diff --git a/test.py b/test.py index da95a5c..ad1a7ed 100755 --- a/test.py +++ b/test.py @@ -1075,6 +1075,30 @@ class SnippetOptions_OnlyExpandWhenWSInFront_OneWithOneWOChoose(_VimTest): keys = " test" + EX + "1\n" wanted = " Expand me!" + +class SnippetOptions_ExpandInwordSnippets_SimpleExpand(_VimTest): + snippets = (("test", "Expand me!", "", "i"), ) + keys = "atest" + EX + wanted = "aExpand me!" +class SnippetOptions_ExpandInwordSnippets_ExpandSingle(_VimTest): + snippets = (("test", "Expand me!", "", "i"), ) + keys = "test" + EX + wanted = "Expand me!" +class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand(_VimTest): + snippets = (("test", "Expand me!", "", "i"), ) + keys = "$test" + EX + wanted = "$Expand me!" +class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand2(_VimTest): + snippets = (("test", "Expand me!", "", "i"), ) + keys = "-test" + EX + wanted = "-Expand me!" +class SnippetOptions_ExpandInwordSnippetsWithOtherChars_Expand3(_VimTest): + snippets = (("test", "Expand me!", "", "i"), ) + keys = "ätest" + EX + wanted = "äExpand me!" + + + ###################### # SELECTING MULTIPLE # ######################