Skip to content

Commit

Permalink
Add code action for unused pattern variable / unused lambda parameter.
Browse files Browse the repository at this point in the history
- Also add as clean up
- Add testcase for clean up functionality

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Nov 6, 2024
1 parent 3698052 commit da2d52d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public CleanUpRegistry() {
new LambdaExpressionCleanup(),
new TryWithResourceCleanUp(),
new LambdaExpressionAndMethodRefCleanUp(),
new OrganizeImportsCleanup());
new OrganizeImportsCleanup(),
new RenameUnusedLocalVariableCleanup());

// Store in a Map so that they can be accessed by ID quickly
cleanUps = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat Inc. 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
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.cleanup;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;

public class RenameUnusedLocalVariableCleanup implements ISimpleCleanUp {

private static final List<String> COMPILER_OPTS = Arrays.asList(JavaCore.COMPILER_PB_UNUSED_LAMBDA_PARAMETER);

@Override
public Collection<String> getIdentifiers() {
return List.of("removeUnusedLambdaParameters", CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES);
}

@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
return RenameUnusedVariableFixCore.createCleanUp(context.getAST(), true);
}

@Override
public List<String> getRequiredCompilerMarkers() {
return COMPILER_OPTS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ private void process(CodeActionParams params, IInvocationContext context, IProbl
case IProblem.UnusedPrivateType:
case IProblem.LocalVariableIsNeverUsed:
case IProblem.ArgumentIsNeverUsed:
case IProblem.LambdaParameterIsNeverUsed:
case IProblem.UnusedPrivateField:
LocalCorrectionsSubProcessor.addUnusedMemberProposal(context, problem, proposals);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
import org.eclipse.jdt.internal.corext.fix.CodeStyleFixCore;
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
import org.eclipse.jdt.internal.corext.fix.SealedClassFixCore;
import org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore;
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
Expand Down Expand Up @@ -434,6 +435,22 @@ public static void addUnimplementedMethodsProposals(IInvocationContext context,
public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<ProposalKindWrapper> proposals) {
int problemId = problem.getProblemId();

if (JavaModelUtil.is22OrHigher(context.getCompilationUnit().getJavaProject()) && (problemId == IProblem.LocalVariableIsNeverUsed || problemId == IProblem.LambdaParameterIsNeverUsed)) {
RenameUnusedVariableFixCore fix = RenameUnusedVariableFixCore.createRenameToUnnamedFix(context.getASTRoot(), problem);
if (fix != null) {
try {
CompilationUnitChange change = fix.createChange(null);
CUCorrectionProposalCore proposal = new CUCorrectionProposalCore(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.UNUSED_MEMBER);
proposals.add(CodeActionHandler.wrap(proposal, CodeActionKind.QuickFix));
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
}
}
}
if (problemId == IProblem.LambdaParameterIsNeverUsed) {
return;
}

UnusedCodeFixCore fix = UnusedCodeFixCore.createUnusedMemberFix(context.getASTRoot(), problem, false);
if (fix != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setup() throws Exception {
javaProject = JavaCore.create(project);

Hashtable<String, String> options = TestOptions.getDefaultOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_19, options);
JavaCore.setComplianceOptions(JavaCore.VERSION_22, options);
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, String.valueOf(99));
javaProject.setOptions(options);

Expand Down Expand Up @@ -640,4 +640,39 @@ public void test() {
assertEquals(expected, actual);
}

@Test
public void testRenameUnusedLambdaParameterCleanup() throws Exception {
String contents = """
package test1;
public class A {
private interface J {
public void run(String a, String b);
}
public void test() {
J j = (a, b) -> System.out.println(a);
j.run("a", "b");
}
}
""";

ICompilationUnit unit = pack1.createCompilationUnit("A.java", contents, false, monitor);
String uri = unit.getUnderlyingResource().getLocationURI().toString();

String expected = """
package test1;
public class A {
private interface J {
public void run(String a, String b);
}
public void test() {
J j = (a, _) -> System.out.println(a);
j.run("a", "b");
}
}
""";
List<TextEdit> textEdits = registry.getEditsForAllActiveCleanUps(new TextDocumentIdentifier(uri), Arrays.asList("removeUnusedLambdaParameters"), monitor);
String actual = TextEditUtil.apply(unit, textEdits);
assertEquals(expected, actual);
}

}

0 comments on commit da2d52d

Please sign in to comment.