Can one automatically get Intellij's regex assistance for one's own regex parameters

不羁岁月 提交于 2019-12-05 03:25:48
Bajal

To activate regex language for a parameter in your IDE, double-click on the regex string and press AltEnter (shortcut for Intention Actions) and choose "Inject Language or Reference", then choose "Regexp" in the resulting list:

This will permanently attach regex to the parameter at all call sites.

To do this to multiple text fragments, hold down ShiftAlt keys and keep double-clicking all the instances (the multi-select feature in IntelliJ).

To create a parameter that automatically has Intellij's language assist feature, so your other dev team members can automatically get regex assistance too, use IntelliJ's IntelliLang plugin by adding jetbrains' annotations library as a compile dependency, eg for gradle:

dependencies {
    ...
    compile 'org.jetbrains:annotations:15.0'
}

Then to use it, specify "RegExp" as the @Language for the parameter:

import org.intellij.lang.annotations.Language;

void myMethod(@Language("RegExp") String regex) {
    // some impl
}

Regex validation of Strings passed to the method will now work from any method invocation by anyone editing the source code in Intellij.


Note: There's an alternative, which is not recommended, to adding a dependency to your build (if you have one), and knowing that the effect is only within your IDE and you'll have to compile within your IDE, Intellij lets you do this:

  1. Add @Language("RegExp") annotation to the method parameter
  2. Click on the red bulb icon to import annotations into the classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!