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 authored and rgrunber committed Jul 4, 2023
1 parent 7a0313e commit edf7fd3
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
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;
}
}
2 changes: 1 addition & 1 deletion org.eclipse.jdt.ls.target/org.eclipse.jdt.ls.tp.target
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<unit id="org.eclipse.jdt.source.feature.group" version="0.0.0"/>
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
<unit id="org.mockito.mockito-core" version="0.0.0"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.29-I-builds/I20230625-1800/"/>
<repository location="https://download.eclipse.org/eclipse/updates/4.29-I-builds/I20230629-1800/"/>
</location>
<location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
<unit id="org.eclipse.xtext.xbase.lib" version="0.0.0"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 edf7fd3

Please sign in to comment.