src/Pure/General/completion.scala
author wenzelm
Sat, 22 Feb 2014 20:56:50 +0100
changeset 55673 0286219c1261
parent 55666 src/Pure/Isar/completion.scala@cc350eb1087e
child 55674 8a213ab0e78a
permissions -rw-r--r--
clarified module location (again, see 763d35697338);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55673
0286219c1261 clarified module location (again, see 763d35697338);
wenzelm
parents: 55666
diff changeset
     1
/*  Title:      Pure/General/completion.scala
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     3
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
     4
Completion of keywords and symbols, with abbreviations.
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     5
*/
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     6
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     7
package isabelle
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
     8
55618
995162143ef4 tuned imports;
wenzelm
parents: 55615
diff changeset
     9
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    10
import scala.collection.immutable.SortedMap
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    11
import scala.util.parsing.combinator.RegexParsers
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    12
import scala.math.Ordering
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    13
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    14
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
    15
object Completion
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
    16
{
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    17
  /* context */
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    18
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    19
  object Context
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    20
  {
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    21
    val outer = Context("", true, false)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    22
    val inner = Context(Markup.Language.UNKNOWN, true, false)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    23
    val ML_outer = Context(Markup.Language.ML, false, false)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    24
    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
    25
  }
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    26
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    27
  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
    28
  {
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    29
    def is_outer: Boolean = language == ""
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    30
  }
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    31
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
    32
53325
ffabc0083786 more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents: 53324
diff changeset
    33
  /* result */
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
    34
53296
65c60c782da5 less aggressive immediate completion, based on input and text;
wenzelm
parents: 53295
diff changeset
    35
  sealed case class Item(
65c60c782da5 less aggressive immediate completion, based on input and text;
wenzelm
parents: 53295
diff changeset
    36
    original: String,
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    37
    name: String,
53296
65c60c782da5 less aggressive immediate completion, based on input and text;
wenzelm
parents: 53295
diff changeset
    38
    replacement: String,
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    39
    move: Int,
53296
65c60c782da5 less aggressive immediate completion, based on input and text;
wenzelm
parents: 53295
diff changeset
    40
    immediate: Boolean)
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    41
  { override def toString: String = name }
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
    42
53325
ffabc0083786 more explicit indication of unique result (see also 45be26b98ca6, 3d654643cf56);
wenzelm
parents: 53324
diff changeset
    43
  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
    44
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
    45
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
    46
  /* init */
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
    47
46625
wenzelm
parents: 46624
diff changeset
    48
  val empty: Completion = new Completion()
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
    49
  def init(): Completion = empty.add_symbols()
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
    50
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
    51
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    52
  /** persistent history **/
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    53
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    54
  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
    55
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    56
  object History
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    57
  {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    58
    val empty: History = new History()
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    59
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    60
    def load(): History =
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    61
    {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    62
      def ignore_error(msg: String): Unit =
55618
995162143ef4 tuned imports;
wenzelm
parents: 55615
diff changeset
    63
        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
    64
          (if (msg == "") "" else "\n" + msg))
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    65
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    66
      val content =
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    67
        if (COMPLETION_HISTORY.is_file) {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    68
          try {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    69
            import XML.Decode._
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    70
            list(pair(Symbol.decode_string, int))(
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    71
              YXML.parse_body(File.read(COMPLETION_HISTORY)))
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    72
          }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    73
          catch {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    74
            case ERROR(msg) => ignore_error(msg); Nil
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    75
            case _: XML.Error => ignore_error(""); Nil
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    76
          }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    77
        }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    78
        else Nil
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    79
      (empty /: content)(_ + _)
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    80
    }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    81
  }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    82
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    83
  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
    84
  {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    85
    override def toString: String = rep.mkString("Completion.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
    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
    88
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    89
    def + (entry: (String, Int)): 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 (name, freq) = entry
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    92
      new History(rep + (name -> (frequency(name) + freq)))
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    93
    }
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 ordering: Ordering[Item] =
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    96
      new Ordering[Item] {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    97
        def compare(item1: Item, item2: Item): Int =
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
    98
        {
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
    99
          frequency(item1.name) compare frequency(item2.name) match {
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   100
            case 0 => item1.name compare item2.name
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   101
            case ord => - ord
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   102
          }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   103
        }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   104
      }
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
    def save()
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   107
    {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   108
      Isabelle_System.mkdirs(COMPLETION_HISTORY.dir)
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   109
      File.write_backup(COMPLETION_HISTORY,
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
          import XML.Encode._
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   112
          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
   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
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   117
  class History_Variable
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   118
  {
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   119
    private var history = History.empty
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   120
    def value: History = synchronized { history }
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 load()
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 h = History.load()
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   125
      synchronized { history = h }
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 update(item: Item, freq: Int = 1): Unit = synchronized {
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   129
      history = history + (item.name -> freq)
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   130
    }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   131
  }
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   132
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   133
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   134
  /** word parsers **/
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   135
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   136
  private object Word_Parsers extends RegexParsers
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   137
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   138
    override val whiteSpace = "".r
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   139
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   140
    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
   141
    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
   142
    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
   143
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   144
    private val word_regex = "[a-zA-Z0-9_']+".r
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   145
    private def word: Parser[String] = word_regex
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   146
    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
   147
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   148
    def is_word(s: CharSequence): Boolean =
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   149
      word_regex.pattern.matcher(s).matches
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   150
53322
a4cd032172e0 allow short words for explicit completion;
wenzelm
parents: 53318
diff changeset
   151
    def read(explicit: Boolean, in: CharSequence): Option[String] =
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   152
    {
53322
a4cd032172e0 allow short words for explicit completion;
wenzelm
parents: 53318
diff changeset
   153
      val parse_word = if (explicit) word else word3
37072
9105c8237c7a renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents: 36012
diff changeset
   154
      val reverse_in = new Library.Reverse(in)
53322
a4cd032172e0 allow short words for explicit completion;
wenzelm
parents: 53318
diff changeset
   155
      parse((reverse_symbol | reverse_symb | escape | parse_word) ^^ (_.reverse), reverse_in) match {
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   156
        case Success(result, _) => Some(result)
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   157
        case _ => None
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   158
      }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   159
    }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   160
  }
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   161
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   162
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   163
  /* abbreviations */
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   164
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   165
  private val caret = '\007'
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   166
  private val antiquote = "@{"
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   167
  private val default_abbrs =
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   168
    Map("@{" -> "@{\007}", "`" -> "\\<open>\007\\<close>")
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   169
}
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   170
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46625
diff changeset
   171
final class Completion private(
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   172
  keywords: Set[String] = Set.empty,
46625
wenzelm
parents: 46624
diff changeset
   173
  words_lex: Scan.Lexicon = Scan.Lexicon.empty,
53318
ec4511548304 allow multiple entries;
wenzelm
parents: 53316
diff changeset
   174
  words_map: Multi_Map[String, String] = Multi_Map.empty,
46625
wenzelm
parents: 46624
diff changeset
   175
  abbrevs_lex: Scan.Lexicon = Scan.Lexicon.empty,
53318
ec4511548304 allow multiple entries;
wenzelm
parents: 53316
diff changeset
   176
  abbrevs_map: Multi_Map[String, (String, String)] = Multi_Map.empty)
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   177
{
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   178
  /* adding stuff */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   179
40533
e38e80686ce5 somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents: 37072
diff changeset
   180
  def + (keyword: String, replace: String): Completion =
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   181
    new Completion(
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   182
      keywords + keyword,
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   183
      words_lex + keyword,
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   184
      words_map + (keyword -> replace),
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   185
      abbrevs_lex,
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   186
      abbrevs_map)
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   187
40533
e38e80686ce5 somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents: 37072
diff changeset
   188
  def + (keyword: String): Completion = this + (keyword, keyword)
e38e80686ce5 somewhat adhoc replacement for 'thus' and 'hence';
wenzelm
parents: 37072
diff changeset
   189
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   190
  private def add_symbols(): Completion =
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   191
  {
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   192
    val words =
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   193
      (for ((x, _) <- Symbol.names.toList) yield (x, x)) :::
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   194
      (for ((x, y) <- Symbol.names.toList) yield ("\\" + y, x)) :::
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   195
      (for ((x, y) <- Symbol.abbrevs.toList if Completion.Word_Parsers.is_word(y)) yield (y, x))
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   196
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   197
    val symbol_abbrs =
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   198
      (for ((x, y) <- Symbol.abbrevs.iterator if !Completion.Word_Parsers.is_word(y))
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   199
        yield (y, x)).toList
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   200
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   201
    val abbrs =
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   202
      for ((a, b) <- symbol_abbrs ::: Completion.default_abbrs.toList)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   203
        yield (a.reverse, (a, b))
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   204
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   205
    new Completion(
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   206
      keywords,
46624
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   207
      words_lex ++ words.map(_._1),
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   208
      words_map ++ words,
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   209
      abbrevs_lex ++ abbrs.map(_._1),
dc4c72092088 streamlined abstract datatype;
wenzelm
parents: 45900
diff changeset
   210
      abbrevs_map ++ abbrs)
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   211
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   212
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   213
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   214
  /* complete */
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   215
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   216
  def complete(
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   217
    history: Completion.History,
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   218
    decode: Boolean,
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   219
    explicit: Boolean,
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   220
    text: CharSequence,
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   221
    context: Completion.Context): Option[Completion.Result] =
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   222
  {
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   223
    val abbrevs_result =
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   224
      Scan.Parsers.parse(Scan.Parsers.literal(abbrevs_lex), new Library.Reverse(text)) match {
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   225
        case Scan.Parsers.Success(reverse_a, _) =>
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   226
          val abbrevs = abbrevs_map.get_list(reverse_a)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   227
          abbrevs match {
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   228
            case Nil => None
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   229
            case (a, _) :: _ =>
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   230
              val ok =
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   231
                if (a == Completion.antiquote) context.antiquotes
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   232
                else context.symbols || Completion.default_abbrs.isDefinedAt(a)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   233
              if (ok) Some((a, abbrevs.map(_._2))) else None
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   234
          }
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   235
        case _ => None
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   236
      }
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   237
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   238
    val words_result =
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   239
      abbrevs_result orElse {
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   240
        Completion.Word_Parsers.read(explicit, text) match {
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   241
          case Some(word) =>
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   242
            val completions =
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   243
              for {
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   244
                s <- words_lex.completions(word)
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   245
                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
   246
                r <- words_map.get_list(s)
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   247
              } yield r
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   248
            if (completions.isEmpty) None
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   249
            else Some(word, completions)
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   250
          case None => None
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   251
        }
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
   252
      }
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   253
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   254
    words_result match {
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
   255
      case Some((word, cs)) =>
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   256
        val ds = (if (decode) cs.map(Symbol.decode(_)) else cs).filter(_ != word)
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
   257
        if (ds.isEmpty) None
53296
65c60c782da5 less aggressive immediate completion, based on input and text;
wenzelm
parents: 53295
diff changeset
   258
        else {
53313
2e745fc40416 less surprising immediate completion;
wenzelm
parents: 53296
diff changeset
   259
          val immediate =
55615
bf4bbe72f740 completion of keywords and symbols based on language context;
wenzelm
parents: 55497
diff changeset
   260
            !Completion.Word_Parsers.is_word(word) &&
53313
2e745fc40416 less surprising immediate completion;
wenzelm
parents: 53296
diff changeset
   261
            Character.codePointCount(word, 0, word.length) > 1
55666
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   262
          val items =
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   263
            ds.map(s => {
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   264
              val (s1, s2) =
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   265
                space_explode(Completion.caret, s) match {
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   266
                  case List(s1, s2) => (s1, s2)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   267
                  case _ => (s, "")
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   268
                }
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   269
              Completion.Item(word, s, s1 + s2, - s2.length, explicit || immediate)
cc350eb1087e refined language context: antiquotes;
wenzelm
parents: 55618
diff changeset
   270
            })
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53325
diff changeset
   271
          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
   272
        }
53275
b34aac6511ab tuned signature;
wenzelm
parents: 53251
diff changeset
   273
      case None => None
31765
a5fdf7a76f9d tuned input: require longer symbol prefix;
wenzelm
parents: 31763
diff changeset
   274
    }
31763
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   275
  }
c2c2d380729d Completion of symbols and keywords.
wenzelm
parents:
diff changeset
   276
}