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