Surround code with language construct


#1

Hello everyone,
Does anybody know if there is a way in Atom to surround a selection with some language construct, like a control loop? This seems to be missing in Atom. I also gave a look in the Discuss forum, even though I’m not really sure how to look for it.

To be clearer, the behavior should be the same as IntelliJ (here). I select some code, I’m provided with a list of constructs, I pick the one I want, the selection is wrapped within it and properly indented.

var data = retrieveDataWithPossiblyFaultyMethod();
// after wrapping, it gets like this:

try {
  var data = retrieveDataWithPossiblyFaultyMethod();
}
catch (error) {
}

I realize this is a language-dependent feature, so I guess it’s ok not to have it in the core (even then, out-of-the-box supported languages could benefit from something like this), but if there is no way to achieve this, I might think about a package for it!

Thank you guys!


Surround code with constructs
#2

The Atom API allows you to insert text at arbitrary locations, so it has the ability to do what you want. No, there isn’t some function like wrapSelectionWith(selection, "if(foo) {", "}"), but you could easily write one given what the Atom API provides.


#3

As a crude workaround, it would be easy enough to cut your selection, insert a snippet like if($2) { $1 }, and then paste.


#4

This feature is something I’ve been trying to figure out forever, is one of the greatest drawbacks =for me to atom and I never seem to find an answer that is not super high level or too general.
Would be nice to see an example of what could achieve the result @b3by is looking for.
So far I’ve done what @DamnedScholar says and yes, works, but has to be easier than that, specially since other IDEs allow you to apply snippets to surround code with even keyboard shortcuts and are super easy to set up.


#5

Thank you guys for the feedback.

I actually considered the snippet solution from @DamnedScholar , but it would just be a temporary hack. I wouldn’t really see my snippet file flooded with so many snippets (a set for each supported language!), even if this would be quite flexible and easy to expand/modify. Still, it is not optimal when it comes to usage - cut the selection, call the snippet, past the selection.

Yesterday I started working on a package for this. Its main behavior leverages the APIs that @leedohm mentioned. The usage will be:

  • select some lines of code
  • call the package with the shortcut
  • a select view like the command palette one will pop up, allowing the user either to search for a construct or to navigate the list with the arrow keys
  • when a construct is selected, the selection in the buffer will be surrounded with it
  • the whole new block will be indented adequately

Moreover:

  • if no line is selected on the buffer, the cursor line will be surrounded with the construct (assuming no multiple cursors are living on the buffer)
  • the user should be able to create custom constructs and store them in a file (still not sure about this)

As @lechip mentioned, it seems to me like a quite important feature for an IDE, and it also is pretty easy to build. I’ll get back with some updates (hopefully tonight!) when a first version of the package will be available.

Thank you again!


#6

Couldn’t this be automated in the snippets package ?

When the user has a selection then insert the selection as the first tab stop. Some snippets (like try...catch) would work out of the box and others (if...else, function statements) would only need a flag to indicate where the code body should be placed.


#7

Sorry, I’m not sure I got this clearly.
I select some code, then I need to write the snippet prefix and press tab. When I do this, my selection is gone. Are you suggesting that the snippet package should automatically paste the previously selected code within the construct created by the snippet? What if just want to replace my selection with the content of my snippet?


#8

You mentioned

Like snippets:available ?

If a snippet is triggered by snippets:available then insert the code selection as a tab stop. If a snippet is triggered by its prefix and TAB then replace the selection.


#9

Ok, now I see.

I was thinking exactly at this kind of behavior. I find it easier to create a new package for such things, since I’m not sure this kind of feature will be accepted as pull request in the snippets package. Plus, a package implementation would guarantee a certain set of constructs to be standard-complaint, and yet the user could have the chance to write new constructs.


#10

I worked on a first version of a package, but it is still under heavy development. Here is the atom link.

I just published the first version, this just gives an idea of what the package is going to be.
I will update the version later tonight (I hope!), in the meantime feel free to give it a try and raise as many issues as you please!

@deprint, where you talking about something like that?


#11

Exactly. It has its issues (one or two exceptions, nothing major), but this is exactly what I’m looking for.