IDEA-151736 Cannot open link in javadoc
authorpeter <peter@jetbrains.com>
Tue, 16 Feb 2016 16:25:13 +0000 (17:25 +0100)
committerpeter <peter@jetbrains.com>
Tue, 16 Feb 2016 16:29:35 +0000 (17:29 +0100)
platform/lang-impl/src/com/intellij/codeInsight/navigation/actions/GotoDeclarationAction.java

index 52fdb9d21ef4198bde08c87b0eeb47332a034347..259307150a89c1c31c397e84e9f5244b2ba6ee3d 100644 (file)
@@ -48,6 +48,7 @@ import com.intellij.openapi.util.TextRange;
 import com.intellij.pom.Navigatable;
 import com.intellij.psi.*;
 import com.intellij.psi.search.PsiElementProcessor;
+import com.intellij.psi.util.PsiTreeUtil;
 import com.intellij.psi.util.PsiUtilCore;
 import com.intellij.ui.awt.RelativePoint;
 import org.jetbrains.annotations.NotNull;
@@ -137,10 +138,16 @@ public class GotoDeclarationAction extends BaseCodeInsightAction implements Code
 
   private static void gotoTargetElement(@NotNull PsiElement element, @NotNull Editor currentEditor, @NotNull PsiFile currentFile) {
     if (element.getContainingFile() == currentFile) {
-      Project project = element.getProject();
-      IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
-      new OpenFileDescriptor(project, currentFile.getViewProvider().getVirtualFile(), element.getTextOffset()).navigateIn(currentEditor);
-      return;
+      int offset = element.getTextOffset();
+      PsiElement leaf = currentFile.findElementAt(offset);
+      // check that element is really physically inside the file
+      // there are fake elements with custom navigation (e.g. opening URL in browser) that override getContainingFile for various reasons
+      if (leaf != null && PsiTreeUtil.isAncestor(element, leaf, false)) {
+        Project project = element.getProject();
+        IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
+        new OpenFileDescriptor(project, currentFile.getViewProvider().getVirtualFile(), offset).navigateIn(currentEditor);
+        return;
+      }
     }
 
     Navigatable navigatable = element instanceof Navigatable ? (Navigatable)element : EditSourceUtil.getDescriptor(element);