I am trying to create a snippet that will let me generate a nice pattern for python docstring such as this one:
####################################################################################################
# ************************************************************************************************ #
# * HOUSING DATASET * #
# ************************************************************************************************ #
# #
# This is what content would look like, multiple lines. #
# The features of the 506 samples can be summarized as: #
# #
####################################################################################################
I want the title to be centered in the pattern, spaces expanded around it to a fixed size. And for the content below I want each line to start with a '# ' pattern and end with the same pattern. Again I want the content lines to be of a fixed size. I managed to do the title part of the snippet with inspiration from Honza pythonx and his all.snippets.
The code I currently have is the following (beware it might be a bit dirty since I am still figuring it out):
global!p
def make_title(twidth, bwidth=None):
b, e = '# *', '* #'
bwidth_inner = bwidth - 3 - max(len(b), len(b + e)) if bwidth else twidth + 2
nspaces = (bwidth_inner - twidth) // 2
mlines = b + ' ' + ' ' * nspaces
mlinee = ' ' + ' ' * (bwidth_inner - twidth -nspaces) + e
return mlines, mlinee
endglobal
snippet header 'Module docstring' b
"""
####################################################################################################
# ************************************************************************************************ #
`!p
width = 80
box = make_title(len(t[1], width)
snip.rv = box[0]
`${1:${VISUAL:title}}`!p
snip.rv = box[1]`
# ************************************************************************************************ #
This code works as intended and generates the title as I want it. So i tried to expand the concept to the content part and wrote the following:
global!p
def make_title(twidth, bwidth=None):
b, e = '# *', '* #'
bwidth_inner = bwidth - 3 - max(len(b), len(b + e)) if bwidth else twidth + 2
nspaces = (bwidth_inner - twidth) // 2
mlines = b + ' ' + ' ' * nspaces
mlinee = ' ' + ' ' * (bwidth_inner - twidth - nspaces) + e
return mlines, mlinee
endglobal
def make_content_line(twidth, bwidth=None):
b, e = '# ', ' #'
bwidth_inner = bwidth - 3 - max(len(b), len(b + e)) if bwidth else twidth + 2
nspaces = (bwidth_inner - twidth) // 2
mlines = b + ' ' + ' ' * nspaces
mlinee = ' ' + ' ' * (bwidth_inner - twidth - nspaces) + e
return mlines, mlinee
endglobal
snippet header 'Module docstring' b
"""
####################################################################################################
# ************************************************************************************************ #
`!p
width = 80
box = make_title(len(t[1], width)
snip.rv = box[0]
`${1:${VISUAL:title}}`!p
snip.rv = box[1]`
# ************************************************************************************************ #
`!p
width = 80
box = make_title(len(t[1], width)
snip.rv = box[0]
`${2:${VISUAL:content}}`!p
snip.rv = box[1]`
Now this almost runs as I wanted, except that the first visual part seems to be linked to the second and modifying the title changes the way the content is displayed initially. Writing the content does not change the title though and I can kind of land on my feet.
So I have two questions: how can I unlink these two parts of the snippet so that there are independent and how can I generate content lines as I write the content ? I guess one way would be to generate a new content_line everytime a user press the jump forward shortcut but I don't know to do that.