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