Any hints on reformatting comments

We've got a local Checkstyle rule that states no tabs in java files.

However whenever I reformat or use 'replace indents' 'tabs with spaces' action in IntelliJ it ignores the tabs in comments. Is there a way to perform kind of replacement for all the editors content? Trick is to keep the indents in the comments. Otherwise a regex search & replace would work.

Many of these 'invalid' comments are auto-generated eclipse template ones of the kind: /* (non-Javadoc) @see<interface method>.... */ these I'd like to delete as they add nothing. But I'm also struggling with multiline replacement. Seems the multiline mode for search & replace doesn't have a regex option? Anyone want to share some editor trick to delete these? A macro would almost do it, but I'd need a 'Select Comment|JavaDoc' action of some kind.

Comment actions Permalink


Would be nice to see an example of comment to process. Eclipse generated comment are to be deleted - Ok, but what is to be fixed then?

E.g. replace supports regexp. Say, "line 1$line2" finds the entry like:
line 1
line 2
To insert such entry use not '$' but '\n': "line1\nline 2".

Convert Indents action applies to leading tabs in non-javadoc comments. But not tabs in the middle.

Settings / Code Style / Java / JavaDoc / Enable JavaDoc formatting makes Reformat (Ctrl+Alt+L) apply to leading tabs in Javadoc comments. But not to tabs in the middle.

Does anything help? What is not working?


Comment actions Permalink

The kind of comments I'm doing a lot of manual deleting of look like:

     /*       * (non-Javadoc)       *       * @see       */

Because of the variable @see content I was trying to wildcard the regex, the wildcards aren't multline it seems? I'll try a regex with explict newlines, but I know some of these comments may be wrapped (often after the @see) due to our code formatting standard, which used to be much more restrictive, so the number of lines may vary.

There's also an old comment style that used CVS or SVN tags to include commit & merge history into the header of every file - these too need to go, but the content of these varies wildly.

There are other devleoper comments and two old style of file headers I commonly need to get rid of (replace with current standards). These often have tabs too which is why I was asking about both removal and tab replacement. Just trying to find a way to make these legacy support tasks less of a chore. What I'd like is an action of some kind I can call to do a bunch of basic stylistic cleanup jobs like this.

Maybe one of the other code formatting plugins or an external sed tool call... if not macros and/or a custom live plugin.

Comment actions Permalink

Seems this will catch a good proportion of these (explict newlines not multiline regex):


Still need to work out how to replace tabs inside irregular comments (Javadoc, block or line) should only work inside comments (not vars, string literals).

Then need to wrap a cople of these search & replaces up with code code formats and tab replacements. Macro(s) or Live plugin?

Comment actions Permalink

Thank you. Formatting does not really do this.

I'm not a regex guru, just found a way to match multiline text: a (?s) expression. In other words it makes the "dot" pattern match line breaks. See:

So, the pattern "(?s).*" matches the whole text, including all line breaks. So, you need to surround this with comment bounds (NB: make sure many comments in one text are matched separately!), embed a tab "\t" there, make it a matchig group, or so. I will try to play with this, but later.



Please sign in to leave a comment.