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