-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from pontem-network/item-presentation
Refactor presentation and lookups
- Loading branch information
Showing
26 changed files
with
213 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.move.ide.presentation | ||
|
||
import com.intellij.ide.projectView.PresentationData | ||
import com.intellij.navigation.ItemPresentation | ||
import com.intellij.openapi.util.Iconable | ||
import com.intellij.psi.PsiElement | ||
import org.move.lang.MoveFile | ||
import org.move.lang.core.psi.MvConst | ||
import org.move.lang.core.psi.MvElement | ||
import org.move.lang.core.psi.MvEnumVariant | ||
import org.move.lang.core.psi.MvFunctionLike | ||
import org.move.lang.core.psi.MvModule | ||
import org.move.lang.core.psi.MvNamedElement | ||
import org.move.lang.core.psi.MvNamedFieldDecl | ||
import org.move.lang.core.psi.signatureText | ||
import org.move.lang.core.types.address | ||
import org.move.lang.moveProject | ||
import org.move.lang.toNioPathOrNull | ||
import org.move.openapiext.rootPath | ||
import java.nio.file.Path | ||
|
||
fun getPresentation(psi: PsiElement): ItemPresentation { | ||
if (psi is MoveFile) { | ||
return psi.presentation!! | ||
} | ||
val location = psi.locationString(tryRelative = true) | ||
val name = presentableName(psi) | ||
return PresentationData(name, location, psi.getIcon(0), null) | ||
} | ||
|
||
fun getPresentationForStructure(psi: PsiElement): ItemPresentation { | ||
if (psi is MoveFile) { | ||
return psi.presentation!! | ||
} | ||
val presentation = buildString { | ||
fun appendCommaList(xs: List<String>) { | ||
append('(') | ||
append(xs.joinToString(", ")) | ||
append(')') | ||
} | ||
append(presentableName(psi)) | ||
when (psi) { | ||
is MvFunctionLike -> { | ||
append(psi.signatureText) | ||
} | ||
is MvConst -> { | ||
psi.type?.let { append(": ${it.text}") } | ||
} | ||
is MvNamedFieldDecl -> { | ||
psi.type?.let { append(": ${it.text}") } | ||
} | ||
is MvEnumVariant -> { | ||
val fields = psi.tupleFields | ||
if (fields != null) { | ||
appendCommaList(fields.tupleFieldDeclList.map { it.type.text }) | ||
} | ||
} | ||
} | ||
} | ||
val icon = psi.getIcon(Iconable.ICON_FLAG_VISIBILITY) | ||
return PresentationData(presentation, null, icon, null) | ||
} | ||
|
||
fun PsiElement.locationString(tryRelative: Boolean): String? = when (this) { | ||
is MvModule -> { | ||
val moveProj = this.moveProject | ||
this.address(moveProj)?.text() ?: "" | ||
} | ||
else -> containingFilePath(tryRelative)?.toString() | ||
} | ||
|
||
private fun PsiElement.containingFilePath(tryRelative: Boolean): Path? { | ||
val containingFilePath = this.containingFile.toNioPathOrNull() ?: return null | ||
if (tryRelative) { | ||
val rootPath = this.project.rootPath | ||
if (rootPath != null) { | ||
return rootPath.relativize(containingFilePath) | ||
} | ||
} | ||
return containingFilePath | ||
} | ||
|
||
private fun presentableName(psi: PsiElement): String? { | ||
return when (psi) { | ||
is MvNamedElement -> psi.name | ||
else -> null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.