Merge pull request #470 from blueyed/python-doc-quotes
Python: add triple doc quotes and handle expansion of `"|"`
This commit is contained in:
commit
c77db7c7d3
@ -36,8 +36,8 @@ DOXYGEN = 0x2
|
||||
SPHINX = 0x3
|
||||
GOOGLE = 0x4
|
||||
|
||||
SINGLE_QUOTES = 0x1
|
||||
DOUBLE_QUOTES = 0x2
|
||||
SINGLE_QUOTES = "'"
|
||||
DOUBLE_QUOTES = '"'
|
||||
|
||||
|
||||
class Arg(object):
|
||||
@ -69,9 +69,35 @@ def get_quoting_style(snip):
|
||||
return DOUBLE_QUOTES
|
||||
|
||||
def triple_quotes(snip):
|
||||
if get_quoting_style(snip) == SINGLE_QUOTES:
|
||||
return "'''"
|
||||
return '"""'
|
||||
return get_quoting_style(snip) * 3
|
||||
|
||||
def triple_quotes_handle_trailing(snip, quoting_style):
|
||||
"""
|
||||
Generate triple quoted strings and handle any trailing quote char,
|
||||
which might be there from some autoclose/autopair plugin,
|
||||
i.e. when expanding ``"|"``.
|
||||
"""
|
||||
if not snip.c:
|
||||
# Do this only once, otherwise the following error would happen:
|
||||
# RuntimeError: The snippets content did not converge: …
|
||||
_, col = vim.current.window.cursor
|
||||
line = vim.current.line
|
||||
|
||||
# Handle already existing quote chars after the trigger.
|
||||
_ret = quoting_style * 3
|
||||
while True:
|
||||
try:
|
||||
nextc = line[col]
|
||||
except IndexError:
|
||||
break
|
||||
if nextc == quoting_style and len(_ret):
|
||||
_ret = _ret[1:]
|
||||
col = col+1
|
||||
else:
|
||||
break
|
||||
snip.rv = _ret
|
||||
else:
|
||||
snip.rv = snip.c
|
||||
|
||||
def get_style(snip):
|
||||
style = snip.opt("g:ultisnips_python_style", "normal")
|
||||
@ -613,4 +639,22 @@ class Test${1:Class}(${2:unittest.TestCase}):
|
||||
${7:pass}
|
||||
endsnippet
|
||||
|
||||
snippet " "triple quoted string (double quotes)" b
|
||||
"""
|
||||
${1:doc}
|
||||
`!p triple_quotes_handle_trailing(snip, '"')`
|
||||
endsnippet
|
||||
|
||||
snippet ' "triple quoted string (single quotes)" b
|
||||
'''
|
||||
${1:doc}
|
||||
`!p triple_quotes_handle_trailing(snip, "'")`
|
||||
endsnippet
|
||||
|
||||
snippet doc "doc block (triple quotes)"
|
||||
`!p snip.rv = triple_quotes(snip)`
|
||||
${1:doc}
|
||||
`!p snip.rv = triple_quotes(snip)`
|
||||
endsnippet
|
||||
|
||||
# vim:ft=snippets:
|
||||
|
Loading…
Reference in New Issue
Block a user