Skip to content

Commit 1d7f2e9

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 show
1 parent 028ff06 commit 1d7f2e9

8 files changed

Lines changed: 303 additions & 9 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=Jump to first declaration
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.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/LocalCorrectionsQuickFixTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.eclipse.jdt.internal.ui.text.correction.AssistContext;
6565
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages;
6666
import org.eclipse.jdt.internal.ui.text.correction.JavaCorrectionProcessor;
67+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedDuplicateNavigationProposal;
6768
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
6869

6970
public class LocalCorrectionsQuickFixTest extends QuickFixTest {
@@ -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
}
@@ -13558,4 +13559,22 @@ public E() {
1355813559

1355913560
assertExpectedExistInProposals(proposals, expected);
1356013561
}
13562+
13563+
@Test
13564+
public void testNavigateToInitialVariableAssist() throws Exception {
13565+
IPackageFragment pack1= fSourceFolder.createPackageFragment("test1", false, null);
13566+
String str= """
13567+
package test1;
13568+
public class E {
13569+
public void foo(int x) {
13570+
double x;
13571+
}
13572+
}
13573+
""";
13574+
ICompilationUnit cu= pack1.createCompilationUnit("E.java", str, false, null);
13575+
13576+
CompilationUnit astRoot= getASTRoot(cu);
13577+
ArrayList<IJavaCompletionProposal> proposals= collectCorrections(cu, astRoot);
13578+
assertTrue(proposals.get(1) instanceof LinkedDuplicateNavigationProposal);
13579+
}
1356113580
}
Lines changed: 98 additions & 0 deletions
Loading

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

Lines changed: 4 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
@@ -531,6 +531,8 @@ public class JavaPluginImages {
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$
533533

534+
public static final String IMG_SHOW_DECLARATION = NAME_PREFIX + "show_declaration.svg"; //$NON-NLS-1$
535+
534536
static {
535537
createManagedFromKey(T_OBJ, IMG_CORRECTION_CHANGE);
536538
createManagedFromKey(T_OBJ, IMG_CORRECTION_MOVE);
@@ -546,6 +548,7 @@ public class JavaPluginImages {
546548
createManagedFromKey(T_OBJ, IMG_OBJS_WARNING_ALT);
547549
createManagedFromKey(T_OBJ, IMG_OBJS_INFO_ALT);
548550
createManagedFromKey(T_OBJ, IMG_BLANK);
551+
createManagedFromKey(T_OBJ, IMG_SHOW_DECLARATION);
549552
}
550553

551554
private static class SmallIntMap<V> {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import org.eclipse.jdt.core.dom.ThrowStatement;
9898
import org.eclipse.jdt.core.dom.Type;
9999
import org.eclipse.jdt.core.dom.TypeDeclaration;
100+
import org.eclipse.jdt.core.dom.VariableDeclaration;
100101
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
101102
import org.eclipse.jdt.core.dom.WhileStatement;
102103
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
@@ -163,6 +164,7 @@
163164
import org.eclipse.jdt.internal.ui.text.correction.proposals.FixCorrectionProposalCore;
164165
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal;
165166
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposalCore;
167+
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedDuplicateNavigationProposal;
166168
import org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedNamesAssistProposal;
167169
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposal;
168170
import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingAnnotationAttributesProposalCore;
@@ -452,6 +454,10 @@ public static void addInvalidVariableNameProposals(IInvocationContext context, I
452454

453455
LinkedNamesAssistProposal proposal= new LinkedNamesAssistProposal(name, context, nameNode, valueSuggestion);
454456
proposals.add(proposal);
457+
if(nameNode.getParent() instanceof VariableDeclaration) {
458+
LinkedDuplicateNavigationProposal showExisting= new LinkedDuplicateNavigationProposal(CorrectionMessages.DuplicateLocal_ShowExisting, nameNode);
459+
proposals.add(showExisting);
460+
}
455461
}
456462

457463
public static void getInvalidOperatorProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {

0 commit comments

Comments
 (0)