author | wenzelm |
Sat, 22 Feb 2014 21:38:26 +0100 | |
changeset 55674 | 8a213ab0e78a |
parent 55673 | 0286219c1261 |
child 55687 | 78c83cd477c1 |
permissions | -rw-r--r-- |
55673
0286219c1261
clarified module location (again, see 763d35697338);
wenzelm
parents:
55666
diff
changeset
|
1 |
/* Title: Pure/General/completion.scala |
31763 | 2 |
Author: Makarius |
3 |
||
55674 | 4 |
Semantic completion within the formal context (reported by prover). |
5 |
Syntactic completion of keywords and symbols, with abbreviations |
|
6 |
(based on language context). |
|
31763 | 7 |
*/ |
8 |
||
9 |
package isabelle |
|
10 |
||
55618 | 11 |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
12 |
import scala.collection.immutable.SortedMap |
31763 | 13 |
import scala.util.parsing.combinator.RegexParsers |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
14 |
import scala.math.Ordering |
31763 | 15 |
|
16 |
||
46624 | 17 |
object Completion |
31763 | 18 |
{ |
55674 | 19 |
/** semantic completion **/ |
20 |
||
21 |
object Reported |
|
22 |
{ |
|
23 |
object Elem |
|
24 |
{ |
|
25 |
private def decode: XML.Decode.T[(String, List[String])] = |
|
26 |
{ |
|
27 |
import XML.Decode._ |
|
28 |
pair(string, list(string)) |
|
29 |
} |
|
30 |
||
31 |
def unapply(tree: XML.Tree): Option[Reported] = |
|
32 |
tree match { |
|
33 |
case XML.Elem(Markup(Markup.COMPLETION, _), body) => |
|
34 |
try { |
|
35 |
val (original, replacements) = decode(body) |
|
36 |
Some(Reported(original, replacements)) |
|
37 |
} |
|
38 |
catch { case _: XML.Error => None } |
|
39 |
case _ => None |
|
40 |
} |
|
41 |
} |
|
42 |
} |
|
43 |
||
44 |
sealed case class Reported(original: String, replacements: List[String]) |
|
45 |
||
46 |
||
47 |
||
48 |
/** syntactic completion **/ |
|
49 |
||
50 |
/* language context */ |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
51 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
52 |
object Context |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
53 |
{ |
55666 | 54 |
val outer = Context("", true, false) |
55 |
val inner = Context(Markup.Language.UNKNOWN, true, false) |
|
56 |
val ML_outer = Context(Markup.Language.ML, false, false) |
|
57 |
val ML_inner = Context(Markup.Language.ML, true, true) |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
58 |
} |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
59 |
|
55666 | 60 |
sealed case class Context(language: String, symbols: Boolean, antiquotes: Boolean) |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
61 |
{ |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
62 |
def is_outer: Boolean = language == "" |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
63 |
} |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
64 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
65 |
|
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53324
diff
changeset
|
66 |
/* result */ |
53275 | 67 |
|
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
68 |
sealed case class Item( |
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
69 |
original: String, |
55666 | 70 |
name: String, |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
71 |
replacement: String, |
55666 | 72 |
move: Int, |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
73 |
immediate: Boolean) |
55666 | 74 |
{ override def toString: String = name } |
53275 | 75 |
|
53325
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53324
diff
changeset
|
76 |
sealed case class Result(original: String, unique: Boolean, items: List[Item]) |
ffabc0083786
more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents:
53324
diff
changeset
|
77 |
|
53275 | 78 |
|
79 |
/* init */ |
|
80 |
||
46625 | 81 |
val empty: Completion = new Completion() |
46624 | 82 |
def init(): Completion = empty.add_symbols() |
83 |
||
84 |
||
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
85 |
/** persistent history **/ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
86 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
87 |
private val COMPLETION_HISTORY = Path.explode("$ISABELLE_HOME_USER/etc/completion_history") |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
88 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
89 |
object History |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
90 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
91 |
val empty: History = new History() |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
92 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
93 |
def load(): History = |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
94 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
95 |
def ignore_error(msg: String): Unit = |
55618 | 96 |
System.err.println("### Ignoring bad content of file " + COMPLETION_HISTORY + |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
97 |
(if (msg == "") "" else "\n" + msg)) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
98 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
99 |
val content = |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
100 |
if (COMPLETION_HISTORY.is_file) { |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
101 |
try { |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
102 |
import XML.Decode._ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
103 |
list(pair(Symbol.decode_string, int))( |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
104 |
YXML.parse_body(File.read(COMPLETION_HISTORY))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
105 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
106 |
catch { |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
107 |
case ERROR(msg) => ignore_error(msg); Nil |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
108 |
case _: XML.Error => ignore_error(""); Nil |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
109 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
110 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
111 |
else Nil |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
112 |
(empty /: content)(_ + _) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
113 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
114 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
115 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
116 |
final class History private(rep: SortedMap[String, Int] = SortedMap.empty) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
117 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
118 |
override def toString: String = rep.mkString("Completion.History(", ",", ")") |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
119 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
120 |
def frequency(name: String): Int = rep.getOrElse(name, 0) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
121 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
122 |
def + (entry: (String, Int)): History = |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
123 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
124 |
val (name, freq) = entry |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
125 |
new History(rep + (name -> (frequency(name) + freq))) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
126 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
127 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
128 |
def ordering: Ordering[Item] = |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
129 |
new Ordering[Item] { |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
130 |
def compare(item1: Item, item2: Item): Int = |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
131 |
{ |
55666 | 132 |
frequency(item1.name) compare frequency(item2.name) match { |
133 |
case 0 => item1.name compare item2.name |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
134 |
case ord => - ord |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
135 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
136 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
137 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
138 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
139 |
def save() |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
140 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
141 |
Isabelle_System.mkdirs(COMPLETION_HISTORY.dir) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
142 |
File.write_backup(COMPLETION_HISTORY, |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
143 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
144 |
import XML.Encode._ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
145 |
YXML.string_of_body(list(pair(Symbol.encode_string, int))(rep.toList)) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
146 |
}) |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
147 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
148 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
149 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
150 |
class History_Variable |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
151 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
152 |
private var history = History.empty |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
153 |
def value: History = synchronized { history } |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
154 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
155 |
def load() |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
156 |
{ |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
157 |
val h = History.load() |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
158 |
synchronized { history = h } |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
159 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
160 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
161 |
def update(item: Item, freq: Int = 1): Unit = synchronized { |
55666 | 162 |
history = history + (item.name -> freq) |
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
163 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
164 |
} |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
165 |
|
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
166 |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
167 |
/** word parsers **/ |
31763 | 168 |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
169 |
private object Word_Parsers extends RegexParsers |
31763 | 170 |
{ |
171 |
override val whiteSpace = "".r |
|
172 |
||
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
173 |
private def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+\^?<\\""".r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
174 |
private def reverse_symb: Parser[String] = """[A-Za-z0-9_']{2,}\^?<\\""".r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
175 |
private def escape: Parser[String] = """[a-zA-Z0-9_']+\\""".r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
176 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
177 |
private val word_regex = "[a-zA-Z0-9_']+".r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
178 |
private def word: Parser[String] = word_regex |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
179 |
private def word3: Parser[String] = """[a-zA-Z0-9_']{3,}""".r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
180 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
181 |
def is_word(s: CharSequence): Boolean = |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
182 |
word_regex.pattern.matcher(s).matches |
31763 | 183 |
|
53322 | 184 |
def read(explicit: Boolean, in: CharSequence): Option[String] = |
31763 | 185 |
{ |
53322 | 186 |
val parse_word = if (explicit) word else word3 |
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
36012
diff
changeset
|
187 |
val reverse_in = new Library.Reverse(in) |
53322 | 188 |
parse((reverse_symbol | reverse_symb | escape | parse_word) ^^ (_.reverse), reverse_in) match { |
31763 | 189 |
case Success(result, _) => Some(result) |
190 |
case _ => None |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
55666 | 194 |
|
195 |
||
196 |
/* abbreviations */ |
|
197 |
||
198 |
private val caret = '\007' |
|
199 |
private val antiquote = "@{" |
|
200 |
private val default_abbrs = |
|
201 |
Map("@{" -> "@{\007}", "`" -> "\\<open>\007\\<close>") |
|
31763 | 202 |
} |
203 |
||
46712 | 204 |
final class Completion private( |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
205 |
keywords: Set[String] = Set.empty, |
46625 | 206 |
words_lex: Scan.Lexicon = Scan.Lexicon.empty, |
53318 | 207 |
words_map: Multi_Map[String, String] = Multi_Map.empty, |
46625 | 208 |
abbrevs_lex: Scan.Lexicon = Scan.Lexicon.empty, |
53318 | 209 |
abbrevs_map: Multi_Map[String, (String, String)] = Multi_Map.empty) |
31763 | 210 |
{ |
211 |
/* adding stuff */ |
|
212 |
||
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
213 |
def + (keyword: String, replace: String): Completion = |
46624 | 214 |
new Completion( |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
215 |
keywords + keyword, |
46624 | 216 |
words_lex + keyword, |
217 |
words_map + (keyword -> replace), |
|
218 |
abbrevs_lex, |
|
219 |
abbrevs_map) |
|
31763 | 220 |
|
40533
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
221 |
def + (keyword: String): Completion = this + (keyword, keyword) |
e38e80686ce5
somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents:
37072
diff
changeset
|
222 |
|
46624 | 223 |
private def add_symbols(): Completion = |
31763 | 224 |
{ |
225 |
val words = |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
226 |
(for ((x, _) <- Symbol.names.toList) yield (x, x)) ::: |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
227 |
(for ((x, y) <- Symbol.names.toList) yield ("\\" + y, x)) ::: |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
228 |
(for ((x, y) <- Symbol.abbrevs.toList if Completion.Word_Parsers.is_word(y)) yield (y, x)) |
31763 | 229 |
|
55666 | 230 |
val symbol_abbrs = |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
231 |
(for ((x, y) <- Symbol.abbrevs.iterator if !Completion.Word_Parsers.is_word(y)) |
55666 | 232 |
yield (y, x)).toList |
233 |
||
234 |
val abbrs = |
|
235 |
for ((a, b) <- symbol_abbrs ::: Completion.default_abbrs.toList) |
|
236 |
yield (a.reverse, (a, b)) |
|
31763 | 237 |
|
46624 | 238 |
new Completion( |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
239 |
keywords, |
46624 | 240 |
words_lex ++ words.map(_._1), |
241 |
words_map ++ words, |
|
242 |
abbrevs_lex ++ abbrs.map(_._1), |
|
243 |
abbrevs_map ++ abbrs) |
|
31763 | 244 |
} |
245 |
||
246 |
||
247 |
/* complete */ |
|
248 |
||
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
249 |
def complete( |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
250 |
history: Completion.History, |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
251 |
decode: Boolean, |
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
252 |
explicit: Boolean, |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
253 |
text: CharSequence, |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
254 |
context: Completion.Context): Option[Completion.Result] = |
31763 | 255 |
{ |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
256 |
val abbrevs_result = |
55666 | 257 |
Scan.Parsers.parse(Scan.Parsers.literal(abbrevs_lex), new Library.Reverse(text)) match { |
258 |
case Scan.Parsers.Success(reverse_a, _) => |
|
259 |
val abbrevs = abbrevs_map.get_list(reverse_a) |
|
260 |
abbrevs match { |
|
261 |
case Nil => None |
|
262 |
case (a, _) :: _ => |
|
263 |
val ok = |
|
264 |
if (a == Completion.antiquote) context.antiquotes |
|
265 |
else context.symbols || Completion.default_abbrs.isDefinedAt(a) |
|
266 |
if (ok) Some((a, abbrevs.map(_._2))) else None |
|
267 |
} |
|
268 |
case _ => None |
|
269 |
} |
|
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
270 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
271 |
val words_result = |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
272 |
abbrevs_result orElse { |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
273 |
Completion.Word_Parsers.read(explicit, text) match { |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
274 |
case Some(word) => |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
275 |
val completions = |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
276 |
for { |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
277 |
s <- words_lex.completions(word) |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
278 |
if (if (keywords(s)) context.is_outer else context.symbols) |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
279 |
r <- words_map.get_list(s) |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
280 |
} yield r |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
281 |
if (completions.isEmpty) None |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
282 |
else Some(word, completions) |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
283 |
case None => None |
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
284 |
} |
53275 | 285 |
} |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
286 |
|
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
287 |
words_result match { |
53275 | 288 |
case Some((word, cs)) => |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
289 |
val ds = (if (decode) cs.map(Symbol.decode(_)) else cs).filter(_ != word) |
53275 | 290 |
if (ds.isEmpty) None |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
291 |
else { |
53313 | 292 |
val immediate = |
55615
bf4bbe72f740
completion of keywords and symbols based on language context;
wenzelm
parents:
55497
diff
changeset
|
293 |
!Completion.Word_Parsers.is_word(word) && |
53313 | 294 |
Character.codePointCount(word, 0, word.length) > 1 |
55666 | 295 |
val items = |
296 |
ds.map(s => { |
|
297 |
val (s1, s2) = |
|
298 |
space_explode(Completion.caret, s) match { |
|
299 |
case List(s1, s2) => (s1, s2) |
|
300 |
case _ => (s, "") |
|
301 |
} |
|
302 |
Completion.Item(word, s, s1 + s2, - s2.length, explicit || immediate) |
|
303 |
}) |
|
53337
b3817a0e3211
sort items according to persistent history of frequency of use;
wenzelm
parents:
53325
diff
changeset
|
304 |
Some(Completion.Result(word, cs.length == 1, items.sorted(history.ordering))) |
53296
65c60c782da5
less aggressive immediate completion, based on input and text;
wenzelm
parents:
53295
diff
changeset
|
305 |
} |
53275 | 306 |
case None => None |
31765 | 307 |
} |
31763 | 308 |
} |
309 |
} |