-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow folding script metadata block into
# /// script
- Loading branch information
1 parent
489d54d
commit 6ac57d7
Showing
4 changed files
with
67 additions
and
1 deletion.
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
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/insyncwithfoo/ryecharm/others/scriptmetadata/ScriptMetadataFoldingBuilder.kt
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,50 @@ | ||
package insyncwithfoo.ryecharm.others.scriptmetadata | ||
|
||
import com.intellij.lang.ASTNode | ||
import com.intellij.lang.folding.CustomFoldingBuilder | ||
import com.intellij.lang.folding.FoldingDescriptor | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.project.DumbAware | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.startOffset | ||
import com.jetbrains.python.psi.PyFile | ||
|
||
|
||
/** | ||
* Fold a script metadata block into `# /// script`. | ||
*/ | ||
internal class ScriptMetadataFoldingBuilder : CustomFoldingBuilder(), DumbAware { | ||
|
||
override fun getLanguagePlaceholderText(node: ASTNode, range: TextRange) = | ||
"# /// script" | ||
|
||
override fun isRegionCollapsedByDefault(node: ASTNode) = false | ||
|
||
override fun buildLanguageFoldRegions( | ||
descriptors: MutableList<FoldingDescriptor>, | ||
root: PsiElement, | ||
document: Document, | ||
quick: Boolean | ||
) { | ||
if (root !is PyFile) { | ||
return | ||
} | ||
|
||
val block = scriptBlock.find(document.charsSequence) ?: return | ||
val blockRange = TextRange(block.range.first, block.range.last + 1) | ||
|
||
for (element in root.children) { | ||
if (element !is PsiComment) { | ||
continue | ||
} | ||
|
||
if (element.startOffset == blockRange.startOffset) { | ||
descriptors += FoldingDescriptor(element, blockRange) | ||
break | ||
} | ||
} | ||
} | ||
|
||
} |
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