2014-04-18 08:58:02 -04:00
|
|
|
"""Helper methods used in UltiSnips snippets."""
|
|
|
|
|
2015-12-08 08:20:04 -05:00
|
|
|
import string, vim
|
|
|
|
|
2014-04-18 08:58:02 -04:00
|
|
|
def complete(tab, opts):
|
|
|
|
"""
|
|
|
|
get options that start with tab
|
|
|
|
|
|
|
|
:param tab: query string
|
|
|
|
:param opts: list that needs to be completed
|
|
|
|
|
|
|
|
:return: a string that start with tab
|
|
|
|
"""
|
|
|
|
msg = "({0})"
|
|
|
|
if tab:
|
|
|
|
opts = [m[len(tab):] for m in opts if m.startswith(tab)]
|
|
|
|
if len(opts) == 1:
|
|
|
|
return opts[0]
|
|
|
|
|
|
|
|
if not len(opts):
|
|
|
|
msg = "{0}"
|
|
|
|
return msg.format("|".join(opts))
|
2015-12-08 08:20:04 -05:00
|
|
|
|
|
|
|
def _parse_comments(s):
|
|
|
|
""" Parses vim's comments option to extract comment format """
|
|
|
|
i = iter(s.split(","))
|
|
|
|
|
|
|
|
rv = []
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
# get the flags and text of a comment part
|
|
|
|
flags, text = next(i).split(':', 1)
|
|
|
|
|
|
|
|
if len(flags) == 0:
|
2016-12-23 04:48:35 -05:00
|
|
|
rv.append(('OTHER', text, text, text, ""))
|
2015-12-08 08:20:04 -05:00
|
|
|
# parse 3-part comment, but ignore those with O flag
|
|
|
|
elif 's' in flags and 'O' not in flags:
|
|
|
|
ctriple = ["TRIPLE"]
|
|
|
|
indent = ""
|
|
|
|
|
|
|
|
if flags[-1] in string.digits:
|
|
|
|
indent = " " * int(flags[-1])
|
|
|
|
ctriple.append(text)
|
|
|
|
|
|
|
|
flags, text = next(i).split(':', 1)
|
|
|
|
assert flags[0] == 'm'
|
|
|
|
ctriple.append(text)
|
|
|
|
|
|
|
|
flags, text = next(i).split(':', 1)
|
|
|
|
assert flags[0] == 'e'
|
|
|
|
ctriple.append(text)
|
|
|
|
ctriple.append(indent)
|
|
|
|
|
|
|
|
rv.append(ctriple)
|
|
|
|
elif 'b' in flags:
|
|
|
|
if len(text) == 1:
|
|
|
|
rv.insert(0, ("SINGLE_CHAR", text, text, text, ""))
|
|
|
|
except StopIteration:
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def get_comment_format():
|
|
|
|
""" Returns a 4-element tuple (first_line, middle_lines, end_line, indent)
|
|
|
|
representing the comment format for the current file.
|
|
|
|
|
|
|
|
It first looks at the 'commentstring', if that ends with %s, it uses that.
|
|
|
|
Otherwise it parses '&comments' and prefers single character comment
|
|
|
|
markers if there are any.
|
|
|
|
"""
|
|
|
|
commentstring = vim.eval("&commentstring")
|
|
|
|
if commentstring.endswith("%s"):
|
|
|
|
c = commentstring[:-2]
|
|
|
|
return (c, c, c, "")
|
|
|
|
comments = _parse_comments(vim.eval("&comments"))
|
|
|
|
for c in comments:
|
|
|
|
if c[0] == "SINGLE_CHAR":
|
|
|
|
return c[1:]
|
|
|
|
return comments[0][1:]
|
|
|
|
|
|
|
|
|
|
|
|
def make_box(twidth, bwidth=None):
|
2015-12-17 10:44:42 -05:00
|
|
|
b, m, e, i = (s.strip() for s in get_comment_format())
|
2015-12-08 08:20:04 -05:00
|
|
|
bwidth_inner = bwidth - 3 - max(len(b), len(i + e)) if bwidth else twidth + 2
|
|
|
|
sline = b + m + bwidth_inner * m[0] + 2 * m[0]
|
|
|
|
nspaces = (bwidth_inner - twidth) // 2
|
|
|
|
mlines = i + m + " " + " " * nspaces
|
|
|
|
mlinee = " " + " "*(bwidth_inner - twidth - nspaces) + m
|
|
|
|
eline = i + m + bwidth_inner * m[0] + 2 * m[0] + e
|
|
|
|
return sline, mlines, mlinee, eline
|
|
|
|
|
|
|
|
def foldmarker():
|
|
|
|
"Return a tuple of (open fold marker, close fold marker)"
|
|
|
|
return vim.eval("&foldmarker").split(",")
|