Skip to content

Commit

Permalink
feat(folding): add file command support to folding builder
Browse files Browse the repository at this point in the history
- Introduce `FileShireCommand.file` method to handle file lookup.
- Extend `ShireFoldingBuilder` to include folding for `USED` elements with file commands.
  • Loading branch information
phodal committed Dec 23, 2024
1 parent 99f55cd commit b502605
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.phodal.shirelang.compiler.execute.command
import com.phodal.shirelang.compiler.ast.LineInfo
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiManager
import com.phodal.shirecore.lookupFile

Expand All @@ -19,10 +20,8 @@ class FileShireCommand(private val myProject: Project, private val prop: String)

override suspend fun doExecute(): String? {
val range: LineInfo? = LineInfo.fromString(prop)

// prop name can be src/file.name#L1-L2
val filename = prop.split("#")[0]
val virtualFile = myProject.lookupFile(filename)
val virtualFile = Companion.file(myProject, prop)

val contentsToByteArray = virtualFile?.contentsToByteArray()
if (contentsToByteArray == null) {
Expand Down Expand Up @@ -56,5 +55,13 @@ class FileShireCommand(private val myProject: Project, private val prop: String)

return output.toString()
}

companion object {
fun file(project: Project, path: String): VirtualFile? {
val filename = path.split("#")[0]
val virtualFile = project.lookupFile(filename)
return virtualFile
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiUtilCore
import com.intellij.psi.util.elementType
import com.phodal.shirelang.compiler.execute.command.FileShireCommand
import com.phodal.shirelang.completion.dataprovider.BuiltinCommand
import com.phodal.shirelang.psi.*

class ShireFoldingBuilder : FoldingBuilderEx() {
Expand All @@ -18,15 +20,25 @@ class ShireFoldingBuilder : FoldingBuilderEx() {
override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
val descriptors = mutableListOf<FoldingDescriptor>()
root.accept(ShireFoldingVisitor(descriptors))

return descriptors.toTypedArray()
}

override fun getPlaceholderText(node: ASTNode, range: TextRange): String? {
val elementType = PsiUtilCore.getElementType(node)
val explicitName = foldedElementsPresentations[elementType]
when (elementType) {
ShireTypes.USED -> {
val commandId = (node.psi as ShireUsed).commandId
if (commandId?.text == BuiltinCommand.FILE.commandName) {
val prop = (node.psi as ShireUsed).commandProp?.text ?: return ""
val virtualFile = FileShireCommand.file((node.psi as ShireUsed).project, prop)
return "/file:${virtualFile?.name}"
}
}
}

val elementText = StringUtil.shortenTextWithEllipsis(node.text, 30, 5)
return explicitName?.let { "$it: $elementText" } ?: elementText
return explicitName?.let{ "$it: $elementText" } ?: elementText
}

private val foldedElementsPresentations = hashMapOf(
Expand All @@ -40,6 +52,7 @@ class ShireFoldingBuilder : FoldingBuilderEx() {
return when (foldingDescriptor.element.elementType) {
ShireTypes.FRONT_MATTER_HEADER -> true
ShireTypes.CODE -> false
ShireTypes.USED -> true
else -> false
}
}
Expand All @@ -55,7 +68,15 @@ class ShireFoldingVisitor(private val descriptors: MutableList<FoldingDescriptor
ShireTypes.CODE -> {
descriptors.add(FoldingDescriptor(element.node, element.textRange))
}

ShireTypes.USED -> {
val commandId = (element as? ShireUsed)?.commandId
if (commandId?.text == BuiltinCommand.FILE.commandName) {
descriptors.add(FoldingDescriptor(element.node, element.textRange))
}
}
}

element.acceptChildren(this)
}

Expand Down

0 comments on commit b502605

Please sign in to comment.