How to delete a comment properly?
I want to delete the comment at the current cursor location, but without caring what type of file the user currently works with. What I'm doing so far is something like this:
This works fine for Java and XML files, it even deletes any superfluous whitespaces. But it doesn't work for Ant files because AntCommentImpl.delete() is not implemented. Am I using a wrong approach or should I blame Ant integration for not being completely implemented?
Please sign in to leave a comment.
Hello Martin,
It's far easier and more reliable to do this via the document API rather
than via the PSI. delete() is indeed not implemented by many custom language
PSI elements.
--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
And is there an easy way to get rid of surrounding whitespace? The nice thing about PsiElement.delete() is that I don't have to care about it. If I delete a comment where nothing else is on the same line the whole line is removed. Simply using document.deleteString(element.getTextOffset(), element.getTextOffset() + element.getTextLength()); doesn't handle this case.
Hello Martin,
Well, you can walk the sibling chain forward and backward from the comment
element and include the PsiWhitespace elements you find into the text range
you delete from the document.
--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
The problem is that I don't want to remove EVERY whitespace. Consider this piece of code:
By calling PsiElement.delete() on both comments I'm left with no blanks after someVariable; but with a single blank line between the two variables, exactly what I try to achieve. Doing this manually seems to be quite a lot of work, so I was hoping for some helper utility which would do this automagically... :)
Never mind, I found quite an elegant solution. Thanks.