From 5d3f337f5408aee75ff8207a3e7f346da987362c Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 11 Feb 2016 09:39:00 +0100 Subject: [PATCH] IDEA-128907 Goto declaration in code fragment opens new editor --- .../actions/GotoDeclarationAction.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/codeInsight/navigation/actions/GotoDeclarationAction.java b/platform/lang-impl/src/com/intellij/codeInsight/navigation/actions/GotoDeclarationAction.java index dd42b6c06a69..055a808ad127 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/navigation/actions/GotoDeclarationAction.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/navigation/actions/GotoDeclarationAction.java @@ -94,7 +94,7 @@ public class GotoDeclarationAction extends BaseCodeInsightAction implements Code return; } } - chooseAmbiguousTarget(editor, offset, elements); + chooseAmbiguousTarget(editor, offset, elements, file); return; } @@ -102,7 +102,7 @@ public class GotoDeclarationAction extends BaseCodeInsightAction implements Code PsiElement navElement = element.getNavigationElement(); navElement = TargetElementUtil.getInstance().getGotoDeclarationTarget(element, navElement); if (navElement != null) { - gotoTargetElement(navElement); + gotoTargetElement(navElement, editor, file); } } catch (IndexNotReadyException e) { @@ -121,13 +121,10 @@ public class GotoDeclarationAction extends BaseCodeInsightAction implements Code return null; } - private static void chooseAmbiguousTarget(final Editor editor, int offset, PsiElement[] elements) { - PsiElementProcessor navigateProcessor = new PsiElementProcessor() { - @Override - public boolean execute(@NotNull final PsiElement element) { - gotoTargetElement(element); - return true; - } + private static void chooseAmbiguousTarget(final Editor editor, int offset, PsiElement[] elements, PsiFile currentFile) { + PsiElementProcessor navigateProcessor = element -> { + gotoTargetElement(element, editor, currentFile); + return true; }; boolean found = chooseAmbiguousTarget(editor, offset, navigateProcessor, CodeInsightBundle.message("declaration.navigation.title"), elements); @@ -136,7 +133,12 @@ public class GotoDeclarationAction extends BaseCodeInsightAction implements Code } } - private static void gotoTargetElement(PsiElement element) { + private static void gotoTargetElement(@NotNull PsiElement element, @NotNull Editor currentEditor, @NotNull PsiFile currentFile) { + if (element.getContainingFile() == currentFile) { + currentEditor.getCaretModel().moveToOffset(element.getTextOffset()); + return; + } + Navigatable navigatable = element instanceof Navigatable ? (Navigatable)element : EditSourceUtil.getDescriptor(element); if (navigatable != null && navigatable.canNavigate()) { navigatable.navigate(true);