src/Tools/jEdit/src/fold_handling.scala
author wenzelm
Fri, 15 Nov 2024 13:31:36 +0100
changeset 81448 9b2e13b3ee43
parent 75393 87ebf5a50283
permissions -rw-r--r--
more rebust mechanics of refresh (see also 82110cbcf9a1 and 2d9b6e32632d): painter.getFontRenderContext might be in undefined state (notably on macOS due to display scaling);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51240
a7a04b449e8b updated headers;
wenzelm
parents: 50542
diff changeset
     1
/*  Title:      Tools/jEdit/src/fold_handling.scala
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     3
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     4
Handling of folds within the text structure.
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     5
*/
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     6
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     8
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
     9
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    10
import isabelle._
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    11
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73353
diff changeset
    12
import java.util.{List => JList}
1d0d9772fff0 tuned imports;
wenzelm
parents: 73353
diff changeset
    13
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    14
import javax.swing.text.Segment
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    15
73353
279e45248e9d tuned --- fewer warnings;
wenzelm
parents: 71783
diff changeset
    16
import scala.jdk.CollectionConverters._
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    17
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    18
import org.gjt.sp.jedit.buffer.{JEditBuffer, FoldHandler}
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    19
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    20
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    21
object Fold_Handling {
58694
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    22
  /* input: dynamic line context  */
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    23
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    24
  class Fold_Handler extends FoldHandler("isabelle") {
58694
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    25
    override def equals(other: Any): Boolean =
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    26
      other match {
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    27
        case that: Fold_Handler => true
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    28
        case _ => false
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    29
      }
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    30
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    31
    override def getFoldLevel(buffer: JEditBuffer, line: Int, seg: Segment): Int =
66176
b51a40281016 tuned signature;
wenzelm
parents: 64621
diff changeset
    32
      Token_Markup.Line_Context.after(buffer, line).structure.depth max 0
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    33
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    34
    override def getPrecedingFoldLevels(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    35
      buffer: JEditBuffer,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    36
      line: Int,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    37
      seg: Segment, level: Int
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    38
    ): JList[Integer] = {
66176
b51a40281016 tuned signature;
wenzelm
parents: 64621
diff changeset
    39
      val structure = Token_Markup.Line_Context.after(buffer, line).structure
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    40
      val result =
63457
wenzelm
parents: 63454
diff changeset
    41
        if (line > 0 && structure.command)
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    42
          Range.inclusive(line - 1, 0, -1).iterator.
66176
b51a40281016 tuned signature;
wenzelm
parents: 64621
diff changeset
    43
            map(i => Token_Markup.Line_Context.after(buffer, i).structure).
63457
wenzelm
parents: 63454
diff changeset
    44
            takeWhile(_.improper).map(_ => structure.depth max 0).toList
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    45
        else Nil
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    46
73353
279e45248e9d tuned --- fewer warnings;
wenzelm
parents: 71783
diff changeset
    47
      if (result.isEmpty) null else result.map(Integer.valueOf).asJava
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    48
    }
58694
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    49
  }
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    50
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    51
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    52
  /* output: static document rendering */
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    53
64621
7116f2634e32 clarified module name;
wenzelm
parents: 63457
diff changeset
    54
  class Document_Fold_Handler(private val rendering: JEdit_Rendering)
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    55
  extends FoldHandler("isabelle-document") {
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    56
    override def equals(other: Any): Boolean =
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    57
      other match {
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    58
        case that: Document_Fold_Handler => this.rendering == that.rendering
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    59
        case _ => false
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    60
      }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73909
diff changeset
    62
    override def getFoldLevel(buffer: JEditBuffer, line: Int, seg: Segment): Int = {
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    63
      def depth(i: Text.Offset): Int =
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    64
        if (i < 0) 0
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    65
        else {
58692
wenzelm
parents: 56589
diff changeset
    66
          rendering.fold_depth(Text.Range(i, i + 1)) match {
wenzelm
parents: 56589
diff changeset
    67
            case Text.Info(_, d) :: _ => d
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    68
            case _ => 0
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    69
          }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    70
        }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    71
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    72
      if (line <= 0) 0
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    73
      else {
56589
71c5d1f516c0 more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents: 52900
diff changeset
    74
        val range = JEdit_Lib.line_range(buffer, line - 1)
71c5d1f516c0 more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents: 52900
diff changeset
    75
        buffer.getFoldLevel(line - 1) - depth(range.start - 1) + depth(range.stop - 1)
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    76
      }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    77
    }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    78
  }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    79
}