src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64621 7116f2634e32
child 64882 c3b42ac0cf81
permissions -rw-r--r--
tuned signature;
wenzelm@53023
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
wenzelm@53023
     2
    Author:     Makarius
wenzelm@53023
     3
wenzelm@53246
     4
Completion popup.
wenzelm@53023
     5
*/
wenzelm@53023
     6
wenzelm@53023
     7
package isabelle.jedit
wenzelm@53023
     8
wenzelm@53023
     9
wenzelm@53023
    10
import isabelle._
wenzelm@53023
    11
wenzelm@53297
    12
import java.awt.{Color, Font, Point, BorderLayout, Dimension}
wenzelm@53784
    13
import java.awt.event.{KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
wenzelm@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@63528
    38
          "<html><b>" + HTML.output(Symbol.print_newlines(a)) + "</b>" +
wenzelm@63528
    39
            HTML.output(bs.map(b => " " + Symbol.print_newlines(b)).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@57612
    55
      GUI_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@57612
    81
      GUI_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@57612
   101
      GUI_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@57612
   111
    // owned by GUI 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@64621
   123
    def rendering(rendering: JEdit_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@61601
   131
                val range0 =
wenzelm@61601
   132
                  Completion.Result.merge(Completion.History.empty,
wenzelm@61734
   133
                    syntax_completion(Completion.History.empty, true, Some(rendering)),
wenzelm@61601
   134
                    Completion.Result.merge(Completion.History.empty,
wenzelm@61601
   135
                      path_completion(rendering),
wenzelm@61601
   136
                      Bibtex_JEdit.completion(Completion.History.empty, text_area, rendering)))
wenzelm@61601
   137
                  .map(_.range)
wenzelm@61601
   138
                rendering.semantic_completion(range0, range) match {
wenzelm@61601
   139
                  case None => range0
wenzelm@56175
   140
                  case Some(Text.Info(_, Completion.No_Completion)) => None
wenzelm@56175
   141
                  case Some(Text.Info(range1, _: Completion.Names)) => Some(range1)
wenzelm@55747
   142
                }
wenzelm@55747
   143
              case _ => None
wenzelm@55747
   144
            }
wenzelm@55747
   145
          }
wenzelm@55747
   146
          else None
wenzelm@55747
   147
      }
wenzelm@55747
   148
    }.map(range => Text.Info(range, rendering.completion_color))
wenzelm@55747
   149
wenzelm@55747
   150
wenzelm@55747
   151
    /* syntax completion */
wenzelm@55747
   152
wenzelm@55747
   153
    def syntax_completion(
wenzelm@56175
   154
      history: Completion.History,
wenzelm@56175
   155
      explicit: Boolean,
wenzelm@64621
   156
      opt_rendering: Option[JEdit_Rendering]): Option[Completion.Result] =
wenzelm@55747
   157
    {
wenzelm@55747
   158
      val buffer = text_area.getBuffer
wenzelm@55747
   159
      val decode = Isabelle_Encoding.is_active(buffer)
wenzelm@55747
   160
wenzelm@59074
   161
      Isabelle.buffer_syntax(buffer) match {
wenzelm@55747
   162
        case Some(syntax) =>
wenzelm@55747
   163
          val context =
wenzelm@56587
   164
            (for {
wenzelm@56587
   165
              rendering <- opt_rendering
wenzelm@56842
   166
              if PIDE.options.bool("jedit_completion_context")
wenzelm@56593
   167
              range = JEdit_Lib.before_caret_range(text_area, rendering)
wenzelm@56587
   168
              context <- rendering.language_context(range)
wenzelm@56587
   169
            } yield context) getOrElse syntax.language_context
wenzelm@55747
   170
wenzelm@56589
   171
          val caret = text_area.getCaretPosition
wenzelm@56589
   172
          val line_range = JEdit_Lib.line_range(buffer, text_area.getCaretLine)
wenzelm@56589
   173
          val line_start = line_range.start
wenzelm@56589
   174
          for {
wenzelm@56589
   175
            line_text <- JEdit_Lib.try_get_text(buffer, line_range)
wenzelm@56589
   176
            result <-
wenzelm@56589
   177
              syntax.completion.complete(
wenzelm@57588
   178
                history, decode, explicit, line_start, line_text, caret - line_start, context)
wenzelm@56589
   179
          } yield result
wenzelm@55747
   180
wenzelm@55747
   181
        case None => None
wenzelm@55747
   182
      }
wenzelm@55747
   183
    }
wenzelm@55747
   184
wenzelm@55747
   185
wenzelm@56843
   186
    /* path completion */
wenzelm@56843
   187
wenzelm@64621
   188
    def path_completion(rendering: JEdit_Rendering): Option[Completion.Result] =
wenzelm@56843
   189
    {
wenzelm@56844
   190
      def complete(text: String): List[(String, List[String])] =
wenzelm@56843
   191
      {
wenzelm@56843
   192
        try {
wenzelm@56843
   193
          val path = Path.explode(text)
wenzelm@56843
   194
          val (dir, base_name) =
wenzelm@56843
   195
            if (text == "" || text.endsWith("/")) (path, "")
wenzelm@56843
   196
            else (path.dir, path.base.implode)
wenzelm@56843
   197
wenzelm@56843
   198
          val directory =
wenzelm@56843
   199
            new JFile(PIDE.resources.append(rendering.snapshot.node_name.master_dir, dir))
wenzelm@56843
   200
          val files = directory.listFiles
wenzelm@56843
   201
          if (files == null) Nil
wenzelm@56843
   202
          else {
wenzelm@56843
   203
            val ignore =
wenzelm@56843
   204
              Library.space_explode(':', PIDE.options.string("jedit_completion_path_ignore")).
wenzelm@56843
   205
                map(s => Pattern.compile(StandardUtilities.globToRE(s)))
wenzelm@56843
   206
            (for {
wenzelm@56843
   207
              file <- files.iterator
wenzelm@56844
   208
wenzelm@56843
   209
              name = file.getName
wenzelm@56844
   210
              if name.startsWith(base_name)
wenzelm@56843
   211
              if !ignore.exists(pat => pat.matcher(name).matches)
wenzelm@56844
   212
wenzelm@56844
   213
              text1 = (dir + Path.basic(name)).implode_short
wenzelm@56844
   214
              if text != text1
wenzelm@56844
   215
wenzelm@56844
   216
              is_dir = new JFile(directory, name).isDirectory
wenzelm@56844
   217
              replacement = text1 + (if (is_dir) "/" else "")
wenzelm@56844
   218
              descr = List(text1, if (is_dir) "(directory)" else "(file)")
wenzelm@56844
   219
            } yield (replacement, descr)).take(PIDE.options.int("completion_limit")).toList
wenzelm@56843
   220
          }
wenzelm@56843
   221
        }
wenzelm@56843
   222
        catch { case ERROR(_) => Nil }
wenzelm@56843
   223
      }
wenzelm@56843
   224
wenzelm@63674
   225
      def is_wrapped(s: String): Boolean =
wenzelm@63674
   226
        s.startsWith("\"") && s.endsWith("\"") ||
wenzelm@63674
   227
        s.startsWith(Symbol.open_decoded) && s.endsWith(Symbol.close_decoded)
wenzelm@63674
   228
wenzelm@56843
   229
      for {
wenzelm@56843
   230
        r1 <- rendering.language_path(JEdit_Lib.before_caret_range(text_area, rendering))
wenzelm@56843
   231
        s1 <- JEdit_Lib.try_get_text(text_area.getBuffer, r1)
wenzelm@63674
   232
        if is_wrapped(s1)
wenzelm@56843
   233
        r2 = Text.Range(r1.start + 1, r1.stop - 1)
wenzelm@63674
   234
        s2 = s1.substring(1, s1.length - 1)
wenzelm@56843
   235
        if Path.is_valid(s2)
wenzelm@56843
   236
        paths = complete(s2)
wenzelm@59319
   237
        if paths.nonEmpty
wenzelm@56844
   238
        items = paths.map(p => Completion.Item(r2, s2, "", 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@57612
   247
      GUI_Thread.require {}
wenzelm@53023
   248
wenzelm@53242
   249
      val buffer = text_area.getBuffer
wenzelm@55692
   250
      val range = item.range
wenzelm@56863
   251
      if (buffer.isEditable) {
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@58767
   312
              loc1.x, loc1.y + painter.getLineHeight, 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@57127
   343
          dismissed()
wenzelm@55693
   344
          completion_popup = Some(completion)
wenzelm@56197
   345
          view.setKeyEventInterceptor(completion.inner_key_listener)
wenzelm@56606
   346
          JEdit_Lib.invalidate_range(text_area, range)
wenzelm@56841
   347
          Pretty_Tooltip.dismissed_all()
wenzelm@56197
   348
          completion.show_popup(false)
wenzelm@55693
   349
        }
wenzelm@55693
   350
      }
wenzelm@55693
   351
wenzelm@56175
   352
      if (buffer.isEditable) {
wenzelm@61601
   353
        val opt_rendering = PIDE.document_view(text_area).map(_.get_rendering())
wenzelm@61601
   354
        val result0 = syntax_completion(history, explicit, opt_rendering)
wenzelm@61601
   355
        val (no_completion, semantic_completion) =
wenzelm@56175
   356
        {
wenzelm@61601
   357
          opt_rendering match {
wenzelm@61601
   358
            case Some(rendering) =>
wenzelm@61601
   359
              val caret_range = JEdit_Lib.before_caret_range(text_area, rendering)
wenzelm@61601
   360
              rendering.semantic_completion(result0.map(_.range), caret_range) match {
wenzelm@61601
   361
                case Some(Text.Info(_, Completion.No_Completion)) => (true, None)
wenzelm@61601
   362
                case Some(Text.Info(range, names: Completion.Names)) =>
wenzelm@61601
   363
                  JEdit_Lib.try_get_text(buffer, range) match {
wenzelm@61601
   364
                    case Some(original) => (false, names.complete(range, history, decode, original))
wenzelm@61601
   365
                    case None => (false, None)
wenzelm@61601
   366
                  }
wenzelm@61601
   367
                case None => (false, None)
wenzelm@56593
   368
              }
wenzelm@61601
   369
            case None => (false, None)
wenzelm@56175
   370
          }
wenzelm@55693
   371
        }
wenzelm@56326
   372
        if (no_completion) false
wenzelm@56326
   373
        else {
wenzelm@56564
   374
          val result =
wenzelm@56586
   375
          {
wenzelm@61601
   376
            val result1 =
wenzelm@56586
   377
              if (word_only) None
wenzelm@61601
   378
              else Completion.Result.merge(history, semantic_completion, result0)
wenzelm@56564
   379
            opt_rendering match {
wenzelm@61601
   380
              case None => result1
wenzelm@56564
   381
              case Some(rendering) =>
wenzelm@61601
   382
                Completion.Result.merge(history, result1,
wenzelm@56843
   383
                  Completion.Result.merge(history,
wenzelm@58549
   384
                    Spell_Checker.completion(text_area, explicit, rendering),
wenzelm@58592
   385
                    Completion.Result.merge(history,
wenzelm@58592
   386
                      path_completion(rendering),
wenzelm@58592
   387
                      Bibtex_JEdit.completion(history, text_area, rendering))))
wenzelm@56564
   388
            }
wenzelm@56586
   389
          }
wenzelm@56175
   390
          result match {
wenzelm@56175
   391
            case Some(result) =>
wenzelm@56175
   392
              result.items match {
wenzelm@56175
   393
                case List(item) if result.unique && item.immediate && immediate =>
wenzelm@56175
   394
                  insert(item)
wenzelm@56326
   395
                  true
wenzelm@56326
   396
                case _ :: _ if !delayed =>
wenzelm@56175
   397
                  open_popup(result)
wenzelm@56326
   398
                  false
wenzelm@56326
   399
                case _ => false
wenzelm@56175
   400
              }
wenzelm@56326
   401
            case None => false
wenzelm@56175
   402
          }
wenzelm@53292
   403
        }
wenzelm@55752
   404
      }
wenzelm@56326
   405
      else false
wenzelm@53292
   406
    }
wenzelm@53292
   407
wenzelm@53292
   408
wenzelm@53292
   409
    /* input key events */
wenzelm@53273
   410
wenzelm@53272
   411
    def input(evt: KeyEvent)
wenzelm@53242
   412
    {
wenzelm@57612
   413
      GUI_Thread.require {}
wenzelm@53233
   414
wenzelm@53273
   415
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   416
        if (!evt.isConsumed) {
wenzelm@53273
   417
          dismissed()
wenzelm@53397
   418
          if (evt.getKeyChar != '\b') {
wenzelm@53784
   419
            val special = JEdit_Lib.special_key(evt)
wenzelm@56326
   420
            val immediate = PIDE.options.bool("jedit_completion_immediate")
wenzelm@53397
   421
            if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53397
   422
              input_delay.revoke()
wenzelm@56326
   423
              action(immediate = immediate)
wenzelm@53397
   424
            }
wenzelm@56326
   425
            else {
wenzelm@56326
   426
              if (!special && action(immediate = immediate, delayed = true))
wenzelm@56326
   427
                input_delay.revoke()
wenzelm@56326
   428
              else
wenzelm@56326
   429
                input_delay.invoke()
wenzelm@56326
   430
            }
wenzelm@53292
   431
          }
wenzelm@53242
   432
        }
wenzelm@53228
   433
      }
wenzelm@53228
   434
    }
wenzelm@53273
   435
wenzelm@53273
   436
    private val input_delay =
wenzelm@57612
   437
      GUI_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53322
   438
        action()
wenzelm@53292
   439
      }
wenzelm@53273
   440
wenzelm@53273
   441
wenzelm@53292
   442
    /* dismiss popup */
wenzelm@53273
   443
wenzelm@53292
   444
    def dismissed(): Boolean =
wenzelm@53292
   445
    {
wenzelm@57612
   446
      GUI_Thread.require {}
wenzelm@53273
   447
wenzelm@53292
   448
      completion_popup match {
wenzelm@53292
   449
        case Some(completion) =>
wenzelm@53292
   450
          completion.hide_popup()
wenzelm@53292
   451
          completion_popup = None
wenzelm@53292
   452
          true
wenzelm@53292
   453
        case None =>
wenzelm@53292
   454
          false
wenzelm@53273
   455
      }
wenzelm@53292
   456
    }
wenzelm@53274
   457
wenzelm@53274
   458
wenzelm@53274
   459
    /* activation */
wenzelm@53274
   460
wenzelm@53274
   461
    private val outer_key_listener =
wenzelm@53274
   462
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   463
wenzelm@53274
   464
    private def activate()
wenzelm@53274
   465
    {
wenzelm@53274
   466
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   467
    }
wenzelm@53274
   468
wenzelm@53274
   469
    private def deactivate()
wenzelm@53274
   470
    {
wenzelm@53274
   471
      dismissed()
wenzelm@53274
   472
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   473
    }
wenzelm@53228
   474
  }
wenzelm@53784
   475
wenzelm@53784
   476
wenzelm@53784
   477
wenzelm@53784
   478
  /** history text field **/
wenzelm@53784
   479
wenzelm@53784
   480
  class History_Text_Field(
wenzelm@53784
   481
    name: String = null,
wenzelm@53784
   482
    instant_popups: Boolean = false,
wenzelm@53784
   483
    enter_adds_to_history: Boolean = true,
wenzelm@53784
   484
    syntax: Outer_Syntax = Outer_Syntax.init) extends
wenzelm@53784
   485
    HistoryTextField(name, instant_popups, enter_adds_to_history)
wenzelm@53784
   486
  {
wenzelm@53784
   487
    text_field =>
wenzelm@53784
   488
wenzelm@53848
   489
    // see https://forums.oracle.com/thread/1361677
wenzelm@53848
   490
    if (GUI.is_macos_laf) text_field.setCaret(new DefaultCaret)
wenzelm@53784
   491
wenzelm@57612
   492
    // owned by GUI thread
wenzelm@53784
   493
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53784
   494
wenzelm@53784
   495
wenzelm@53784
   496
    /* dismiss */
wenzelm@53784
   497
wenzelm@53784
   498
    private def dismissed(): Boolean =
wenzelm@53784
   499
    {
wenzelm@53784
   500
      completion_popup match {
wenzelm@53784
   501
        case Some(completion) =>
wenzelm@53784
   502
          completion.hide_popup()
wenzelm@53784
   503
          completion_popup = None
wenzelm@53784
   504
          true
wenzelm@53784
   505
        case None =>
wenzelm@53784
   506
          false
wenzelm@53784
   507
      }
wenzelm@53784
   508
    }
wenzelm@53784
   509
wenzelm@53784
   510
wenzelm@53784
   511
    /* insert */
wenzelm@53784
   512
wenzelm@53784
   513
    private def insert(item: Completion.Item)
wenzelm@53784
   514
    {
wenzelm@57612
   515
      GUI_Thread.require {}
wenzelm@53784
   516
wenzelm@55692
   517
      val range = item.range
wenzelm@56863
   518
      if (text_field.isEditable) {
wenzelm@53784
   519
        val content = text_field.getText
wenzelm@55692
   520
        JEdit_Lib.try_get_text(content, range) match {
wenzelm@53784
   521
          case Some(text) if text == item.original =>
wenzelm@53784
   522
            text_field.setText(
wenzelm@55692
   523
              content.substring(0, range.start) +
wenzelm@53784
   524
              item.replacement +
wenzelm@55692
   525
              content.substring(range.stop))
wenzelm@55692
   526
            text_field.getCaret.setDot(range.start + item.replacement.length + item.move)
wenzelm@53784
   527
          case _ =>
wenzelm@53784
   528
        }
wenzelm@53784
   529
      }
wenzelm@53784
   530
    }
wenzelm@53784
   531
wenzelm@53784
   532
wenzelm@56170
   533
    /* completion action: text field */
wenzelm@53784
   534
wenzelm@53784
   535
    def action()
wenzelm@53784
   536
    {
wenzelm@53784
   537
      GUI.layered_pane(text_field) match {
wenzelm@53784
   538
        case Some(layered) if text_field.isEditable =>
wenzelm@53784
   539
          val history = PIDE.completion_history.value
wenzelm@53784
   540
wenzelm@53784
   541
          val caret = text_field.getCaret.getDot
wenzelm@55813
   542
          val text = text_field.getText
wenzelm@55748
   543
wenzelm@55749
   544
          val context = syntax.language_context
wenzelm@55748
   545
wenzelm@57588
   546
          syntax.completion.complete(history, true, false, 0, text, caret, context) match {
wenzelm@53784
   547
            case Some(result) =>
wenzelm@53784
   548
              val fm = text_field.getFontMetrics(text_field.getFont)
wenzelm@53784
   549
              val loc =
wenzelm@53784
   550
                SwingUtilities.convertPoint(text_field, fm.stringWidth(text), fm.getHeight, layered)
wenzelm@53784
   551
wenzelm@55978
   552
              val items = result.items.map(new Item(_))
wenzelm@55711
   553
              val completion =
wenzelm@55978
   554
                new Completion_Popup(None, layered, loc, text_field.getFont, items)
wenzelm@55978
   555
                {
wenzelm@55978
   556
                  override def complete(item: Completion.Item) {
wenzelm@55978
   557
                    PIDE.completion_history.update(item)
wenzelm@55978
   558
                    insert(item)
wenzelm@55978
   559
                  }
wenzelm@55978
   560
                  override def propagate(evt: KeyEvent) {
wenzelm@55978
   561
                    if (!evt.isConsumed) text_field.processKeyEvent(evt)
wenzelm@55978
   562
                  }
wenzelm@56197
   563
                  override def shutdown(focus: Boolean) { if (focus) text_field.requestFocus }
wenzelm@53784
   564
                }
wenzelm@57127
   565
              dismissed()
wenzelm@53784
   566
              completion_popup = Some(completion)
wenzelm@56197
   567
              completion.show_popup(true)
wenzelm@53784
   568
wenzelm@53784
   569
            case None =>
wenzelm@53784
   570
          }
wenzelm@53784
   571
        case _ =>
wenzelm@53784
   572
      }
wenzelm@53784
   573
    }
wenzelm@53784
   574
wenzelm@53784
   575
wenzelm@53784
   576
    /* process key event */
wenzelm@53784
   577
wenzelm@53784
   578
    private def process(evt: KeyEvent)
wenzelm@53784
   579
    {
wenzelm@53784
   580
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53784
   581
        dismissed()
wenzelm@53784
   582
        if (evt.getKeyChar != '\b') {
wenzelm@53784
   583
          val special = JEdit_Lib.special_key(evt)
wenzelm@53784
   584
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53784
   585
            process_delay.revoke()
wenzelm@53784
   586
            action()
wenzelm@53784
   587
          }
wenzelm@53784
   588
          else process_delay.invoke()
wenzelm@53784
   589
        }
wenzelm@53784
   590
      }
wenzelm@53784
   591
    }
wenzelm@53784
   592
wenzelm@53784
   593
    private val process_delay =
wenzelm@57612
   594
      GUI_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53784
   595
        action()
wenzelm@53784
   596
      }
wenzelm@53784
   597
wenzelm@53784
   598
    override def processKeyEvent(evt0: KeyEvent)
wenzelm@53784
   599
    {
wenzelm@53784
   600
      val evt = KeyEventWorkaround.processKeyEvent(evt0)
wenzelm@53784
   601
      if (evt != null) {
wenzelm@53784
   602
        evt.getID match {
wenzelm@53784
   603
          case KeyEvent.KEY_PRESSED =>
wenzelm@53784
   604
            val key_code = evt.getKeyCode
wenzelm@53784
   605
            if (key_code == KeyEvent.VK_ESCAPE) {
wenzelm@53784
   606
              if (dismissed()) evt.consume
wenzelm@53784
   607
            }
wenzelm@53784
   608
          case KeyEvent.KEY_TYPED =>
wenzelm@53784
   609
            super.processKeyEvent(evt)
wenzelm@53784
   610
            process(evt)
wenzelm@53784
   611
            evt.consume
wenzelm@53784
   612
          case _ =>
wenzelm@53784
   613
        }
wenzelm@53784
   614
        if (!evt.isConsumed) super.processKeyEvent(evt)
wenzelm@53784
   615
      }
wenzelm@53784
   616
    }
wenzelm@53784
   617
  }
wenzelm@53023
   618
}
wenzelm@53023
   619
wenzelm@53233
   620
wenzelm@53023
   621
class Completion_Popup private(
wenzelm@56605
   622
  opt_range: Option[Text.Range],
wenzelm@53246
   623
  layered: JLayeredPane,
wenzelm@53246
   624
  location: Point,
wenzelm@53272
   625
  font: Font,
wenzelm@55978
   626
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   627
{
wenzelm@53023
   628
  completion =>
wenzelm@53023
   629
wenzelm@57612
   630
  GUI_Thread.require {}
wenzelm@53405
   631
wenzelm@59319
   632
  require(items.nonEmpty)
wenzelm@53405
   633
  val multi = items.length > 1
wenzelm@53232
   634
wenzelm@53232
   635
wenzelm@53232
   636
  /* actions */
wenzelm@53232
   637
wenzelm@53275
   638
  def complete(item: Completion.Item) { }
wenzelm@53232
   639
  def propagate(evt: KeyEvent) { }
wenzelm@56197
   640
  def shutdown(focus: Boolean) { }
wenzelm@53023
   641
wenzelm@53023
   642
wenzelm@53023
   643
  /* list view */
wenzelm@53023
   644
wenzelm@53023
   645
  private val list_view = new ListView(items)
wenzelm@53272
   646
  list_view.font = font
wenzelm@53023
   647
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   648
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   649
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   650
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   651
wenzelm@53398
   652
  for (cond <-
wenzelm@53398
   653
    List(JComponent.WHEN_FOCUSED,
wenzelm@53398
   654
      JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
wenzelm@53398
   655
      JComponent.WHEN_IN_FOCUSED_WINDOW)) list_view.peer.setInputMap(cond, null)
wenzelm@53398
   656
wenzelm@53023
   657
  private def complete_selected(): Boolean =
wenzelm@53023
   658
  {
wenzelm@53023
   659
    list_view.selection.items.toList match {
wenzelm@55978
   660
      case item :: _ => complete(item.item); true
wenzelm@53023
   661
      case _ => false
wenzelm@53023
   662
    }
wenzelm@53023
   663
  }
wenzelm@53023
   664
wenzelm@53023
   665
  private def move_items(n: Int)
wenzelm@53023
   666
  {
wenzelm@53023
   667
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   668
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   669
    if (i != j) {
wenzelm@53023
   670
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   671
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   672
    }
wenzelm@53023
   673
  }
wenzelm@53023
   674
wenzelm@53023
   675
  private def move_pages(n: Int)
wenzelm@53023
   676
  {
wenzelm@53023
   677
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   678
    move_items(page_size * n)
wenzelm@53023
   679
  }
wenzelm@53023
   680
wenzelm@53023
   681
wenzelm@53023
   682
  /* event handling */
wenzelm@53023
   683
wenzelm@56197
   684
  val inner_key_listener =
wenzelm@53226
   685
    JEdit_Lib.key_listener(
wenzelm@53226
   686
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   687
        {
wenzelm@53226
   688
          if (!e.isConsumed) {
wenzelm@53226
   689
            e.getKeyCode match {
wenzelm@57833
   690
              case KeyEvent.VK_ENTER if PIDE.options.bool("jedit_completion_select_enter") =>
wenzelm@57833
   691
                if (complete_selected()) e.consume
wenzelm@57833
   692
                hide_popup()
wenzelm@57833
   693
              case KeyEvent.VK_TAB if PIDE.options.bool("jedit_completion_select_tab") =>
wenzelm@53226
   694
                if (complete_selected()) e.consume
wenzelm@53226
   695
                hide_popup()
wenzelm@53226
   696
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   697
                hide_popup()
wenzelm@53226
   698
                e.consume
wenzelm@53405
   699
              case KeyEvent.VK_UP | KeyEvent.VK_KP_UP if multi =>
wenzelm@53407
   700
                move_items(-1)
wenzelm@53407
   701
                e.consume
wenzelm@53405
   702
              case KeyEvent.VK_DOWN | KeyEvent.VK_KP_DOWN if multi =>
wenzelm@53407
   703
                move_items(1)
wenzelm@53407
   704
                e.consume
wenzelm@53405
   705
              case KeyEvent.VK_PAGE_UP if multi =>
wenzelm@53407
   706
                move_pages(-1)
wenzelm@53407
   707
                e.consume
wenzelm@53405
   708
              case KeyEvent.VK_PAGE_DOWN if multi =>
wenzelm@53407
   709
                move_pages(1)
wenzelm@53407
   710
                e.consume
wenzelm@53226
   711
              case _ =>
wenzelm@53226
   712
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   713
                  hide_popup()
wenzelm@53226
   714
            }
wenzelm@53226
   715
          }
wenzelm@53232
   716
          propagate(e)
wenzelm@53226
   717
        },
wenzelm@53232
   718
      key_typed = propagate _
wenzelm@53226
   719
    )
wenzelm@53023
   720
wenzelm@53274
   721
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   722
wenzelm@53023
   723
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   724
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   725
      if (complete_selected()) e.consume
wenzelm@53023
   726
      hide_popup()
wenzelm@53023
   727
    }
wenzelm@53023
   728
  })
wenzelm@53023
   729
wenzelm@53023
   730
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   731
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   732
  })
wenzelm@53023
   733
wenzelm@53023
   734
wenzelm@53023
   735
  /* main content */
wenzelm@53023
   736
wenzelm@53023
   737
  override def getFocusTraversalKeysEnabled = false
wenzelm@53297
   738
  completion.setBorder(new LineBorder(Color.BLACK))
wenzelm@53023
   739
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   740
wenzelm@53023
   741
wenzelm@53023
   742
  /* popup */
wenzelm@53023
   743
wenzelm@56605
   744
  def active_range: Option[Text.Range] =
wenzelm@56605
   745
    if (isDisplayable) opt_range else None
wenzelm@56605
   746
wenzelm@53023
   747
  private val popup =
wenzelm@53023
   748
  {
wenzelm@53247
   749
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   750
    val size =
wenzelm@53230
   751
    {
wenzelm@53230
   752
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@64621
   753
      val bounds = JEdit_Rendering.popup_bounds
wenzelm@53247
   754
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   755
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   756
      new Dimension(w, h)
wenzelm@53230
   757
    }
wenzelm@53247
   758
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   759
  }
wenzelm@53246
   760
wenzelm@56197
   761
  private def show_popup(focus: Boolean)
wenzelm@53246
   762
  {
wenzelm@53246
   763
    popup.show
wenzelm@56197
   764
    if (focus) list_view.requestFocus
wenzelm@53023
   765
  }
wenzelm@53023
   766
wenzelm@53232
   767
  private def hide_popup()
wenzelm@53023
   768
  {
wenzelm@56197
   769
    shutdown(list_view.peer.isFocusOwner)
wenzelm@56197
   770
    popup.hide
wenzelm@53023
   771
  }
wenzelm@53023
   772
}