src/Tools/jEdit/src/fold_handling.scala
author wenzelm
Tue, 20 Dec 2016 21:35:56 +0100
changeset 64621 7116f2634e32
parent 63457 be6ceddff102
child 66176 b51a40281016
permissions -rw-r--r--
clarified module name;
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
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    12
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
    13
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    14
import scala.collection.convert.WrapAsJava
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    15
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    16
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
    17
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    18
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    19
object Fold_Handling
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    20
{
58694
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    21
  /* input: dynamic line context  */
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    22
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    23
  class Fold_Handler extends FoldHandler("isabelle")
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    24
  {
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 =
63454
08a1f61a49a6 tuned signature;
wenzelm
parents: 58748
diff changeset
    32
      Token_Markup.Line_Context.next(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(
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    35
      buffer: JEditBuffer, line: Int, seg: Segment, level: Int): java.util.List[Integer] =
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    36
    {
63457
wenzelm
parents: 63454
diff changeset
    37
      val structure = Token_Markup.Line_Context.next(buffer, line).structure
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    38
      val result =
63457
wenzelm
parents: 63454
diff changeset
    39
        if (line > 0 && structure.command)
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    40
          Range.inclusive(line - 1, 0, -1).iterator.
63454
08a1f61a49a6 tuned signature;
wenzelm
parents: 58748
diff changeset
    41
            map(i => Token_Markup.Line_Context.next(buffer, i).structure).
63457
wenzelm
parents: 63454
diff changeset
    42
            takeWhile(_.improper).map(_ => structure.depth max 0).toList
58700
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    43
        else Nil
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    44
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    45
      if (result.isEmpty) null
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    46
      else WrapAsJava.seqAsJavaList(result.map(i => new Integer(i)))
4717d18cc619 clarified Line_Structure wrt. command span;
wenzelm
parents: 58697
diff changeset
    47
    }
58694
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    48
  }
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
  /* output: static document rendering */
983e98da2a42 support line context with depth;
wenzelm
parents: 58692
diff changeset
    52
64621
7116f2634e32 clarified module name;
wenzelm
parents: 63457
diff changeset
    53
  class Document_Fold_Handler(private val rendering: JEdit_Rendering)
50542
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    54
    extends FoldHandler("isabelle-document")
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    55
  {
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
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    62
    override def getFoldLevel(buffer: JEditBuffer, line: Int, seg: Segment): Int =
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    63
    {
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    64
      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
    65
        if (i < 0) 0
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    66
        else {
58692
wenzelm
parents: 56589
diff changeset
    67
          rendering.fold_depth(Text.Range(i, i + 1)) match {
wenzelm
parents: 56589
diff changeset
    68
            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
    69
            case _ => 0
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
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    73
      if (line <= 0) 0
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    74
      else {
56589
71c5d1f516c0 more robust JEdit_Lib.line_range, according to usual jEdit confusion at end of last line;
wenzelm
parents: 52900
diff changeset
    75
        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
    76
        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
    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
  }
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    80
}
58bd88159f8f fold handling within Pretty_Text_Area, based on formal document content, which is static here;
wenzelm
parents:
diff changeset
    81