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