Skip to content

Commit

Permalink
hide type arguments for adt if item name is too long (more than 10 ch…
Browse files Browse the repository at this point in the history
…aracters)
  • Loading branch information
mkurnikov committed Oct 24, 2024
1 parent e68459d commit a3beaeb
Showing 1 changed file with 25 additions and 48 deletions.
73 changes: 25 additions & 48 deletions src/main/kotlin/org/move/ide/hints/type/MvTypeHintsFactory.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.move.ide.hints.type

import com.intellij.codeInsight.hints.declarative.CollapseState
import com.intellij.codeInsight.hints.declarative.CollapseState.Collapsed
import com.intellij.codeInsight.hints.declarative.CollapseState.Expanded
import com.intellij.codeInsight.hints.declarative.PresentationTreeBuilder
Expand All @@ -10,44 +11,18 @@ import org.move.lang.core.types.ty.TyTuple
import org.move.lang.core.types.ty.TyVector

object MvTypeHintsFactory {
private const val COLLAPSE_FROM_LEVEL: Int = 2
//
// private const val CAPTURE_OF = "capture of "
// private const val UNNAMED_MARK = "<unnamed>"
private const val ANONYMOUS_MARK = "<anonymous>"

fun typeHint(type: Ty, treeBuilder: PresentationTreeBuilder) {
treeBuilder.typeHint(0, type)
}

// private const val UNNAMED_MARK = "<unnamed>"
private const val ANONYMOUS_MARK = "<anonymous>"

private fun PresentationTreeBuilder.typeHint(level: Int, type: Ty) {
when (type) {
is TyTuple -> tupleTypeHint(level, type)
is TyAdt -> adtTypeHint(level, type)
is TyVector -> vectorTypeHint(level, type)
// is PsiClassType -> classTypeHint(level, type)
// is PsiDisjunctionType -> {
// join(
// type.disjunctions,
// op = {
// typeHint(level, it)
// },
// separator = {
// text(" | ")
// }
// )
// }
// is PsiIntersectionType -> {
// join(
// type.conjuncts,
// op = {
// typeHint(level, it)
// },
// separator = {
// text(" & ")
// }
// )
// }
else -> {
text(type.hintText())
}
Expand All @@ -59,7 +34,7 @@ object MvTypeHintsFactory {
text(itemName)
if (tyAdt.typeArguments.isEmpty()) return
collapsibleList(
state = if (level < COLLAPSE_FROM_LEVEL) Expanded else Collapsed,
state = calcCollapseState(level, tyAdt),
expandedState = {
toggleButton { text("<") }
join(
Expand All @@ -78,7 +53,7 @@ object MvTypeHintsFactory {

private fun PresentationTreeBuilder.tupleTypeHint(level: Int, tyTuple: TyTuple) {
collapsibleList(
state = if (level < COLLAPSE_FROM_LEVEL) Expanded else Collapsed,
state = calcCollapseState(level, tyTuple),
expandedState = {
toggleButton { text("(") }
join(
Expand All @@ -98,7 +73,7 @@ object MvTypeHintsFactory {
private fun PresentationTreeBuilder.vectorTypeHint(level: Int, tyVector: TyVector) {
text("vector")
collapsibleList(
state = if (level < COLLAPSE_FROM_LEVEL) Expanded else Collapsed,
state = calcCollapseState(level, tyVector),
expandedState = {
toggleButton { text("[") }
typeHint(level + 1, tyVector.item)
Expand Down Expand Up @@ -136,22 +111,6 @@ object MvTypeHintsFactory {
// text("<...>")
// }
// })
// }

// private fun <T> PresentationTreeBuilder.join(
// elements: Array<T>,
// op: PresentationTreeBuilder.(T) -> Unit,
// separator: PresentationTreeBuilder.() -> Unit
// ) {
// var isFirst = true
// for (element in elements) {
// if (isFirst) {
// isFirst = false
// } else {
// separator()
// }
// op(this, element)
// }
// }

private fun <T> PresentationTreeBuilder.join(
Expand All @@ -169,4 +128,22 @@ object MvTypeHintsFactory {
op(this, element)
}
}

private const val COLLAPSE_FROM_LEVEL: Int = 2
private const val TY_ADT_NAME_LENGTH_COLLAPSE_LIMIT: Int = 10

private fun calcCollapseState(level: Int, ty: Ty): CollapseState {
when (ty) {
is TyAdt -> {
// check whether the name is too long, collapse the type arguments if so
val itemName = ty.item.name
if (itemName != null) {
if (itemName.length > TY_ADT_NAME_LENGTH_COLLAPSE_LIMIT) {
return Collapsed
}
}
}
}
return if (level < COLLAPSE_FROM_LEVEL) Expanded else Collapsed
}
}

0 comments on commit a3beaeb

Please sign in to comment.