UltiSnips generate python docstring with a fixed size box wrapping text










1















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.










share|improve this question



















  • 2





    What you call nice pattern I would call high-maintenance commenting style; don't do this!

    – Ingo Karkat
    Nov 15 '18 at 11:44






  • 1





    I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

    – PiggyGenius
    Nov 15 '18 at 13:11












  • For the sake of transparency, I have to tell you that I don't know how to do it.

    – MeanStreet
    Nov 20 '18 at 10:00















1















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.










share|improve this question



















  • 2





    What you call nice pattern I would call high-maintenance commenting style; don't do this!

    – Ingo Karkat
    Nov 15 '18 at 11:44






  • 1





    I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

    – PiggyGenius
    Nov 15 '18 at 13:11












  • For the sake of transparency, I have to tell you that I don't know how to do it.

    – MeanStreet
    Nov 20 '18 at 10:00













1












1








1








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.










share|improve this question
















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.







python vim code-snippets docstring ultisnips






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '18 at 7:52







PiggyGenius

















asked Nov 15 '18 at 10:46









PiggyGeniusPiggyGenius

1501420




1501420







  • 2





    What you call nice pattern I would call high-maintenance commenting style; don't do this!

    – Ingo Karkat
    Nov 15 '18 at 11:44






  • 1





    I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

    – PiggyGenius
    Nov 15 '18 at 13:11












  • For the sake of transparency, I have to tell you that I don't know how to do it.

    – MeanStreet
    Nov 20 '18 at 10:00












  • 2





    What you call nice pattern I would call high-maintenance commenting style; don't do this!

    – Ingo Karkat
    Nov 15 '18 at 11:44






  • 1





    I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

    – PiggyGenius
    Nov 15 '18 at 13:11












  • For the sake of transparency, I have to tell you that I don't know how to do it.

    – MeanStreet
    Nov 20 '18 at 10:00







2




2





What you call nice pattern I would call high-maintenance commenting style; don't do this!

– Ingo Karkat
Nov 15 '18 at 11:44





What you call nice pattern I would call high-maintenance commenting style; don't do this!

– Ingo Karkat
Nov 15 '18 at 11:44




1




1





I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

– PiggyGenius
Nov 15 '18 at 13:11






I see your point but I would still like the answer. Knowing how to do this should be useful in other contexts anyway.

– PiggyGenius
Nov 15 '18 at 13:11














For the sake of transparency, I have to tell you that I don't know how to do it.

– MeanStreet
Nov 20 '18 at 10:00





For the sake of transparency, I have to tell you that I don't know how to do it.

– MeanStreet
Nov 20 '18 at 10:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317688%2fultisnips-generate-python-docstring-with-a-fixed-size-box-wrapping-text%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53317688%2fultisnips-generate-python-docstring-with-a-fixed-size-box-wrapping-text%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands