Skip to content

Commit ecd3b61

Browse files
committed
Add code action for unused pattern variable / unused lambda parameter.
- Also add as clean up - Add testcase for clean up functionality Signed-off-by: Roland Grunberg <[email protected]>
1 parent 3698052 commit ecd3b61

File tree

5 files changed

+105
-2
lines changed

5 files changed

+105
-2
lines changed

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpRegistry.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public CleanUpRegistry() {
5555
new LambdaExpressionCleanup(),
5656
new TryWithResourceCleanUp(),
5757
new LambdaExpressionAndMethodRefCleanUp(),
58-
new OrganizeImportsCleanup());
58+
new OrganizeImportsCleanup(),
59+
new RenameUnusedLocalVariableCleanup());
5960

6061
// Store in a Map so that they can be accessed by ID quickly
6162
cleanUps = new HashMap<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat Inc. and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License 2.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-2.0
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Red Hat Inc. - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.jdt.ls.core.internal.cleanup;
14+
15+
import java.util.Arrays;
16+
import java.util.Collection;
17+
import java.util.List;
18+
19+
import org.eclipse.core.runtime.CoreException;
20+
import org.eclipse.jdt.core.JavaCore;
21+
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
22+
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
23+
import org.eclipse.jdt.ui.cleanup.CleanUpContext;
24+
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
25+
26+
/**
27+
* Represents a clean up that renames unused lambda parameter identifier to an
28+
* underscore (_)
29+
*/
30+
public class RenameUnusedLocalVariableCleanup implements ISimpleCleanUp {
31+
32+
private static final List<String> COMPILER_OPTS = Arrays.asList(JavaCore.COMPILER_PB_UNUSED_LAMBDA_PARAMETER, JavaCore.COMPILER_PB_UNUSED_LOCAL);
33+
34+
@Override
35+
public Collection<String> getIdentifiers() {
36+
return List.of("removeUnusedLambdaParameters", CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES);
37+
}
38+
39+
@Override
40+
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
41+
return RenameUnusedVariableFixCore.createCleanUp(context.getAST(), true);
42+
}
43+
44+
@Override
45+
public List<String> getRequiredCompilerMarkers() {
46+
return COMPILER_OPTS;
47+
}
48+
49+
}

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/QuickFixProcessor.java

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ private void process(CodeActionParams params, IInvocationContext context, IProbl
342342
case IProblem.UnusedPrivateType:
343343
case IProblem.LocalVariableIsNeverUsed:
344344
case IProblem.ArgumentIsNeverUsed:
345+
case IProblem.LambdaParameterIsNeverUsed:
345346
case IProblem.UnusedPrivateField:
346347
LocalCorrectionsSubProcessor.addUnusedMemberProposal(context, problem, proposals);
347348
break;

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/LocalCorrectionsSubProcessor.java

+17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
9393
import org.eclipse.jdt.internal.corext.fix.CodeStyleFixCore;
9494
import org.eclipse.jdt.internal.corext.fix.IProposableFix;
95+
import org.eclipse.jdt.internal.corext.fix.RenameUnusedVariableFixCore;
9596
import org.eclipse.jdt.internal.corext.fix.SealedClassFixCore;
9697
import org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore;
9798
import org.eclipse.jdt.internal.corext.fix.UnusedCodeFixCore;
@@ -434,6 +435,22 @@ public static void addUnimplementedMethodsProposals(IInvocationContext context,
434435
public static void addUnusedMemberProposal(IInvocationContext context, IProblemLocation problem, Collection<ProposalKindWrapper> proposals) {
435436
int problemId = problem.getProblemId();
436437

438+
if (JavaModelUtil.is22OrHigher(context.getCompilationUnit().getJavaProject()) && (problemId == IProblem.LocalVariableIsNeverUsed || problemId == IProblem.LambdaParameterIsNeverUsed)) {
439+
RenameUnusedVariableFixCore fix = RenameUnusedVariableFixCore.createRenameToUnnamedFix(context.getASTRoot(), problem);
440+
if (fix != null) {
441+
try {
442+
CompilationUnitChange change = fix.createChange(null);
443+
CUCorrectionProposalCore proposal = new CUCorrectionProposalCore(change.getName(), change.getCompilationUnit(), change, IProposalRelevance.UNUSED_MEMBER);
444+
proposals.add(CodeActionHandler.wrap(proposal, CodeActionKind.QuickFix));
445+
} catch (CoreException e) {
446+
JavaLanguageServerPlugin.log(e);
447+
}
448+
}
449+
}
450+
if (problemId == IProblem.LambdaParameterIsNeverUsed) {
451+
return;
452+
}
453+
437454
UnusedCodeFixCore fix = UnusedCodeFixCore.createUnusedMemberFix(context.getASTRoot(), problem, false);
438455
if (fix != null) {
439456
try {

org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/cleanup/CleanUpsTest.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setup() throws Exception {
5858
javaProject = JavaCore.create(project);
5959

6060
Hashtable<String, String> options = TestOptions.getDefaultOptions();
61-
JavaCore.setComplianceOptions(JavaCore.VERSION_19, options);
61+
JavaCore.setComplianceOptions(JavaCore.VERSION_22, options);
6262
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, String.valueOf(99));
6363
javaProject.setOptions(options);
6464

@@ -640,4 +640,39 @@ public void test() {
640640
assertEquals(expected, actual);
641641
}
642642

643+
@Test
644+
public void testRenameUnusedLambdaParameterCleanup() throws Exception {
645+
String contents = """
646+
package test1;
647+
public class A {
648+
private interface J {
649+
public void run(String a, String b);
650+
}
651+
public void test() {
652+
J j = (a, b) -> System.out.println(a);
653+
j.run("a", "b");
654+
}
655+
}
656+
""";
657+
658+
ICompilationUnit unit = pack1.createCompilationUnit("A.java", contents, false, monitor);
659+
String uri = unit.getUnderlyingResource().getLocationURI().toString();
660+
661+
String expected = """
662+
package test1;
663+
public class A {
664+
private interface J {
665+
public void run(String a, String b);
666+
}
667+
public void test() {
668+
J j = (a, _) -> System.out.println(a);
669+
j.run("a", "b");
670+
}
671+
}
672+
""";
673+
List<TextEdit> textEdits = registry.getEditsForAllActiveCleanUps(new TextDocumentIdentifier(uri), Arrays.asList("removeUnusedLambdaParameters"), monitor);
674+
String actual = TextEditUtil.apply(unit, textEdits);
675+
assertEquals(expected, actual);
676+
}
677+
643678
}

0 commit comments

Comments
 (0)