简体   繁体   English

Intellij idea:如何使用regexp创建自定义检查规则

[英]Intellij idea: How do I create custom Inspection rule with regexp

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. 我正在寻找一种在Intellij中创建检查规则的方法,该方法确定何时有内联注释并将它们移动到上面的自己的行中。

Example: Find 示例:查找

someCode() // someComment

and replace with 并替换为

// someComment
someCode()

I was able to find the correct regexp to find and replace: 我能够找到正确的正则表达式来查找和替换:

Find: (. \\S. )(//)(. \\S. ) 查找:(。 \\ S。 )(//)(。 \\ S。
Replace: $2$3\\n$1 替换:$ 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. 我已经阅读了https://www.jetbrains.com/idea/help/creating-custom-inspections.html上的文档但是找不到任何使用regexp进行搜索和替换的示例。

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. 占位符似乎允许使用正则表达式 ,但主要结构似乎不这样做,因为它需要特定的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM