review fixes

This commit is contained in:
Stanislav Seletskiy 2015-04-25 21:38:22 +06:00
parent 87c3a7ba43
commit 904fbdecf5
4 changed files with 114 additions and 30 deletions

View File

@ -655,9 +655,9 @@ The options currently supported are: >
indentation too. indentation too.
e Context snippets - With this option expansion of snippet can be e Context snippets - With this option expansion of snippet can be
controlled not only by previous characters in line, but by any given python controlled not only by previous characters in line, but by any given
expression. This option can be specified along with other options, python expression. This option can be specified along with other
like 'b'. See |UltiSnips-context-snippets| for more info. options, like 'b'. See |UltiSnips-context-snippets| for more info.
The end line is the 'endsnippet' keyword on a line by itself. > The end line is the 'endsnippet' keyword on a line by itself. >
@ -821,9 +821,9 @@ The variables automatically defined in python code are: >
fn - The current filename fn - The current filename
path - The complete path to the current file path - The complete path to the current file
t - The values of the placeholders, t[1] is the text of ${1}, and so on t - The values of the placeholders, t[1] is the text of ${1}, etc.
snip - UltiSnips.TextObjects.SnippetUtil object instance. Has methods that snip - UltiSnips.TextObjects.SnippetUtil object instance. Has methods
simplify indentation handling. that simplify indentation handling.
context - Result of context condition. See |UltiSnips-context-snippets|. context - Result of context condition. See |UltiSnips-context-snippets|.
The 'snip' object provides the following methods: > The 'snip' object provides the following methods: >
@ -1308,21 +1308,21 @@ In that case snippet should be defined using this syntax: >
snippet tab_trigger "description" "expression" options snippet tab_trigger "description" "expression" options
'expression' can be any python expression. If 'expression' evaluates to The 'expression' can be any python expression. If 'expression' evaluates to
'True', then this snippet will be chosen for expansion. 'expression' must be 'True', then this snippet will be chosen for expansion. The 'expression' must
wrapped with double-quotes. be wrapped with double-quotes.
Following python modules are automatically imported: 're', 'os', 'vim', The following python modules are automatically imported into the scope before
'string', 'random'. 'expression' is evaluated: 're', 'os', 'vim', 'string', 'random'.
Also, variables are declared in local scope for use in expression: > Also, the following variables are defined:
'window' - alias for 'vim.current.window' 'window' - alias for 'vim.current.window'
'buffer' - alias for 'vim.current.window.buffer' 'buffer' - alias for 'vim.current.window.buffer'
'cursor' - alias for 'vim.current.cursor' 'cursor' - alias for 'vim.current.cursor'
'line' and 'column' - aliases for cursor position 'line' and 'column' - aliases for cursor position
Keep in mind, that lines in vim numbered from 1, and lists in python starts Keep in mind, that lines in vim numbered from 1, and lists in python starts
from 0, so to access current line you need to use 'line-1' expression. from 0, so to access the current line you need to use 'line-1'.
------------------- SNIP ------------------- ------------------- SNIP -------------------
snippet r "return" "re.match('^\s+if err ', buffer[line-2])" be snippet r "return" "re.match('^\s+if err ', buffer[line-2])" be
@ -1330,7 +1330,7 @@ return err
endsnippet endsnippet
------------------- SNAP ------------------- ------------------- SNAP -------------------
That snippet will expand to 'return err' only if previous line is starting That snippet will expand to 'return err' only if the previous line is starting
from 'if err' prefix. from 'if err' prefix.
Note: context snippets prioritized over non-context ones. So, if there are Note: context snippets prioritized over non-context ones. So, if there are
@ -1353,10 +1353,10 @@ endsnippet
------------------- SNAP ------------------- ------------------- SNAP -------------------
That snippet will expand into 'if err != nil' if previous line will That snippet will expand into 'if err != nil' if previous line will
match 'err :=' prefix, otherwise default 'if' snippet will be expanded. match 'err :=' prefix, otherwise the default 'if' snippet will be expanded.
It's good idea to move context conditions to separate library, so it can be It's good idea to move context conditions to a separate module, so it can be
used by other UltiSnips users. In that case, library should be imported used by other UltiSnips users. In that case, module should be imported
using 'global' keyword, like this: using 'global' keyword, like this:
------------------- SNIP ------------------- ------------------- SNIP -------------------
@ -1372,15 +1372,15 @@ else:
endsnippet endsnippet
------------------- SNAP ------------------- ------------------- SNAP -------------------
That snippet will expand only if cursor is located in the return statement, That snippet will expand only if the cursor is located in the return statement,
and then it will expand either to 'err' or to 'nil' depending in which 'if' and then it will expand either to 'err' or to 'nil' depending on which 'if'
statement it's located. 'is_return_argument' and 'is_in_err_condition' are statement it's located. 'is_return_argument' and 'is_in_err_condition' are
part of custom python library which is called 'my_utils' in this example. part of custom python module which is called 'my_utils' in this example.
Context condition can return any value which python can use as Context condition can return any value which python can use as condition in
condition in it's 'if' statement, and if it's considired 'True', then snippet it's 'if' statement, and if it's considered 'True', then snippet will be
will be expanded. Moreover, result of condition will be accessed in the expanded. The evaluated value of 'condition' is available in the 'context'
'context' variable: variable inside the snippet:
------------------- SNIP ------------------- ------------------- SNIP -------------------
snippet + "var +=" "re.match('\s*(.*?)\s*:?=', buffer[line-2])" ie snippet + "var +=" "re.match('\s*(.*?)\s*:?=', buffer[line-2])" ie

View File

@ -104,9 +104,7 @@ class SnippetDefinition(object):
'column': current.window.cursor[1], 'column': current.window.cursor[1],
'cursor': current.window.cursor, 'cursor': current.window.cursor,
} }
exec(code, locals) exec(code, locals)
return context["match"] return context["match"]
def has_option(self, opt): def has_option(self, opt):
@ -142,7 +140,7 @@ class SnippetDefinition(object):
@property @property
def context(self): def context(self):
"""Returns matched context.""" """The matched context."""
return self._context return self._context
def matches(self, trigger): def matches(self, trigger):

View File

@ -476,7 +476,6 @@ class SnippetManager(object):
elif feedkey: elif feedkey:
_vim.command('return %s' % _vim.escape(feedkey)) _vim.command('return %s' % _vim.escape(feedkey))
@err_to_scratch_buffer
def _snips(self, before, partial): def _snips(self, before, partial):
"""Returns all the snippets for the given text before the cursor. """Returns all the snippets for the given text before the cursor.

View File

@ -0,0 +1,87 @@
from test.vim_test_case import VimTestCase as _VimTest
from test.constant import *
class ContextSnippets_SimpleSnippet(_VimTest):
files = { 'us/all.snippets': r"""
snippet a "desc" "True" e
abc
endsnippet
"""}
keys = 'a' + EX
wanted = 'abc'
class ContextSnippets_ExpandOnTrue(_VimTest):
files = { 'us/all.snippets': r"""
global !p
def check_context():
return True
endglobal
snippet a "desc" "check_context()" e
abc
endsnippet
"""}
keys = 'a' + EX
wanted = 'abc'
class ContextSnippets_DoNotExpandOnFalse(_VimTest):
files = { 'us/all.snippets': r"""
global !p
def check_context():
return False
endglobal
snippet a "desc" "check_context()" e
abc
endsnippet
"""}
keys = 'a' + EX
wanted = keys
class ContextSnippets_UseContext(_VimTest):
files = { 'us/all.snippets': r"""
global !p
def wrap(ins):
return "< " + ins + " >"
endglobal
snippet a "desc" "wrap(buffer[line-1])" e
{ `!p snip.rv = context` }
endsnippet
"""}
keys = 'a' + EX
wanted = '{ < a > }'
class ContextSnippets_SnippetPriority(_VimTest):
files = { 'us/all.snippets': r"""
snippet i "desc" "re.search('err :=', buffer[line-2])" e
if err != nil {
${1:// pass}
}
endsnippet
snippet i
if ${1:true} {
${2:// pass}
}
endsnippet
"""}
keys = r"""
err := some_call()
i""" + EX + JF + """
i""" + EX
wanted = r"""
err := some_call()
if err != nil {
// pass
}
if true {
// pass
}"""