Skip to content

Commit b46395a

Browse files
committed
Don't show hints if there's only one param
1 parent aed6f00 commit b46395a

2 files changed

Lines changed: 21 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## [Unreleased]
44
### Changed
55
- Hide the hints that are 1 character long
6+
- Don't show hints in call expressions that take a single parameter
67

78
### Fixed
89
- Hints showing when placing a positional argument after keyword ones

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

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ class PythonInlayHintsProvider : InlayParameterHintsProvider {
2626
return inlayInfos
2727
}
2828

29-
// Get the arguments of the call expression and quit if there are no arguments
30-
// or the only argument is unpacking (*list, **dict)
31-
val args = element.arguments
32-
if (args.isEmpty() || (args.size == 1 && args[0] is PyStarArgument)) {
33-
return inlayInfos
29+
// Don't show hints if there's no arguments
30+
// Or the only argument is unpacking (*list, **dict)
31+
element.arguments.let {
32+
if (it.isEmpty() || (it.size == 1 && it[0] is PyStarArgument)) return inlayInfos
3433
}
3534

3635
// Try to resolve the object that made this call
@@ -46,48 +45,39 @@ class PythonInlayHintsProvider : InlayParameterHintsProvider {
4645
resolved = PsiTreeUtil.getNextSiblingOfType(resolved, PyLambdaExpression::class.java) ?: return inlayInfos
4746
}
4847

49-
val dataclassAttributes = mutableListOf<String>()
50-
var hasExplicitInit = false
48+
var classAttributes = listOf<PyTargetExpression>()
5149
if (resolved is PyClass) {
5250
// This call is made by a class (initialization), so we want to find the parameters it takes.
5351
// In order to do so, we first have to check for an init method, and if not found,
5452
// We will use the class attributes instead. (Handle dataclasses, attrs, etc.)
55-
resolved.classAttributes.forEach {
56-
// TODO: Somehow make sure we take only correct attributes
57-
val attributeName = it.name ?: return@forEach
58-
dataclassAttributes.add(attributeName)
59-
}
60-
6153
val evalContext = TypeEvalContext.codeAnalysis(element.project, element.containingFile)
6254
val initMethod = resolved.findMethodByName("__init__", false, evalContext)
6355
resolved = if (initMethod != null) {
64-
// Take a note that this class has init, prioritize it over the attributes
65-
hasExplicitInit = true
6656
initMethod
6757
} else {
68-
// Try to find init in its parent classes instead, otherwise stick to the attributes
58+
classAttributes = resolved.classAttributes
6959
resolved.findMethodByName("__init__", true, evalContext) ?: resolved
7060
}
7161
}
7262

7363
val resolvedParameters = getElementFilteredParameters(resolved)
74-
// If there's no parameters in the object, we use the dataclass attributes instead, if there is any
75-
if (resolvedParameters.isEmpty() && dataclassAttributes.isNotEmpty() && !hasExplicitInit) {
76-
dataclassAttributes.zip(args).forEach { (attr, arg) ->
77-
if (arg is PyStarArgument || arg is PyKeywordArgument) {
78-
// It's a keyword argument or unpacking,
79-
// we don't need to show hits after this
80-
return inlayInfos
81-
}
82-
83-
if (isHintNameValid(attr, arg)) {
84-
inlayInfos.add(InlayInfo(attr, arg.textOffset))
85-
}
86-
}
64+
val finalParameters = if (resolvedParameters.isEmpty() && classAttributes.isNotEmpty()) {
65+
// If there's no parameters in the object,
66+
// we use the class attributes instead,
67+
// in case this is a class
68+
classAttributes
69+
} else if (resolvedParameters.isEmpty()) {
70+
return inlayInfos
71+
} else {
72+
resolvedParameters
73+
}
74+
75+
if (finalParameters.size == 1) {
76+
// Don't need a hint if there's only one parameter
8777
return inlayInfos
8878
}
8979

90-
resolvedParameters.zip(args).forEach { (param, arg) ->
80+
finalParameters.zip(element.arguments).forEach { (param, arg) ->
9181
val paramName = param.name ?: return@forEach
9282
if (arg is PyStarArgument || arg is PyKeywordArgument) {
9383
// It's a keyword argument or unpacking,

0 commit comments

Comments
 (0)