author | wenzelm |
Fri, 30 Aug 2013 12:59:28 +0200 | |
changeset 53324 | c12a3edcd8e4 |
parent 53322 | a4cd032172e0 |
child 53325 | ffabc0083786 |
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 |
||
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
16 |
sealed case class Item( |
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
17 |
original: String, |
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
18 |
replacement: String, |
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
19 |
description: String, |
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
20 |
immediate: Boolean) |
53275 | 21 |
{ override def toString: String = description } |
22 |
||
23 |
||
24 |
/* init */ |
|
25 |
||
46625 | 26 |
val empty: Completion = new Completion() |
46624 | 27 |
def init(): Completion = empty.add_symbols() |
28 |
||
29 |
||
31763 | 30 |
/** word completion **/ |
31 |
||
46624 | 32 |
private val word_regex = "[a-zA-Z0-9_']+".r |
33 |
private def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches |
|
31763 | 34 |
|
46624 | 35 |
private object Parse extends RegexParsers |
31763 | 36 |
{ |
37 |
override val whiteSpace = "".r |
|
38 |
||
43462 | 39 |
def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+\^?<\\""".r |
40 |
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
|
41 |
def escape: Parser[String] = """[a-zA-Z0-9_']+\\""".r |
53322 | 42 |
def word: Parser[String] = word_regex |
43 |
def word3: Parser[String] = """[a-zA-Z0-9_']{3,}""".r |
|
31763 | 44 |
|
53322 | 45 |
def read(explicit: Boolean, in: CharSequence): Option[String] = |
31763 | 46 |
{ |
53322 | 47 |
val parse_word = if (explicit) word else word3 |
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
36012
diff
changeset
|
48 |
val reverse_in = new Library.Reverse(in) |
53322 | 49 |
parse((reverse_symbol | reverse_symb | escape | parse_word) ^^ (_.reverse), reverse_in) match { |
31763 | 50 |
case Success(result, _) => Some(result) |
51 |
case _ => None |
|
52 |
} |
|
53 |
} |
|
54 |
} |
|
55 |
} |
|
56 |
||
46712 | 57 |
final class Completion private( |
46625 | 58 |
words_lex: Scan.Lexicon = Scan.Lexicon.empty, |
53318 | 59 |
words_map: Multi_Map[String, String] = Multi_Map.empty, |
46625 | 60 |
abbrevs_lex: Scan.Lexicon = Scan.Lexicon.empty, |
53318 | 61 |
abbrevs_map: Multi_Map[String, (String, String)] = Multi_Map.empty) |
31763 | 62 |
{ |
63 |
/* adding stuff */ |
|
64 |
||
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
65 |
def + (keyword: String, replace: String): Completion = |
46624 | 66 |
new Completion( |
67 |
words_lex + keyword, |
|
68 |
words_map + (keyword -> replace), |
|
69 |
abbrevs_lex, |
|
70 |
abbrevs_map) |
|
31763 | 71 |
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
72 |
def + (keyword: String): Completion = this + (keyword, keyword) |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
73 |
|
46624 | 74 |
private def add_symbols(): Completion = |
31763 | 75 |
{ |
76 |
val words = |
|
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43462
diff
changeset
|
77 |
(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
|
78 |
(for ((x, y) <- Symbol.names) yield ("\\" + y, x)).toList ::: |
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53313
diff
changeset
|
79 |
(for ((x, y) <- Symbol.abbrevs.iterator if Completion.is_word(y)) yield (y, x)).toList |
31763 | 80 |
|
81 |
val abbrs = |
|
53316
c3e549e0d3c7
allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents:
53313
diff
changeset
|
82 |
(for ((x, y) <- Symbol.abbrevs.iterator if !Completion.is_word(y)) |
31765 | 83 |
yield (y.reverse.toString, (y, x))).toList |
31763 | 84 |
|
46624 | 85 |
new Completion( |
86 |
words_lex ++ words.map(_._1), |
|
87 |
words_map ++ words, |
|
88 |
abbrevs_lex ++ abbrs.map(_._1), |
|
89 |
abbrevs_map ++ abbrs) |
|
31763 | 90 |
} |
91 |
||
92 |
||
93 |
/* complete */ |
|
94 |
||
53322 | 95 |
def complete(decode: Boolean, explicit: Boolean, line: CharSequence) |
96 |
: Option[(String, List[Completion.Item])] = |
|
31763 | 97 |
{ |
53275 | 98 |
val raw_result = |
99 |
abbrevs_lex.parse(abbrevs_lex.keyword, new Library.Reverse(line)) match { |
|
100 |
case abbrevs_lex.Success(reverse_a, _) => |
|
53318 | 101 |
val abbrevs = abbrevs_map.get_list(reverse_a) |
102 |
abbrevs match { |
|
103 |
case Nil => None |
|
104 |
case (a, _) :: _ => Some((a, abbrevs.map(_._2))) |
|
105 |
} |
|
53275 | 106 |
case _ => |
53322 | 107 |
Completion.Parse.read(explicit, line) match { |
53275 | 108 |
case Some(word) => |
53318 | 109 |
words_lex.completions(word).map(words_map.get_list(_)).flatten match { |
53275 | 110 |
case Nil => None |
111 |
case cs => Some(word, cs.sorted) |
|
112 |
} |
|
113 |
case None => None |
|
114 |
} |
|
115 |
} |
|
116 |
raw_result match { |
|
117 |
case Some((word, cs)) => |
|
53295
45be26b98ca6
preserve original word -- relevant for true uniqueness of immediate completion;
wenzelm
parents:
53279
diff
changeset
|
118 |
val ds = (if (decode) cs.map(Symbol.decode(_)).sorted else cs) |
53275 | 119 |
if (ds.isEmpty) None |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
120 |
else { |
53313 | 121 |
val immediate = |
122 |
!Completion.is_word(word) && |
|
123 |
Character.codePointCount(word, 0, word.length) > 1 |
|
53324 | 124 |
Some((word, ds.map(s => Completion.Item(word, s, s, explicit || immediate)))) |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
125 |
} |
53275 | 126 |
case None => None |
31765 | 127 |
} |
31763 | 128 |
} |
129 |
} |