diff --git a/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingNecromancy.kt b/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingNecromancy.kt index b26e05b0f67d..7ab41f5fb983 100644 --- a/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingNecromancy.kt +++ b/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingNecromancy.kt @@ -1,8 +1,6 @@ // Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInsight.folding.impl -import com.intellij.codeInsight.folding.impl.CodeFoldingRegion.SaveType.PlainText -import com.intellij.codeInsight.folding.impl.CodeFoldingRegion.SaveType.SecretPlaceholder import com.intellij.codeInsight.folding.impl.CodeFoldingZombie.Companion.putRegion import com.intellij.openapi.editor.impl.zombie.Necromancy import com.intellij.util.io.DataInputOutputUtil.* @@ -43,17 +41,13 @@ internal object CodeFoldingNecromancy : Necromancy { } private fun writeRegion(output: DataOutput, region: CodeFoldingRegion) { - output.writeByte(region.saveType.value.toInt()) writeINT(output, region.startOffset) writeINT(output, region.endOffset) + writeUTF(output, region.placeholderText) writeGroupId(output, region.groupId) output.writeBoolean(region.neverExpands) output.writeBoolean(region.isExpanded) output.writeBoolean(region.isAutoCreated) - when (region) { - is CodeFoldingRegion.PlainTextCodeFoldingRegion -> writeUTF(output, region.placeholderText) - is CodeFoldingRegion.SecretPlaceholderCodeFoldingRegion -> writeINT(output, region.placeholderLength) - } } private fun writeGroupId(output: DataOutput, groupId: Long?) { @@ -77,25 +71,14 @@ internal object CodeFoldingNecromancy : Necromancy { } private fun read(input: DataInput): CodeFoldingRegion { - val typeByte = input.readByte() - val type = CodeFoldingRegion.SaveType.entries.find { it.value == typeByte} ?: error("Invalid CodeFoldingRegion type is found") - val start = readINT(input) val end = readINT(input) + val placeholder = readUTF(input) val groupId = readGroupId(input) val neverExpands = input.readBoolean() val isExpanded = input.readBoolean() val isAutoCreated = input.readBoolean() - return when (type) { - PlainText -> { - val placeholder = readUTF(input) - CodeFoldingRegion.PlainTextCodeFoldingRegion(start, end, groupId, neverExpands, isExpanded, isAutoCreated, placeholder) - } - SecretPlaceholder -> { - val placeholder = readINT(input) - CodeFoldingRegion.SecretPlaceholderCodeFoldingRegion(start, end, groupId, neverExpands, isExpanded, isAutoCreated, placeholder) - } - } + return CodeFoldingRegion(start, end, placeholder, groupId, neverExpands, isExpanded, isAutoCreated) } private fun readGroupId(input: DataInput): Long? { diff --git a/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingZombie.kt b/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingZombie.kt index f8c2dfe52804..31991d3ff62a 100644 --- a/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingZombie.kt +++ b/platform/foldings/src/com/intellij/codeInsight/folding/impl/CodeFoldingZombie.kt @@ -12,10 +12,10 @@ import com.intellij.openapi.editor.impl.FoldingKeys.AUTO_CREATED_ZOMBIE import com.intellij.openapi.editor.impl.FoldingKeys.ZOMBIE_REGION_KEY import com.intellij.openapi.editor.impl.FoldingModelImpl import com.intellij.openapi.editor.impl.zombie.Zombie -import com.intellij.openapi.util.NlsSafe import com.intellij.openapi.util.registry.Registry import com.intellij.util.concurrency.annotations.RequiresEdt import java.text.BreakIterator + internal class CodeFoldingZombie( val regions: List, val groupedRegions: Map>, @@ -28,10 +28,10 @@ internal class CodeFoldingZombie( val regions = ArrayList() val groupedRegions = HashMap>() for (foldRegion in foldRegions) { - val regionState = CodeFoldingRegion.create( + val regionState = CodeFoldingRegion( foldRegion.startOffset, foldRegion.endOffset, - foldRegion.placeholderText, + createPlaceholderText(foldRegion.placeholderText), foldRegion.group?.id, foldRegion.shouldNeverExpand(), foldRegion.isExpanded, @@ -42,6 +42,15 @@ internal class CodeFoldingZombie( return CodeFoldingZombie(regions, groupedRegions) } + private fun createPlaceholderText(text: String): String { + return if (Registry.`is`("cache.folding.model.hide.placeholder")) { + CodeFoldingRegion.PLACEHOLDER_SYMBOL.repeat(text.graphemeCount()) + } + else { + text + } + } + fun putRegion( region: CodeFoldingRegion, regions: MutableList, @@ -128,103 +137,25 @@ internal class CodeFoldingZombie( } } -internal sealed class CodeFoldingRegion { - abstract val startOffset: Int - abstract val endOffset: Int - abstract val placeholderText: String - abstract val groupId: Long? - abstract val neverExpands: Boolean - abstract val isExpanded: Boolean - abstract val isAutoCreated: Boolean - abstract val saveType: SaveType - - operator fun component1(): Int = startOffset - operator fun component2(): Int = endOffset - operator fun component3(): String = placeholderText - operator fun component4(): Long? = groupId - operator fun component5(): Boolean = neverExpands - operator fun component6(): Boolean = isExpanded - operator fun component7(): Boolean = isAutoCreated - - - class PlainTextCodeFoldingRegion( - override val startOffset: Int, - override val endOffset: Int, - override val groupId: Long?, - override val neverExpands: Boolean, - override val isExpanded: Boolean, - override val isAutoCreated: Boolean, - override val placeholderText: String, - ) : CodeFoldingRegion() { - override val saveType: SaveType - get() = SaveType.PlainText - } - - class SecretPlaceholderCodeFoldingRegion( - override val startOffset: Int, - override val endOffset: Int, - override val groupId: Long?, - override val neverExpands: Boolean, - override val isExpanded: Boolean, - override val isAutoCreated: Boolean, - val placeholderLength: Int, - ) : CodeFoldingRegion() { +internal data class CodeFoldingRegion( + val startOffset: Int, + val endOffset: Int, + val placeholderText: String, + val groupId: Long?, + val neverExpands: Boolean, + val isExpanded: Boolean, + val isAutoCreated: Boolean, +) { companion object { const val PLACEHOLDER_SYMBOL = " " } - - override val placeholderText: String - get() = PLACEHOLDER_SYMBOL.repeat(placeholderLength) - - override val saveType: SaveType - get() = SaveType.SecretPlaceholder - } - override fun toString(): String { val groupStr = if (groupId == null) "" else " $groupId," return "($startOffset-$endOffset,$groupStr '$placeholderText', ${(if (isExpanded) "-" else "+")}, ${if (isAutoCreated) "AUTO" else "MANUAL"})" } - - companion object { - fun create( - startOffset: Int, - endOffset: Int, - @NlsSafe placeholderText: String, - id: Long?, - shouldNeverExpand: Boolean, - expanded: Boolean, - autoCreated: Boolean, - ) = if (Registry.`is`("cache.folding.model.hide.placeholder")) { - SecretPlaceholderCodeFoldingRegion( - startOffset, - endOffset, - id, - shouldNeverExpand, - expanded, - autoCreated, - placeholderText.graphemeCount() - ) - } - else { - PlainTextCodeFoldingRegion( - startOffset, - endOffset, - id, - shouldNeverExpand, - expanded, - autoCreated, - placeholderText - ) - } - } - - enum class SaveType(val value: Byte) { - PlainText(0), - SecretPlaceholder(1) - } } -internal fun String.graphemeCount(): Int { +private fun String.graphemeCount(): Int { val iterator = BreakIterator.getCharacterInstance() iterator.setText(this)