Skip to content

Commit ca876df

Browse files
committed
Link to Navigate to First Variable Declaration from duplicates
This commit adds support in the Quick Fix hover and dialog to navigate directly to the original Variable Declaration when a duplicate declaration error is shown
1 parent 50cbc3b commit ca876df

10 files changed

Lines changed: 364 additions & 11 deletions

File tree

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,5 @@ private CorrectionMessages() {
515515
public static String PreviewFeaturesSubProcessor_open_compliance_page_enable_preview_features_info;
516516
public static String PreviewFeaturesSubProcessor_open_compliance_properties_page_enable_preview_features;
517517
public static String PreviewFeaturesSubProcessor_open_compliance_properties_page_enable_preview_features_info;
518+
public static String DuplicateLocal_ShowExisting;
518519
}

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/CorrectionMessages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ TypeMismatchSubProcessor_insertnullcheck_description=Insert '!= null' check
157157
TypeMismatchSubProcessor_changetooptionalempty_description=Change to empty Optional
158158
TypeMismatchSubProcessor_changetooptionalof_description=Wrap with Optional
159159
TypeMismatchSubProcessor_changetooptionalofnullable_description=Wrap with nullable Optional
160+
DuplicateLocal_ShowExisting=Navigate to declared variable
160161

161162
RemoveDeclarationCorrectionProposal_removeunusedfield_description=Remove declaration of ''{0}'' and assignments without possible side effects
162163
RemoveDeclarationCorrectionProposal_removeunusedmethod_description=Remove method ''{0}''

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsBaseSubProcessor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
import org.eclipse.jdt.internal.ui.text.correction.proposals.GenerateForLoopAssistProposalCore;
191191
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposalCore;
192192
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposalCore;
193+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistShowDeclarationProposalCore;
193194
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposalCore;
194195
import org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposalCore;
195196
import org.eclipse.jdt.internal.ui.text.correction.proposals.NewLocalVariableCorrectionProposalCore;
@@ -1130,6 +1131,10 @@ public void getInvalidVariableNameProposals(IInvocationContext context, IProblem
11301131

11311132
LinkedNamesAssistProposalCore proposalCore= new LinkedNamesAssistProposalCore(name, context, nameNode, valueSuggestion);
11321133
proposals.add(linkedNamesAssistProposalToT(proposalCore));
1134+
if(nameNode.getParent() instanceof VariableDeclaration) {
1135+
LinkedNamesAssistShowDeclarationProposalCore showExisting= new LinkedNamesAssistShowDeclarationProposalCore(CorrectionMessages.DuplicateLocal_ShowExisting,context, nameNode);
1136+
proposals.add(LinkedNamesAssistShowDuplicateProposalToT(showExisting));
1137+
}
11331138
}
11341139

11351140
private void addRemoveProposal(IInvocationContext context, ASTNode selectedNode, Collection<T> proposals) {
@@ -3423,6 +3428,7 @@ protected LocalCorrectionsBaseSubProcessor() {
34233428
protected abstract T replaceCorrectionProposalToT(ReplaceCorrectionProposalCore core, int uid);
34243429
protected abstract T cuCorrectionProposalToT(CUCorrectionProposalCore core, int uid);
34253430
protected abstract T linkedNamesAssistProposalToT(LinkedNamesAssistProposalCore core);
3431+
protected abstract T LinkedNamesAssistShowDuplicateProposalToT(LinkedNamesAssistShowDeclarationProposalCore core);
34263432
protected abstract T assignToVariableAssistProposalToT(AssignToVariableAssistProposalCore core);
34273433
protected abstract T newVariableCorrectionProposalToT(NewVariableCorrectionProposalCore core, int uid);
34283434
protected abstract T newLocalVariableCorrectionProposalToT(NewLocalVariableCorrectionProposalCore core, int uid);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jdt.internal.ui.text.correction.proposals;
15+
16+
import org.eclipse.jdt.core.dom.SimpleName;
17+
import org.eclipse.jdt.core.manipulation.CUCorrectionProposalCore;
18+
19+
import org.eclipse.jdt.ui.text.java.IInvocationContext;
20+
21+
import org.eclipse.jdt.internal.ui.text.correction.IProposalRelevance;
22+
23+
24+
/**
25+
* A template proposal.
26+
*/
27+
public class LinkedNamesAssistShowDeclarationProposalCore extends CUCorrectionProposalCore {
28+
29+
public static final String ASSIST_ID= "org.eclipse.jdt.ui.correction.showOriginalDeclaration.assist"; //$NON-NLS-1$
30+
31+
private SimpleName fNode;
32+
private String fLabel;
33+
34+
public LinkedNamesAssistShowDeclarationProposalCore(String label, IInvocationContext context, SimpleName node) {
35+
super(label, context.getCompilationUnit(), IProposalRelevance.LINKED_NAMES_ASSIST);
36+
fLabel= label;
37+
fNode= node;
38+
}
39+
40+
public String getLabel() {
41+
return fLabel;
42+
}
43+
44+
public SimpleName getNode() {
45+
return fNode;
46+
}
47+
48+
@Override
49+
public String getCommandId() {
50+
return ASSIST_ID;
51+
}
52+
53+
}

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -65,6 +65,7 @@
6565
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
6666
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
6767
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
68+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistShowDuplicateProposal;
6869

6970
public class LocalCorrectionsQuickFixTest extends QuickFixTest {
7071

@@ -7763,7 +7764,7 @@ public void foo() {
77637764

77647765
CompilationUnit astRoot= getASTRoot(cu);
77657766
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7766-
assertNumberOfProposals(proposals, 2);
7767+
assertNumberOfProposals(proposals, 3);
77677768
assertCorrectLabels(proposals);
77687769
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
77697770
}
@@ -7788,7 +7789,7 @@ public void foo(int count) {
77887789

77897790
CompilationUnit astRoot= getASTRoot(cu);
77907791
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7791-
assertNumberOfProposals(proposals, 2);
7792+
assertNumberOfProposals(proposals, 3);
77927793
assertCorrectLabels(proposals);
77937794
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
77947795
}
@@ -7815,7 +7816,7 @@ class Inner {
78157816

78167817
CompilationUnit astRoot= getASTRoot(cu);
78177818
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7818-
assertNumberOfProposals(proposals, 2);
7819+
assertNumberOfProposals(proposals, 3);
78197820
assertCorrectLabels(proposals);
78207821
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
78217822
}
@@ -7843,7 +7844,7 @@ class Inner {
78437844

78447845
CompilationUnit astRoot= getASTRoot(cu);
78457846
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7846-
assertNumberOfProposals(proposals, 2);
7847+
assertNumberOfProposals(proposals, 3);
78477848
assertCorrectLabels(proposals);
78487849
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
78497850
}
@@ -7872,7 +7873,7 @@ public void foo() {
78727873

78737874
CompilationUnit astRoot= getASTRoot(cu);
78747875
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7875-
assertNumberOfProposals(proposals, 2);
7876+
assertNumberOfProposals(proposals, 3);
78767877
assertCorrectLabels(proposals);
78777878
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
78787879
}
@@ -7900,7 +7901,7 @@ public void foo(int count) {
79007901

79017902
CompilationUnit astRoot= getASTRoot(cu);
79027903
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
7903-
assertNumberOfProposals(proposals, 2);
7904+
assertNumberOfProposals(proposals, 3);
79047905
assertCorrectLabels(proposals);
79057906
assertTrue(proposals.get(0) instanceof LinkedNamesAssistProposal);
79067907
}
@@ -13602,4 +13603,23 @@ public E() {
1360213603

1360313604
assertExpectedExistInProposals(proposals, expected);
1360413605
}
13606+
13607+
@Test
13608+
public void testNavigateToInitialVariableAssist() throws Exception {
13609+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
13610+
String str= """
13611+
package test1;
13612+
public class E {
13613+
public void foo(int x) {
13614+
double x;
13615+
}
13616+
}
13617+
""";
13618+
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);
13619+
13620+
CompilationUnit astRoot= getASTRoot(cu);
13621+
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
13622+
assertNumberOfProposals(proposals, 2);
13623+
assertTrue(proposals.get(1) instanceof LinkedNamesAssistShowDuplicateProposal);
13624+
}
1360513625
}
Lines changed: 98 additions & 0 deletions
Loading

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/JavaPluginImages.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2020 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -530,6 +530,7 @@ public class JavaPluginImages {
530530
public static final String IMG_CORRECTION_ADD= NAME_PREFIX + "add_correction.gif"; //$NON-NLS-1$
531531
public static final String IMG_CORRECTION_CAST= NAME_PREFIX + "correction_cast.gif"; //$NON-NLS-1$
532532
public static final String IMG_CORRECTION_MULTI_FIX= NAME_PREFIX + "correction_multi_fix.gif"; //$NON-NLS-1$
533+
public static final String IMG_SHOW_DECLARATION = NAME_PREFIX + "show_declaration.svg"; //$NON-NLS-1$
533534

534535
static {
535536
createManagedFromKey(T_OBJ, IMG_CORRECTION_CHANGE);
@@ -546,6 +547,7 @@ public class JavaPluginImages {
546547
createManagedFromKey(T_OBJ, IMG_OBJS_WARNING_ALT);
547548
createManagedFromKey(T_OBJ, IMG_OBJS_INFO_ALT);
548549
createManagedFromKey(T_OBJ, IMG_BLANK);
550+
createManagedFromKey(T_OBJ, IMG_SHOW_DECLARATION);
549551
}
550552

551553
private static class SmallIntMap<V> {

org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/correction/LocalCorrectionsSubProcessor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposalCore;
126126
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
127127
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposalCore;
128+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistShowDeclarationProposalCore;
129+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistShowDuplicateProposal;
128130
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposal;
129131
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposalCore;
130132
import org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal;
@@ -815,4 +817,8 @@ protected ICommandAccess generateForLoopAssistProposalToT(GenerateForLoopAssistP
815817
protected ICommandAccess linkedNamesAssistProposalToT(LinkedNamesAssistProposalCore core) {
816818
return new LinkedNamesAssistProposal(core.getLabel(), core.getContext(), core.getNode(), core.getValueSuggestion());
817819
}
818-
}
820+
@Override
821+
protected ICommandAccess LinkedNamesAssistShowDuplicateProposalToT(LinkedNamesAssistShowDeclarationProposalCore core) {
822+
return new LinkedNamesAssistShowDuplicateProposal(core.getLabel(), core.getNode());
823+
}
824+
}

0 commit comments

Comments
 (0)