review fixes
This commit is contained in:
parent
87c3a7ba43
commit
904fbdecf5
@ -655,9 +655,9 @@ The options currently supported are: >
|
||||
indentation too.
|
||||
|
||||
e Context snippets - With this option expansion of snippet can be
|
||||
controlled not only by previous characters in line, but by any given python
|
||||
expression. This option can be specified along with other options,
|
||||
like 'b'. See |UltiSnips-context-snippets| for more info.
|
||||
controlled not only by previous characters in line, but by any given
|
||||
python expression. This option can be specified along with other
|
||||
options, like 'b'. See |UltiSnips-context-snippets| for more info.
|
||||
|
||||
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
|
||||
path - The complete path to the current file
|
||||
t - The values of the placeholders, t[1] is the text of ${1}, and so on
|
||||
snip - UltiSnips.TextObjects.SnippetUtil object instance. Has methods that
|
||||
simplify indentation handling.
|
||||
t - The values of the placeholders, t[1] is the text of ${1}, etc.
|
||||
snip - UltiSnips.TextObjects.SnippetUtil object instance. Has methods
|
||||
that simplify indentation handling.
|
||||
context - Result of context condition. See |UltiSnips-context-snippets|.
|
||||
|
||||
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
|
||||
|
||||
'expression' can be any python expression. If 'expression' evaluates to
|
||||
'True', then this snippet will be chosen for expansion. 'expression' must be
|
||||
wrapped with double-quotes.
|
||||
The 'expression' can be any python expression. If 'expression' evaluates to
|
||||
'True', then this snippet will be chosen for expansion. The 'expression' must
|
||||
be wrapped with double-quotes.
|
||||
|
||||
Following python modules are automatically imported: 're', 'os', 'vim',
|
||||
'string', 'random'.
|
||||
The following python modules are automatically imported into the scope before
|
||||
'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'
|
||||
'buffer' - alias for 'vim.current.window.buffer'
|
||||
'cursor' - alias for 'vim.current.cursor'
|
||||
'line' and 'column' - aliases for cursor position
|
||||
|
||||
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 -------------------
|
||||
snippet r "return" "re.match('^\s+if err ', buffer[line-2])" be
|
||||
@ -1330,7 +1330,7 @@ return err
|
||||
endsnippet
|
||||
------------------- 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.
|
||||
|
||||
Note: context snippets prioritized over non-context ones. So, if there are
|
||||
@ -1353,10 +1353,10 @@ endsnippet
|
||||
------------------- SNAP -------------------
|
||||
|
||||
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
|
||||
used by other UltiSnips users. In that case, library should be imported
|
||||
It's good idea to move context conditions to a separate module, so it can be
|
||||
used by other UltiSnips users. In that case, module should be imported
|
||||
using 'global' keyword, like this:
|
||||
|
||||
------------------- SNIP -------------------
|
||||
@ -1372,15 +1372,15 @@ else:
|
||||
endsnippet
|
||||
------------------- SNAP -------------------
|
||||
|
||||
That snippet will expand only if cursor is located in the return statement,
|
||||
and then it will expand either to 'err' or to 'nil' depending in which 'if'
|
||||
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 on which 'if'
|
||||
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
|
||||
condition in it's 'if' statement, and if it's considired 'True', then snippet
|
||||
will be expanded. Moreover, result of condition will be accessed in the
|
||||
'context' variable:
|
||||
Context condition can return any value which python can use as condition in
|
||||
it's 'if' statement, and if it's considered 'True', then snippet will be
|
||||
expanded. The evaluated value of 'condition' is available in the 'context'
|
||||
variable inside the snippet:
|
||||
|
||||
------------------- SNIP -------------------
|
||||
snippet + "var +=" "re.match('\s*(.*?)\s*:?=', buffer[line-2])" ie
|
||||
|
@ -104,9 +104,7 @@ class SnippetDefinition(object):
|
||||
'column': current.window.cursor[1],
|
||||
'cursor': current.window.cursor,
|
||||
}
|
||||
|
||||
exec(code, locals)
|
||||
|
||||
return context["match"]
|
||||
|
||||
def has_option(self, opt):
|
||||
@ -142,7 +140,7 @@ class SnippetDefinition(object):
|
||||
|
||||
@property
|
||||
def context(self):
|
||||
"""Returns matched context."""
|
||||
"""The matched context."""
|
||||
return self._context
|
||||
|
||||
def matches(self, trigger):
|
||||
|
@ -476,7 +476,6 @@ class SnippetManager(object):
|
||||
elif feedkey:
|
||||
_vim.command('return %s' % _vim.escape(feedkey))
|
||||
|
||||
@err_to_scratch_buffer
|
||||
def _snips(self, before, partial):
|
||||
"""Returns all the snippets for the given text before the cursor.
|
||||
|
||||
|
87
test/test_ContextSnippets.py
Normal file
87
test/test_ContextSnippets.py
Normal 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
|
||||
}"""
|
Loading…
Reference in New Issue
Block a user