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?

5 comments
Comment actions Permalink

Hello Martin,

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:

 file.findElementAt(editor.getCaretModel().getOffset());
> element = PsiTreeUtil.getParentOfType(element, PsiComment.class,
> false);
> if (element != null) {
> try {
> element.delete();
> } catch (IncorrectOperationException ignore) {
> }
> }]]>

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?


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!"


0
Comment actions Permalink

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.

0
Comment actions Permalink

Hello Martin,

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.


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!"


0
Comment actions Permalink

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... :)

0
Comment actions Permalink

Never mind, I found quite an elegant solution. Thanks.

0

Please sign in to leave a comment.