author | wenzelm |
Fri, 15 Nov 2024 13:31:36 +0100 | |
changeset 81448 | 9b2e13b3ee43 |
parent 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
51240 | 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 | 12 |
import java.util.{List => JList} |
13 |
||
58700 | 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 | 16 |
import scala.jdk.CollectionConverters._ |
58700 | 17 |
|
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 | 21 |
object Fold_Handling { |
58694 | 22 |
/* input: dynamic line context */ |
23 |
||
75393 | 24 |
class Fold_Handler extends FoldHandler("isabelle") { |
58694 | 25 |
override def equals(other: Any): Boolean = |
26 |
other match { |
|
27 |
case that: Fold_Handler => true |
|
28 |
case _ => false |
|
29 |
} |
|
30 |
||
31 |
override def getFoldLevel(buffer: JEditBuffer, line: Int, seg: Segment): Int = |
|
66176 | 32 |
Token_Markup.Line_Context.after(buffer, line).structure.depth max 0 |
58700 | 33 |
|
34 |
override def getPrecedingFoldLevels( |
|
75393 | 35 |
buffer: JEditBuffer, |
36 |
line: Int, |
|
37 |
seg: Segment, level: Int |
|
38 |
): JList[Integer] = { |
|
66176 | 39 |
val structure = Token_Markup.Line_Context.after(buffer, line).structure |
58700 | 40 |
val result = |
63457 | 41 |
if (line > 0 && structure.command) |
58700 | 42 |
Range.inclusive(line - 1, 0, -1).iterator. |
66176 | 43 |
map(i => Token_Markup.Line_Context.after(buffer, i).structure). |
63457 | 44 |
takeWhile(_.improper).map(_ => structure.depth max 0).toList |
58700 | 45 |
else Nil |
46 |
||
73353 | 47 |
if (result.isEmpty) null else result.map(Integer.valueOf).asJava |
58700 | 48 |
} |
58694 | 49 |
} |
50 |
||
51 |
||
52 |
/* output: static document rendering */ |
|
53 |
||
64621 | 54 |
class Document_Fold_Handler(private val rendering: JEdit_Rendering) |
75393 | 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 | 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 | 66 |
rendering.fold_depth(Text.Range(i, i + 1)) match { |
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 |
} |