author | wenzelm |
Mon, 19 Jun 2017 21:15:06 +0200 | |
changeset 66120 | e03ff7e831cc |
parent 66118 | src/Tools/jEdit/src/bibtex_jedit.scala@03dd799fe042 |
child 66150 | c2e19b9e1398 |
permissions | -rw-r--r-- |
66120 | 1 |
/* Title: Tools/jEdit/src/jedit_bibtex.scala |
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
3 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
4 |
BibTeX support in Isabelle/jEdit. |
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
6 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle.jedit |
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
8 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
9 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
10 |
import isabelle._ |
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
11 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
12 |
|
58547 | 13 |
import scala.collection.mutable |
14 |
||
58548 | 15 |
import java.awt.event.{ActionListener, ActionEvent} |
16 |
||
58546 | 17 |
import javax.swing.text.Segment |
18 |
import javax.swing.tree.DefaultMutableTreeNode |
|
58548 | 19 |
import javax.swing.{JMenu, JMenuItem} |
58546 | 20 |
|
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
21 |
import org.gjt.sp.jedit.Buffer |
58548 | 22 |
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea} |
58546 | 23 |
import org.gjt.sp.jedit.syntax.{Token => JEditToken, TokenMarker, TokenHandler, ParserRuleSet} |
24 |
||
25 |
import sidekick.{SideKickParser, SideKickParsedData} |
|
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
26 |
|
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
27 |
|
66120 | 28 |
object JEdit_Bibtex |
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
29 |
{ |
64829
07f209e957bc
refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents:
64828
diff
changeset
|
30 |
/** completion **/ |
58592 | 31 |
|
32 |
def complete(name: String): List[String] = |
|
64829
07f209e957bc
refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents:
64828
diff
changeset
|
33 |
(for { |
64831 | 34 |
Text.Info(_, (entry, _)) <- Document_Model.bibtex_entries_iterator() |
64829
07f209e957bc
refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents:
64828
diff
changeset
|
35 |
if entry.toLowerCase.containsSlice(name.toLowerCase) |
07f209e957bc
refer to bibtex entries via general Document_Model, instead of editor buffers;
wenzelm
parents:
64828
diff
changeset
|
36 |
} yield entry).toList |
58592 | 37 |
|
66118 | 38 |
def completion(history: Completion.History, rendering: Rendering, caret: Text.Offset) |
39 |
: Option[Completion.Result] = Bibtex.completion(history, rendering, caret, complete _) |
|
58592 | 40 |
|
41 |
||
58546 | 42 |
|
58548 | 43 |
/** context menu **/ |
44 |
||
45 |
def context_menu(text_area0: JEditTextArea): List[JMenuItem] = |
|
46 |
{ |
|
47 |
text_area0 match { |
|
48 |
case text_area: TextArea => |
|
49 |
text_area.getBuffer match { |
|
50 |
case buffer: Buffer |
|
64828 | 51 |
if (Bibtex.check_name(JEdit_Lib.buffer_name(buffer)) && buffer.isEditable) => |
58548 | 52 |
val menu = new JMenu("BibTeX entries") |
53 |
for (entry <- Bibtex.entries) { |
|
54 |
val item = new JMenuItem(entry.kind) |
|
55 |
item.addActionListener(new ActionListener { |
|
56 |
def actionPerformed(evt: ActionEvent): Unit = |
|
57 |
Isabelle.insert_line_padding(text_area, entry.template) |
|
58 |
}) |
|
59 |
menu.add(item) |
|
60 |
} |
|
61 |
List(menu) |
|
62 |
case _ => Nil |
|
63 |
} |
|
64 |
case _ => Nil |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
||
69 |
||
58546 | 70 |
/** token markup **/ |
71 |
||
72 |
/* token style */ |
|
73 |
||
74 |
private def token_style(context: String, token: Bibtex.Token): Byte = |
|
75 |
token.kind match { |
|
76 |
case Bibtex.Token.Kind.COMMAND => JEditToken.KEYWORD2 |
|
77 |
case Bibtex.Token.Kind.ENTRY => JEditToken.KEYWORD1 |
|
78 |
case Bibtex.Token.Kind.KEYWORD => JEditToken.OPERATOR |
|
79 |
case Bibtex.Token.Kind.NAT => JEditToken.LITERAL2 |
|
80 |
case Bibtex.Token.Kind.STRING => JEditToken.LITERAL1 |
|
81 |
case Bibtex.Token.Kind.NAME => JEditToken.LABEL |
|
82 |
case Bibtex.Token.Kind.IDENT => |
|
83 |
if (Bibtex.is_month(token.source)) JEditToken.LITERAL3 |
|
84 |
else |
|
85 |
Bibtex.get_entry(context) match { |
|
86 |
case Some(entry) if entry.is_required(token.source) => JEditToken.KEYWORD3 |
|
87 |
case Some(entry) if entry.is_optional(token.source) => JEditToken.KEYWORD4 |
|
88 |
case _ => JEditToken.DIGIT |
|
89 |
} |
|
90 |
case Bibtex.Token.Kind.SPACE => JEditToken.NULL |
|
91 |
case Bibtex.Token.Kind.COMMENT => JEditToken.COMMENT1 |
|
92 |
case Bibtex.Token.Kind.ERROR => JEditToken.INVALID |
|
93 |
} |
|
94 |
||
95 |
||
96 |
/* line context */ |
|
97 |
||
98 |
private val context_rules = new ParserRuleSet("bibtex", "MAIN") |
|
99 |
||
58699 | 100 |
private class Line_Context(val context: Option[Bibtex.Line_Context]) |
101 |
extends TokenMarker.LineContext(context_rules, null) |
|
102 |
{ |
|
103 |
override def hashCode: Int = context.hashCode |
|
104 |
override def equals(that: Any): Boolean = |
|
105 |
that match { |
|
106 |
case other: Line_Context => context == other.context |
|
107 |
case _ => false |
|
108 |
} |
|
109 |
} |
|
58546 | 110 |
|
111 |
||
112 |
/* token marker */ |
|
113 |
||
114 |
class Token_Marker extends TokenMarker |
|
115 |
{ |
|
116 |
override def markTokens(context: TokenMarker.LineContext, |
|
117 |
handler: TokenHandler, raw_line: Segment): TokenMarker.LineContext = |
|
118 |
{ |
|
119 |
val line_ctxt = |
|
120 |
context match { |
|
121 |
case c: Line_Context => c.context |
|
58589 | 122 |
case _ => Some(Bibtex.Ignored) |
58546 | 123 |
} |
124 |
val line = if (raw_line == null) new Segment else raw_line |
|
125 |
||
58595
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
126 |
def no_markup = |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
127 |
{ |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
128 |
val styled_token = (JEditToken.NULL, line.subSequence(0, line.count).toString) |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
129 |
(List(styled_token), new Line_Context(None)) |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
130 |
} |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
131 |
|
58546 | 132 |
val context1 = |
133 |
{ |
|
134 |
val (styled_tokens, context1) = |
|
135 |
line_ctxt match { |
|
136 |
case Some(ctxt) => |
|
58595
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
137 |
try { |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
138 |
val (chunks, ctxt1) = Bibtex.parse_line(line, ctxt) |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
139 |
val styled_tokens = |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
140 |
for { chunk <- chunks; tok <- chunk.tokens } |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
141 |
yield (token_style(chunk.kind, tok), tok.source) |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
142 |
(styled_tokens, new Line_Context(Some(ctxt1))) |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
143 |
} |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
144 |
catch { case ERROR(msg) => Output.warning(msg); no_markup } |
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
145 |
case None => no_markup |
58546 | 146 |
} |
147 |
||
148 |
var offset = 0 |
|
149 |
for ((style, token) <- styled_tokens) { |
|
150 |
val length = token.length |
|
151 |
val end_offset = offset + length |
|
152 |
||
153 |
if ((offset until end_offset).exists(i => line.charAt(i) == '\t')) { |
|
154 |
for (i <- offset until end_offset) |
|
155 |
handler.handleToken(line, style, i, 1, context1) |
|
156 |
} |
|
157 |
else handler.handleToken(line, style, offset, length, context1) |
|
158 |
||
159 |
offset += length |
|
160 |
} |
|
161 |
handler.handleToken(line, JEditToken.END, line.count, 0, context1) |
|
162 |
context1 |
|
163 |
} |
|
164 |
val context2 = context1.intern |
|
165 |
handler.setLineContext(context2) |
|
166 |
context2 |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
||
171 |
||
172 |
/** Sidekick parser **/ |
|
173 |
||
174 |
class Sidekick_Parser extends SideKickParser("bibtex") |
|
175 |
{ |
|
176 |
override def supportsCompletion = false |
|
177 |
||
58747 | 178 |
private class Asset(label: String, label_html: String, range: Text.Range, source: String) |
179 |
extends Isabelle_Sidekick.Asset(label, range) { |
|
58546 | 180 |
override def getShortString: String = label_html |
181 |
override def getLongString: String = source |
|
182 |
} |
|
183 |
||
184 |
def parse(buffer: Buffer, error_source: errorlist.DefaultErrorSource): SideKickParsedData = |
|
185 |
{ |
|
186 |
val data = Isabelle_Sidekick.root_data(buffer) |
|
187 |
||
188 |
try { |
|
189 |
var offset = 0 |
|
190 |
for (chunk <- Bibtex.parse(JEdit_Lib.buffer_text(buffer))) { |
|
191 |
val kind = chunk.kind |
|
192 |
val name = chunk.name |
|
193 |
val source = chunk.source |
|
194 |
if (kind != "") { |
|
195 |
val label = kind + (if (name == "") "" else " " + name) |
|
196 |
val label_html = |
|
62113 | 197 |
"<html><b>" + HTML.output(kind) + "</b>" + |
198 |
(if (name == "") "" else " " + HTML.output(name)) + "</html>" |
|
60215 | 199 |
val range = Text.Range(offset, offset + source.length) |
58747 | 200 |
val asset = new Asset(label, label_html, range, source) |
58546 | 201 |
data.root.add(new DefaultMutableTreeNode(asset)) |
202 |
} |
|
60215 | 203 |
offset += source.length |
58546 | 204 |
} |
205 |
data |
|
206 |
} |
|
58595
127f192b755c
more defensive error handling -- token marker must not crash;
wenzelm
parents:
58592
diff
changeset
|
207 |
catch { case ERROR(msg) => Output.warning(msg); null } |
58546 | 208 |
} |
209 |
} |
|
58545
30b75b7958d6
citation tooltip/hyperlink based on open buffers with .bib files;
wenzelm
parents:
diff
changeset
|
210 |
} |