src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Sat May 03 23:15:00 2014 +0200 (2014-05-03 ago)
changeset 56844 52e5bf245b2a
parent 56843 b2bfcd8cda80
child 56863 5fdb61a9a010
permissions -rw-r--r--
standardize to implode_short form;
clarified treatment of directories;
wenzelm@53023
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
wenzelm@53023
     2
    Author:     Makarius
wenzelm@53023
     3
wenzelm@53246
     4
Completion popup.
wenzelm@53023
     5
*/
wenzelm@53023
     6
wenzelm@53023
     7
package isabelle.jedit
wenzelm@53023
     8
wenzelm@53023
     9
wenzelm@53023
    10
import isabelle._
wenzelm@53023
    11
wenzelm@53297
    12
import java.awt.{Color, Font, Point, BorderLayout, Dimension}
wenzelm@53784
    13
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
wenzelm@56843
    14
import java.io.{File => JFile}
wenzelm@56843
    15
import java.util.regex.Pattern
wenzelm@53246
    16
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities}
wenzelm@53297
    17
import javax.swing.border.LineBorder
wenzelm@53848
    18
import javax.swing.text.DefaultCaret
wenzelm@53023
    19
wenzelm@53023
    20
import scala.swing.{ListView, ScrollPane}
wenzelm@53023
    21
import scala.swing.event.MouseClicked
wenzelm@53023
    22
wenzelm@53784
    23
import org.gjt.sp.jedit.View
wenzelm@56325
    24
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea, Selection}
wenzelm@53784
    25
import org.gjt.sp.jedit.gui.{HistoryTextField, KeyEventWorkaround}
wenzelm@56843
    26
import org.gjt.sp.util.StandardUtilities
wenzelm@53023
    27
wenzelm@53023
    28
wenzelm@53023
    29
object Completion_Popup
wenzelm@53023
    30
{
wenzelm@55978
    31
  /** items with HTML rendering **/
wenzelm@55978
    32
wenzelm@55978
    33
  private class Item(val item: Completion.Item)
wenzelm@55978
    34
  {
wenzelm@55978
    35
    private val html =
wenzelm@55978
    36
      item.description match {
wenzelm@55978
    37
        case a :: bs =>
wenzelm@55978
    38
          "<html><b>" + HTML.encode(a) + "</b>" +
wenzelm@55978
    39
            HTML.encode(bs.map(" " + _).mkString) + "</html>"
wenzelm@55978
    40
        case Nil => ""
wenzelm@55978
    41
      }
wenzelm@55978
    42
    override def toString: String = html
wenzelm@55978
    43
  }
wenzelm@55978
    44
wenzelm@55978
    45
wenzelm@55978
    46
wenzelm@53784
    47
  /** jEdit text area **/
wenzelm@53235
    48
wenzelm@53242
    49
  object Text_Area
wenzelm@53233
    50
  {
wenzelm@53274
    51
    private val key = new Object
wenzelm@53274
    52
wenzelm@55712
    53
    def apply(text_area: TextArea): Option[Completion_Popup.Text_Area] =
wenzelm@55712
    54
    {
wenzelm@56662
    55
      Swing_Thread.require {}
wenzelm@53274
    56
      text_area.getClientProperty(key) match {
wenzelm@53274
    57
        case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
wenzelm@53274
    58
        case _ => None
wenzelm@53274
    59
      }
wenzelm@55712
    60
    }
wenzelm@53274
    61
wenzelm@55712
    62
    def active_range(text_area: TextArea): Option[Text.Range] =
wenzelm@55712
    63
      apply(text_area) match {
wenzelm@55712
    64
        case Some(text_area_completion) => text_area_completion.active_range
wenzelm@55712
    65
        case None => None
wenzelm@55711
    66
      }
wenzelm@55711
    67
wenzelm@56586
    68
    def action(text_area: TextArea, word_only: Boolean): Boolean =
wenzelm@56170
    69
      apply(text_area) match {
wenzelm@56170
    70
        case Some(text_area_completion) =>
wenzelm@56170
    71
          if (text_area_completion.active_range.isDefined)
wenzelm@56586
    72
            text_area_completion.action(word_only = word_only)
wenzelm@56177
    73
          else
wenzelm@56586
    74
            text_area_completion.action(immediate = true, explicit = true, word_only = word_only)
wenzelm@56170
    75
          true
wenzelm@56170
    76
        case None => false
wenzelm@56170
    77
      }
wenzelm@56170
    78
wenzelm@53274
    79
    def exit(text_area: JEditTextArea)
wenzelm@53274
    80
    {
wenzelm@56662
    81
      Swing_Thread.require {}
wenzelm@53274
    82
      apply(text_area) match {
wenzelm@53274
    83
        case None =>
wenzelm@53274
    84
        case Some(text_area_completion) =>
wenzelm@53274
    85
          text_area_completion.deactivate()
wenzelm@53274
    86
          text_area.putClientProperty(key, null)
wenzelm@53274
    87
      }
wenzelm@53274
    88
    }
wenzelm@53274
    89
wenzelm@53274
    90
    def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
wenzelm@53274
    91
    {
wenzelm@53274
    92
      exit(text_area)
wenzelm@53274
    93
      val text_area_completion = new Text_Area(text_area)
wenzelm@53274
    94
      text_area.putClientProperty(key, text_area_completion)
wenzelm@53274
    95
      text_area_completion.activate()
wenzelm@53274
    96
      text_area_completion
wenzelm@53274
    97
    }
wenzelm@53274
    98
wenzelm@55712
    99
    def dismissed(text_area: TextArea): Boolean =
wenzelm@53274
   100
    {
wenzelm@56662
   101
      Swing_Thread.require {}
wenzelm@53274
   102
      apply(text_area) match {
wenzelm@53274
   103
        case Some(text_area_completion) => text_area_completion.dismissed()
wenzelm@53274
   104
        case None => false
wenzelm@53274
   105
      }
wenzelm@53274
   106
    }
wenzelm@53272
   107
  }
wenzelm@53272
   108
wenzelm@53274
   109
  class Text_Area private(text_area: JEditTextArea)
wenzelm@53272
   110
  {
wenzelm@55711
   111
    // owned by Swing thread
wenzelm@53272
   112
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53272
   113
wenzelm@55711
   114
    def active_range: Option[Text.Range] =
wenzelm@55711
   115
      completion_popup match {
wenzelm@56605
   116
        case Some(completion) => completion.active_range
wenzelm@55711
   117
        case None => None
wenzelm@55711
   118
      }
wenzelm@55711
   119
wenzelm@53272
   120
wenzelm@55747
   121
    /* rendering */
wenzelm@55747
   122
wenzelm@55747
   123
    def rendering(rendering: Rendering, line_range: Text.Range): Option[Text.Info[Color]] =
wenzelm@55747
   124
    {
wenzelm@55747
   125
      active_range match {
wenzelm@55747
   126
        case Some(range) => range.try_restrict(line_range)
wenzelm@55747
   127
        case None =>
wenzelm@55767
   128
          if (line_range.contains(text_area.getCaretPosition)) {
wenzelm@56593
   129
            JEdit_Lib.before_caret_range(text_area, rendering).try_restrict(line_range) match {
wenzelm@55747
   130
              case Some(range) if !range.is_singularity =>
wenzelm@56173
   131
                rendering.semantic_completion(range) match {
wenzelm@56175
   132
                  case Some(Text.Info(_, Completion.No_Completion)) => None
wenzelm@56175
   133
                  case Some(Text.Info(range1, _: Completion.Names)) => Some(range1)
wenzelm@55747
   134
                  case None =>
wenzelm@56175
   135
                    syntax_completion(Completion.History.empty, false, Some(rendering)).map(_.range)
wenzelm@55747
   136
                }
wenzelm@55747
   137
              case _ => None
wenzelm@55747
   138
            }
wenzelm@55747
   139
          }
wenzelm@55747
   140
          else None
wenzelm@55747
   141
      }
wenzelm@55747
   142
    }.map(range => Text.Info(range, rendering.completion_color))
wenzelm@55747
   143
wenzelm@55747
   144
wenzelm@55747
   145
    /* syntax completion */
wenzelm@55747
   146
wenzelm@55747
   147
    def syntax_completion(
wenzelm@56175
   148
      history: Completion.History,
wenzelm@56175
   149
      explicit: Boolean,
wenzelm@56175
   150
      opt_rendering: Option[Rendering]): Option[Completion.Result] =
wenzelm@55747
   151
    {
wenzelm@55747
   152
      val buffer = text_area.getBuffer
wenzelm@55747
   153
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55747
   154
wenzelm@55747
   155
      Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@55747
   156
        case Some(syntax) =>
wenzelm@55747
   157
          val context =
wenzelm@56587
   158
            (for {
wenzelm@56587
   159
              rendering <- opt_rendering
wenzelm@56842
   160
              if PIDE.options.bool("jedit_completion_context")
wenzelm@56593
   161
              range = JEdit_Lib.before_caret_range(text_area, rendering)
wenzelm@56587
   162
              context <- rendering.language_context(range)
wenzelm@56587
   163
            } yield context) getOrElse syntax.language_context
wenzelm@55747
   164
wenzelm@56589
   165
          val caret = text_area.getCaretPosition
wenzelm@56589
   166
          val line_range = JEdit_Lib.line_range(buffer, text_area.getCaretLine)
wenzelm@56589
   167
          val line_start = line_range.start
wenzelm@56589
   168
          for {
wenzelm@56589
   169
            line_text <- JEdit_Lib.try_get_text(buffer, line_range)
wenzelm@56589
   170
            result <-
wenzelm@56589
   171
              syntax.completion.complete(
wenzelm@56589
   172
                history, decode, explicit, line_start, line_text, caret - line_start, false, context)
wenzelm@56589
   173
          } yield result
wenzelm@55747
   174
wenzelm@55747
   175
        case None => None
wenzelm@55747
   176
      }
wenzelm@55747
   177
    }
wenzelm@55747
   178
wenzelm@55747
   179
wenzelm@56564
   180
    /* spell-checker completion */
wenzelm@56564
   181
wenzelm@56564
   182
    def spell_checker_completion(rendering: Rendering): Option[Completion.Result] =
wenzelm@56587
   183
    {
wenzelm@56587
   184
      for {
wenzelm@56587
   185
        spell_checker <- PIDE.spell_checker.get
wenzelm@56593
   186
        range = JEdit_Lib.before_caret_range(text_area, rendering)
wenzelm@56593
   187
        word <- Spell_Checker.current_word(text_area, rendering, range)
wenzelm@56593
   188
        words = spell_checker.complete(word.info)
wenzelm@56587
   189
        if !words.isEmpty
wenzelm@56587
   190
        descr = "(from dictionary " + quote(spell_checker.toString) + ")"
wenzelm@56593
   191
        items =
wenzelm@56593
   192
          words.map(w => Completion.Item(word.range, word.info, "", List(w, descr), w, 0, false))
wenzelm@56593
   193
      } yield Completion.Result(word.range, word.info, false, items)
wenzelm@56587
   194
    }
wenzelm@56564
   195
wenzelm@56564
   196
wenzelm@56843
   197
    /* path completion */
wenzelm@56843
   198
wenzelm@56843
   199
    def path_completion(rendering: Rendering): Option[Completion.Result] =
wenzelm@56843
   200
    {
wenzelm@56844
   201
      def complete(text: String): List[(String, List[String])] =
wenzelm@56843
   202
      {
wenzelm@56843
   203
        try {
wenzelm@56843
   204
          val path = Path.explode(text)
wenzelm@56843
   205
          val (dir, base_name) =
wenzelm@56843
   206
            if (text == "" || text.endsWith("/")) (path, "")
wenzelm@56843
   207
            else (path.dir, path.base.implode)
wenzelm@56843
   208
wenzelm@56843
   209
          val directory =
wenzelm@56843
   210
            new JFile(PIDE.resources.append(rendering.snapshot.node_name.master_dir, dir))
wenzelm@56843
   211
          val files = directory.listFiles
wenzelm@56843
   212
          if (files == null) Nil
wenzelm@56843
   213
          else {
wenzelm@56843
   214
            val ignore =
wenzelm@56843
   215
              Library.space_explode(':', PIDE.options.string("jedit_completion_path_ignore")).
wenzelm@56843
   216
                map(s => Pattern.compile(StandardUtilities.globToRE(s)))
wenzelm@56843
   217
            (for {
wenzelm@56843
   218
              file <- files.iterator
wenzelm@56844
   219
wenzelm@56843
   220
              name = file.getName
wenzelm@56844
   221
              if name.startsWith(base_name)
wenzelm@56843
   222
              if !ignore.exists(pat => pat.matcher(name).matches)
wenzelm@56844
   223
wenzelm@56844
   224
              text1 = (dir + Path.basic(name)).implode_short
wenzelm@56844
   225
              if text != text1
wenzelm@56844
   226
wenzelm@56844
   227
              is_dir = new JFile(directory, name).isDirectory
wenzelm@56844
   228
              replacement = text1 + (if (is_dir) "/" else "")
wenzelm@56844
   229
              descr = List(text1, if (is_dir) "(directory)" else "(file)")
wenzelm@56844
   230
            } yield (replacement, descr)).take(PIDE.options.int("completion_limit")).toList
wenzelm@56843
   231
          }
wenzelm@56843
   232
        }
wenzelm@56843
   233
        catch { case ERROR(_) => Nil }
wenzelm@56843
   234
      }
wenzelm@56843
   235
wenzelm@56843
   236
      for {
wenzelm@56843
   237
        r1 <- rendering.language_path(JEdit_Lib.before_caret_range(text_area, rendering))
wenzelm@56843
   238
        s1 <- JEdit_Lib.try_get_text(text_area.getBuffer, r1)
wenzelm@56843
   239
        s2 <- Library.try_unquote(s1)
wenzelm@56843
   240
        r2 = Text.Range(r1.start + 1, r1.stop - 1)
wenzelm@56843
   241
        if Path.is_valid(s2)
wenzelm@56843
   242
        paths = complete(s2)
wenzelm@56843
   243
        if !paths.isEmpty
wenzelm@56844
   244
        items = paths.map(p => Completion.Item(r2, s2, "", p._2, p._1, 0, false))
wenzelm@56843
   245
      } yield Completion.Result(r2, s2, false, items)
wenzelm@56843
   246
    }
wenzelm@56843
   247
wenzelm@56843
   248
wenzelm@56170
   249
    /* completion action: text area */
wenzelm@53273
   250
wenzelm@53275
   251
    private def insert(item: Completion.Item)
wenzelm@53242
   252
    {
wenzelm@56662
   253
      Swing_Thread.require {}
wenzelm@53023
   254
wenzelm@53242
   255
      val buffer = text_area.getBuffer
wenzelm@55692
   256
      val range = item.range
wenzelm@55692
   257
      if (buffer.isEditable && range.length > 0) {
wenzelm@53242
   258
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@55692
   259
          JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@53242
   260
            case Some(text) if text == item.original =>
wenzelm@56325
   261
              text_area.getSelectionAtOffset(text_area.getCaretPosition) match {
wenzelm@56325
   262
wenzelm@56325
   263
                /*rectangular selection as "tall caret"*/
wenzelm@56325
   264
                case selection : Selection.Rect
wenzelm@56325
   265
                if selection.getStart(buffer, text_area.getCaretLine) == range.stop =>
wenzelm@56325
   266
                  text_area.moveCaretPosition(range.stop)
wenzelm@56325
   267
                  (0 until Character.codePointCount(item.original, 0, item.original.length))
wenzelm@56325
   268
                    .foreach(_ => text_area.backspace())
wenzelm@56325
   269
                  text_area.setSelectedText(selection, item.replacement)
wenzelm@56325
   270
                  text_area.moveCaretPosition(text_area.getCaretPosition + item.move)
wenzelm@56325
   271
wenzelm@56325
   272
                /*other selections: rectangular, multiple range, ...*/
wenzelm@56325
   273
                case selection
wenzelm@56325
   274
                if selection != null &&
wenzelm@56325
   275
                    selection.getStart(buffer, text_area.getCaretLine) == range.start &&
wenzelm@56325
   276
                    selection.getEnd(buffer, text_area.getCaretLine) == range.stop =>
wenzelm@56325
   277
                  text_area.moveCaretPosition(range.stop + item.move)
wenzelm@56325
   278
                  text_area.getSelection.foreach(text_area.setSelectedText(_, item.replacement))
wenzelm@56325
   279
wenzelm@56325
   280
                /*no selection*/
wenzelm@56325
   281
                case _ =>
wenzelm@56325
   282
                  buffer.remove(range.start, range.length)
wenzelm@56325
   283
                  buffer.insert(range.start, item.replacement)
wenzelm@56325
   284
                  text_area.moveCaretPosition(range.start + item.replacement.length + item.move)
wenzelm@56325
   285
              }
wenzelm@53242
   286
            case _ =>
wenzelm@53242
   287
          }
wenzelm@53233
   288
        }
wenzelm@53233
   289
      }
wenzelm@53233
   290
    }
wenzelm@53242
   291
wenzelm@56586
   292
    def action(
wenzelm@56586
   293
      immediate: Boolean = false,
wenzelm@56586
   294
      explicit: Boolean = false,
wenzelm@56586
   295
      delayed: Boolean = false,
wenzelm@56586
   296
      word_only: Boolean = false): Boolean =
wenzelm@53292
   297
    {
wenzelm@53292
   298
      val view = text_area.getView
wenzelm@53292
   299
      val layered = view.getLayeredPane
wenzelm@53292
   300
      val buffer = text_area.getBuffer
wenzelm@53292
   301
      val painter = text_area.getPainter
wenzelm@53273
   302
wenzelm@55693
   303
      val history = PIDE.completion_history.value
wenzelm@55693
   304
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55693
   305
wenzelm@55693
   306
      def open_popup(result: Completion.Result)
wenzelm@55693
   307
      {
wenzelm@55693
   308
        val font =
wenzelm@55825
   309
          painter.getFont.deriveFont(
wenzelm@55825
   310
            Font_Info.main_size(PIDE.options.real("jedit_popup_font_scale")))
wenzelm@55693
   311
wenzelm@55711
   312
        val range = result.range
wenzelm@55711
   313
wenzelm@55711
   314
        val loc1 = text_area.offsetToXY(range.start)
wenzelm@55693
   315
        if (loc1 != null) {
wenzelm@55693
   316
          val loc2 =
wenzelm@55693
   317
            SwingUtilities.convertPoint(painter,
wenzelm@55693
   318
              loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@55693
   319
wenzelm@55978
   320
          val items = result.items.map(new Item(_))
wenzelm@55693
   321
          val completion =
wenzelm@56197
   322
            new Completion_Popup(Some(range), layered, loc2, font, items)
wenzelm@55978
   323
            {
wenzelm@55693
   324
              override def complete(item: Completion.Item) {
wenzelm@55693
   325
                PIDE.completion_history.update(item)
wenzelm@55693
   326
                insert(item)
wenzelm@55693
   327
              }
wenzelm@55693
   328
              override def propagate(evt: KeyEvent) {
wenzelm@56840
   329
                if (view.getKeyEventInterceptor == null)
wenzelm@56840
   330
                  JEdit_Lib.propagate_key(view, evt)
wenzelm@56840
   331
                else if (view.getKeyEventInterceptor == inner_key_listener) {
wenzelm@56197
   332
                  try {
wenzelm@56197
   333
                    view.setKeyEventInterceptor(null)
wenzelm@56197
   334
                    JEdit_Lib.propagate_key(view, evt)
wenzelm@56197
   335
                  }
wenzelm@56197
   336
                  finally {
wenzelm@56197
   337
                    if (isDisplayable) view.setKeyEventInterceptor(inner_key_listener)
wenzelm@56197
   338
                  }
wenzelm@56197
   339
                }
wenzelm@56171
   340
                if (evt.getID == KeyEvent.KEY_TYPED) input(evt)
wenzelm@55693
   341
              }
wenzelm@56197
   342
              override def shutdown(focus: Boolean) {
wenzelm@56197
   343
                if (view.getKeyEventInterceptor == inner_key_listener)
wenzelm@56197
   344
                  view.setKeyEventInterceptor(null)
wenzelm@56197
   345
                if (focus) text_area.requestFocus
wenzelm@56606
   346
                JEdit_Lib.invalidate_range(text_area, range)
wenzelm@56197
   347
              }
wenzelm@55693
   348
            }
wenzelm@55693
   349
          completion_popup = Some(completion)
wenzelm@56197
   350
          view.setKeyEventInterceptor(completion.inner_key_listener)
wenzelm@56606
   351
          JEdit_Lib.invalidate_range(text_area, range)
wenzelm@56841
   352
          Pretty_Tooltip.dismissed_all()
wenzelm@56197
   353
          completion.show_popup(false)
wenzelm@55693
   354
        }
wenzelm@55693
   355
      }
wenzelm@55693
   356
wenzelm@56175
   357
      if (buffer.isEditable) {
wenzelm@56175
   358
        val (no_completion, semantic_completion, opt_rendering) =
wenzelm@56175
   359
        {
wenzelm@56175
   360
          PIDE.document_view(text_area) match {
wenzelm@56175
   361
            case Some(doc_view) =>
wenzelm@56175
   362
              val rendering = doc_view.get_rendering()
wenzelm@56175
   363
              val (no_completion, result) =
wenzelm@56593
   364
              {
wenzelm@56593
   365
                val caret_range = JEdit_Lib.before_caret_range(text_area, rendering)
wenzelm@56593
   366
                rendering.semantic_completion(caret_range) match {
wenzelm@56593
   367
                  case Some(Text.Info(_, Completion.No_Completion)) => (true, None)
wenzelm@56593
   368
                  case Some(Text.Info(range, names: Completion.Names)) =>
wenzelm@56593
   369
                    val result =
wenzelm@56593
   370
                      JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@56593
   371
                        case Some(original) => names.complete(range, history, decode, original)
wenzelm@56593
   372
                        case None => None
wenzelm@56593
   373
                      }
wenzelm@56593
   374
                    (false, result)
wenzelm@56587
   375
                  case None => (false, None)
wenzelm@56175
   376
                }
wenzelm@56593
   377
              }
wenzelm@56175
   378
              (no_completion, result, Some(rendering))
wenzelm@56587
   379
            case None => (false, None, None)
wenzelm@56175
   380
          }
wenzelm@55693
   381
        }
wenzelm@56326
   382
        if (no_completion) false
wenzelm@56326
   383
        else {
wenzelm@56564
   384
          val result =
wenzelm@56586
   385
          {
wenzelm@56586
   386
            val result0 =
wenzelm@56586
   387
              if (word_only) None
wenzelm@56586
   388
              else
wenzelm@56587
   389
                Completion.Result.merge(history, semantic_completion,
wenzelm@56587
   390
                  syntax_completion(history, explicit, opt_rendering))
wenzelm@56564
   391
            opt_rendering match {
wenzelm@56564
   392
              case None => result0
wenzelm@56564
   393
              case Some(rendering) =>
wenzelm@56843
   394
                Completion.Result.merge(history, result0,
wenzelm@56843
   395
                  Completion.Result.merge(history,
wenzelm@56843
   396
                    spell_checker_completion(rendering), path_completion(rendering)))
wenzelm@56564
   397
            }
wenzelm@56586
   398
          }
wenzelm@56175
   399
          result match {
wenzelm@56175
   400
            case Some(result) =>
wenzelm@56175
   401
              result.items match {
wenzelm@56175
   402
                case List(item) if result.unique && item.immediate && immediate =>
wenzelm@56175
   403
                  insert(item)
wenzelm@56326
   404
                  true
wenzelm@56326
   405
                case _ :: _ if !delayed =>
wenzelm@56175
   406
                  open_popup(result)
wenzelm@56326
   407
                  false
wenzelm@56326
   408
                case _ => false
wenzelm@56175
   409
              }
wenzelm@56326
   410
            case None => false
wenzelm@56175
   411
          }
wenzelm@53292
   412
        }
wenzelm@55752
   413
      }
wenzelm@56326
   414
      else false
wenzelm@53292
   415
    }
wenzelm@53292
   416
wenzelm@53292
   417
wenzelm@53292
   418
    /* input key events */
wenzelm@53273
   419
wenzelm@53272
   420
    def input(evt: KeyEvent)
wenzelm@53242
   421
    {
wenzelm@56662
   422
      Swing_Thread.require {}
wenzelm@53233
   423
wenzelm@53273
   424
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   425
        if (!evt.isConsumed) {
wenzelm@53273
   426
          dismissed()
wenzelm@53397
   427
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   428
            val special = JEdit_Lib.special_key(evt)
wenzelm@56326
   429
            val immediate = PIDE.options.bool("jedit_completion_immediate")
wenzelm@53397
   430
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   431
              input_delay.revoke()
wenzelm@56326
   432
              action(immediate = immediate)
wenzelm@53397
   433
            }
wenzelm@56326
   434
            else {
wenzelm@56326
   435
              if (!special && action(immediate = immediate, delayed = true))
wenzelm@56326
   436
                input_delay.revoke()
wenzelm@56326
   437
              else
wenzelm@56326
   438
                input_delay.invoke()
wenzelm@56326
   439
            }
wenzelm@53292
   440
          }
wenzelm@53242
   441
        }
wenzelm@53228
   442
      }
wenzelm@53228
   443
    }
wenzelm@53273
   444
wenzelm@53273
   445
    private val input_delay =
wenzelm@53292
   446
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   447
        action()
wenzelm@53292
   448
      }
wenzelm@53273
   449
wenzelm@53273
   450
wenzelm@53292
   451
    /* dismiss popup */
wenzelm@53273
   452
wenzelm@53292
   453
    def dismissed(): Boolean =
wenzelm@53292
   454
    {
wenzelm@56662
   455
      Swing_Thread.require {}
wenzelm@53273
   456
wenzelm@53292
   457
      completion_popup match {
wenzelm@53292
   458
        case Some(completion) =>
wenzelm@53292
   459
          completion.hide_popup()
wenzelm@53292
   460
          completion_popup = None
wenzelm@53292
   461
          true
wenzelm@53292
   462
        case None =>
wenzelm@53292
   463
          false
wenzelm@53273
   464
      }
wenzelm@53292
   465
    }
wenzelm@53274
   466
wenzelm@53274
   467
wenzelm@53274
   468
    /* activation */
wenzelm@53274
   469
wenzelm@53274
   470
    private val outer_key_listener =
wenzelm@53274
   471
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   472
wenzelm@53274
   473
    private def activate()
wenzelm@53274
   474
    {
wenzelm@53274
   475
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   476
    }
wenzelm@53274
   477
wenzelm@53274
   478
    private def deactivate()
wenzelm@53274
   479
    {
wenzelm@53274
   480
      dismissed()
wenzelm@53274
   481
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   482
    }
wenzelm@53228
   483
  }
wenzelm@53784
   484
wenzelm@53784
   485
wenzelm@53784
   486
wenzelm@53784
   487
  /** history text field **/
wenzelm@53784
   488
wenzelm@53784
   489
  class History_Text_Field(
wenzelm@53784
   490
    name: String = null,
wenzelm@53784
   491
    instant_popups: Boolean = false,
wenzelm@53784
   492
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   493
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   494
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   495
  {
wenzelm@53784
   496
    text_field =>
wenzelm@53784
   497
wenzelm@53848
   498
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   499
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   500
wenzelm@55711
   501
    // owned by Swing thread
wenzelm@53784
   502
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   503
wenzelm@53784
   504
wenzelm@53784
   505
    /* dismiss */
wenzelm@53784
   506
wenzelm@53784
   507
    private def dismissed(): Boolean =
wenzelm@53784
   508
    {
wenzelm@53784
   509
      completion_popup match {
wenzelm@53784
   510
        case Some(completion) =>
wenzelm@53784
   511
          completion.hide_popup()
wenzelm@53784
   512
          completion_popup = None
wenzelm@53784
   513
          true
wenzelm@53784
   514
        case None =>
wenzelm@53784
   515
          false
wenzelm@53784
   516
      }
wenzelm@53784
   517
    }
wenzelm@53784
   518
wenzelm@53784
   519
wenzelm@53784
   520
    /* insert */
wenzelm@53784
   521
wenzelm@53784
   522
    private def insert(item: Completion.Item)
wenzelm@53784
   523
    {
wenzelm@56662
   524
      Swing_Thread.require {}
wenzelm@53784
   525
wenzelm@55692
   526
      val range = item.range
wenzelm@55692
   527
      if (text_field.isEditable && range.length > 0) {
wenzelm@53784
   528
        val content = text_field.getText
wenzelm@55692
   529
        JEdit_Lib.try_get_text(content, range) match {
wenzelm@53784
   530
          case Some(text) if text == item.original =>
wenzelm@53784
   531
            text_field.setText(
wenzelm@55692
   532
              content.substring(0, range.start) +
wenzelm@53784
   533
              item.replacement +
wenzelm@55692
   534
              content.substring(range.stop))
wenzelm@55692
   535
            text_field.getCaret.setDot(range.start + item.replacement.length + item.move)
wenzelm@53784
   536
          case _ =>
wenzelm@53784
   537
        }
wenzelm@53784
   538
      }
wenzelm@53784
   539
    }
wenzelm@53784
   540
wenzelm@53784
   541
wenzelm@56170
   542
    /* completion action: text field */
wenzelm@53784
   543
wenzelm@53784
   544
    def action()
wenzelm@53784
   545
    {
wenzelm@53784
   546
      GUI.layered_pane(text_field) match {
wenzelm@53784
   547
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   548
          val history = PIDE.completion_history.value
wenzelm@53784
   549
wenzelm@53784
   550
          val caret = text_field.getCaret.getDot
wenzelm@55813
   551
          val text = text_field.getText
wenzelm@55748
   552
wenzelm@55749
   553
          val context = syntax.language_context
wenzelm@55748
   554
wenzelm@55813
   555
          syntax.completion.complete(history, true, false, 0, text, caret, false, context) match {
wenzelm@53784
   556
            case Some(result) =>
wenzelm@53784
   557
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   558
              val loc =
wenzelm@53784
   559
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   560
wenzelm@55978
   561
              val items = result.items.map(new Item(_))
wenzelm@55711
   562
              val completion =
wenzelm@55978
   563
                new Completion_Popup(None, layered, loc, text_field.getFont, items)
wenzelm@55978
   564
                {
wenzelm@55978
   565
                  override def complete(item: Completion.Item) {
wenzelm@55978
   566
                    PIDE.completion_history.update(item)
wenzelm@55978
   567
                    insert(item)
wenzelm@55978
   568
                  }
wenzelm@55978
   569
                  override def propagate(evt: KeyEvent) {
wenzelm@55978
   570
                    if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@55978
   571
                  }
wenzelm@56197
   572
                  override def shutdown(focus: Boolean) { if (focus) text_field.requestFocus }
wenzelm@53784
   573
                }
wenzelm@53784
   574
              completion_popup = Some(completion)
wenzelm@56197
   575
              completion.show_popup(true)
wenzelm@53784
   576
wenzelm@53784
   577
            case None =>
wenzelm@53784
   578
          }
wenzelm@53784
   579
        case _ =>
wenzelm@53784
   580
      }
wenzelm@53784
   581
    }
wenzelm@53784
   582
wenzelm@53784
   583
wenzelm@53784
   584
    /* process key event */
wenzelm@53784
   585
wenzelm@53784
   586
    private def process(evt: KeyEvent)
wenzelm@53784
   587
    {
wenzelm@53784
   588
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   589
        dismissed()
wenzelm@53784
   590
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   591
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   592
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   593
            process_delay.revoke()
wenzelm@53784
   594
            action()
wenzelm@53784
   595
          }
wenzelm@53784
   596
          else process_delay.invoke()
wenzelm@53784
   597
        }
wenzelm@53784
   598
      }
wenzelm@53784
   599
    }
wenzelm@53784
   600
wenzelm@53784
   601
    private val process_delay =
wenzelm@53784
   602
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   603
        action()
wenzelm@53784
   604
      }
wenzelm@53784
   605
wenzelm@53784
   606
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   607
    {
wenzelm@53784
   608
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   609
      if (evt != null) {
wenzelm@53784
   610
        evt.getID match {
wenzelm@53784
   611
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   612
            val key_code = evt.getKeyCode
wenzelm@53784
   613
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   614
              if (dismissed()) evt.consume
wenzelm@53784
   615
            }
wenzelm@53784
   616
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   617
            super.processKeyEvent(evt)
wenzelm@53784
   618
            process(evt)
wenzelm@53784
   619
            evt.consume
wenzelm@53784
   620
          case _ =>
wenzelm@53784
   621
        }
wenzelm@53784
   622
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   623
      }
wenzelm@53784
   624
    }
wenzelm@53784
   625
  }
wenzelm@53023
   626
}
wenzelm@53023
   627
wenzelm@53233
   628
wenzelm@53023
   629
class Completion_Popup private(
wenzelm@56605
   630
  opt_range: Option[Text.Range],
wenzelm@53246
   631
  layered: JLayeredPane,
wenzelm@53246
   632
  location: Point,
wenzelm@53272
   633
  font: Font,
wenzelm@55978
   634
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   635
{
wenzelm@53023
   636
  completion =>
wenzelm@53023
   637
wenzelm@56662
   638
  Swing_Thread.require {}
wenzelm@53405
   639
wenzelm@53232
   640
  require(!items.isEmpty)
wenzelm@53405
   641
  val multi = items.length > 1
wenzelm@53232
   642
wenzelm@53232
   643
wenzelm@53232
   644
  /* actions */
wenzelm@53232
   645
wenzelm@53275
   646
  def complete(item: Completion.Item) { }
wenzelm@53232
   647
  def propagate(evt: KeyEvent) { }
wenzelm@56197
   648
  def shutdown(focus: Boolean) { }
wenzelm@53023
   649
wenzelm@53023
   650
wenzelm@53023
   651
  /* list view */
wenzelm@53023
   652
wenzelm@53023
   653
  private val list_view = new ListView(items)
wenzelm@53272
   654
  list_view.font = font
wenzelm@53023
   655
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   656
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   657
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   658
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   659
wenzelm@53398
   660
  for (cond <-
wenzelm@53398
   661
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   662
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   663
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   664
wenzelm@53023
   665
  private def complete_selected(): Boolean =
wenzelm@53023
   666
  {
wenzelm@53023
   667
    list_view.selection.items.toList match {
wenzelm@55978
   668
      case item :: _ => complete(item.item); true
wenzelm@53023
   669
      case _ => false
wenzelm@53023
   670
    }
wenzelm@53023
   671
  }
wenzelm@53023
   672
wenzelm@53023
   673
  private def move_items(n: Int)
wenzelm@53023
   674
  {
wenzelm@53023
   675
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   676
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   677
    if (i != j) {
wenzelm@53023
   678
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   679
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   680
    }
wenzelm@53023
   681
  }
wenzelm@53023
   682
wenzelm@53023
   683
  private def move_pages(n: Int)
wenzelm@53023
   684
  {
wenzelm@53023
   685
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   686
    move_items(page_size * n)
wenzelm@53023
   687
  }
wenzelm@53023
   688
wenzelm@53023
   689
wenzelm@53023
   690
  /* event handling */
wenzelm@53023
   691
wenzelm@56197
   692
  val inner_key_listener =
wenzelm@53226
   693
    JEdit_Lib.key_listener(
wenzelm@53226
   694
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   695
        {
wenzelm@53226
   696
          if (!e.isConsumed) {
wenzelm@53226
   697
            e.getKeyCode match {
wenzelm@53226
   698
              case KeyEvent.VK_TAB =>
wenzelm@53226
   699
                if (complete_selected()) e.consume
wenzelm@53226
   700
                hide_popup()
wenzelm@53226
   701
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   702
                hide_popup()
wenzelm@53226
   703
                e.consume
wenzelm@53405
   704
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   705
                move_items(-1)
wenzelm@53407
   706
                e.consume
wenzelm@53405
   707
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   708
                move_items(1)
wenzelm@53407
   709
                e.consume
wenzelm@53405
   710
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   711
                move_pages(-1)
wenzelm@53407
   712
                e.consume
wenzelm@53405
   713
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   714
                move_pages(1)
wenzelm@53407
   715
                e.consume
wenzelm@53226
   716
              case _ =>
wenzelm@53226
   717
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   718
                  hide_popup()
wenzelm@53226
   719
            }
wenzelm@53226
   720
          }
wenzelm@53232
   721
          propagate(e)
wenzelm@53226
   722
        },
wenzelm@53232
   723
      key_typed = propagate _
wenzelm@53226
   724
    )
wenzelm@53023
   725
wenzelm@53274
   726
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   727
wenzelm@53023
   728
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   729
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   730
      if (complete_selected()) e.consume
wenzelm@53023
   731
      hide_popup()
wenzelm@53023
   732
    }
wenzelm@53023
   733
  })
wenzelm@53023
   734
wenzelm@53023
   735
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   736
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   737
  })
wenzelm@53023
   738
wenzelm@53023
   739
wenzelm@53023
   740
  /* main content */
wenzelm@53023
   741
wenzelm@53023
   742
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   743
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   744
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   745
wenzelm@53023
   746
wenzelm@53023
   747
  /* popup */
wenzelm@53023
   748
wenzelm@56605
   749
  def active_range: Option[Text.Range] =
wenzelm@56605
   750
    if (isDisplayable) opt_range else None
wenzelm@56605
   751
wenzelm@53023
   752
  private val popup =
wenzelm@53023
   753
  {
wenzelm@53247
   754
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   755
    val size =
wenzelm@53230
   756
    {
wenzelm@53230
   757
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   758
      val bounds = Rendering.popup_bounds
wenzelm@53247
   759
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   760
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   761
      new Dimension(w, h)
wenzelm@53230
   762
    }
wenzelm@53247
   763
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   764
  }
wenzelm@53246
   765
wenzelm@56197
   766
  private def show_popup(focus: Boolean)
wenzelm@53246
   767
  {
wenzelm@53246
   768
    popup.show
wenzelm@56197
   769
    if (focus) list_view.requestFocus
wenzelm@53023
   770
  }
wenzelm@53023
   771
wenzelm@53232
   772
  private def hide_popup()
wenzelm@53023
   773
  {
wenzelm@56197
   774
    shutdown(list_view.peer.isFocusOwner)
wenzelm@56197
   775
    popup.hide
wenzelm@53023
   776
  }
wenzelm@53023
   777
}
wenzelm@53023
   778