Snippets (?) using lenght of arguments - Dynamic content


#1

Hello.

I would like to have snippets producing “pretty” comments like the two above.

# ----------------- #
# -- 1st example -- #
# ----------------- #

The size of the dash lines must be adapted to the content of the single line of text:

# ---------------------------- #
# -- Another example larger -- #
# ---------------------------- #

Typically I would like to use a snippet showing

# ------------------- #
# -- Short_comment -- #
# ------------------- #

where Short_comment is a $1 argument regarding to the syntax used for defining snippets.

My problem is to adapt the size of the dash line regarding to the value of $1. Is this possible or not ?


#2

It’s not possible to do this with snippets right now. But you could do this with a bit of code or the snippets package could be enhanced.


#3

Thanks. Is there some easy to read package where to start to do my first atom package ?


#4

There’s the Your First Package document in the docs:


#5

Until you get the new feature you should use this package for that example … https://atom.io/packages/wrap-selection


#6

Thanks a lot for the two lats advices. The tiny package is a good example of a very simple package.


#7

Hi, @projetmbc I’ve made the wrap selection package so if you have any questions of things which you want to me to add please let me know. Its fresh new package and there are few simple things I am planning to add.

PS: Thanks @mark_hahn for recommending my package. Always happy to see that other find it useful too.


#8

Thanks for the help, I will take a look at the code as soon as possible (I have a lot of work for the moment).


#9

@Trudko; I’ve just took a look and this sounds like black magic for me for the moment.

wrapWithText: ->
    editor = atom.workspace.getActiveEditor()
    selections = editor.getSelections()
    for selection in selections
        if not selection.isEmpty()
            range = selection.getBufferRange()
            selection.destroy()
            editor.addCursorAtBufferPosition(range.start)
            editor.addCursorAtBufferPosition(range.end)

In this lines, could I go trough each single selection such as to find the width of this selection ? Can I remove the text of a selection and replace it by another ?


#10

@projetmbc of course it sounds like black magic because it is! :smiley:

What I am doing here is I go through each selection finds where it start(range variable) and I put one cursor at the start of selection and one at the end. What you could do is in for loop:

width = selection.getText().length
selection.insertText(“Text to replace the text in selection”)


#11

Thanks a lot for your help mister black magician. :wink: I will try to find 5 min to play with atom packages… As soon as I made something usable, I will add it to my user’s profile.

Last question, but not the least. Is there an easy way to know a language associated to a file from a package point of view ?


#12

For the file currently active:

editor = atom.workspace.getActiveEditor()
grammarName = editor.getGrammar().name

#13

So cute. :slight_smile: I love that !