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