[param hints] minor, refactoring

This commit is contained in:
Yaroslav Lepenkin
2017-05-11 18:10:17 +03:00
parent d660f1806f
commit 1f86bd3986
2 changed files with 27 additions and 27 deletions
@@ -24,54 +24,54 @@ import com.intellij.psi.util.TypeConversionUtil
object JavaInlayHintsProvider {
fun createHints(callExpression: PsiCallExpression): Set<InlayInfo> {
fun hints(callExpression: PsiCallExpression): Set<InlayInfo> {
val resolveResult = callExpression.resolveMethodGenerics()
val hints = createHintsForResolvedMethod(callExpression, resolveResult)
val hints = methodHints(callExpression, resolveResult)
if (hints.isNotEmpty()) return hints
return when (callExpression) {
is PsiMethodCallExpressionImpl -> createMergedHints(callExpression, callExpression.methodExpression.multiResolve(false))
is PsiNewExpressionImpl -> createMergedHints(callExpression, callExpression.constructorFakeReference.multiResolve(false))
is PsiMethodCallExpressionImpl -> mergedHints(callExpression, callExpression.methodExpression.multiResolve(false))
is PsiNewExpressionImpl -> mergedHints(callExpression, callExpression.constructorFakeReference.multiResolve(false))
else -> emptySet()
}
}
private fun createMergedHints(callExpression: PsiCallExpression,
results: Array<out ResolveResult>): Set<InlayInfo> {
private fun mergedHints(callExpression: PsiCallExpression,
results: Array<out ResolveResult>): Set<InlayInfo> {
val resultSet = results
.filter { it.element != null }
.map { createHintsForResolvedMethod(callExpression, it) }
.map { methodHints(callExpression, it) }
if (resultSet.isEmpty() || resultSet.any { it.isEmpty() }) return emptySet()
return resultSet.reduce { left, right -> left.intersect(right) }
}
private fun createHintsForResolvedMethod(callExpression: PsiCallExpression, resolveResult: ResolveResult): Set<InlayInfo> {
private fun methodHints(callExpression: PsiCallExpression, resolveResult: ResolveResult): Set<InlayInfo> {
val element = resolveResult.element
val substitutor = (resolveResult as? JavaResolveResult)?.substitutor ?: PsiSubstitutor.EMPTY
if (element is PsiMethod && isMethodToShow(element, callExpression)) {
val info = getCallInfo(callExpression, element)
return createHintSet(info, substitutor)
val info = callInfo(callExpression, element)
return hintSet(info, substitutor)
}
return emptySet()
}
private fun createHintSet(info: CallInfo, substitutor: PsiSubstitutor): Set<InlayInfo> {
private fun hintSet(info: CallInfo, substitutor: PsiSubstitutor): Set<InlayInfo> {
val resultSet = mutableSetOf<InlayInfo>()
val varargInlay = info.createVarargInlay(substitutor)
val varargInlay = info.varargsInlay(substitutor)
if (varargInlay != null) {
resultSet.add(varargInlay)
}
if (isShowForParamsWithSameType()) {
resultSet.addAll(info.createSameTypeInlays())
resultSet.addAll(info.sameTypeInlays())
}
resultSet.addAll(info.createUnclearInlays(substitutor))
resultSet.addAll(info.unclearInlays(substitutor))
return resultSet
}
@@ -115,7 +115,7 @@ object JavaInlayHintsProvider {
return false
}
private fun getCallInfo(callExpression: PsiCallExpression, method: PsiMethod): CallInfo {
private fun callInfo(callExpression: PsiCallExpression, method: PsiMethod): CallInfo {
val params = method.parameterList.parameters
val hasVarArg = params.lastOrNull()?.isVarArgs ?: false
val regularParamsCount = if (hasVarArg) params.size - 1 else params.size
@@ -135,12 +135,12 @@ object JavaInlayHintsProvider {
}
private fun createInlayInfo(info: CallArgumentInfo, showOnlyIfExistedBefore: Boolean = false): InlayInfo? {
return createInlayInfo(info.argument, info.parameter, showOnlyIfExistedBefore)
private fun inlayInfo(info: CallArgumentInfo, showOnlyIfExistedBefore: Boolean = false): InlayInfo? {
return inlayInfo(info.argument, info.parameter, showOnlyIfExistedBefore)
}
private fun createInlayInfo(callArgument: PsiExpression, methodParam: PsiParameter, showOnlyIfExistedBefore: Boolean = false): InlayInfo? {
private fun inlayInfo(callArgument: PsiExpression, methodParam: PsiParameter, showOnlyIfExistedBefore: Boolean = false): InlayInfo? {
val paramName = methodParam.name ?: return null
val paramToShow = (if (methodParam.type is PsiEllipsisType) "..." else "") + paramName
return InlayInfo(paramToShow, callArgument.textRange.startOffset, showOnlyIfExistedBefore)
@@ -168,13 +168,13 @@ private fun isUnclearExpression(callArgument: PsiElement): Boolean {
private class CallInfo(val regularArgs: List<CallArgumentInfo>, val varArg: PsiParameter?, val varArgExpressions: List<PsiExpression>) {
fun createUnclearInlays(substitutor: PsiSubstitutor): List<InlayInfo> {
fun unclearInlays(substitutor: PsiSubstitutor): List<InlayInfo> {
val inlays = mutableListOf<InlayInfo>()
for (callInfo in regularArgs) {
val inlay = when {
isUnclearExpression(callInfo.argument) -> createInlayInfo(callInfo)
!callInfo.isAssignable(substitutor) -> createInlayInfo(callInfo, showOnlyIfExistedBefore = true)
isUnclearExpression(callInfo.argument) -> inlayInfo(callInfo)
!callInfo.isAssignable(substitutor) -> inlayInfo(callInfo, showOnlyIfExistedBefore = true)
else -> null
}
@@ -185,7 +185,7 @@ private class CallInfo(val regularArgs: List<CallArgumentInfo>, val varArg: PsiP
}
fun createSameTypeInlays(): List<InlayInfo> {
fun sameTypeInlays(): List<InlayInfo> {
val all = regularArgs.map { it.parameter.typeText() }
val duplicated = all.toMutableList()
@@ -195,22 +195,22 @@ private class CallInfo(val regularArgs: List<CallArgumentInfo>, val varArg: PsiP
return regularArgs
.filter { duplicated.contains(it.parameter.typeText()) }
.mapNotNull { createInlayInfo(it) }
.mapNotNull { inlayInfo(it) }
}
fun createVarargInlay(substitutor: PsiSubstitutor): InlayInfo? {
fun varargsInlay(substitutor: PsiSubstitutor): InlayInfo? {
if (varArg == null) return null
var hasUnassignable = false
for (expr in varArgExpressions) {
if (isUnclearExpression(expr)) {
return createInlayInfo(varArgExpressions.first(), varArg)
return inlayInfo(varArgExpressions.first(), varArg)
}
hasUnassignable = hasUnassignable || !varArg.isAssignable(expr, substitutor)
}
return if (hasUnassignable) createInlayInfo(varArgExpressions.first(), varArg, showOnlyIfExistedBefore = true) else null
return if (hasUnassignable) inlayInfo(varArgExpressions.first(), varArg, showOnlyIfExistedBefore = true) else null
}
}
@@ -40,7 +40,7 @@ class JavaInlayParameterHintsProvider : InlayParameterHintsProvider {
override fun getParameterHints(element: PsiElement): List<InlayInfo> {
if (element is PsiCallExpression) {
return JavaInlayHintsProvider.createHints(element).toList()
return JavaInlayHintsProvider.hints(element).toList()
}
return emptyList()
}