mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-27 16:57:49 +08:00
ConditionalOperatorConverter: Cleanup: remove final modifiers
This commit is contained in:
parent
be7ac20f04
commit
1cde070dd6
@ -88,8 +88,8 @@ public class ConditionalOperatorConverter extends PsiElementBaseIntentionAction
|
|||||||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element)
|
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element)
|
||||||
throws IncorrectOperationException {
|
throws IncorrectOperationException {
|
||||||
// Get the factory for making new PsiElements, and the code style manager to format new statements
|
// Get the factory for making new PsiElements, and the code style manager to format new statements
|
||||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
|
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||||
final CodeStyleManager codeStylist = CodeStyleManager.getInstance(project);
|
CodeStyleManager codeStylist = CodeStyleManager.getInstance(project);
|
||||||
|
|
||||||
// Get the parent of the "?" element in the ternary statement to find the conditional expression that contains it
|
// Get the parent of the "?" element in the ternary statement to find the conditional expression that contains it
|
||||||
PsiConditionalExpression conditionalExpression =
|
PsiConditionalExpression conditionalExpression =
|
||||||
@ -116,9 +116,8 @@ public class ConditionalOperatorConverter extends PsiElementBaseIntentionAction
|
|||||||
// If the original statement is a declaration based on a ternary operator,
|
// If the original statement is a declaration based on a ternary operator,
|
||||||
// split the declaration and assignment
|
// split the declaration and assignment
|
||||||
if (originalStatement instanceof PsiDeclarationStatement declaration) {
|
if (originalStatement instanceof PsiDeclarationStatement declaration) {
|
||||||
|
|
||||||
// Find the local variable within the declaration statement
|
// Find the local variable within the declaration statement
|
||||||
final PsiElement[] declaredElements = declaration.getDeclaredElements();
|
PsiElement[] declaredElements = declaration.getDeclaredElements();
|
||||||
PsiLocalVariable variable = null;
|
PsiLocalVariable variable = null;
|
||||||
for (PsiElement declaredElement : declaredElements) {
|
for (PsiElement declaredElement : declaredElements) {
|
||||||
if (declaredElement instanceof PsiLocalVariable &&
|
if (declaredElement instanceof PsiLocalVariable &&
|
||||||
@ -133,7 +132,7 @@ public class ConditionalOperatorConverter extends PsiElementBaseIntentionAction
|
|||||||
|
|
||||||
// Ensure that the variable declaration is not combined with other declarations, and add a mark
|
// Ensure that the variable declaration is not combined with other declarations, and add a mark
|
||||||
variable.normalizeDeclaration();
|
variable.normalizeDeclaration();
|
||||||
final Object marker = new Object();
|
Object marker = new Object();
|
||||||
PsiTreeUtil.mark(conditionalExpression, marker);
|
PsiTreeUtil.mark(conditionalExpression, marker);
|
||||||
|
|
||||||
// Create a new expression to declare the local variable
|
// Create a new expression to declare the local variable
|
||||||
@ -154,7 +153,7 @@ public class ConditionalOperatorConverter extends PsiElementBaseIntentionAction
|
|||||||
variableInitializer.delete();
|
variableInitializer.delete();
|
||||||
|
|
||||||
// Get the grandparent of the local var declaration, and add the new declaration just beneath it
|
// Get the grandparent of the local var declaration, and add the new declaration just beneath it
|
||||||
final PsiElement variableParent = variable.getParent();
|
PsiElement variableParent = variable.getParent();
|
||||||
originalStatement = variableParent.getParent().addAfter(statement, variableParent);
|
originalStatement = variableParent.getParent().addAfter(statement, variableParent);
|
||||||
conditionalExpression = (PsiConditionalExpression) PsiTreeUtil.releaseMark(originalStatement, marker);
|
conditionalExpression = (PsiConditionalExpression) PsiTreeUtil.releaseMark(originalStatement, marker);
|
||||||
}
|
}
|
||||||
@ -168,7 +167,7 @@ public class ConditionalOperatorConverter extends PsiElementBaseIntentionAction
|
|||||||
newIfStmt = (PsiIfStatement) codeStylist.reformat(newIfStmt);
|
newIfStmt = (PsiIfStatement) codeStylist.reformat(newIfStmt);
|
||||||
|
|
||||||
// Replace the conditional expression with the one from the original ternary expression
|
// Replace the conditional expression with the one from the original ternary expression
|
||||||
final PsiReferenceExpression condition = (PsiReferenceExpression) conditionalExpression.getCondition().copy();
|
PsiReferenceExpression condition = (PsiReferenceExpression) conditionalExpression.getCondition().copy();
|
||||||
PsiExpression newIfStmtCondition = newIfStmt.getCondition();
|
PsiExpression newIfStmtCondition = newIfStmt.getCondition();
|
||||||
if (newIfStmtCondition == null) {
|
if (newIfStmtCondition == null) {
|
||||||
return;
|
return;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user