author | wenzelm |
Wed, 02 Apr 2025 23:18:12 +0200 | |
changeset 82418 | 6898054035d6 |
parent 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
43414 | 1 |
/* Title: Tools/jEdit/src/token_markup.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Outer syntax token markup. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
73909 | 12 |
import java.util.{List => JList} |
13 |
||
63421 | 14 |
import javax.swing.text.Segment |
15 |
||
65259 | 16 |
import org.gjt.sp.jedit.{Mode, Buffer} |
58701
cc83453fac15
make double-sure that line context is present, e.g. relevant for last line after visible text;
wenzelm
parents:
58699
diff
changeset
|
17 |
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler, DummyTokenHandler, |
65259 | 18 |
ParserRuleSet, ModeProvider, XModeHandler} |
63422 | 19 |
import org.gjt.sp.jedit.indent.IndentRule |
61192 | 20 |
import org.gjt.sp.jedit.buffer.JEditBuffer |
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
21 |
|
43414 | 22 |
|
75393 | 23 |
object Token_Markup { |
58529 | 24 |
/* line context */ |
43414 | 25 |
|
73617
20d0abffee99
more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents:
73353
diff
changeset
|
26 |
def mode_rule_set(mode: String): ParserRuleSet = |
20d0abffee99
more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents:
73353
diff
changeset
|
27 |
new ParserRuleSet(mode, "MAIN") |
20d0abffee99
more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents:
73353
diff
changeset
|
28 |
|
75393 | 29 |
object Line_Context { |
63444 | 30 |
def init(mode: String): Line_Context = |
63603 | 31 |
new Line_Context(mode, Some(Scan.Finished), Line_Structure.init) |
63454 | 32 |
|
73618
4b413b78cd94
more robust indentation: proper line context after insert;
wenzelm
parents:
73617
diff
changeset
|
33 |
def refresh(buffer: JEditBuffer, line: Int): Unit = |
4b413b78cd94
more robust indentation: proper line context after insert;
wenzelm
parents:
73617
diff
changeset
|
34 |
buffer.markTokens(line, DummyTokenHandler.INSTANCE) |
4b413b78cd94
more robust indentation: proper line context after insert;
wenzelm
parents:
73617
diff
changeset
|
35 |
|
66176 | 36 |
def before(buffer: JEditBuffer, line: Int): Line_Context = |
63454 | 37 |
if (line == 0) init(JEdit_Lib.buffer_mode(buffer)) |
66176 | 38 |
else after(buffer, line - 1) |
63454 | 39 |
|
75393 | 40 |
def after(buffer: JEditBuffer, line: Int): Line_Context = { |
63454 | 41 |
val line_mgr = JEdit_Lib.buffer_line_manager(buffer) |
42 |
def context = |
|
43 |
line_mgr.getLineContext(line) match { |
|
44 |
case c: Line_Context => Some(c) |
|
45 |
case _ => None |
|
46 |
} |
|
73618
4b413b78cd94
more robust indentation: proper line context after insert;
wenzelm
parents:
73617
diff
changeset
|
47 |
|
63454 | 48 |
context getOrElse { |
73618
4b413b78cd94
more robust indentation: proper line context after insert;
wenzelm
parents:
73617
diff
changeset
|
49 |
refresh(buffer, line) |
63454 | 50 |
context getOrElse init(JEdit_Lib.buffer_mode(buffer)) |
51 |
} |
|
52 |
} |
|
58748 | 53 |
} |
54 |
||
58699 | 55 |
class Line_Context( |
75393 | 56 |
val mode: String, |
57 |
val context: Option[Scan.Line_Context], |
|
58 |
val structure: Line_Structure) |
|
59 |
extends TokenMarker.LineContext(mode_rule_set(mode), null) { |
|
63454 | 60 |
def get_context: Scan.Line_Context = context.getOrElse(Scan.Finished) |
61 |
||
63444 | 62 |
override def hashCode: Int = (mode, context, structure).hashCode |
43414 | 63 |
override def equals(that: Any): Boolean = |
64 |
that match { |
|
63444 | 65 |
case other: Line_Context => |
66 |
mode == other.mode && context == other.context && structure == other.structure |
|
43414 | 67 |
case _ => false |
68 |
} |
|
69 |
} |
|
70 |
||
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
71 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
72 |
/* tokens from line (inclusive) */ |
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
73 |
|
75393 | 74 |
private def try_line_tokens( |
75 |
syntax: Outer_Syntax, |
|
76 |
buffer: JEditBuffer, |
|
77 |
line: Int |
|
78 |
): Option[List[Token]] = { |
|
66176 | 79 |
val line_context = Line_Context.before(buffer, line) |
58748 | 80 |
for { |
81 |
ctxt <- line_context.context |
|
67014 | 82 |
text <- JEdit_Lib.get_text(buffer, JEdit_Lib.line_range(buffer, line)) |
59083 | 83 |
} yield Token.explode_line(syntax.keywords, text, ctxt)._1 |
58748 | 84 |
} |
58699 | 85 |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
86 |
def line_token_iterator( |
58750 | 87 |
syntax: Outer_Syntax, |
88 |
buffer: JEditBuffer, |
|
89 |
start_line: Int, |
|
90 |
end_line: Int): Iterator[Text.Info[Token]] = |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
91 |
for { |
58750 | 92 |
line <- Range(start_line max 0, end_line min buffer.getLineCount).iterator |
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
93 |
tokens <- try_line_tokens(syntax, buffer, line).iterator |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
94 |
starts = |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
95 |
tokens.iterator.scanLeft(buffer.getLineStartOffset(line))( |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
96 |
(i, tok) => i + tok.source.length) |
58750 | 97 |
(i, tok) <- starts zip tokens.iterator |
98 |
} yield Text.Info(Text.Range(i, i + tok.source.length), tok) |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
99 |
|
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
100 |
def line_token_reverse_iterator( |
58750 | 101 |
syntax: Outer_Syntax, |
102 |
buffer: JEditBuffer, |
|
103 |
start_line: Int, |
|
104 |
end_line: Int): Iterator[Text.Info[Token]] = |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
105 |
for { |
58750 | 106 |
line <- Range(start_line min (buffer.getLineCount - 1), end_line max -1, -1).iterator |
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
107 |
tokens <- try_line_tokens(syntax, buffer, line).iterator |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
108 |
stops = |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
109 |
tokens.reverseIterator.scanLeft(buffer.getLineEndOffset(line) min buffer.getLength)( |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
110 |
(i, tok) => i - tok.source.length) |
58750 | 111 |
(i, tok) <- stops zip tokens.reverseIterator |
112 |
} yield Text.Info(Text.Range(i - tok.source.length, i), tok) |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
113 |
|
58694 | 114 |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
115 |
/* tokens from offset (inclusive) */ |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
116 |
|
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
117 |
def token_iterator(syntax: Outer_Syntax, buffer: JEditBuffer, offset: Text.Offset): |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
118 |
Iterator[Text.Info[Token]] = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
119 |
if (JEdit_Lib.buffer_range(buffer).contains(offset)) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
120 |
line_token_iterator(syntax, buffer, buffer.getLineOfOffset(offset), buffer.getLineCount). |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
121 |
dropWhile(info => !info.range.contains(offset)) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
122 |
else Iterator.empty |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
123 |
|
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
124 |
def token_reverse_iterator(syntax: Outer_Syntax, buffer: JEditBuffer, offset: Text.Offset): |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
125 |
Iterator[Text.Info[Token]] = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
126 |
if (JEdit_Lib.buffer_range(buffer).contains(offset)) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
127 |
line_token_reverse_iterator(syntax, buffer, buffer.getLineOfOffset(offset), -1). |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
128 |
dropWhile(info => !info.range.contains(offset)) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
129 |
else Iterator.empty |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
130 |
|
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
131 |
|
58809 | 132 |
/* command spans */ |
58802 | 133 |
|
75393 | 134 |
def command_span( |
135 |
syntax: Outer_Syntax, |
|
136 |
buffer: JEditBuffer, |
|
137 |
offset: Text.Offset |
|
138 |
): Option[Text.Info[Command_Span.Span]] = { |
|
63441 | 139 |
val keywords = syntax.keywords |
140 |
||
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
141 |
def maybe_command_start(i: Text.Offset): Option[Text.Info[Token]] = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
142 |
token_reverse_iterator(syntax, buffer, i). |
63441 | 143 |
find(info => keywords.is_before_command(info.info) || info.info.is_command) |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
144 |
|
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
145 |
def maybe_command_stop(i: Text.Offset): Option[Text.Info[Token]] = |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
146 |
token_iterator(syntax, buffer, i). |
63441 | 147 |
find(info => keywords.is_before_command(info.info) || info.info.is_command) |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
148 |
|
58802 | 149 |
if (JEdit_Lib.buffer_range(buffer).contains(offset)) { |
75393 | 150 |
val start_info = { |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
151 |
val info1 = maybe_command_start(offset) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
152 |
info1 match { |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
153 |
case Some(Text.Info(range1, tok1)) if tok1.is_command => |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
154 |
val info2 = maybe_command_start(range1.start - 1) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
155 |
info2 match { |
63441 | 156 |
case Some(Text.Info(_, tok2)) if keywords.is_before_command(tok2) => info2 |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
157 |
case _ => info1 |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
158 |
} |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
159 |
case _ => info1 |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
160 |
} |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
161 |
} |
63441 | 162 |
val (start_before_command, start, start_next) = |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
163 |
start_info match { |
63441 | 164 |
case Some(Text.Info(range, tok)) => |
165 |
(keywords.is_before_command(tok), range.start, range.stop) |
|
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
166 |
case None => (false, 0, 0) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
167 |
} |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
168 |
|
75393 | 169 |
val stop_info = { |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
170 |
val info1 = maybe_command_stop(start_next) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
171 |
info1 match { |
63441 | 172 |
case Some(Text.Info(range1, tok1)) if tok1.is_command && start_before_command => |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
173 |
maybe_command_stop(range1.stop) |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
174 |
case _ => info1 |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
175 |
} |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
176 |
} |
58802 | 177 |
val stop = |
59924
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
178 |
stop_info match { |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
179 |
case Some(Text.Info(range, _)) => range.start |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
180 |
case None => buffer.getLength |
801b979ec0c2
more general notion of command span: command keyword not necessarily at start;
wenzelm
parents:
59286
diff
changeset
|
181 |
} |
58802 | 182 |
|
67014 | 183 |
val text = JEdit_Lib.get_text(buffer, Text.Range(start, stop)).getOrElse("") |
58802 | 184 |
val spans = syntax.parse_spans(text) |
185 |
||
186 |
(spans.iterator.scanLeft(start)(_ + _.length) zip spans.iterator). |
|
187 |
map({ case (i, span) => Text.Info(Text.Range(i, i + span.length), span) }). |
|
188 |
find(_.range.contains(offset)) |
|
189 |
} |
|
190 |
else None |
|
191 |
} |
|
192 |
||
58809 | 193 |
private def _command_span_iterator( |
75393 | 194 |
syntax: Outer_Syntax, |
195 |
buffer: JEditBuffer, |
|
196 |
offset: Text.Offset, |
|
197 |
next_offset: Text.Range => Text.Offset |
|
198 |
): Iterator[Text.Info[Command_Span.Span]] = { |
|
199 |
new Iterator[Text.Info[Command_Span.Span]] { |
|
58809 | 200 |
private var next_span = command_span(syntax, buffer, offset) |
201 |
def hasNext: Boolean = next_span.isDefined |
|
75393 | 202 |
def next(): Text.Info[Command_Span.Span] = { |
73344 | 203 |
val span = next_span.getOrElse(Iterator.empty.next()) |
58809 | 204 |
next_span = command_span(syntax, buffer, next_offset(span.range)) |
205 |
span |
|
206 |
} |
|
207 |
} |
|
75393 | 208 |
} |
58809 | 209 |
|
210 |
def command_span_iterator(syntax: Outer_Syntax, buffer: JEditBuffer, offset: Text.Offset) |
|
211 |
: Iterator[Text.Info[Command_Span.Span]] = |
|
212 |
_command_span_iterator(syntax, buffer, offset max 0, range => range.stop) |
|
213 |
||
214 |
def command_span_reverse_iterator(syntax: Outer_Syntax, buffer: JEditBuffer, offset: Text.Offset) |
|
215 |
: Iterator[Text.Info[Command_Span.Span]] = |
|
216 |
_command_span_iterator(syntax, buffer, |
|
217 |
(offset min buffer.getLength) - 1, range => range.start - 1) |
|
218 |
||
58802 | 219 |
|
58694 | 220 |
/* token marker */ |
58683 | 221 |
|
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
222 |
class Marker( |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
223 |
protected val mode: String, |
75393 | 224 |
protected val opt_buffer: Option[Buffer] |
225 |
) extends TokenMarker { |
|
73617
20d0abffee99
more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents:
73353
diff
changeset
|
226 |
addRuleSet(mode_rule_set(mode)) |
20d0abffee99
more robust: avoid sporadic crash of JEditBuffer.tokenMarker.getMainRuleSet().getModeName();
wenzelm
parents:
73353
diff
changeset
|
227 |
|
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
228 |
override def hashCode: Int = (mode, opt_buffer).hashCode |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
229 |
override def equals(that: Any): Boolean = |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
230 |
that match { |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
231 |
case other: Marker => mode == other.mode && opt_buffer == other.opt_buffer |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
232 |
case _ => false |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
233 |
} |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
59076
diff
changeset
|
234 |
|
59076
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
235 |
override def toString: String = |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
236 |
opt_buffer match { |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
237 |
case None => "Marker(" + mode + ")" |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
238 |
case Some(buffer) => "Marker(" + mode + "," + JEdit_Lib.buffer_name(buffer) + ")" |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
239 |
} |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
240 |
|
75393 | 241 |
override def markTokens( |
242 |
context: TokenMarker.LineContext, |
|
243 |
handler: TokenHandler, |
|
244 |
raw_line: Segment |
|
245 |
): TokenMarker.LineContext = { |
|
46210
553ec602d337
paranoia null check -- prevent spurious crash of jedit token markup;
wenzelm
parents:
46178
diff
changeset
|
246 |
val line = if (raw_line == null) new Segment else raw_line |
63444 | 247 |
val line_context = |
248 |
context match { case c: Line_Context => c case _ => Line_Context.init(mode) } |
|
58748 | 249 |
val structure = line_context.structure |
46210
553ec602d337
paranoia null check -- prevent spurious crash of jedit token markup;
wenzelm
parents:
46178
diff
changeset
|
250 |
|
75393 | 251 |
val context1 = { |
59076
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
252 |
val opt_syntax = |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
253 |
opt_buffer match { |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
254 |
case Some(buffer) => Isabelle.buffer_syntax(buffer) |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
255 |
case None => Isabelle.mode_syntax(mode) |
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
256 |
} |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
257 |
val (styled_tokens, context1) = |
59076
65babcd8b0e6
clarified token marker / syntax for mode vs. buffer;
wenzelm
parents:
59063
diff
changeset
|
258 |
(line_context.context, opt_syntax) match { |
58694 | 259 |
case (Some(ctxt), _) if mode == "isabelle-ml" || mode == "sml" => |
260 |
val (tokens, ctxt1) = ML_Lex.tokenize_line(mode == "sml", line, ctxt) |
|
64621 | 261 |
val styled_tokens = |
262 |
tokens.map(tok => (JEdit_Rendering.ml_token_markup(tok), tok.source)) |
|
63444 | 263 |
(styled_tokens, new Line_Context(line_context.mode, Some(ctxt1), structure)) |
58694 | 264 |
|
265 |
case (Some(ctxt), Some(syntax)) if syntax.has_tokens => |
|
59083 | 266 |
val (tokens, ctxt1) = Token.explode_line(syntax.keywords, line, ctxt) |
63603 | 267 |
val structure1 = structure.update(syntax.keywords, tokens) |
58694 | 268 |
val styled_tokens = |
64621 | 269 |
tokens.map(tok => (JEdit_Rendering.token_markup(syntax, tok), tok.source)) |
63444 | 270 |
(styled_tokens, new Line_Context(line_context.mode, Some(ctxt1), structure1)) |
58694 | 271 |
|
272 |
case _ => |
|
273 |
val styled_token = (JEditToken.NULL, line.subSequence(0, line.count).toString) |
|
63444 | 274 |
(List(styled_token), new Line_Context(line_context.mode, None, structure)) |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
275 |
} |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
276 |
|
65258 | 277 |
val extended = Syntax_Style.extended(line) |
71933
aec0f7b58cc6
proper rendering of complex codepoints, e.g. \<^url> code: 0x01F310;
wenzelm
parents:
71783
diff
changeset
|
278 |
def special(i: Int): Boolean = extended.isDefinedAt(i) || line.charAt(i) == '\t' |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
279 |
|
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
280 |
var offset = 0 |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
281 |
for ((style, token) <- styled_tokens) { |
55500 | 282 |
val length = token.length |
71933
aec0f7b58cc6
proper rendering of complex codepoints, e.g. \<^url> code: 0x01F310;
wenzelm
parents:
71783
diff
changeset
|
283 |
if ((offset until (offset + length)).exists(special)) { |
aec0f7b58cc6
proper rendering of complex codepoints, e.g. \<^url> code: 0x01F310;
wenzelm
parents:
71783
diff
changeset
|
284 |
for ((c, i) <- Codepoint.iterator_offset(token)) { |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
285 |
val style1 = |
71933
aec0f7b58cc6
proper rendering of complex codepoints, e.g. \<^url> code: 0x01F310;
wenzelm
parents:
71783
diff
changeset
|
286 |
extended.get(offset + i) match { |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
287 |
case None => style |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
288 |
case Some(ext) => ext(style) |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
289 |
} |
71933
aec0f7b58cc6
proper rendering of complex codepoints, e.g. \<^url> code: 0x01F310;
wenzelm
parents:
71783
diff
changeset
|
290 |
handler.handleToken(line, style1, offset + i, Character.charCount(c), context1) |
43553 | 291 |
} |
292 |
} |
|
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
293 |
else handler.handleToken(line, style, offset, length, context1) |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
294 |
offset += length |
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
295 |
} |
44702
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
296 |
handler.handleToken(line, JEditToken.END, line.count, 0, context1) |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
297 |
context1 |
eb00752507c7
improved handling of extended styles and hard tabs when prover is inactive;
wenzelm
parents:
44701
diff
changeset
|
298 |
} |
58694 | 299 |
|
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
300 |
val context2 = context1.intern |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
301 |
handler.setLineContext(context2) |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
302 |
context2 |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
303 |
} |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
304 |
} |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
305 |
|
43416
e730cdd97dcf
more precise imitatation of original TokenMarker: no locking, interned context etc.;
wenzelm
parents:
43414
diff
changeset
|
306 |
|
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
307 |
/* mode provider */ |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
308 |
|
75393 | 309 |
class Mode_Provider(orig_provider: ModeProvider) extends ModeProvider { |
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
310 |
for (mode <- orig_provider.getModes) addMode(mode) |
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
311 |
|
75393 | 312 |
override def loadMode(mode: Mode, xmh: XModeHandler): Unit = { |
43452
5cf548485529
avoid setTokenMarker fluctuation on buffer reload etc. via static isabelle_token_marker, which is installed by hijacking the jEdit ModeProvider;
wenzelm
parents:
43443
diff
changeset
|
313 |
super.loadMode(mode, xmh) |
73882 | 314 |
Isabelle.mode_token_marker(mode.getName).foreach(mode.setTokenMarker) |
63422 | 315 |
Isabelle.indent_rule(mode.getName).foreach(indent_rule => |
73909 | 316 |
Untyped.set[JList[IndentRule]](mode, "indentRules", JList.of(indent_rule))) |
53277
6aa348237973
more uniform configuration of editor modes and token markers;
wenzelm
parents:
53274
diff
changeset
|
317 |
} |
44358
2a2df4de1bbe
more robust initialization of token marker and line context wrt. session startup;
wenzelm
parents:
44356
diff
changeset
|
318 |
} |
43414 | 319 |
} |