src/Pure/Tools/spell_checker.scala
author wenzelm
Mon, 25 Oct 2021 17:26:27 +0200
changeset 74576 0b43d42cfde7
parent 73909 1d0d9772fff0
child 75393 87ebf5a50283
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
65367
83c30e290702 tuned headers;
wenzelm
parents: 65139
diff changeset
     1
/*  Title:      Pure/Tools/spell_checker.scala
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     3
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     4
Spell checker with completion, based on JOrtho (see
68224
1f7308050349 prefer HTTPS;
wenzelm
parents: 67014
diff changeset
     5
https://sourceforge.net/projects/jortho).
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     6
*/
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     7
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     8
package isabelle
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
     9
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    10
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    11
import java.lang.Class
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73367
diff changeset
    12
import java.util.{List => JList}
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    13
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    14
import scala.collection.mutable
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    15
import scala.annotation.tailrec
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    16
import scala.collection.immutable.SortedMap
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    17
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    18
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    19
object Spell_Checker
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    20
{
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    21
  /* words within text */
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    22
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    23
  def marked_words(base: Text.Offset, text: String, mark: Text.Info[String] => Boolean)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    24
    : List[Text.Info[String]] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    25
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    26
    val result = new mutable.ListBuffer[Text.Info[String]]
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    27
    var offset = 0
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    28
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    29
    def apostrophe(c: Int): Boolean =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    30
      c == '\'' && (offset + 1 == text.length || text(offset + 1) != '\'')
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    31
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
    32
    @tailrec def scan(pred: Int => Boolean): Unit =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    33
    {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    34
      if (offset < text.length) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    35
        val c = text.codePointAt(offset)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    36
        if (pred(c)) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    37
          offset += Character.charCount(c)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    38
          scan(pred)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    39
        }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    40
      }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    41
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    42
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    43
    while (offset < text.length) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    44
      scan(c => !Character.isLetter(c))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    45
      val start = offset
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    46
      scan(c => Character.isLetterOrDigit(c) || apostrophe(c))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    47
      val stop = offset
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    48
      if (stop - start >= 2) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    49
        val info = Text.Info(Text.Range(base + start, base + stop), text.substring(start, stop))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    50
        if (mark(info)) result += info
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    51
      }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    52
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    53
    result.toList
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    54
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    55
66116
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    56
  def current_word(rendering: Rendering, range: Text.Range): Option[Text.Info[String]] =
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    57
  {
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    58
    for {
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    59
      spell_range <- rendering.spell_checker_point(range)
72856
3a27e6f83ce1 clarified signature;
wenzelm
parents: 72375
diff changeset
    60
      text <- rendering.get_text(spell_range)
66116
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    61
      info <- marked_words(spell_range.start, text, info => info.range.overlaps(range)).headOption
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    62
    } yield info
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    63
  }
dad409cd3423 clarified modules;
wenzelm
parents: 65999
diff changeset
    64
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    65
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    66
  /* dictionaries */
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    67
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    68
  class Dictionary private[Spell_Checker](val path: Path)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    69
  {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    70
    val lang: String = path.drop_ext.file_name
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    71
    val user_path: Path = Path.explode("$ISABELLE_HOME_USER/dictionaries") + Path.basic(lang)
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    72
    override def toString: String = lang
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    73
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    74
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    75
  private object Decl
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    76
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    77
    def apply(name: String, include: Boolean): String =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    78
      if (include) name else "-" + name
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    79
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    80
    def unapply(decl: String): Option[(String, Boolean)] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    81
    {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    82
      val decl1 = decl.trim
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    83
      if (decl1 == "" || decl1.startsWith("#")) None
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    84
      else
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    85
        Library.try_unprefix("-", decl1.trim) match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    86
          case None => Some((decl1, true))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    87
          case Some(decl2) => Some((decl2, false))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    88
        }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    89
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    90
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    91
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
    92
  def dictionaries: List[Dictionary] =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    93
    for {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    94
      path <- Path.split(Isabelle_System.getenv("JORTHO_DICTIONARIES"))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    95
      if path.is_file
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    96
    } yield new Dictionary(path)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    97
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    98
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
    99
  /* create spell checker */
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   100
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   101
  def apply(dictionary: Dictionary): Spell_Checker = new Spell_Checker(dictionary)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   102
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   103
  private sealed case class Update(include: Boolean, permanent: Boolean)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   104
}
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   105
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   106
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   107
class Spell_Checker private(dictionary: Spell_Checker.Dictionary)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   108
{
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   109
  override def toString: String = dictionary.toString
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   110
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   111
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   112
  /* main dictionary content */
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   113
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   114
  private var dict = new Object
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   115
  private var updates = SortedMap.empty[String, Spell_Checker.Update]
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   116
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   117
  private def included_iterator(): Iterator[String] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   118
    for {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   119
      (word, upd) <- updates.iterator
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   120
      if upd.include
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   121
    } yield word
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   122
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   123
  private def excluded(word: String): Boolean =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   124
    updates.get(word) match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   125
      case Some(upd) => !upd.include
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   126
      case None => false
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   127
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   128
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
   129
  private def load(): Unit =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   130
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   131
    val main_dictionary = split_lines(File.read_gzip(dictionary.path))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   132
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   133
    val permanent_updates =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   134
      if (dictionary.user_path.is_file)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   135
        for {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   136
          Spell_Checker.Decl(word, include) <- split_lines(File.read(dictionary.user_path))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   137
        } yield (word, Spell_Checker.Update(include, true))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   138
      else Nil
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   140
    updates =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   141
      updates -- (for ((name, upd) <- updates.iterator; if upd.permanent) yield name) ++
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   142
        permanent_updates
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   143
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   144
    val factory_class = Class.forName("com.inet.jortho.DictionaryFactory")
72973
fc69884a6e5a clarified modules;
wenzelm
parents: 72856
diff changeset
   145
    val factory = Untyped.constructor(factory_class).newInstance()
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   146
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   147
    val add = Untyped.method(factory_class, "add", classOf[String])
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   148
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   149
    for {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   150
      word <- main_dictionary.iterator ++ included_iterator()
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   151
      if !excluded(word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   152
    } add.invoke(factory, word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   153
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   154
    dict = Untyped.method(factory_class, "create").invoke(factory)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   155
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   156
  load()
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   157
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
   158
  private def save(): Unit =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   159
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   160
    val permanent_decls =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   161
      (for {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   162
        (word, upd) <- updates.iterator
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   163
        if upd.permanent
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   164
      } yield Spell_Checker.Decl(word, upd.include)).toList
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   165
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   166
    if (permanent_decls.nonEmpty || dictionary.user_path.is_file) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   167
      val header = """# User updates for spell-checker dictionary
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   168
#
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   169
#   * each line contains at most one word
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   170
#   * extra blanks are ignored
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   171
#   * lines starting with "#" are stripped
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   172
#   * lines starting with "-" indicate excluded words
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   173
#
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   174
#:mode=text:encoding=UTF-8:
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   175
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   176
"""
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 71735
diff changeset
   177
      Isabelle_System.make_directory(dictionary.user_path.expand.dir)
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   178
      File.write(dictionary.user_path, header + cat_lines(permanent_decls))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   179
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   180
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   181
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
   182
  def update(word: String, include: Boolean, permanent: Boolean): Unit =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   183
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   184
    updates += (word -> Spell_Checker.Update(include, permanent))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   185
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   186
    if (include) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   187
      if (permanent) save()
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   188
      Untyped.method(dict.getClass, "add", classOf[String]).invoke(dict, word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   189
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   190
    else { save(); load() }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   191
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   192
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72973
diff changeset
   193
  def reset(): Unit =
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   194
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   195
    updates = SortedMap.empty
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   196
    load()
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   197
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   198
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   199
  def reset_enabled(): Int =
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69367
diff changeset
   200
    updates.valuesIterator.count(upd => !upd.permanent)
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   201
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   202
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   203
  /* check known words */
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   204
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   205
  def contains(word: String): Boolean =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   206
    Untyped.method(dict.getClass.getSuperclass, "exist", classOf[String]).
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   207
      invoke(dict, word).asInstanceOf[java.lang.Boolean].booleanValue
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   208
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   209
  def check(word: String): Boolean =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   210
    word match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   211
      case Word.Case(c) if c != Word.Lowercase =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   212
        contains(word) || contains(Word.lowercase(word))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   213
      case _ =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   214
        contains(word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   215
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   216
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   217
  def marked_words(base: Text.Offset, text: String): List[Text.Info[String]] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   218
    Spell_Checker.marked_words(base, text, info => !check(info.info))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   219
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   220
66117
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   221
  /* completion: suggestions for unknown words */
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   222
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   223
  private def suggestions(word: String): Option[List[String]] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   224
  {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   225
    val res =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   226
      Untyped.method(dict.getClass.getSuperclass, "searchSuggestions", classOf[String]).
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73367
diff changeset
   227
        invoke(dict, word).asInstanceOf[JList[AnyRef]].toArray.toList.map(_.toString)
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   228
    if (res.isEmpty) None else Some(res)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   229
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   230
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   231
  def complete(word: String): List[String] =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   232
    if (check(word)) Nil
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   233
    else {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   234
      val word_case = Word.Case.unapply(word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   235
      def recover_case(s: String) =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   236
        word_case match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   237
          case Some(c) => Word.Case(c, s)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   238
          case None => s
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   239
        }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   240
      val result =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   241
        word_case match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   242
          case Some(c) if c != Word.Lowercase =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   243
            suggestions(word) orElse suggestions(Word.lowercase(word))
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   244
          case _ =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   245
            suggestions(word)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   246
        }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   247
      result.getOrElse(Nil).map(recover_case)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   248
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   249
66117
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   250
  def completion(rendering: Rendering, caret: Text.Offset): Option[Completion.Result] =
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   251
  {
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   252
    val caret_range = rendering.before_caret_range(caret)
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   253
    for {
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   254
      word <- Spell_Checker.current_word(rendering, caret_range)
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   255
      words = complete(word.info)
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   256
      if words.nonEmpty
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   257
      descr = "(from dictionary " + quote(dictionary.toString) + ")"
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   258
      items =
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   259
        words.map(w => Completion.Item(word.range, word.info, "", List(w, descr), w, 0, false))
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   260
    } yield Completion.Result(word.range, word.info, false, items)
e6f808d1307c tuned signature;
wenzelm
parents: 66116
diff changeset
   261
  }
65139
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   262
}
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   263
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   264
class Spell_Checker_Variable
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   265
{
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   266
  private val no_spell_checker: (String, Option[Spell_Checker]) = ("", None)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   267
  private var current_spell_checker = no_spell_checker
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   268
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   269
  def get: Option[Spell_Checker] = synchronized { current_spell_checker._2 }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   270
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   271
  def update(options: Options): Unit = synchronized {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   272
    if (options.bool("spell_checker")) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   273
      val lang = options.string("spell_checker_dictionary")
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   274
      if (current_spell_checker._1 != lang) {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   275
        Spell_Checker.dictionaries.find(_.lang == lang) match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   276
          case Some(dictionary) =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   277
            val spell_checker =
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   278
              Exn.capture { Spell_Checker(dictionary) } match {
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   279
                case Exn.Res(spell_checker) => Some(spell_checker)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   280
                case Exn.Exn(_) => None
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   281
              }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   282
            current_spell_checker = (lang, spell_checker)
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   283
          case None =>
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   284
            current_spell_checker = no_spell_checker
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   285
        }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   286
      }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   287
    }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   288
    else current_spell_checker = no_spell_checker
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   289
  }
0a2c0712e432 clarified modules: spell-checker in Pure;
wenzelm
parents:
diff changeset
   290
}