Skip to content

Commit 17d07db

Browse files
authored
Fix bugs and speed up some code (#21)
* Move checks which have to validate once * Fix `await` statements hint * Replace `containingFile` with `qualifiedName` for speed up purposes * Simplify code
1 parent 67ae3fb commit 17d07db

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/main/kotlin/space/whitememory/pythoninlayparams/types/AbstractPythonInlayTypeHintsCollector.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ abstract class AbstractPythonInlayTypeHintsCollector(editor: Editor) :
2626

2727
protected fun renderTypeHint(element: PyElement, typeEvalContext: TypeEvalContext, sink: InlayHintsSink) {
2828
val typeAnnotation = HintResolver.getExpressionAnnotationType(element, typeEvalContext)
29-
val hintName =
30-
HintGenerator.generateTypeHintText(element, typeAnnotation, typeEvalContext).takeIf { it.isNotEmpty() }
31-
?: return
29+
val hintName = HintGenerator.generateTypeHintText(
30+
element, typeAnnotation, typeEvalContext
31+
)
32+
33+
if (hintName.isEmpty()) {
34+
return
35+
}
3236

3337
val resolvedHintName = resolveInlayPresentation(hintName)
3438
displayTypeHint(element, sink, resolvedHintName)

src/main/kotlin/space/whitememory/pythoninlayparams/types/hints/HintResolver.kt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package space.whitememory.pythoninlayparams.types.hints
22

33
import com.intellij.psi.util.PsiTreeUtil
44
import com.jetbrains.python.PyNames
5+
import com.jetbrains.python.PyTokenTypes
56
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider
67
import com.jetbrains.python.psi.*
78
import com.jetbrains.python.psi.impl.PyCallExpressionHelper
@@ -10,17 +11,6 @@ import space.whitememory.pythoninlayparams.types.variables.PythonVariablesInlayT
1011

1112
enum class HintResolver {
1213

13-
UNDERSCORE_HINT {
14-
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings) = true
15-
16-
override fun shouldShowTypeHint(
17-
element: PyTargetExpression,
18-
typeAnnotation: PyType?,
19-
typeEvalContext: TypeEvalContext,
20-
settings: PythonVariablesInlayTypeHintsProvider.Settings
21-
): Boolean = element.name != PyNames.UNDERSCORE
22-
},
23-
2414
GLOBALS_HINT {
2515
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings) = true
2616

@@ -37,17 +27,6 @@ enum class HintResolver {
3727
}
3828
},
3929

40-
QUALIFIED_HINT {
41-
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings) = true
42-
43-
override fun shouldShowTypeHint(
44-
element: PyTargetExpression,
45-
typeAnnotation: PyType?,
46-
typeEvalContext: TypeEvalContext,
47-
settings: PythonVariablesInlayTypeHintsProvider.Settings
48-
): Boolean = !element.isQualified
49-
},
50-
5130
GENERAL_HINT {
5231
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings) = true
5332

@@ -60,6 +39,10 @@ enum class HintResolver {
6039
val assignedValue = PyUtil.peelArgument(element.findAssignedValue())
6140

6241
if (assignedValue is PyPrefixExpression) {
42+
if (assignedValue.operator == PyTokenTypes.AWAIT_KEYWORD) {
43+
return shouldShowTypeHint(element, typeEvalContext)
44+
}
45+
6346
return shouldShowTypeHint(assignedValue.operand as PyElement, typeEvalContext)
6447
}
6548

@@ -76,9 +59,13 @@ enum class HintResolver {
7659
typeEvalContext: TypeEvalContext,
7760
settings: PythonVariablesInlayTypeHintsProvider.Settings
7861
): Boolean {
62+
val assignedValue = PyUtil.peelArgument(element.findAssignedValue())
63+
64+
// Handle case `var = async_func()` without `await` keyword
65+
if (assignedValue is PyCallExpression) return true
66+
7967
if (typeAnnotation is PyClassType && isElementInsideTypingModule(typeAnnotation.pyClass)) return false
8068

81-
val assignedValue = PyUtil.peelArgument(element.findAssignedValue())
8269

8370
if (assignedValue is PySubscriptionExpression) {
8471
assignedValue.rootOperand.reference?.resolve()?.let {
@@ -401,9 +388,10 @@ enum class HintResolver {
401388
}
402389

403390
private fun isElementInsideTypingModule(element: PyElement): Boolean {
404-
PyUtil.getContainingPyFile(element)?.let {
405-
return it.name == "${PyTypingTypeProvider.TYPING}${PyNames.DOT_PYI}"
406-
|| it.name == "typing_extensions${PyNames.DOT_PY}"
391+
if (element is PyQualifiedNameOwner) {
392+
element.qualifiedName?.let {
393+
return it.startsWith("${PyTypingTypeProvider.TYPING}.") || it.startsWith("typing_extensions.")
394+
}
407395
}
408396

409397
return false
@@ -416,7 +404,13 @@ enum class HintResolver {
416404
return null
417405
}
418406

419-
fun shouldShowTypeHint(element: PyElement, typeEvalContext: TypeEvalContext): Boolean {
407+
fun shouldShowTypeHint(
408+
element: PyElement,
409+
typeEvalContext: TypeEvalContext
410+
): Boolean {
411+
if (element.name == PyNames.UNDERSCORE) return false
412+
if (element is PyTargetExpression && element.isQualified) return false
413+
420414
val typeAnnotation = getExpressionAnnotationType(element, typeEvalContext)
421415

422416
if (

0 commit comments

Comments
 (0)