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