Skip to content

Commit

Permalink
ConditionalOperatorConverter: Cleanup: remove final modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
karollewandowski committed Sep 5, 2023
1 parent be7ac20 commit 1cde070
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable Ps
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element)
throws IncorrectOperationException {
// Get the factory for making new PsiElements, and the code style manager to format new statements
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
final CodeStyleManager codeStylist = CodeStyleManager.getInstance(project);
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
CodeStyleManager codeStylist = CodeStyleManager.getInstance(project);

// Get the parent of the "?" element in the ternary statement to find the conditional expression that contains it
PsiConditionalExpression conditionalExpression =
Expand All @@ -116,9 +116,8 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
// If the original statement is a declaration based on a ternary operator,
// split the declaration and assignment
if (originalStatement instanceof PsiDeclarationStatement declaration) {

// Find the local variable within the declaration statement
final PsiElement[] declaredElements = declaration.getDeclaredElements();
PsiElement[] declaredElements = declaration.getDeclaredElements();
PsiLocalVariable variable = null;
for (PsiElement declaredElement : declaredElements) {
if (declaredElement instanceof PsiLocalVariable &&
Expand All @@ -133,7 +132,7 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement

// Ensure that the variable declaration is not combined with other declarations, and add a mark
variable.normalizeDeclaration();
final Object marker = new Object();
Object marker = new Object();
PsiTreeUtil.mark(conditionalExpression, marker);

// Create a new expression to declare the local variable
Expand All @@ -154,7 +153,7 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
variableInitializer.delete();

// 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);
conditionalExpression = (PsiConditionalExpression) PsiTreeUtil.releaseMark(originalStatement, marker);
}
Expand All @@ -168,7 +167,7 @@ public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement
newIfStmt = (PsiIfStatement) codeStylist.reformat(newIfStmt);

// 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();
if (newIfStmtCondition == null) {
return;
Expand Down

0 comments on commit 1cde070

Please sign in to comment.