| author | traytel |
| Thu, 29 Aug 2013 23:21:48 +0200 | |
| changeset 53300 | e414487da3f8 |
| parent 53279 | 763d35697338 |
| child 53295 | 45be26b98ca6 |
| permissions | -rw-r--r-- |
| 53279 | 1 |
/* Title: Pure/Isar/completion.scala |
| 31763 | 2 |
Author: Makarius |
3 |
||
4 |
Completion of symbols and keywords. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import scala.util.parsing.combinator.RegexParsers |
|
10 |
||
11 |
||
| 46624 | 12 |
object Completion |
| 31763 | 13 |
{
|
| 53275 | 14 |
/* items */ |
15 |
||
16 |
sealed case class Item(original: String, replacement: String, description: String) |
|
17 |
{ override def toString: String = description }
|
|
18 |
||
19 |
||
20 |
/* init */ |
|
21 |
||
| 46625 | 22 |
val empty: Completion = new Completion() |
| 46624 | 23 |
def init(): Completion = empty.add_symbols() |
24 |
||
25 |
||
| 31763 | 26 |
/** word completion **/ |
27 |
||
| 46624 | 28 |
private val word_regex = "[a-zA-Z0-9_']+".r |
29 |
private def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches |
|
| 31763 | 30 |
|
| 46624 | 31 |
private object Parse extends RegexParsers |
| 31763 | 32 |
{
|
33 |
override val whiteSpace = "".r |
|
34 |
||
| 43462 | 35 |
def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+\^?<\\""".r |
36 |
def reverse_symb: Parser[String] = """[A-Za-z0-9_']{2,}\^?<\\""".r
|
|
|
53251
7facc08da806
complete symbols only in backslash forms -- less intrusive editing, greater chance of finding escape sequence in text;
wenzelm
parents:
46712
diff
changeset
|
37 |
def escape: Parser[String] = """[a-zA-Z0-9_']+\\""".r |
|
7facc08da806
complete symbols only in backslash forms -- less intrusive editing, greater chance of finding escape sequence in text;
wenzelm
parents:
46712
diff
changeset
|
38 |
def word: Parser[String] = """[a-zA-Z0-9_']{3,}""".r
|
| 31763 | 39 |
|
40 |
def read(in: CharSequence): Option[String] = |
|
41 |
{
|
|
|
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
36012
diff
changeset
|
42 |
val reverse_in = new Library.Reverse(in) |
|
53251
7facc08da806
complete symbols only in backslash forms -- less intrusive editing, greater chance of finding escape sequence in text;
wenzelm
parents:
46712
diff
changeset
|
43 |
parse((reverse_symbol | reverse_symb | escape | word) ^^ (_.reverse), reverse_in) match {
|
| 31763 | 44 |
case Success(result, _) => Some(result) |
45 |
case _ => None |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
} |
|
50 |
||
| 46712 | 51 |
final class Completion private( |
| 46625 | 52 |
words_lex: Scan.Lexicon = Scan.Lexicon.empty, |
53 |
words_map: Map[String, String] = Map.empty, |
|
54 |
abbrevs_lex: Scan.Lexicon = Scan.Lexicon.empty, |
|
55 |
abbrevs_map: Map[String, (String, String)] = Map.empty) |
|
| 31763 | 56 |
{
|
57 |
/* adding stuff */ |
|
58 |
||
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
59 |
def + (keyword: String, replace: String): Completion = |
| 46624 | 60 |
new Completion( |
61 |
words_lex + keyword, |
|
62 |
words_map + (keyword -> replace), |
|
63 |
abbrevs_lex, |
|
64 |
abbrevs_map) |
|
| 31763 | 65 |
|
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
66 |
def + (keyword: String): Completion = this + (keyword, keyword) |
|
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
67 |
|
| 46624 | 68 |
private def add_symbols(): Completion = |
| 31763 | 69 |
{
|
70 |
val words = |
|
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43462
diff
changeset
|
71 |
(for ((x, _) <- Symbol.names) yield (x, x)).toList ::: |
|
53251
7facc08da806
complete symbols only in backslash forms -- less intrusive editing, greater chance of finding escape sequence in text;
wenzelm
parents:
46712
diff
changeset
|
72 |
(for ((x, y) <- Symbol.names) yield ("\\" + y, x)).toList :::
|
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43462
diff
changeset
|
73 |
(for ((x, y) <- Symbol.abbrevs if Completion.is_word(y)) yield (y, x)).toList |
| 31763 | 74 |
|
75 |
val abbrs = |
|
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43462
diff
changeset
|
76 |
(for ((x, y) <- Symbol.abbrevs if !Completion.is_word(y)) |
| 31765 | 77 |
yield (y.reverse.toString, (y, x))).toList |
| 31763 | 78 |
|
| 46624 | 79 |
new Completion( |
80 |
words_lex ++ words.map(_._1), |
|
81 |
words_map ++ words, |
|
82 |
abbrevs_lex ++ abbrs.map(_._1), |
|
83 |
abbrevs_map ++ abbrs) |
|
| 31763 | 84 |
} |
85 |
||
86 |
||
87 |
/* complete */ |
|
88 |
||
| 53275 | 89 |
def complete(decode: Boolean, line: CharSequence): Option[(String, List[Completion.Item])] = |
| 31763 | 90 |
{
|
| 53275 | 91 |
val raw_result = |
92 |
abbrevs_lex.parse(abbrevs_lex.keyword, new Library.Reverse(line)) match {
|
|
93 |
case abbrevs_lex.Success(reverse_a, _) => |
|
94 |
val (word, c) = abbrevs_map(reverse_a) |
|
95 |
Some(word, List(c)) |
|
96 |
case _ => |
|
97 |
Completion.Parse.read(line) match {
|
|
98 |
case Some(word) => |
|
99 |
words_lex.completions(word).map(words_map(_)) match {
|
|
100 |
case Nil => None |
|
101 |
case cs => Some(word, cs.sorted) |
|
102 |
} |
|
103 |
case None => None |
|
104 |
} |
|
105 |
} |
|
106 |
raw_result match {
|
|
107 |
case Some((word, cs)) => |
|
108 |
val ds = (if (decode) cs.map(Symbol.decode(_)).sorted else cs).filter(_ != word) |
|
109 |
if (ds.isEmpty) None |
|
110 |
else Some((word, ds.map(s => Completion.Item(word, s, s)))) |
|
111 |
case None => None |
|
| 31765 | 112 |
} |
| 31763 | 113 |
} |
114 |
} |