mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
52 lines
2.0 KiB
Markdown
52 lines
2.0 KiB
Markdown
# Conditional Operator Converter [][docs]
|
|
*Reference: [Code Intentions in IntelliJ SDK Docs][docs:conditional_operator_intention]*
|
|
|
|
## Quickstart
|
|
|
|
Conditional Operator Converter provides an intention for converting the *ternary operator* into the *if* statement, i.e.:
|
|
|
|
```java
|
|
public class X {
|
|
void f(boolean isMale) {
|
|
String title = isMale ? "Mr." : "Ms.";
|
|
System.out.println("title = " + title);
|
|
}
|
|
}
|
|
```
|
|
|
|
will become:
|
|
|
|
```java
|
|
public class X {
|
|
void f(boolean isMale) {
|
|
String title;
|
|
if (isMale) {
|
|
title = "Mr.";
|
|
} else {
|
|
title = "Ms.";
|
|
}
|
|
System.out.println("title = " + title);
|
|
}
|
|
}
|
|
```
|
|
|
|
To invoke the intention action, it is necessary to place the caret on the `?` character of the ternary operator.
|
|
The converter in the `isAvailable` method, has defined the token check to match `JavaTokenType.QUEST`, which is `?` character.
|
|
|
|
### Extension Points
|
|
|
|
| Name | Implementation | Extension Point Class |
|
|
| ------------------------------ | ----------------------------------------------------------------- | ------------------------------------------------------------------ |
|
|
| `com.intellij.intentionAction` | [ConditionalOperatorConverter][file:ConditionalOperatorConverter] | [PsiElementBaseIntentionAction][sdk:PsiElementBaseIntentionAction] |
|
|
|
|
*Reference: [Plugin Extension Points in IntelliJ SDK Docs][docs:ep]*
|
|
|
|
|
|
[docs]: https://www.jetbrains.org/intellij/sdk/docs
|
|
[docs:conditional_operator_intention]: https://www.jetbrains.org/intellij/sdk/docs/tutorials/code_intentions.html
|
|
[docs:ep]: https://www.jetbrains.org/intellij/sdk/docs/basics/plugin_structure/plugin_extensions.html
|
|
|
|
[file:ConditionalOperatorConverter]: ./src/main/java/org/intellij/sdk/intention/ConditionalOperatorConverter.java
|
|
|
|
[sdk:PsiElementBaseIntentionAction]: upsource:///platform/lang-api/src/com/intellij/codeInsight/intention/PsiElementBaseIntentionAction.java
|