Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Join and Split variable quick actions. #2732

Merged
merged 1 commit into from Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
@@ -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);
}
}