PY-56268/PY-84930 go to type declaration: support callables

GitOrigin-RevId: e05dd19b681fb80f60346e1c75c34252d19d83e9
This commit is contained in:
Morgan Bartholomew
2026-02-16 11:17:01 +00:00
committed by intellij-monorepo-bot
parent 07082866c3
commit 922a65e590
2 changed files with 20 additions and 5 deletions
@@ -64,7 +64,7 @@ public final class GotoTypeDeclarationAction extends BaseCodeInsightAction imple
}
@VisibleForTesting
public static PsiElement @Nullable [] findSymbolTypes(@NotNull Editor editor, int offset) {
public static @Nullable PsiElement @Nullable [] findSymbolTypes(@NotNull Editor editor, int offset) {
return findSymbolTypes(editor, offset, TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED |
TargetElementUtil.ELEMENT_NAME_ACCEPTED |
TargetElementUtil.LOOKUP_ITEM_ACCEPTED);
@@ -75,7 +75,7 @@ public final class GotoTypeDeclarationAction extends BaseCodeInsightAction imple
/// This function is the highest-level way to trigger the "Go to Type Declaration" action.
/// It's composed of a couple of lower-level functions.
@ApiStatus.Internal
public static PsiElement @Nullable [] findSymbolTypes(@NotNull Editor editor,
public static @Nullable PsiElement @Nullable [] findSymbolTypes(@NotNull Editor editor,
int offset,
@MagicConstant(flagsFromClass = TargetElementUtil.class) int flags) {
PsiElement targetElement = TargetElementUtil.getInstance().findTargetElement(editor, flags, offset);
@@ -2,6 +2,7 @@
package com.jetbrains.python.codeInsight
import com.intellij.codeInsight.navigation.actions.GotoTypeDeclarationAction
import com.intellij.idea.TestFor
import com.intellij.psi.PsiElement
import com.jetbrains.python.PythonFileType
import com.jetbrains.python.fixtures.PyTestCase
@@ -125,10 +126,24 @@ class PyGotoTypeDeclarationTest : PyTestCase() {
assertEquals((myFixture.file as PyFile).findTopLevelAttribute("Movie"), type)
}
private fun findSymbolType(text: String): PsiElement = findSymbolTypes(text).single()
@TestFor(issues = ["PY-84930"])
fun `test goto type declaration for callable`() {
val type = findSymbolType(
"""
from typing import Callable
class A: ...
a<caret>: Callable[[], A]
"""
)
private fun findSymbolTypes(text: String): List<PsiElement> {
assertEquals((myFixture.file as PyFile).findTopLevelClass("A"), type)
}
private fun findSymbolType(text: String): PsiElement = findSymbolTypes(text).single()!!
private fun findSymbolTypes(text: String): List<PsiElement?> {
myFixture.configureByText(PythonFileType.INSTANCE, text)
return GotoTypeDeclarationAction.findSymbolTypes(myFixture.editor, myFixture.caretOffset)?.asList() ?: emptyList()
return GotoTypeDeclarationAction.findSymbolTypes(myFixture.editor, myFixture.caretOffset)?.asList().orEmpty()
}
}