Over a million developers have joined DZone.

Easy Unwrapping with IntelliJ IDEA

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

In complicated code constructs that contain numerous nested statements, you sometimes need to accurately get rid of the enclosing parts. When you try to manually delete such statements, it is too easy to break the syntax of the whole construct. With the new IntelliJ IDEA’s unwrapping feature, this task becomes just a snap.

Consider the following example, where a string variable is being analyzed:

[img_assist|nid=3370|title=|desc=|link=none|align=left|width=640|height=299]

 

 

 

 

 

 

 

 

Let’s remove one of the branches. To do that, place the cursor at the branch containing the statement, and press Ctrl+ Shift+Delete. IntelliJ IDEA shows you which results you can obtain: the part that will remain is displayed on the blue background, while the grey background denotes the pieces of code to be deleted:

[img_assist|nid=3371|title=|desc=|link=none|align=left|width=640|height=258]

 

 

 

 

 

 

 

 

After clicking the Remove ‘else…’ option, we get the promised results:

[img_assist|nid=3372|title=|desc=|link=none|align=left|width=640|height=226]

 

 

 

 

 

 

 

 

As you see, all parentheses are properly balanced, and the syntax is correct.

However, Java developers are not the only ones who can enjoy this powerful feature. The same “stripping” can be done in XML or HTML code.

Consider a situation when you have to clean up some HTML code and get rid of inline styles, for example, remove bold fonts. What will you do in this case – delete an opening tag, then delete the closing tag, with the real possibility to lose something in process. With IntelliJ IDEA, all you have to do is to place the caret somewhere inside the tags, and press Ctrl+Shift+Delete. The unnecessary tags are removed silently.

This facility is even more helpful for XML constructs, which can have deep nesting. You can sequentially remove enclosing tags level by level, until getting to the innermost one you want to preserve.

Do I need to mention that such unwrapping can be undone?

Enjoy!

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}