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