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