Skip to content

Commit 818003e

Browse files
authored
Merge pull request #13 from takemikami/suppor-pytest-param
support pytest.param
2 parents 19d2a41 + 8a9313c commit 818003e

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/main/kotlin/com/github/takemikami/intellij/plugin/pytestparametrize/PytestParametrizeInlayHintsCollector.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.intellij.codeInsight.hints.presentation.InlayPresentation
66
import com.intellij.openapi.editor.Editor
77
import com.intellij.psi.PsiElement
88
import com.jetbrains.python.psi.*
9+
import com.jetbrains.python.psi.impl.PyKeywordArgumentImpl
910

1011
@Suppress("UnstableApiUsage")
1112
class PytestParametrizeInlayHintsCollector(
@@ -48,11 +49,21 @@ class PytestParametrizeInlayHintsCollector(
4849

4950
val hintName: InlayPresentation = factory.seq()
5051
for ((idx, paramset) in valList.elements.withIndex()) {
51-
if (paramset !is PyParenthesizedExpression) { continue }
52+
if (paramset !is PyParenthesizedExpression
53+
&& !(paramset is PyCallExpression && "pytest.param" == paramset.firstChild.text) ) { continue }
5254

5355
// Sequence number of parameter set
5456
if(settings.showParametrizeOrderHints) {
55-
val idxHint = if (idx < ids.size) ids[idx] else idx
57+
val idxHintOfPytestParam =
58+
if (paramset is PyCallExpression && "pytest.param" == paramset.firstChild.text) {
59+
val texts = paramset.lastChild.children
60+
.filter { it is PyKeywordArgumentImpl && "id" == it.firstChild.text }
61+
.map { it.lastChild.text }
62+
if (texts.isNotEmpty())
63+
texts.first().removeSurrounding("\"").removeSurrounding("\'")
64+
else null
65+
} else null
66+
val idxHint = idxHintOfPytestParam ?: if (idx < ids.size) ids[idx] else idx
5667
sink.addInlineElement(
5768
paramset.textOffset,
5869
false,
@@ -62,16 +73,22 @@ class PytestParametrizeInlayHintsCollector(
6273
}
6374

6475
if (!settings.showParametrizeNameHints) { continue }
65-
val paramsetTuple = paramset.children.first()
76+
val paramsetTuple = when (paramset) {
77+
is PyParenthesizedExpression
78+
-> paramset.children.first()
79+
is PyCallExpression
80+
-> paramset.children.last()
81+
else -> null
82+
} ?: return true
6683
for ((pidx, param) in paramsetTuple.children.withIndex()) {
6784
if (pidx >= nameKeys.size || param !is PyExpression) { break }
6885

6986
// parameter name
7087
sink.addInlineElement(
71-
param.textOffset,
72-
false,
73-
factory.roundWithBackground(factory.seq(factory.smallText("${nameKeys[pidx]}:"), hintName)),
74-
false
88+
param.textOffset,
89+
false,
90+
factory.roundWithBackground(factory.seq(factory.smallText("${nameKeys[pidx]}:"), hintName)),
91+
false
7592
)
7693
}
7794
}

0 commit comments

Comments
 (0)