|
| 1 | +package com.matej21.intellij.betterPhpDoc |
| 2 | + |
| 3 | +import com.intellij.lang.ASTNode |
| 4 | +import com.intellij.openapi.util.NullableLazyValue |
| 5 | +import com.intellij.psi.PsiElement |
| 6 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment |
| 7 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.impl.tags.PhpDocParamTagImpl |
| 8 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocParamTag |
| 9 | +import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag |
| 10 | +import com.jetbrains.php.lang.psi.elements.Method |
| 11 | +import com.jetbrains.php.lang.psi.elements.Parameter |
| 12 | +import com.jetbrains.php.lang.psi.elements.PhpPsiElement |
| 13 | +import com.jetbrains.php.lang.psi.resolve.types.PhpType |
| 14 | + |
| 15 | + |
| 16 | +class MyPhpDocParamTag(node: ASTNode) : PhpDocParamTagImpl(node) { |
| 17 | + |
| 18 | + override fun getVarName(): String? { |
| 19 | + val varName = super.getVarName() |
| 20 | + if (varName != null && !varName.isEmpty()) { |
| 21 | + return varName |
| 22 | + } |
| 23 | + val parameter = getCorrespondingParameter() |
| 24 | + if (parameter != null) { |
| 25 | + return parameter.name |
| 26 | + } |
| 27 | + return "" |
| 28 | + } |
| 29 | + |
| 30 | + override fun getType(): PhpType { |
| 31 | + val type = super.getType() |
| 32 | + val hasVarNameDefinition = !(super.getVarName() ?: "").isEmpty() |
| 33 | + if (hasVarNameDefinition) { |
| 34 | + return type |
| 35 | + } |
| 36 | + val parameter = getCorrespondingParameter() ?: return type |
| 37 | + type.add(parameter.declaredType) |
| 38 | + |
| 39 | + return type |
| 40 | + } |
| 41 | + |
| 42 | + private fun getCorrespondingParameter(): Parameter? { |
| 43 | + var prevDocTag = prevPsiSibling |
| 44 | + var nthParameter: Int = 0 |
| 45 | + while (prevDocTag != null && prevDocTag is PhpDocTag) { |
| 46 | + if (prevDocTag is PhpDocParamTag) { |
| 47 | + nthParameter++ |
| 48 | + } |
| 49 | + prevDocTag = prevDocTag.prevPsiSibling |
| 50 | + } |
| 51 | + val comment = parent as PhpDocComment |
| 52 | + val nextSibling = comment.nextPsiSibling |
| 53 | + val method = nextSibling as? Method ?: return null |
| 54 | + if (method.parameters.size > nthParameter) { |
| 55 | + return method.parameters[nthParameter] |
| 56 | + } |
| 57 | + return null |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments