-
Notifications
You must be signed in to change notification settings - Fork 122
Add editor hyperlink option for 'Open in new tab' for Types & Methods #2301
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
Open
SougandhS
wants to merge
1
commit into
eclipse-jdt:master
Choose a base branch
from
SougandhS:openMethod
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...t.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaElementOpenInNewHyperlinkDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation. | ||
| * | ||
| * 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: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.jdt.internal.ui.javaeditor; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.eclipse.jface.text.IRegion; | ||
| import org.eclipse.jface.text.hyperlink.IHyperlink; | ||
|
|
||
| import org.eclipse.ui.IEditorPart; | ||
|
|
||
| import org.eclipse.jdt.core.IJavaElement; | ||
| import org.eclipse.jdt.core.ITypeRoot; | ||
|
|
||
| import org.eclipse.jdt.ui.actions.SelectionDispatchAction; | ||
|
|
||
|
|
||
| /** | ||
| * Java element open in new tab hyperlink detector for types and methods. | ||
| */ | ||
| public class JavaElementOpenInNewHyperlinkDetector extends JavaElementHyperlinkDetector { | ||
|
|
||
| @Override | ||
| protected void addHyperlinks(List<IHyperlink> hyperlinksCollector, IRegion wordRegion, SelectionDispatchAction openAction, IJavaElement element, boolean qualify, JavaEditor editor) { | ||
| if (isDeclaredInCurrentEditor(element, editor)) { | ||
| hyperlinksCollector.add(new JavaElementOpenInNewImplementationHyperlink(wordRegion, element, editor, qualify)); | ||
| } | ||
| } | ||
|
|
||
| private boolean isDeclaredInCurrentEditor(IJavaElement typeOrMethod, IEditorPart editor) { | ||
| int type= typeOrMethod.getElementType(); | ||
| if (type != IJavaElement.METHOD && type != IJavaElement.TYPE) { | ||
| return false; | ||
| } | ||
|
|
||
| IJavaElement editorElement= EditorUtility.getEditorInputJavaElement(editor, false); | ||
| if (editorElement instanceof ITypeRoot editorTypeRoot) { | ||
|
|
||
| ITypeRoot elementTypeRoot= (ITypeRoot) typeOrMethod.getAncestor(IJavaElement.COMPILATION_UNIT); | ||
| if (elementTypeRoot == null) { | ||
| elementTypeRoot= (ITypeRoot) typeOrMethod.getAncestor(IJavaElement.CLASS_FILE); | ||
| } | ||
| return elementTypeRoot.equals(editorTypeRoot); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
119 changes: 119 additions & 0 deletions
119
...i/org/eclipse/jdt/internal/ui/javaeditor/JavaElementOpenInNewImplementationHyperlink.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2026 IBM Corporation. | ||
| * | ||
| * 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: | ||
| * IBM Corporation - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.jdt.internal.ui.javaeditor; | ||
|
|
||
| import org.eclipse.core.runtime.Assert; | ||
|
|
||
| import org.eclipse.jface.text.IRegion; | ||
| import org.eclipse.jface.text.hyperlink.IHyperlink; | ||
|
|
||
| import org.eclipse.ui.IEditorPart; | ||
| import org.eclipse.ui.IWorkbenchPage; | ||
| import org.eclipse.ui.IWorkbenchWindow; | ||
| import org.eclipse.ui.PartInitException; | ||
| import org.eclipse.ui.PlatformUI; | ||
|
|
||
| import org.eclipse.jdt.core.IJavaElement; | ||
| import org.eclipse.jdt.core.IMethod; | ||
| import org.eclipse.jdt.core.IType; | ||
|
|
||
| import org.eclipse.jdt.internal.corext.util.Messages; | ||
|
|
||
| import org.eclipse.jdt.ui.JavaElementLabels; | ||
| import org.eclipse.jdt.ui.JavaUI; | ||
|
|
||
| import org.eclipse.jdt.internal.ui.JavaPlugin; | ||
|
|
||
|
|
||
| /** | ||
| * Java element open in new tab implementation hyperlink. | ||
| */ | ||
|
SougandhS marked this conversation as resolved.
|
||
| public class JavaElementOpenInNewImplementationHyperlink implements IHyperlink { | ||
|
|
||
| private final IRegion region; | ||
| private final IJavaElement javaElement; | ||
| private final boolean qualify; | ||
|
|
||
| /** | ||
| * Creates a new Java element open new tab hyperlink for types and methods. | ||
| * | ||
| * @param region the region of the link | ||
| * @param javaElement the element (type or method) to open | ||
| * element | ||
| * @param editor the editor | ||
| */ | ||
| public JavaElementOpenInNewImplementationHyperlink(IRegion region, IJavaElement javaElement, IEditorPart editor, boolean qualify) { | ||
| Assert.isNotNull(region); | ||
| Assert.isNotNull(javaElement); | ||
| Assert.isTrue(javaElement instanceof IMethod || javaElement instanceof IType); | ||
| this.qualify = qualify; | ||
| this.region= region; | ||
| this.javaElement= javaElement; | ||
| } | ||
|
|
||
| @Override | ||
| public IRegion getHyperlinkRegion() { | ||
| return region; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHyperlinkText() { | ||
| if (qualify) { | ||
| String elementLabel= JavaElementLabels.getElementLabel(javaElement, JavaElementLabels.ALL_FULLY_QUALIFIED); | ||
| return Messages.format(JavaEditorMessages.JavaElementOpenHyperlink_hyperlinkText_qualified, new Object[] { elementLabel }); | ||
| } | ||
| return JavaEditorMessages.JavaElementOpenHyperlink_hyperlinkText; | ||
| } | ||
|
|
||
| @Override | ||
| public String getTypeLabel() { | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Opens the given implementation hyperlink for types and methods in a new cloned tab. | ||
| */ | ||
| @Override | ||
| public void open() { | ||
| openInNewTab(javaElement); | ||
| } | ||
|
|
||
| /** | ||
| * Opens the method or type in a new cloned tab | ||
| * | ||
| * @param javaElement the method or type | ||
| */ | ||
| public static void openInNewTab(final IJavaElement javaElement) { | ||
| try { | ||
| IEditorPart existingEditor = EditorUtility.isOpenInEditor(javaElement); | ||
| IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | ||
| if (existingEditor == null || window == null) { | ||
| return; | ||
| } | ||
| IWorkbenchPage page= window.getActivePage(); | ||
| if (page == null) { | ||
| return; | ||
| } | ||
| IEditorPart target = page.openEditor( | ||
| existingEditor.getEditorInput(), | ||
| existingEditor.getSite().getId(), | ||
| true, | ||
| IWorkbenchPage.MATCH_NONE); | ||
|
|
||
| JavaUI.revealInEditor(target, javaElement); | ||
| } catch (PartInitException e) { | ||
| JavaPlugin.log(e); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.