1 /* Title: Pure/Thy/completion.scala
4 Completion of symbols and keywords.
9 import scala.util.matching.Regex
10 import scala.util.parsing.combinator.RegexParsers
13 private object Completion
15 /** word completion **/
17 val word_regex = "[a-zA-Z0-9_']+".r
18 def is_word(s: CharSequence): Boolean = word_regex.pattern.matcher(s).matches
20 object Parse extends RegexParsers
22 override val whiteSpace = "".r
24 def reverse_symbol: Parser[String] = """>[A-Za-z0-9_']+<\\""".r
25 def reverse_symb: Parser[String] = """[A-Za-z0-9_']{2,}<\\""".r
26 def word: Parser[String] = "[a-zA-Z0-9_']{3,}".r
28 def read(in: CharSequence): Option[String] =
30 val reverse_in = new Library.Reverse(in)
31 parse((reverse_symbol | reverse_symb | word) ^^ (_.reverse), reverse_in) match {
32 case Success(result, _) => Some(result)
43 protected val words_lex = Scan.Lexicon()
44 protected val words_map = Map[String, String]()
46 protected val abbrevs_lex = Scan.Lexicon()
47 protected val abbrevs_map = Map[String, (String, String)]()
52 def + (keyword: String): Completion =
56 override val words_lex = old.words_lex + keyword
57 override val words_map = old.words_map + (keyword -> keyword)
58 override val abbrevs_lex = old.abbrevs_lex
59 override val abbrevs_map = old.abbrevs_map
63 def + (symbols: Symbol.Interpretation): Completion =
66 (for ((x, _) <- symbols.names) yield (x, x)).toList :::
67 (for ((x, y) <- symbols.names) yield (y, x)).toList :::
68 (for ((x, y) <- symbols.abbrevs if Completion.is_word(y)) yield (y, x)).toList
71 (for ((x, y) <- symbols.abbrevs if !Completion.is_word(y))
72 yield (y.reverse.toString, (y, x))).toList
76 override val words_lex = old.words_lex ++ words.map(_._1)
77 override val words_map = old.words_map ++ words
78 override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
79 override val abbrevs_map = old.abbrevs_map ++ abbrs
86 def complete(line: CharSequence): Option[(String, List[String])] =
88 abbrevs_lex.parse(abbrevs_lex.keyword, new Library.Reverse(line)) match {
89 case abbrevs_lex.Success(reverse_a, _) =>
90 val (word, c) = abbrevs_map(reverse_a)
93 Completion.Parse.read(line) match {
95 words_lex.completions(word).map(words_map(_)) match {
97 case cs => Some(word, cs.sortWith(_ < _))