Skip to content

Commit

Permalink
Add support for Join and Split variable quick actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gayanper committed Jun 27, 2023
1 parent 230d394 commit c21d2aa
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
Expand Up @@ -115,8 +115,10 @@
import org.eclipse.jdt.internal.corext.dom.Selection;
import org.eclipse.jdt.internal.corext.fix.FixMessages;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.JoinVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.LambdaExpressionAndMethodRefFixCore;
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore;
import org.eclipse.jdt.internal.corext.fix.SplitVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.StringConcatToTextBlockFixCore;
import org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore;
import org.eclipse.jdt.internal.corext.refactoring.surround.SurroundWithTryWithResourcesAnalyzer;
Expand Down Expand Up @@ -223,6 +225,10 @@ public List<ChangeCorrectionProposal> getAssists(CodeActionParams params, IInvoc
if (!problemExists(locations, javaDocCommentProblems)) {
JavadocTagsSubProcessor.getMissingJavadocCommentProposals(context, coveringNode, resultingCollections, JavaCodeActionKind.QUICK_ASSIST);
}

// Variable quick fixes
getSplitVariableProposal(context, coveringNode, resultingCollections);
getJoinVariableProposal(context, coveringNode, resultingCollections);
return resultingCollections;
}
return Collections.emptyList();
Expand Down Expand Up @@ -1835,4 +1841,33 @@ private static boolean getStringConcatToTextBlockProposal(IInvocationContext con
return false;
}

private boolean getJoinVariableProposal(IInvocationContext context, ASTNode coveringNode, ArrayList<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections != null) {
SplitVariableFixCore fix = SplitVariableFixCore.createSplitVariableFix(context.getASTRoot(), coveringNode);
if (fix != null) {
try {
resultingCollections.add(new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, fix.createChange(null), IProposalRelevance.SPLIT_VARIABLE_DECLARATION));
return true;
} catch (CoreException e) {
// ignore
}
}
}
return false;
}

private boolean getSplitVariableProposal(IInvocationContext context, ASTNode coveringNode, ArrayList<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections != null) {
JoinVariableFixCore fix = JoinVariableFixCore.createJoinVariableFix(context.getASTRoot(), coveringNode);
if (fix != null) {
try {
resultingCollections.add(new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, fix.createChange(null), IProposalRelevance.JOIN_VARIABLE_DECLARATION));
return true;
} catch (CoreException e) {
// ignore
}
}
}
return false;
}
}
@@ -0,0 +1,99 @@
/*******************************************************************************
* Copyright (c) 2023 Gayan Perera and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Gayan Perera - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.correction;

import java.util.Hashtable;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.ls.core.internal.CodeActionUtil;
import org.eclipse.jdt.ls.core.internal.JavaCodeActionKind;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionKind;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.junit.Before;
import org.junit.Test;

public class VariableQuickFixTest extends AbstractSelectionTest {

private IJavaProject fJProject1;

private IPackageFragmentRoot fSourceFolder;

@Before
public void setup() throws Exception {
fJProject1 = newEmptyProject();
Hashtable<String, String> options = TestOptions.getDefaultOptions();
fJProject1.setOptions(options);
fSourceFolder = fJProject1.getPackageFragmentRoot(fJProject1.getProject().getFolder("src"));
setOnly(CodeActionKind.QuickFix);
}

@Test
public void testSplitVariableExpectDeclarationAndAssignment() throws Exception {
setOnly(JavaCodeActionKind.QUICK_ASSIST);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
//@formatter:off
String contents = "package test1;\r\n"
+ "public class V {\r\n"
+ " public void foo() {\r\n"
+ " int maxCount = 10;\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
ICompilationUnit cu = pack1.createCompilationUnit("V.java", contents, false, null);
//@formatter:off
String expected = "package test1;\r\n"
+ "public class V {\r\n"
+ " public void foo() {\r\n"
+ " int maxCount;\r\n"
+ " maxCount = 10;\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, CodeActionUtil.getRange(cu, "maxCount"));
Expected e1 = new Expected("Split variable declaration", expected, JavaCodeActionKind.QUICK_ASSIST);
assertCodeActions(codeActions, e1);
}

@Test
public void testJoinVariableExpectDeclarationAndAssignment() throws Exception {
setOnly(JavaCodeActionKind.QUICK_ASSIST);
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
//@formatter:off
String contents = "package test1;\r\n"
+ "public class V {\r\n"
+ " public void foo() {\r\n"
+ " int maxCount;\r\n"
+ " maxCount = 10;\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
ICompilationUnit cu = pack1.createCompilationUnit("V.java", contents, false, null);
//@formatter:off
String expected = "package test1;\r\n"
+ "public class V {\r\n"
+ " public void foo() {\r\n"
+ " int maxCount = 10;\r\n"
+ " }\r\n"
+ "}";
//@formatter:on
List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, CodeActionUtil.getRange(cu, "maxCount"));
Expected e1 = new Expected("Join variable declaration", expected, JavaCodeActionKind.QUICK_ASSIST);
assertCodeActions(codeActions, e1);
}
}

0 comments on commit c21d2aa

Please sign in to comment.