src/Pure/Thy/completion.scala
changeset 31765 a5fdf7a76f9d
parent 31763 c2c2d380729d
child 31780 d78e5cff9a9f
equal deleted inserted replaced
31764:e767fee21b22 31765:a5fdf7a76f9d
    47 
    47 
    48   object Parse extends RegexParsers
    48   object Parse extends RegexParsers
    49   {
    49   {
    50     override val whiteSpace = "".r
    50     override val whiteSpace = "".r
    51 
    51 
    52     def rev_symb: Parser[String] = """>?[A-Za-z0-9_']+<\\""".r
    52     def rev_symbol: Parser[String] = """>[A-Za-z0-9_']+<\\""".r
       
    53     def rev_symb: Parser[String] = """[A-Za-z0-9_']{2,}<\\""".r
    53     def word: Parser[String] = "[a-zA-Z0-9_']{3,}".r
    54     def word: Parser[String] = "[a-zA-Z0-9_']{3,}".r
    54 
    55 
    55     def read(in: CharSequence): Option[String] =
    56     def read(in: CharSequence): Option[String] =
    56     {
    57     {
    57       parse((rev_symb | word) ^^ (_.reverse), new Reverse(in)) match {
    58       val rev_in = new Reverse(in)
       
    59       parse((rev_symbol | rev_symb | word) ^^ (_.reverse), rev_in) match {
    58         case Success(result, _) => Some(result)
    60         case Success(result, _) => Some(result)
    59         case _ => None
    61         case _ => None
    60       }
    62       }
    61     }
    63     }
    62   }
    64   }
    63 
       
    64 }
    65 }
    65 
    66 
    66 class Completion
    67 class Completion
    67 {
    68 {
    68   /* representation */
    69   /* representation */
    94       (for ((x, y) <- symbols.names) yield (y, x)).toList :::
    95       (for ((x, y) <- symbols.names) yield (y, x)).toList :::
    95       (for ((x, y) <- symbols.abbrevs if Completion.is_word(y)) yield (y, x)).toList
    96       (for ((x, y) <- symbols.abbrevs if Completion.is_word(y)) yield (y, x)).toList
    96 
    97 
    97     val abbrs =
    98     val abbrs =
    98       (for ((x, y) <- symbols.abbrevs if !Completion.is_word(y))
    99       (for ((x, y) <- symbols.abbrevs if !Completion.is_word(y))
    99         yield (y.reverse.toString, (y, symbols.decode(x)))).toList
   100         yield (y.reverse.toString, (y, x))).toList
   100 
   101 
   101     val old = this
   102     val old = this
   102     new Completion {
   103     new Completion {
   103       override val words_lex = old.words_lex ++ words.map(_._1)
   104       override val words_lex = old.words_lex ++ words.map(_._1)
   104       override val words_map = old.words_map ++ words.map(p => (p._1, symbols.decode(p._2)))
   105       override val words_map = old.words_map ++ words
   105       override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
   106       override val abbrevs_lex = old.abbrevs_lex ++ abbrs.map(_._1)
   106       override val abbrevs_map = old.abbrevs_map ++ abbrs
   107       override val abbrevs_map = old.abbrevs_map ++ abbrs
   107     }
   108     }
   108   }
   109   }
   109 
   110 
   110 
   111 
   111   /* complete */
   112   /* complete */
   112 
   113 
   113   def complete(line: CharSequence): List[(String, String)] =
   114   def complete(line: CharSequence): Option[(String, List[String])] =
   114   {
   115   {
   115     val abbrs =
   116     abbrevs_lex.parse(abbrevs_lex.keyword, new Completion.Reverse(line)) match {
   116       abbrevs_lex.parse(abbrevs_lex.keyword, new Completion.Reverse(line)) match {
   117       case abbrevs_lex.Success(rev_a, _) =>
   117         case abbrevs_lex.Success(rev_a, _) => List(abbrevs_map(rev_a))
   118         val (word, c) = abbrevs_map(rev_a)
   118         case _ => Nil
   119         Some(word, List(c))
   119       }
   120       case _ =>
   120 
   121         Completion.Parse.read(line) match {
   121     val compls =
   122           case Some(word) =>
   122       Completion.Parse.read(line) match {
   123             words_lex.completions(word) match {
   123         case Some(word) => words_lex.completions(word).map(w => (word, words_map(w)))
   124               case Nil => None
   124         case _ => Nil
   125               case cs => Some(word, cs.map(words_map(_)).sort(Completion.length_ord _))
   125       }
   126             }
   126 
   127           case None => None
   127     (abbrs ::: compls).sort((p1, p2) => Completion.length_ord(p1._2, p2._2))
   128         }
       
   129     }
   128   }
   130   }
   129 
       
   130 }
   131 }