Skip to content

Commit

Permalink
fix(folding): improve file reference folding in DevInFileReferenceFol…
Browse files Browse the repository at this point in the history
…dingBuilder #101

The `DevInFileReferenceFoldingBuilder` now correctly identifies file references within property values, ensuring that only the file path is folded, not the entire property value. This change fixes a bug where the entire property was being collapsed, making it difficult to read the code. The folding logic has been refactored to use a more specific check for file references, ensuring that other types of property values are not incorrectly folded.
  • Loading branch information
phodal committed Mar 14, 2024
1 parent 9c1aae4 commit 2a7a330
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DevInCustomVariableFoldingBuilder : FoldingBuilderEx() {
val descriptors = mutableListOf<FoldingDescriptor>()
root.accept(object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element.elementType == DevInTypes.VARIABLE_ID || element.elementType == DevInTypes.PROPERTY_VALUE) {
if (element.elementType == DevInTypes.VARIABLE_ID) {
descriptors.add(FoldingDescriptor(element.node, element.textRange))
}
element.acceptChildren(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@ import com.intellij.psi.util.elementType

class DevInFileReferenceFoldingBuilder : FoldingBuilderEx() {
override fun isCollapsedByDefault(node: ASTNode): Boolean = true
override fun getPlaceholderText(node: ASTNode): String {
val parent = node.treeParent ?: return node.text
val prop = parent.findChildByType(DevInTypes.PROPERTY_VALUE) ?: return node.text

return prop.text.split("/").last()
}
override fun getPlaceholderText(node: ASTNode): String =
if (node.elementType == DevInTypes.PROPERTY_VALUE) {
node.text.split("/").last()
} else {
node.text
}

override fun buildFoldRegions(root: PsiElement, document: Document, quick: Boolean): Array<FoldingDescriptor> {
val descriptors = ArrayList<FoldingDescriptor>()
val descriptors = arrayListOf<FoldingDescriptor>()
root.accept(object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
if (element.elementType == DevInTypes.AGENT_ID && element.text == "file" && isFileReference(element)) {
descriptors.add(FoldingDescriptor(element.node, element.textRange, null, emptySet(), true))
if (element.elementType == DevInTypes.PROPERTY_VALUE) {
val agentId = element.parent?.findElementAt(1)?.text
if (agentId == "file" && element.text.contains("/")) {
descriptors.add(
FoldingDescriptor(element.node, element.textRange, null, emptySet(), true)
)
}
}

element.acceptChildren(this)
}

private fun isFileReference(element: PsiElement): Boolean {
val text = element.text
return text.split("/").isNotEmpty()
}
})

return descriptors.toTypedArray()
Expand Down

0 comments on commit 2a7a330

Please sign in to comment.