Make _parse_comments use &commentstring before &comments.

Also refactor and move python code from all.snippets into python file.

Fixes #629.
This commit is contained in:
Holger Rapp 2015-10-07 16:23:00 +02:00
parent 2d835f9b1a
commit 2d64dabfd1
2 changed files with 73 additions and 63 deletions

View File

@ -10,69 +10,7 @@ priority -60
# NICE BOXES #
##############
global !p
import string, vim
""" Maps a filetype to comment format used for boxes.
Automatically filled during usage"""
_commentDict = { }
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:
rv.append((text, text, text, ""))
# parse 3-part comment, but ignore those with O flag
elif 's' in flags and 'O' not in flags:
ctriple = []
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, (text,text,text, ""))
except StopIteration:
return rv
def _get_comment_format():
""" Returns a 4-element tuple representing the comment format for
the current file. """
return _parse_comments(vim.eval("&comments"))[0]
def make_box(twidth, bwidth=None):
b, m, e, i = _get_comment_format()
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(",")
from vimsnippets import foldmarker, make_box
endglobal
snippet box "A nice box with the current comment symbol" b

View File

@ -1,5 +1,7 @@
"""Helper methods used in UltiSnips snippets."""
import string, vim
def complete(tab, opts):
"""
get options that start with tab
@ -18,3 +20,73 @@ def complete(tab, opts):
if not len(opts):
msg = "{0}"
return msg.format("|".join(opts))
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:
rv.append((text, text, text, ""))
# 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):
b, m, e, i = _get_comment_format()
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(",")