src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Mon Mar 17 12:24:00 2014 +0100 (2014-03-17 ago)
changeset 56173 62f10624339a
parent 56171 15351577da10
child 56175 79c29244b377
permissions -rw-r--r--
tuned signature;
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@56173
   143
                  case Some(semantic) =>
wenzelm@56173
   144
                    if (semantic.info.no_completion) None
wenzelm@56173
   145
                    else Some(semantic.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@56173
   181
                rendering.language_context(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@56173
   263
            rendering.semantic_completion(before_caret_range(rendering)) match {
wenzelm@56173
   264
              case Some(semantic) =>
wenzelm@56173
   265
                if (semantic.info.no_completion)
wenzelm@56173
   266
                  Some(Completion.Result.empty(semantic.range))
wenzelm@56170
   267
                else
wenzelm@56173
   268
                  JEdit_Lib.try_get_text(buffer, semantic.range) match {
wenzelm@56173
   269
                    case Some(original) =>
wenzelm@56173
   270
                      semantic.info.complete(semantic.range, history, decode, original)
wenzelm@56170
   271
                    case None => None
wenzelm@56170
   272
                  }
wenzelm@56170
   273
              case None => None
wenzelm@56170
   274
            }
wenzelm@56170
   275
          case None => None
wenzelm@55693
   276
        }
wenzelm@55693
   277
wenzelm@55752
   278
      if (buffer.isEditable) {
wenzelm@55752
   279
        semantic_completion() orElse syntax_completion(explicit) match {
wenzelm@55747
   280
          case Some(result) =>
wenzelm@55747
   281
            result.items match {
wenzelm@55747
   282
              case List(item) if result.unique && item.immediate && immediate =>
wenzelm@55752
   283
                insert(item)
wenzelm@55752
   284
              case _ :: _ =>
wenzelm@55752
   285
                open_popup(result)
wenzelm@55752
   286
              case _ =>
wenzelm@53292
   287
            }
wenzelm@55752
   288
          case None =>
wenzelm@53292
   289
        }
wenzelm@55752
   290
      }
wenzelm@53292
   291
    }
wenzelm@53292
   292
wenzelm@53292
   293
wenzelm@53292
   294
    /* input key events */
wenzelm@53273
   295
wenzelm@53272
   296
    def input(evt: KeyEvent)
wenzelm@53242
   297
    {
wenzelm@53242
   298
      Swing_Thread.require()
wenzelm@53233
   299
wenzelm@53273
   300
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   301
        if (!evt.isConsumed) {
wenzelm@53273
   302
          dismissed()
wenzelm@53397
   303
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   304
            val special = JEdit_Lib.special_key(evt)
wenzelm@53397
   305
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   306
              input_delay.revoke()
wenzelm@53397
   307
              action(immediate = PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53397
   308
            }
wenzelm@53397
   309
            else input_delay.invoke()
wenzelm@53292
   310
          }
wenzelm@53242
   311
        }
wenzelm@53228
   312
      }
wenzelm@53228
   313
    }
wenzelm@53273
   314
wenzelm@53273
   315
    private val input_delay =
wenzelm@53292
   316
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   317
        action()
wenzelm@53292
   318
      }
wenzelm@53273
   319
wenzelm@53273
   320
wenzelm@53292
   321
    /* dismiss popup */
wenzelm@53273
   322
wenzelm@53292
   323
    def dismissed(): Boolean =
wenzelm@53292
   324
    {
wenzelm@53292
   325
      Swing_Thread.require()
wenzelm@53273
   326
wenzelm@53292
   327
      completion_popup match {
wenzelm@53292
   328
        case Some(completion) =>
wenzelm@53292
   329
          completion.hide_popup()
wenzelm@53292
   330
          completion_popup = None
wenzelm@53292
   331
          true
wenzelm@53292
   332
        case None =>
wenzelm@53292
   333
          false
wenzelm@53273
   334
      }
wenzelm@53292
   335
    }
wenzelm@53274
   336
wenzelm@53274
   337
wenzelm@53274
   338
    /* activation */
wenzelm@53274
   339
wenzelm@53274
   340
    private val outer_key_listener =
wenzelm@53274
   341
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   342
wenzelm@53274
   343
    private def activate()
wenzelm@53274
   344
    {
wenzelm@53274
   345
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   346
    }
wenzelm@53274
   347
wenzelm@53274
   348
    private def deactivate()
wenzelm@53274
   349
    {
wenzelm@53274
   350
      dismissed()
wenzelm@53274
   351
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   352
    }
wenzelm@53228
   353
  }
wenzelm@53784
   354
wenzelm@53784
   355
wenzelm@53784
   356
wenzelm@53784
   357
  /** history text field **/
wenzelm@53784
   358
wenzelm@53784
   359
  class History_Text_Field(
wenzelm@53784
   360
    name: String = null,
wenzelm@53784
   361
    instant_popups: Boolean = false,
wenzelm@53784
   362
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   363
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   364
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   365
  {
wenzelm@53784
   366
    text_field =>
wenzelm@53784
   367
wenzelm@53848
   368
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   369
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   370
wenzelm@55711
   371
    // owned by Swing thread
wenzelm@53784
   372
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   373
wenzelm@53784
   374
wenzelm@53784
   375
    /* dismiss */
wenzelm@53784
   376
wenzelm@53784
   377
    private def dismissed(): Boolean =
wenzelm@53784
   378
    {
wenzelm@53784
   379
      completion_popup match {
wenzelm@53784
   380
        case Some(completion) =>
wenzelm@53784
   381
          completion.hide_popup()
wenzelm@53784
   382
          completion_popup = None
wenzelm@53784
   383
          true
wenzelm@53784
   384
        case None =>
wenzelm@53784
   385
          false
wenzelm@53784
   386
      }
wenzelm@53784
   387
    }
wenzelm@53784
   388
wenzelm@53784
   389
wenzelm@53784
   390
    /* insert */
wenzelm@53784
   391
wenzelm@53784
   392
    private def insert(item: Completion.Item)
wenzelm@53784
   393
    {
wenzelm@53784
   394
      Swing_Thread.require()
wenzelm@53784
   395
wenzelm@55692
   396
      val range = item.range
wenzelm@55692
   397
      if (text_field.isEditable && range.length > 0) {
wenzelm@53784
   398
        val content = text_field.getText
wenzelm@55692
   399
        JEdit_Lib.try_get_text(content, range) match {
wenzelm@53784
   400
          case Some(text) if text == item.original =>
wenzelm@53784
   401
            text_field.setText(
wenzelm@55692
   402
              content.substring(0, range.start) +
wenzelm@53784
   403
              item.replacement +
wenzelm@55692
   404
              content.substring(range.stop))
wenzelm@55692
   405
            text_field.getCaret.setDot(range.start + item.replacement.length + item.move)
wenzelm@53784
   406
          case _ =>
wenzelm@53784
   407
        }
wenzelm@53784
   408
      }
wenzelm@53784
   409
    }
wenzelm@53784
   410
wenzelm@53784
   411
wenzelm@56170
   412
    /* completion action: text field */
wenzelm@53784
   413
wenzelm@53784
   414
    def action()
wenzelm@53784
   415
    {
wenzelm@53784
   416
      GUI.layered_pane(text_field) match {
wenzelm@53784
   417
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   418
          val history = PIDE.completion_history.value
wenzelm@53784
   419
wenzelm@53784
   420
          val caret = text_field.getCaret.getDot
wenzelm@55813
   421
          val text = text_field.getText
wenzelm@55748
   422
wenzelm@55749
   423
          val context = syntax.language_context
wenzelm@55748
   424
wenzelm@55813
   425
          syntax.completion.complete(history, true, false, 0, text, caret, false, context) match {
wenzelm@53784
   426
            case Some(result) =>
wenzelm@53784
   427
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   428
              val loc =
wenzelm@53784
   429
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   430
wenzelm@55978
   431
              val items = result.items.map(new Item(_))
wenzelm@55711
   432
              val completion =
wenzelm@55978
   433
                new Completion_Popup(None, layered, loc, text_field.getFont, items)
wenzelm@55978
   434
                {
wenzelm@55978
   435
                  override def complete(item: Completion.Item) {
wenzelm@55978
   436
                    PIDE.completion_history.update(item)
wenzelm@55978
   437
                    insert(item)
wenzelm@55978
   438
                  }
wenzelm@55978
   439
                  override def propagate(evt: KeyEvent) {
wenzelm@55978
   440
                    if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@55978
   441
                  }
wenzelm@55978
   442
                  override def refocus() { text_field.requestFocus }
wenzelm@53784
   443
                }
wenzelm@53784
   444
              completion_popup = Some(completion)
wenzelm@53784
   445
              completion.show_popup()
wenzelm@53784
   446
wenzelm@53784
   447
            case None =>
wenzelm@53784
   448
          }
wenzelm@53784
   449
        case _ =>
wenzelm@53784
   450
      }
wenzelm@53784
   451
    }
wenzelm@53784
   452
wenzelm@53784
   453
wenzelm@53784
   454
    /* process key event */
wenzelm@53784
   455
wenzelm@53784
   456
    private def process(evt: KeyEvent)
wenzelm@53784
   457
    {
wenzelm@53784
   458
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   459
        dismissed()
wenzelm@53784
   460
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   461
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   462
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   463
            process_delay.revoke()
wenzelm@53784
   464
            action()
wenzelm@53784
   465
          }
wenzelm@53784
   466
          else process_delay.invoke()
wenzelm@53784
   467
        }
wenzelm@53784
   468
      }
wenzelm@53784
   469
    }
wenzelm@53784
   470
wenzelm@53784
   471
    private val process_delay =
wenzelm@53784
   472
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   473
        action()
wenzelm@53784
   474
      }
wenzelm@53784
   475
wenzelm@53784
   476
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   477
    {
wenzelm@53784
   478
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   479
      if (evt != null) {
wenzelm@53784
   480
        evt.getID match {
wenzelm@53784
   481
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   482
            val key_code = evt.getKeyCode
wenzelm@53784
   483
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   484
              if (dismissed()) evt.consume
wenzelm@53784
   485
            }
wenzelm@53784
   486
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   487
            super.processKeyEvent(evt)
wenzelm@53784
   488
            process(evt)
wenzelm@53784
   489
            evt.consume
wenzelm@53784
   490
          case _ =>
wenzelm@53784
   491
        }
wenzelm@53784
   492
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   493
      }
wenzelm@53784
   494
    }
wenzelm@53784
   495
  }
wenzelm@53023
   496
}
wenzelm@53023
   497
wenzelm@53233
   498
wenzelm@53023
   499
class Completion_Popup private(
wenzelm@55711
   500
  val active_range: Option[(Text.Range, () => Unit)],
wenzelm@53246
   501
  layered: JLayeredPane,
wenzelm@53246
   502
  location: Point,
wenzelm@53272
   503
  font: Font,
wenzelm@55978
   504
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   505
{
wenzelm@53023
   506
  completion =>
wenzelm@53023
   507
wenzelm@53023
   508
  Swing_Thread.require()
wenzelm@53405
   509
wenzelm@53232
   510
  require(!items.isEmpty)
wenzelm@53405
   511
  val multi = items.length > 1
wenzelm@53232
   512
wenzelm@53232
   513
wenzelm@53232
   514
  /* actions */
wenzelm@53232
   515
wenzelm@53275
   516
  def complete(item: Completion.Item) { }
wenzelm@53232
   517
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   518
  def refocus() { }
wenzelm@53023
   519
wenzelm@53023
   520
wenzelm@53023
   521
  /* list view */
wenzelm@53023
   522
wenzelm@53023
   523
  private val list_view = new ListView(items)
wenzelm@53272
   524
  list_view.font = font
wenzelm@53023
   525
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   526
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   527
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   528
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   529
wenzelm@53398
   530
  for (cond <-
wenzelm@53398
   531
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   532
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   533
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   534
wenzelm@53023
   535
  private def complete_selected(): Boolean =
wenzelm@53023
   536
  {
wenzelm@53023
   537
    list_view.selection.items.toList match {
wenzelm@55978
   538
      case item :: _ => complete(item.item); true
wenzelm@53023
   539
      case _ => false
wenzelm@53023
   540
    }
wenzelm@53023
   541
  }
wenzelm@53023
   542
wenzelm@53023
   543
  private def move_items(n: Int)
wenzelm@53023
   544
  {
wenzelm@53023
   545
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   546
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   547
    if (i != j) {
wenzelm@53023
   548
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   549
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   550
    }
wenzelm@53023
   551
  }
wenzelm@53023
   552
wenzelm@53023
   553
  private def move_pages(n: Int)
wenzelm@53023
   554
  {
wenzelm@53023
   555
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   556
    move_items(page_size * n)
wenzelm@53023
   557
  }
wenzelm@53023
   558
wenzelm@53023
   559
wenzelm@53023
   560
  /* event handling */
wenzelm@53023
   561
wenzelm@53274
   562
  private val inner_key_listener =
wenzelm@53226
   563
    JEdit_Lib.key_listener(
wenzelm@53226
   564
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   565
        {
wenzelm@53226
   566
          if (!e.isConsumed) {
wenzelm@53226
   567
            e.getKeyCode match {
wenzelm@53226
   568
              case KeyEvent.VK_TAB =>
wenzelm@53226
   569
                if (complete_selected()) e.consume
wenzelm@53226
   570
                hide_popup()
wenzelm@53226
   571
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   572
                hide_popup()
wenzelm@53226
   573
                e.consume
wenzelm@53405
   574
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   575
                move_items(-1)
wenzelm@53407
   576
                e.consume
wenzelm@53405
   577
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   578
                move_items(1)
wenzelm@53407
   579
                e.consume
wenzelm@53405
   580
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   581
                move_pages(-1)
wenzelm@53407
   582
                e.consume
wenzelm@53405
   583
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   584
                move_pages(1)
wenzelm@53407
   585
                e.consume
wenzelm@53226
   586
              case _ =>
wenzelm@53226
   587
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   588
                  hide_popup()
wenzelm@53226
   589
            }
wenzelm@53226
   590
          }
wenzelm@53232
   591
          propagate(e)
wenzelm@53226
   592
        },
wenzelm@53232
   593
      key_typed = propagate _
wenzelm@53226
   594
    )
wenzelm@53023
   595
wenzelm@53274
   596
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   597
wenzelm@53023
   598
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   599
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   600
      if (complete_selected()) e.consume
wenzelm@53023
   601
      hide_popup()
wenzelm@53023
   602
    }
wenzelm@53023
   603
  })
wenzelm@53023
   604
wenzelm@53023
   605
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   606
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   607
  })
wenzelm@53023
   608
wenzelm@53023
   609
wenzelm@53023
   610
  /* main content */
wenzelm@53023
   611
wenzelm@53023
   612
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   613
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   614
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   615
wenzelm@53023
   616
wenzelm@53023
   617
  /* popup */
wenzelm@53023
   618
wenzelm@53023
   619
  private val popup =
wenzelm@53023
   620
  {
wenzelm@53247
   621
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   622
    val size =
wenzelm@53230
   623
    {
wenzelm@53230
   624
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   625
      val bounds = Rendering.popup_bounds
wenzelm@53247
   626
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   627
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   628
      new Dimension(w, h)
wenzelm@53230
   629
    }
wenzelm@53247
   630
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   631
  }
wenzelm@53246
   632
wenzelm@53246
   633
  private def show_popup()
wenzelm@53246
   634
  {
wenzelm@53246
   635
    popup.show
wenzelm@53246
   636
    list_view.requestFocus
wenzelm@53023
   637
  }
wenzelm@53023
   638
wenzelm@54377
   639
  private val hide_popup_delay =
wenzelm@54377
   640
    Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_dismiss_delay")) {
wenzelm@55711
   641
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   642
      popup.hide
wenzelm@54377
   643
    }
wenzelm@54377
   644
wenzelm@53232
   645
  private def hide_popup()
wenzelm@53023
   646
  {
wenzelm@54376
   647
    if (list_view.peer.isFocusOwner) refocus()
wenzelm@54377
   648
wenzelm@55711
   649
    if (PIDE.options.seconds("jedit_completion_dismiss_delay").is_zero) {
wenzelm@55711
   650
      active_range match { case Some((_, invalidate)) => invalidate() case _ => }
wenzelm@54377
   651
      popup.hide
wenzelm@55711
   652
    }
wenzelm@54377
   653
    else hide_popup_delay.invoke()
wenzelm@53023
   654
  }
wenzelm@53023
   655
}
wenzelm@53023
   656