Java/Scala: Delete to delimiter? (like Emacs paredit)

One of the awesome features of Emacs "paredit" mode that works with Lisp-like as well as other languages like Java/Scala is the ability to just hit Ctrl-k (kill), and it will smartly delete text until the appropriate delimiter, which could be a closing quote-mark, or a closing brace/paren/square-bracket. It will essentially delete things while still keeping your code syntactically valid, i.e. it will delete exactly the right number of closing parens/braces, etc.

Is there such an action or keyboard shortcut in Intellij IDEA? Or how would I define a macro to do this?

5 comments
Comment actions Permalink

The closest I think you'll come is to use Ctrl+W to "Extend Selection" (and it's opposite Ctrl+Shift+W to decrease a selection). This will intelligently select text increasing its size each time to include the next logical "block". Once you have what you want selected, just hit the delete key. See Selecting Text in the Editor in the help guide, or in the Webhelp.


You can also look at the "unwrap" action as it may be useful  for some situations. It is set to Ctrl+Shift+Delete by default on Windows, or via the menu Code > Unwrap. This will allow you to remove the wrapping of a text block. For example, removing the if condition and the 'supporting' braces from around a block of code.

0
Comment actions Permalink

Thanks, yes I am aware of the expand-selection Ctrl+W, but it only allows me to delete the entire current-selection. I want to be able to delete from the current position to the end of the selection. So if there was an action that deletes to the end of the current selection, that would be something I could use to make a macro perhaps. But really, what I want is something that doesn't require me to make a selection, and simply deletes to the end of the current expression while at the same time retaining the various delimiters etc to maintain syntactic validity.

0
Comment actions Permalink

You can search the plugins repository. Perhaps one of the 'emacs' plugins -- such as emacsIDEAs -- support what you want. If not, you can make a request to the plug-in author or roll your own. I'm fairly certain it is not natively supported in IDEA (based on my 11 years of use and on searching Help > Find Actions (Ctrl+Shift+A) for "delete" does not reveal anything that looks like what you want.)

0
Comment actions Permalink

Thank you, will do that. Yes I did search Emacs in the plugins repos, but didn't seem to find what I want. Maybe I need to roll my own.

0
Comment actions Permalink

I have a solution that almost works: Record a macro sequence (Mac key-bindings):

  1. Expand Selection (Alt Up)
  2. Expand Selection (Alt Up)
  3. Right with selection (Shift RightArrow)
  4. Cut Selection (Cmd X)

Then I bind this macro to Ctrl-K. It works great, e.g. (the vertical bar is the caret position when this is invoked):

List( "first", "second", "third |string",  "fourth")   => List( "first", "second", "third ", "fourth")   

{100, 200, {300|, 400, 500}, 20}   => {100, 200, {300}, 20}

The case which doesn't quite work exactly the way I want is when I use parentheses:

(1, 2, (3, |4, 5), 6, 7, 8)    => (1, 2, (3, , 6, 7, 8)  

Notice that it deletes the enclosing right-parenthesis, which is because Expand-Selection includes the enclosing parentheses. If the enclosing expression is a string or has braces, then Expand-Selection does not include the right-hand-side quotes or brace.

If I knew how to do Expand-Selection without including the enclosing parentheses, then this would be perfect, but I can live with what I have now.
0

Please sign in to leave a comment.