Skip to content

Commit 4d03c28

Browse files
committed
Filter argument names better
1 parent fdd94a6 commit 4d03c28

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/main/kotlin/space/whitememory/pythoninlayparams/PythonInlayParameterHintsProvider.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PythonInlayParameterHintsProvider : InlayParameterHintsProvider {
1616
val classHints = Option("hints.classes.parameters", { "Class hints" }, true)
1717
val functionHints = Option("hints.functions.parameters", { "Function hints" }, true)
1818
val lambdaHints = Option("hints.lambdas.parameters", { "Lambda hints" }, true)
19+
val hideOverlaps = Option("hints.overlaps.parameters", { "Hide overlaps" }, true)
1920
}
2021

2122
private val forbiddenBuiltinFiles = setOf("builtins.pyi", "typing.pyi")
@@ -26,14 +27,15 @@ class PythonInlayParameterHintsProvider : InlayParameterHintsProvider {
2627

2728
override fun getDescription() = "Help you pass correct arguments by showing parameter names at call sites"
2829

29-
override fun getSupportedOptions() = listOf(classHints, functionHints, lambdaHints)
30+
override fun getSupportedOptions() = listOf(classHints, functionHints, lambdaHints, hideOverlaps)
3031

3132
override fun getProperty(key: String?): String? {
3233
val prefix = "inlay.parameters.hints"
3334
return when (key) {
3435
"$prefix.classes.parameters" -> "Show parameter names for class constructors and dataclasses."
3536
"$prefix.functions.parameters" -> "Show parameter names for function and method calls."
3637
"$prefix.lambdas.parameters" -> "Show parameter names for lambda calls."
38+
"$prefix.overlaps.parameters" -> "Hide hints when a parameter name is completely overlapped by a longer argument name."
3739
else -> null
3840
}
3941
}
@@ -123,7 +125,7 @@ class PythonInlayParameterHintsProvider : InlayParameterHintsProvider {
123125
}
124126
}
125127

126-
if (isHintNameValid(paramName, arg)) {
128+
if (isHintNameValid(paramName.lowercase(), arg)) {
127129
inlayInfos.add(InlayInfo(paramName, arg.textOffset))
128130
}
129131
}
@@ -133,11 +135,22 @@ class PythonInlayParameterHintsProvider : InlayParameterHintsProvider {
133135

134136
/**
135137
* Checks if the given parameter name is valid for the given argument.
136-
* This is used to skip parameters that start with __, or are the same as the argument.
138+
* From the point of the argument similarity compared to the parameter name,
139+
* If the argument is very similar, we don't need to show it.
137140
*/
138-
private fun isHintNameValid(name: String, argument: PyExpression): Boolean {
139-
// TODO: More filters
140-
return name != argument.name?.lowercase() && !name.startsWith("__") && name.length > 1
141+
private fun isHintNameValid(paramName: String, argument: PyExpression): Boolean {
142+
if (paramName.startsWith("__") && paramName.length == 1) return false
143+
144+
val argumentName = if (argument is PySubscriptionExpression) {
145+
// It's a __getitem__ call (subscription), let's take the argument name from it
146+
val key = PsiTreeUtil.getChildOfType(argument, PyStringLiteralExpression::class.java)
147+
key?.stringValue?.lowercase() ?: argument.name?.lowercase() ?: return true
148+
} else {
149+
argument.name?.lowercase() ?: return true
150+
}
151+
152+
if (hideOverlaps.isEnabled() && paramName in argumentName) return false
153+
return paramName != argumentName
141154
}
142155

143156
/**

0 commit comments

Comments
 (0)