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