src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Mon Mar 17 23:16:26 2014 +0100 (2014-03-17 ago)
changeset 56197 416f7a00e4cb
parent 56177 bfa9dfb722de
child 56325 ffbfc92e6508
permissions -rw-r--r--
back to KeyEventInterceptor (see 423e29f1f304), but without focus change, which helps to avoid loosing key events due to quick opening and closing of popups;
discontinued obsolete option jedit_completion_dismiss_delay (see 750561986828);
more explicit shutdown;
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@55712
    22
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
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@56170
    65
    def action(text_area: TextArea): 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@56177
    69
            text_area_completion.action()
wenzelm@56177
    70
          else
wenzelm@56170
    71
            text_area_completion.action(immediate = true, explicit = true)
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@55711
   113
        case Some(completion) =>
wenzelm@55711
   114
          completion.active_range match {
wenzelm@56197
   115
            case Some(range) if completion.isDisplayable => Some(range)
wenzelm@55711
   116
            case _ => None
wenzelm@55711
   117
          }
wenzelm@55711
   118
        case None => None
wenzelm@55711
   119
      }
wenzelm@55711
   120
wenzelm@53272
   121
wenzelm@55767
   122
    /* caret */
wenzelm@55767
   123
wenzelm@55767
   124
    def before_caret_range(rendering: Rendering): Text.Range =
wenzelm@55767
   125
    {
wenzelm@55767
   126
      val snapshot = rendering.snapshot
wenzelm@55767
   127
      val former_caret = snapshot.revert(text_area.getCaretPosition)
wenzelm@55767
   128
      snapshot.convert(Text.Range(former_caret - 1, former_caret))
wenzelm@55767
   129
    }
wenzelm@55767
   130
wenzelm@55767
   131
wenzelm@55747
   132
    /* rendering */
wenzelm@55747
   133
wenzelm@55747
   134
    def rendering(rendering: Rendering, line_range: Text.Range): Option[Text.Info[Color]] =
wenzelm@55747
   135
    {
wenzelm@55747
   136
      active_range match {
wenzelm@55747
   137
        case Some(range) => range.try_restrict(line_range)
wenzelm@55747
   138
        case None =>
wenzelm@55747
   139
          val buffer = text_area.getBuffer
wenzelm@55767
   140
          if (line_range.contains(text_area.getCaretPosition)) {
wenzelm@55767
   141
            before_caret_range(rendering).try_restrict(line_range) match {
wenzelm@55747
   142
              case Some(range) if !range.is_singularity =>
wenzelm@56173
   143
                rendering.semantic_completion(range) match {
wenzelm@56175
   144
                  case Some(Text.Info(_, Completion.No_Completion)) => None
wenzelm@56175
   145
                  case Some(Text.Info(range1, _: Completion.Names)) => Some(range1)
wenzelm@55747
   146
                  case None =>
wenzelm@56175
   147
                    syntax_completion(Completion.History.empty, false, Some(rendering)).map(_.range)
wenzelm@55747
   148
                }
wenzelm@55747
   149
              case _ => None
wenzelm@55747
   150
            }
wenzelm@55747
   151
          }
wenzelm@55747
   152
          else None
wenzelm@55747
   153
      }
wenzelm@55747
   154
    }.map(range => Text.Info(range, rendering.completion_color))
wenzelm@55747
   155
wenzelm@55747
   156
wenzelm@55747
   157
    /* syntax completion */
wenzelm@55747
   158
wenzelm@55747
   159
    def syntax_completion(
wenzelm@56175
   160
      history: Completion.History,
wenzelm@56175
   161
      explicit: Boolean,
wenzelm@56175
   162
      opt_rendering: Option[Rendering]): Option[Completion.Result] =
wenzelm@55747
   163
    {
wenzelm@55747
   164
      val buffer = text_area.getBuffer
wenzelm@55747
   165
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55747
   166
wenzelm@55747
   167
      Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@55747
   168
        case Some(syntax) =>
wenzelm@55747
   169
          val caret = text_area.getCaretPosition
wenzelm@55813
   170
wenzelm@55747
   171
          val line = buffer.getLineOfOffset(caret)
wenzelm@55813
   172
          val line_start = buffer.getLineStartOffset(line)
wenzelm@55813
   173
          val line_length = (buffer.getLineEndOffset(line) min buffer.getLength) - line_start
wenzelm@55813
   174
          val line_text = buffer.getSegment(line_start, line_length)
wenzelm@55748
   175
wenzelm@55747
   176
          val context =
wenzelm@56175
   177
            (opt_rendering match {
wenzelm@55747
   178
              case Some(rendering) =>
wenzelm@56173
   179
                rendering.language_context(before_caret_range(rendering))
wenzelm@55747
   180
              case None => None
wenzelm@55749
   181
            }) getOrElse syntax.language_context
wenzelm@55747
   182
wenzelm@55813
   183
          syntax.completion.complete(
wenzelm@55850
   184
            history, decode, explicit, line_start, line_text, caret - line_start, false, context)
wenzelm@55747
   185
wenzelm@55747
   186
        case None => None
wenzelm@55747
   187
      }
wenzelm@55747
   188
    }
wenzelm@55747
   189
wenzelm@55747
   190
wenzelm@56170
   191
    /* completion action: text area */
wenzelm@53273
   192
wenzelm@53275
   193
    private def insert(item: Completion.Item)
wenzelm@53242
   194
    {
wenzelm@53242
   195
      Swing_Thread.require()
wenzelm@53023
   196
wenzelm@53242
   197
      val buffer = text_area.getBuffer
wenzelm@55692
   198
      val range = item.range
wenzelm@55692
   199
      if (buffer.isEditable && range.length > 0) {
wenzelm@53242
   200
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@55692
   201
          JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@53242
   202
            case Some(text) if text == item.original =>
wenzelm@55692
   203
              buffer.remove(range.start, range.length)
wenzelm@55692
   204
              buffer.insert(range.start, item.replacement)
wenzelm@55692
   205
              text_area.moveCaretPosition(range.start + item.replacement.length + item.move)
wenzelm@53242
   206
            case _ =>
wenzelm@53242
   207
          }
wenzelm@53233
   208
        }
wenzelm@53233
   209
      }
wenzelm@53233
   210
    }
wenzelm@53242
   211
wenzelm@53322
   212
    def action(immediate: Boolean = false, explicit: Boolean = false)
wenzelm@53292
   213
    {
wenzelm@53292
   214
      val view = text_area.getView
wenzelm@53292
   215
      val layered = view.getLayeredPane
wenzelm@53292
   216
      val buffer = text_area.getBuffer
wenzelm@53292
   217
      val painter = text_area.getPainter
wenzelm@53273
   218
wenzelm@55693
   219
      val history = PIDE.completion_history.value
wenzelm@55693
   220
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55693
   221
wenzelm@55693
   222
      def open_popup(result: Completion.Result)
wenzelm@55693
   223
      {
wenzelm@55693
   224
        val font =
wenzelm@55825
   225
          painter.getFont.deriveFont(
wenzelm@55825
   226
            Font_Info.main_size(PIDE.options.real("jedit_popup_font_scale")))
wenzelm@55693
   227
wenzelm@55711
   228
        val range = result.range
wenzelm@55711
   229
        def invalidate(): Unit = JEdit_Lib.invalidate_range(text_area, range)
wenzelm@55711
   230
wenzelm@55711
   231
        val loc1 = text_area.offsetToXY(range.start)
wenzelm@55693
   232
        if (loc1 != null) {
wenzelm@55693
   233
          val loc2 =
wenzelm@55693
   234
            SwingUtilities.convertPoint(painter,
wenzelm@55693
   235
              loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@55693
   236
wenzelm@55978
   237
          val items = result.items.map(new Item(_))
wenzelm@55693
   238
          val completion =
wenzelm@56197
   239
            new Completion_Popup(Some(range), layered, loc2, font, items)
wenzelm@55978
   240
            {
wenzelm@55693
   241
              override def complete(item: Completion.Item) {
wenzelm@55693
   242
                PIDE.completion_history.update(item)
wenzelm@55693
   243
                insert(item)
wenzelm@55693
   244
              }
wenzelm@55693
   245
              override def propagate(evt: KeyEvent) {
wenzelm@56197
   246
                if (view.getKeyEventInterceptor == inner_key_listener) {
wenzelm@56197
   247
                  try {
wenzelm@56197
   248
                    view.setKeyEventInterceptor(null)
wenzelm@56197
   249
                    JEdit_Lib.propagate_key(view, evt)
wenzelm@56197
   250
                  }
wenzelm@56197
   251
                  finally {
wenzelm@56197
   252
                    if (isDisplayable) view.setKeyEventInterceptor(inner_key_listener)
wenzelm@56197
   253
                  }
wenzelm@56197
   254
                }
wenzelm@56171
   255
                if (evt.getID == KeyEvent.KEY_TYPED) input(evt)
wenzelm@55693
   256
              }
wenzelm@56197
   257
              override def shutdown(focus: Boolean) {
wenzelm@56197
   258
                if (view.getKeyEventInterceptor == inner_key_listener)
wenzelm@56197
   259
                  view.setKeyEventInterceptor(null)
wenzelm@56197
   260
                if (focus) text_area.requestFocus
wenzelm@56197
   261
                invalidate()
wenzelm@56197
   262
              }
wenzelm@55693
   263
            }
wenzelm@55693
   264
          completion_popup = Some(completion)
wenzelm@56197
   265
          view.setKeyEventInterceptor(completion.inner_key_listener)
wenzelm@55711
   266
          invalidate()
wenzelm@56197
   267
          completion.show_popup(false)
wenzelm@55693
   268
        }
wenzelm@55693
   269
      }
wenzelm@55693
   270
wenzelm@56175
   271
      if (buffer.isEditable) {
wenzelm@56175
   272
        val (no_completion, semantic_completion, opt_rendering) =
wenzelm@56175
   273
        {
wenzelm@56175
   274
          PIDE.document_view(text_area) match {
wenzelm@56175
   275
            case Some(doc_view) =>
wenzelm@56175
   276
              val rendering = doc_view.get_rendering()
wenzelm@56175
   277
              val (no_completion, result) =
wenzelm@56175
   278
                rendering.semantic_completion(before_caret_range(rendering)) match {
wenzelm@56175
   279
                  case Some(Text.Info(_, Completion.No_Completion)) =>
wenzelm@56175
   280
                    (true, None)
wenzelm@56175
   281
                  case Some(Text.Info(range, names: Completion.Names)) =>
wenzelm@56175
   282
                    val result =
wenzelm@56175
   283
                      JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@56175
   284
                        case Some(original) => names.complete(range, history, decode, original)
wenzelm@56175
   285
                        case None => None
wenzelm@56175
   286
                      }
wenzelm@56175
   287
                    (false, result)
wenzelm@56175
   288
                  case None =>
wenzelm@56175
   289
                    (false, None)
wenzelm@56175
   290
                }
wenzelm@56175
   291
              (no_completion, result, Some(rendering))
wenzelm@56175
   292
            case None =>
wenzelm@56175
   293
              (false, None, None)
wenzelm@56175
   294
          }
wenzelm@55693
   295
        }
wenzelm@56175
   296
        if (!no_completion) {
wenzelm@56175
   297
          val result =
wenzelm@56175
   298
            Completion.Result.merge(history,
wenzelm@56175
   299
              semantic_completion, syntax_completion(history, explicit, opt_rendering))
wenzelm@55693
   300
wenzelm@56175
   301
          result match {
wenzelm@56175
   302
            case Some(result) =>
wenzelm@56175
   303
              result.items match {
wenzelm@56175
   304
                case List(item) if result.unique && item.immediate && immediate =>
wenzelm@56175
   305
                  insert(item)
wenzelm@56175
   306
                case _ :: _ =>
wenzelm@56175
   307
                  open_popup(result)
wenzelm@56175
   308
                case _ =>
wenzelm@56175
   309
              }
wenzelm@56175
   310
            case None =>
wenzelm@56175
   311
          }
wenzelm@53292
   312
        }
wenzelm@55752
   313
      }
wenzelm@53292
   314
    }
wenzelm@53292
   315
wenzelm@53292
   316
wenzelm@53292
   317
    /* input key events */
wenzelm@53273
   318
wenzelm@53272
   319
    def input(evt: KeyEvent)
wenzelm@53242
   320
    {
wenzelm@53242
   321
      Swing_Thread.require()
wenzelm@53233
   322
wenzelm@53273
   323
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   324
        if (!evt.isConsumed) {
wenzelm@53273
   325
          dismissed()
wenzelm@53397
   326
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   327
            val special = JEdit_Lib.special_key(evt)
wenzelm@53397
   328
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   329
              input_delay.revoke()
wenzelm@53397
   330
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   331
            }
wenzelm@53397
   332
            else input_delay.invoke()
wenzelm@53292
   333
          }
wenzelm@53242
   334
        }
wenzelm@53228
   335
      }
wenzelm@53228
   336
    }
wenzelm@53273
   337
wenzelm@53273
   338
    private val input_delay =
wenzelm@53292
   339
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   340
        action()
wenzelm@53292
   341
      }
wenzelm@53273
   342
wenzelm@53273
   343
wenzelm@53292
   344
    /* dismiss popup */
wenzelm@53273
   345
wenzelm@53292
   346
    def dismissed(): Boolean =
wenzelm@53292
   347
    {
wenzelm@53292
   348
      Swing_Thread.require()
wenzelm@53273
   349
wenzelm@53292
   350
      completion_popup match {
wenzelm@53292
   351
        case Some(completion) =>
wenzelm@53292
   352
          completion.hide_popup()
wenzelm@53292
   353
          completion_popup = None
wenzelm@53292
   354
          true
wenzelm@53292
   355
        case None =>
wenzelm@53292
   356
          false
wenzelm@53273
   357
      }
wenzelm@53292
   358
    }
wenzelm@53274
   359
wenzelm@53274
   360
wenzelm@53274
   361
    /* activation */
wenzelm@53274
   362
wenzelm@53274
   363
    private val outer_key_listener =
wenzelm@53274
   364
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   365
wenzelm@53274
   366
    private def activate()
wenzelm@53274
   367
    {
wenzelm@53274
   368
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   369
    }
wenzelm@53274
   370
wenzelm@53274
   371
    private def deactivate()
wenzelm@53274
   372
    {
wenzelm@53274
   373
      dismissed()
wenzelm@53274
   374
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   375
    }
wenzelm@53228
   376
  }
wenzelm@53784
   377
wenzelm@53784
   378
wenzelm@53784
   379
wenzelm@53784
   380
  /** history text field **/
wenzelm@53784
   381
wenzelm@53784
   382
  class History_Text_Field(
wenzelm@53784
   383
    name: String = null,
wenzelm@53784
   384
    instant_popups: Boolean = false,
wenzelm@53784
   385
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   386
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   387
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   388
  {
wenzelm@53784
   389
    text_field =>
wenzelm@53784
   390
wenzelm@53848
   391
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   392
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   393
wenzelm@55711
   394
    // owned by Swing thread
wenzelm@53784
   395
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   396
wenzelm@53784
   397
wenzelm@53784
   398
    /* dismiss */
wenzelm@53784
   399
wenzelm@53784
   400
    private def dismissed(): Boolean =
wenzelm@53784
   401
    {
wenzelm@53784
   402
      completion_popup match {
wenzelm@53784
   403
        case Some(completion) =>
wenzelm@53784
   404
          completion.hide_popup()
wenzelm@53784
   405
          completion_popup = None
wenzelm@53784
   406
          true
wenzelm@53784
   407
        case None =>
wenzelm@53784
   408
          false
wenzelm@53784
   409
      }
wenzelm@53784
   410
    }
wenzelm@53784
   411
wenzelm@53784
   412
wenzelm@53784
   413
    /* insert */
wenzelm@53784
   414
wenzelm@53784
   415
    private def insert(item: Completion.Item)
wenzelm@53784
   416
    {
wenzelm@53784
   417
      Swing_Thread.require()
wenzelm@53784
   418
wenzelm@55692
   419
      val range = item.range
wenzelm@55692
   420
      if (text_field.isEditable && range.length > 0) {
wenzelm@53784
   421
        val content = text_field.getText
wenzelm@55692
   422
        JEdit_Lib.try_get_text(content, range) match {
wenzelm@53784
   423
          case Some(text) if text == item.original =>
wenzelm@53784
   424
            text_field.setText(
wenzelm@55692
   425
              content.substring(0, range.start) +
wenzelm@53784
   426
              item.replacement +
wenzelm@55692
   427
              content.substring(range.stop))
wenzelm@55692
   428
            text_field.getCaret.setDot(range.start + item.replacement.length + item.move)
wenzelm@53784
   429
          case _ =>
wenzelm@53784
   430
        }
wenzelm@53784
   431
      }
wenzelm@53784
   432
    }
wenzelm@53784
   433
wenzelm@53784
   434
wenzelm@56170
   435
    /* completion action: text field */
wenzelm@53784
   436
wenzelm@53784
   437
    def action()
wenzelm@53784
   438
    {
wenzelm@53784
   439
      GUI.layered_pane(text_field) match {
wenzelm@53784
   440
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   441
          val history = PIDE.completion_history.value
wenzelm@53784
   442
wenzelm@53784
   443
          val caret = text_field.getCaret.getDot
wenzelm@55813
   444
          val text = text_field.getText
wenzelm@55748
   445
wenzelm@55749
   446
          val context = syntax.language_context
wenzelm@55748
   447
wenzelm@55813
   448
          syntax.completion.complete(history, true, false, 0, text, caret, false, context) match {
wenzelm@53784
   449
            case Some(result) =>
wenzelm@53784
   450
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   451
              val loc =
wenzelm@53784
   452
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   453
wenzelm@55978
   454
              val items = result.items.map(new Item(_))
wenzelm@55711
   455
              val completion =
wenzelm@55978
   456
                new Completion_Popup(None, layered, loc, text_field.getFont, items)
wenzelm@55978
   457
                {
wenzelm@55978
   458
                  override def complete(item: Completion.Item) {
wenzelm@55978
   459
                    PIDE.completion_history.update(item)
wenzelm@55978
   460
                    insert(item)
wenzelm@55978
   461
                  }
wenzelm@55978
   462
                  override def propagate(evt: KeyEvent) {
wenzelm@55978
   463
                    if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@55978
   464
                  }
wenzelm@56197
   465
                  override def shutdown(focus: Boolean) { if (focus) text_field.requestFocus }
wenzelm@53784
   466
                }
wenzelm@53784
   467
              completion_popup = Some(completion)
wenzelm@56197
   468
              completion.show_popup(true)
wenzelm@53784
   469
wenzelm@53784
   470
            case None =>
wenzelm@53784
   471
          }
wenzelm@53784
   472
        case _ =>
wenzelm@53784
   473
      }
wenzelm@53784
   474
    }
wenzelm@53784
   475
wenzelm@53784
   476
wenzelm@53784
   477
    /* process key event */
wenzelm@53784
   478
wenzelm@53784
   479
    private def process(evt: KeyEvent)
wenzelm@53784
   480
    {
wenzelm@53784
   481
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   482
        dismissed()
wenzelm@53784
   483
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   484
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   485
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   486
            process_delay.revoke()
wenzelm@53784
   487
            action()
wenzelm@53784
   488
          }
wenzelm@53784
   489
          else process_delay.invoke()
wenzelm@53784
   490
        }
wenzelm@53784
   491
      }
wenzelm@53784
   492
    }
wenzelm@53784
   493
wenzelm@53784
   494
    private val process_delay =
wenzelm@53784
   495
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   496
        action()
wenzelm@53784
   497
      }
wenzelm@53784
   498
wenzelm@53784
   499
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   500
    {
wenzelm@53784
   501
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   502
      if (evt != null) {
wenzelm@53784
   503
        evt.getID match {
wenzelm@53784
   504
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   505
            val key_code = evt.getKeyCode
wenzelm@53784
   506
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   507
              if (dismissed()) evt.consume
wenzelm@53784
   508
            }
wenzelm@53784
   509
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   510
            super.processKeyEvent(evt)
wenzelm@53784
   511
            process(evt)
wenzelm@53784
   512
            evt.consume
wenzelm@53784
   513
          case _ =>
wenzelm@53784
   514
        }
wenzelm@53784
   515
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   516
      }
wenzelm@53784
   517
    }
wenzelm@53784
   518
  }
wenzelm@53023
   519
}
wenzelm@53023
   520
wenzelm@53233
   521
wenzelm@53023
   522
class Completion_Popup private(
wenzelm@56197
   523
  val active_range: Option[Text.Range],
wenzelm@53246
   524
  layered: JLayeredPane,
wenzelm@53246
   525
  location: Point,
wenzelm@53272
   526
  font: Font,
wenzelm@55978
   527
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   528
{
wenzelm@53023
   529
  completion =>
wenzelm@53023
   530
wenzelm@53023
   531
  Swing_Thread.require()
wenzelm@53405
   532
wenzelm@53232
   533
  require(!items.isEmpty)
wenzelm@53405
   534
  val multi = items.length > 1
wenzelm@53232
   535
wenzelm@53232
   536
wenzelm@53232
   537
  /* actions */
wenzelm@53232
   538
wenzelm@53275
   539
  def complete(item: Completion.Item) { }
wenzelm@53232
   540
  def propagate(evt: KeyEvent) { }
wenzelm@56197
   541
  def shutdown(focus: Boolean) { }
wenzelm@53023
   542
wenzelm@53023
   543
wenzelm@53023
   544
  /* list view */
wenzelm@53023
   545
wenzelm@53023
   546
  private val list_view = new ListView(items)
wenzelm@53272
   547
  list_view.font = font
wenzelm@53023
   548
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   549
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   550
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   551
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   552
wenzelm@53398
   553
  for (cond <-
wenzelm@53398
   554
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   555
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   556
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   557
wenzelm@53023
   558
  private def complete_selected(): Boolean =
wenzelm@53023
   559
  {
wenzelm@53023
   560
    list_view.selection.items.toList match {
wenzelm@55978
   561
      case item :: _ => complete(item.item); true
wenzelm@53023
   562
      case _ => false
wenzelm@53023
   563
    }
wenzelm@53023
   564
  }
wenzelm@53023
   565
wenzelm@53023
   566
  private def move_items(n: Int)
wenzelm@53023
   567
  {
wenzelm@53023
   568
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   569
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   570
    if (i != j) {
wenzelm@53023
   571
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   572
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   573
    }
wenzelm@53023
   574
  }
wenzelm@53023
   575
wenzelm@53023
   576
  private def move_pages(n: Int)
wenzelm@53023
   577
  {
wenzelm@53023
   578
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   579
    move_items(page_size * n)
wenzelm@53023
   580
  }
wenzelm@53023
   581
wenzelm@53023
   582
wenzelm@53023
   583
  /* event handling */
wenzelm@53023
   584
wenzelm@56197
   585
  val inner_key_listener =
wenzelm@53226
   586
    JEdit_Lib.key_listener(
wenzelm@53226
   587
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   588
        {
wenzelm@53226
   589
          if (!e.isConsumed) {
wenzelm@53226
   590
            e.getKeyCode match {
wenzelm@53226
   591
              case KeyEvent.VK_TAB =>
wenzelm@53226
   592
                if (complete_selected()) e.consume
wenzelm@53226
   593
                hide_popup()
wenzelm@53226
   594
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   595
                hide_popup()
wenzelm@53226
   596
                e.consume
wenzelm@53405
   597
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   598
                move_items(-1)
wenzelm@53407
   599
                e.consume
wenzelm@53405
   600
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   601
                move_items(1)
wenzelm@53407
   602
                e.consume
wenzelm@53405
   603
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   604
                move_pages(-1)
wenzelm@53407
   605
                e.consume
wenzelm@53405
   606
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   607
                move_pages(1)
wenzelm@53407
   608
                e.consume
wenzelm@53226
   609
              case _ =>
wenzelm@53226
   610
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   611
                  hide_popup()
wenzelm@53226
   612
            }
wenzelm@53226
   613
          }
wenzelm@53232
   614
          propagate(e)
wenzelm@53226
   615
        },
wenzelm@53232
   616
      key_typed = propagate _
wenzelm@53226
   617
    )
wenzelm@53023
   618
wenzelm@53274
   619
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   620
wenzelm@53023
   621
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   622
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   623
      if (complete_selected()) e.consume
wenzelm@53023
   624
      hide_popup()
wenzelm@53023
   625
    }
wenzelm@53023
   626
  })
wenzelm@53023
   627
wenzelm@53023
   628
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   629
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   630
  })
wenzelm@53023
   631
wenzelm@53023
   632
wenzelm@53023
   633
  /* main content */
wenzelm@53023
   634
wenzelm@53023
   635
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   636
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   637
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   638
wenzelm@53023
   639
wenzelm@53023
   640
  /* popup */
wenzelm@53023
   641
wenzelm@53023
   642
  private val popup =
wenzelm@53023
   643
  {
wenzelm@53247
   644
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   645
    val size =
wenzelm@53230
   646
    {
wenzelm@53230
   647
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   648
      val bounds = Rendering.popup_bounds
wenzelm@53247
   649
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   650
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   651
      new Dimension(w, h)
wenzelm@53230
   652
    }
wenzelm@53247
   653
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   654
  }
wenzelm@53246
   655
wenzelm@56197
   656
  private def show_popup(focus: Boolean)
wenzelm@53246
   657
  {
wenzelm@53246
   658
    popup.show
wenzelm@56197
   659
    if (focus) list_view.requestFocus
wenzelm@53023
   660
  }
wenzelm@53023
   661
wenzelm@53232
   662
  private def hide_popup()
wenzelm@53023
   663
  {
wenzelm@56197
   664
    shutdown(list_view.peer.isFocusOwner)
wenzelm@56197
   665
    popup.hide
wenzelm@53023
   666
  }
wenzelm@53023
   667
}
wenzelm@53023
   668