Intellij idea: How do I create custom Inspection rule with regexp
Asked Answered
L

2

6

I`m looking for a way to create an inspection rule in Intellij that identifies when there are inline comments and moves them to their own line above.

Example: Find

someCode() // someComment

and replace with

// someComment
someCode()

I was able to find the correct regexp to find and replace:

Find: (.\S. )(//)(.\S.)
Replace: $2$3\n$1

But I can`t find a way to make a rule of it.

I have read the documentation on https://www.jetbrains.com/idea/help/creating-custom-inspections.html But can`t find any examples that uses regexp for search and replace.

Loon answered 16/9, 2015 at 12:1 Comment(0)
S
0

Your regexp seems to be wrong. It does not work the way you think.

This one should work ^(.*)//(.*)

However, I'm not completelly sure that you can use it as a custom inspection.

According to documentation. You need to define some structure based on "placeholders". The placeholders seem to allow regular expressions, but the main structure doesn't seem to do so as it requires an specific syntax.

Simplicidentate answered 31/7, 2018 at 8:34 Comment(0)
B
0

This regex preserves the indentation:

Search: ^( *)(.*)(//.*)
Replace: $1$3\n$1$2
Bergsonism answered 2/11 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.