author | wenzelm |
Wed, 21 Jun 2017 14:06:16 +0200 | |
changeset 66150 | c2e19b9e1398 |
parent 66143 | 51f74025a3e3 |
child 66151 | 26eecd42cbc5 |
permissions | -rw-r--r-- |
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Tools/VSCode/src/vscode_rendering.scala |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
3 |
|
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
4 |
Isabelle/VSCode-specific implementation of quasi-abstract rendering and |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
5 |
markup interpretation. |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
6 |
*/ |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
7 |
|
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
8 |
package isabelle.vscode |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
9 |
|
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
10 |
|
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
11 |
import isabelle._ |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
12 |
|
64778
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
13 |
import java.io.{File => JFile} |
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
14 |
|
65913 | 15 |
import scala.annotation.tailrec |
16 |
||
64778
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
17 |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
18 |
object VSCode_Rendering |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
19 |
{ |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
20 |
/* decorations */ |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
21 |
|
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
22 |
private def color_decorations(prefix: String, types: Set[Rendering.Color.Value], |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
23 |
colors: List[Text.Info[Rendering.Color.Value]]): List[Document_Model.Decoration] = |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
24 |
{ |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
25 |
val color_ranges = |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
26 |
(Map.empty[Rendering.Color.Value, List[Text.Range]] /: colors) { |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
27 |
case (m, Text.Info(range, c)) => m + (c -> (range :: m.getOrElse(c, Nil))) |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
28 |
} |
65105 | 29 |
types.toList.map(c => |
65140 | 30 |
Document_Model.Decoration.ranges(prefix + c.toString, color_ranges.getOrElse(c, Nil).reverse)) |
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
31 |
} |
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
32 |
|
65176 | 33 |
private val background_colors = |
34 |
Rendering.Color.background_colors - Rendering.Color.active - Rendering.Color.active_result - |
|
35 |
Rendering.Color.entity |
|
36 |
||
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
37 |
private val dotted_colors = |
65134
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
38 |
Set(Rendering.Color.writeln, Rendering.Color.information, Rendering.Color.warning) |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
39 |
|
65104
66b19d05dcee
decorations for background and foreground colors;
wenzelm
parents:
65095
diff
changeset
|
40 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
41 |
/* diagnostic messages */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
42 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
43 |
private val message_severity = |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
44 |
Map( |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
45 |
Markup.LEGACY -> Protocol.DiagnosticSeverity.Warning, |
65095 | 46 |
Markup.ERROR -> Protocol.DiagnosticSeverity.Error) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
47 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
48 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
49 |
/* markup elements */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
50 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
51 |
private val diagnostics_elements = |
65134
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
52 |
Markup.Elements(Markup.LEGACY, Markup.ERROR) |
65095 | 53 |
|
65176 | 54 |
private val background_elements = |
55 |
Rendering.background_elements - Markup.ENTITY -- Rendering.active_elements |
|
56 |
||
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
57 |
private val dotted_elements = |
65134
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
58 |
Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING) |
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
59 |
|
65134
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
60 |
val tooltip_elements = |
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
61 |
Markup.Elements(Markup.WRITELN, Markup.INFORMATION, Markup.WARNING, Markup.BAD) ++ |
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65122
diff
changeset
|
62 |
Rendering.tooltip_elements |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
63 |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
64 |
private val hyperlink_elements = |
64833 | 65 |
Markup.Elements(Markup.ENTITY, Markup.PATH, Markup.POSITION, Markup.CITATION) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
66 |
} |
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
67 |
|
66114 | 68 |
class VSCode_Rendering(snapshot: Document.Snapshot, _model: Document_Model) |
69 |
extends Rendering(snapshot, _model.resources.options, _model.session) |
|
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
70 |
{ |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
71 |
rendering => |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
72 |
|
66114 | 73 |
def model: Document_Model = _model |
66143 | 74 |
def resources: VSCode_Resources = model.resources |
66114 | 75 |
|
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
76 |
|
64877 | 77 |
/* completion */ |
78 |
||
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
79 |
def completion(caret_pos: Line.Position, caret: Text.Offset): List[Protocol.CompletionItem] = |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
80 |
{ |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
81 |
val doc = model.content.doc |
66057 | 82 |
val line = caret_pos.line |
83 |
doc.offset(Line.Position(line)) match { |
|
84 |
case None => Nil |
|
85 |
case Some(line_start) => |
|
86 |
val history = Completion.History.empty |
|
87 |
val caret_range = before_caret_range(caret) |
|
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
88 |
|
66057 | 89 |
val syntax = model.syntax() |
90 |
val syntax_completion = |
|
66055 | 91 |
syntax.completion.complete(history, unicode = false, explicit = true, |
66057 | 92 |
line_start, doc.lines(line).text, caret - line_start, |
93 |
language_context(caret_range) getOrElse syntax.language_context) |
|
64877 | 94 |
|
66057 | 95 |
val (no_completion, semantic_completion) = |
96 |
rendering.semantic_completion_result( |
|
66114 | 97 |
history, false, syntax_completion.map(_.range), caret_range) |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
98 |
|
66057 | 99 |
if (no_completion) Nil |
100 |
else { |
|
66121 | 101 |
val results = |
102 |
Completion.Result.merge(history, |
|
103 |
Completion.Result.merge(history, semantic_completion, syntax_completion), |
|
66141 | 104 |
VSCode_Spell_Checker.completion(rendering, caret)) |
66142 | 105 |
val items = |
106 |
results match { |
|
107 |
case None => Nil |
|
108 |
case Some(result) => |
|
109 |
result.items.map(item => |
|
110 |
Protocol.CompletionItem( |
|
111 |
label = item.replacement, |
|
112 |
detail = Some(item.description.mkString(" ")), |
|
113 |
range = Some(doc.range(item.range)))) |
|
114 |
} |
|
115 |
items ::: VSCode_Spell_Checker.menu_items(rendering, caret) |
|
66057 | 116 |
} |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
117 |
} |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
118 |
} |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65925
diff
changeset
|
119 |
|
64877 | 120 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
121 |
/* diagnostics */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
122 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
123 |
def diagnostics: List[Text.Info[Command.Results]] = |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
124 |
snapshot.cumulate[Command.Results]( |
64830 | 125 |
model.content.text_range, Command.Results.empty, VSCode_Rendering.diagnostics_elements, _ => |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
126 |
{ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
127 |
case (res, Text.Info(_, msg @ XML.Elem(Markup(_, Markup.Serial(i)), body))) |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
128 |
if body.nonEmpty => Some(res + (i -> msg)) |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
129 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
130 |
case _ => None |
64802 | 131 |
}).filterNot(info => info.info.is_empty) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
132 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
133 |
def diagnostics_output(results: List[Text.Info[Command.Results]]): List[Protocol.Diagnostic] = |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
134 |
{ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
135 |
(for { |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
136 |
Text.Info(text_range, res) <- results.iterator |
64830 | 137 |
range = model.content.doc.range(text_range) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
138 |
(_, XML.Elem(Markup(name, _), body)) <- res.iterator |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
139 |
} yield { |
66143 | 140 |
val message = resources.output_pretty_message(body) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
141 |
val severity = VSCode_Rendering.message_severity.get(name) |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
142 |
Protocol.Diagnostic(range, message, severity = severity) |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
143 |
}).toList |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
144 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
145 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64668
diff
changeset
|
146 |
|
65146 | 147 |
/* text color */ |
148 |
||
149 |
def text_color(range: Text.Range): List[Text.Info[Rendering.Color.Value]] = |
|
150 |
{ |
|
151 |
snapshot.select(range, Rendering.text_color_elements, _ => |
|
152 |
{ |
|
65174
c0388fbd8096
avoid extra decorations for regular command keywords;
wenzelm
parents:
65173
diff
changeset
|
153 |
case Text.Info(_, XML.Elem(Markup(name, props), _)) => |
c0388fbd8096
avoid extra decorations for regular command keywords;
wenzelm
parents:
65173
diff
changeset
|
154 |
if (name != Markup.IMPROPER && props.contains((Markup.KIND, Markup.COMMAND))) None |
c0388fbd8096
avoid extra decorations for regular command keywords;
wenzelm
parents:
65173
diff
changeset
|
155 |
else Rendering.text_color.get(name) |
65146 | 156 |
}) |
157 |
} |
|
158 |
||
159 |
||
65913 | 160 |
/* text overview color */ |
161 |
||
162 |
private sealed case class Color_Info( |
|
163 |
color: Rendering.Color.Value, offset: Text.Offset, end_offset: Text.Offset, end_line: Int) |
|
164 |
||
165 |
def text_overview_color: List[Text.Info[Rendering.Color.Value]] = |
|
166 |
{ |
|
167 |
@tailrec def loop(offset: Text.Offset, line: Int, lines: List[Line], colors: List[Color_Info]) |
|
168 |
: List[Text.Info[Rendering.Color.Value]] = |
|
169 |
{ |
|
170 |
if (lines.nonEmpty) { |
|
171 |
val end_offset = offset + lines.head.text.length |
|
172 |
val colors1 = |
|
173 |
(overview_color(Text.Range(offset, end_offset)), colors) match { |
|
174 |
case (Some(color), old :: rest) if color == old.color && line == old.end_line => |
|
175 |
old.copy(end_offset = end_offset, end_line = line + 1) :: rest |
|
176 |
case (Some(color), _) => |
|
177 |
Color_Info(color, offset, end_offset, line + 1) :: colors |
|
178 |
case (None, _) => colors |
|
179 |
} |
|
180 |
loop(end_offset + 1, line + 1, lines.tail, colors1) |
|
181 |
} |
|
182 |
else { |
|
183 |
colors.reverse.map(info => |
|
184 |
Text.Info(Text.Range(info.offset, info.end_offset), info.color)) |
|
185 |
} |
|
186 |
} |
|
187 |
loop(0, 0, model.content.doc.lines, Nil) |
|
188 |
} |
|
189 |
||
190 |
||
65122
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
191 |
/* dotted underline */ |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
192 |
|
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
193 |
def dotted(range: Text.Range): List[Text.Info[Rendering.Color.Value]] = |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
194 |
message_underline_color(VSCode_Rendering.dotted_elements, range) |
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
195 |
|
1edb570f5b17
decorations for dotted underline: less intrusive;
wenzelm
parents:
65120
diff
changeset
|
196 |
|
65095 | 197 |
/* decorations */ |
198 |
||
65120
db6cc23fcf33
proper reset of published decorations: initial value is Nil, afterwards it is a list of canonical length and order;
wenzelm
parents:
65107
diff
changeset
|
199 |
def decorations: List[Document_Model.Decoration] = // list of canonical length and order |
65925 | 200 |
Par_List.map((f: () => List[Document_Model.Decoration]) => f(), |
201 |
List( |
|
202 |
() => |
|
203 |
VSCode_Rendering.color_decorations("background_", VSCode_Rendering.background_colors, |
|
204 |
background(VSCode_Rendering.background_elements, model.content.text_range, Set.empty)), |
|
205 |
() => |
|
206 |
VSCode_Rendering.color_decorations("foreground_", Rendering.Color.foreground_colors, |
|
207 |
foreground(model.content.text_range)), |
|
208 |
() => |
|
209 |
VSCode_Rendering.color_decorations("text_", Rendering.Color.text_colors, |
|
210 |
text_color(model.content.text_range)), |
|
211 |
() => |
|
212 |
VSCode_Rendering.color_decorations("text_overview_", Rendering.Color.text_overview_colors, |
|
213 |
text_overview_color), |
|
214 |
() => |
|
215 |
VSCode_Rendering.color_decorations("dotted_", VSCode_Rendering.dotted_colors, |
|
216 |
dotted(model.content.text_range)))).flatten ::: |
|
66141 | 217 |
List(VSCode_Spell_Checker.decoration(rendering)) |
65095 | 218 |
|
219 |
def decoration_output(decoration: Document_Model.Decoration): Protocol.Decoration = |
|
220 |
{ |
|
221 |
val content = |
|
65105 | 222 |
for (Text.Info(text_range, msgs) <- decoration.content) |
65095 | 223 |
yield { |
224 |
val range = model.content.doc.range(text_range) |
|
65173 | 225 |
Protocol.DecorationOpts(range, |
66143 | 226 |
msgs.map(msg => Protocol.MarkedString(resources.output_pretty_tooltip(msg)))) |
65095 | 227 |
} |
228 |
Protocol.Decoration(decoration.typ, content) |
|
229 |
} |
|
230 |
||
231 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
232 |
/* tooltips */ |
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
233 |
|
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
234 |
def timing_threshold: Double = options.real("vscode_timing_threshold") |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
235 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
236 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
237 |
/* hyperlinks */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
238 |
|
64667 | 239 |
def hyperlink_source_file(source_name: String, line1: Int, range: Symbol.Range) |
240 |
: Option[Line.Node_Range] = |
|
241 |
{ |
|
64775 | 242 |
for { |
66143 | 243 |
platform_path <- resources.source_file(source_name) |
64778
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
244 |
file <- |
64779 | 245 |
(try { Some(new JFile(platform_path).getCanonicalFile) } |
64778
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
246 |
catch { case ERROR(_) => None }) |
64775 | 247 |
} |
64667 | 248 |
yield { |
64778
7884a19de325
proper interpretation of Resources.source_file as platform file;
wenzelm
parents:
64777
diff
changeset
|
249 |
Line.Node_Range(file.getPath, |
64841
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
250 |
if (range.start > 0) { |
66143 | 251 |
resources.get_file_content(file) match { |
64841
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
252 |
case Some(text) => |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
253 |
val chunk = Symbol.Text_Chunk(text) |
65196
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
wenzelm
parents:
65176
diff
changeset
|
254 |
val doc = Line.Document(text) |
64841
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
255 |
doc.range(chunk.decode(range)) |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
256 |
case _ => |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
257 |
Line.Range(Line.Position((line1 - 1) max 0)) |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
258 |
} |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
259 |
} |
d53696aed874
added node_name(String): imitate jEdit buffer operations;
wenzelm
parents:
64833
diff
changeset
|
260 |
else Line.Range(Line.Position((line1 - 1) max 0))) |
64667 | 261 |
} |
262 |
} |
|
263 |
||
264 |
def hyperlink_command(id: Document_ID.Generic, range: Symbol.Range): Option[Line.Node_Range] = |
|
265 |
{ |
|
266 |
if (snapshot.is_outdated) None |
|
267 |
else |
|
268 |
for { |
|
269 |
start <- snapshot.find_command_position(id, range.start) |
|
270 |
stop <- snapshot.find_command_position(id, range.stop) |
|
271 |
} yield Line.Node_Range(start.name, Line.Range(start.pos, stop.pos)) |
|
272 |
} |
|
273 |
||
274 |
def hyperlink_position(pos: Position.T): Option[Line.Node_Range] = |
|
275 |
pos match { |
|
276 |
case Position.Item_File(name, line, range) => hyperlink_source_file(name, line, range) |
|
277 |
case Position.Item_Id(id, range) => hyperlink_command(id, range) |
|
278 |
case _ => None |
|
279 |
} |
|
280 |
||
281 |
def hyperlink_def_position(pos: Position.T): Option[Line.Node_Range] = |
|
282 |
pos match { |
|
283 |
case Position.Item_Def_File(name, line, range) => hyperlink_source_file(name, line, range) |
|
284 |
case Position.Item_Def_Id(id, range) => hyperlink_command(id, range) |
|
285 |
case _ => None |
|
286 |
} |
|
287 |
||
64651 | 288 |
def hyperlinks(range: Text.Range): List[Line.Node_Range] = |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
289 |
{ |
64651 | 290 |
snapshot.cumulate[List[Line.Node_Range]]( |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
291 |
range, Nil, VSCode_Rendering.hyperlink_elements, _ => |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
292 |
{ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
293 |
case (links, Text.Info(_, XML.Elem(Markup.Path(name), _))) => |
65488 | 294 |
val file = perhaps_append_file(snapshot.node_name, name) |
64654 | 295 |
Some(Line.Node_Range(file) :: links) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
296 |
|
64667 | 297 |
case (links, Text.Info(info_range, XML.Elem(Markup(Markup.ENTITY, props), _))) => |
298 |
hyperlink_def_position(props).map(_ :: links) |
|
299 |
||
300 |
case (links, Text.Info(info_range, XML.Elem(Markup(Markup.POSITION, props), _))) => |
|
301 |
hyperlink_position(props).map(_ :: links) |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
302 |
|
64833 | 303 |
case (links, Text.Info(info_range, XML.Elem(Markup.Citation(name), _))) => |
304 |
val iterator = |
|
305 |
for { |
|
66150 | 306 |
Text.Info(entry_range, (entry, model)) <- resources.bibtex_entries_iterator() |
64833 | 307 |
if entry == name |
308 |
} yield Line.Node_Range(model.node_name.node, model.content.doc.range(entry_range)) |
|
309 |
if (iterator.isEmpty) None else Some((links /: iterator)(_ :+ _)) |
|
310 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
311 |
case _ => None |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
312 |
}) match { case Text.Info(_, links) :: _ => links.reverse case _ => Nil } |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64622
diff
changeset
|
313 |
} |
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
diff
changeset
|
314 |
} |