src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Thu Aug 29 22:35:50 2013 +0200 (2013-08-29 ago)
changeset 53296 65c60c782da5
parent 53293 fd27b8f5a479
child 53297 64c31de7f21c
permissions -rw-r--r--
less aggressive immediate completion, based on input and text;
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@53229
    12
import java.awt.{Font, Point, BorderLayout, Dimension}
wenzelm@53296
    13
import java.awt.event.{InputEvent, KeyEvent, MouseEvent, MouseAdapter, FocusAdapter, FocusEvent}
wenzelm@53246
    14
import javax.swing.{JPanel, JComponent, JLayeredPane, SwingUtilities}
wenzelm@53023
    15
wenzelm@53023
    16
import scala.swing.{ListView, ScrollPane}
wenzelm@53023
    17
import scala.swing.event.MouseClicked
wenzelm@53023
    18
wenzelm@53296
    19
import org.gjt.sp.jedit.{View, Debug}
wenzelm@53228
    20
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@53023
    21
wenzelm@53023
    22
wenzelm@53023
    23
object Completion_Popup
wenzelm@53023
    24
{
wenzelm@53272
    25
  /* setup for jEdit text area */
wenzelm@53235
    26
wenzelm@53242
    27
  object Text_Area
wenzelm@53233
    28
  {
wenzelm@53274
    29
    private val key = new Object
wenzelm@53274
    30
wenzelm@53274
    31
    def apply(text_area: JEditTextArea): Option[Completion_Popup.Text_Area] =
wenzelm@53274
    32
      text_area.getClientProperty(key) match {
wenzelm@53274
    33
        case text_area_completion: Completion_Popup.Text_Area => Some(text_area_completion)
wenzelm@53274
    34
        case _ => None
wenzelm@53274
    35
      }
wenzelm@53274
    36
wenzelm@53274
    37
    def exit(text_area: JEditTextArea)
wenzelm@53274
    38
    {
wenzelm@53274
    39
      Swing_Thread.require()
wenzelm@53274
    40
      apply(text_area) match {
wenzelm@53274
    41
        case None =>
wenzelm@53274
    42
        case Some(text_area_completion) =>
wenzelm@53274
    43
          text_area_completion.deactivate()
wenzelm@53274
    44
          text_area.putClientProperty(key, null)
wenzelm@53274
    45
      }
wenzelm@53274
    46
    }
wenzelm@53274
    47
wenzelm@53274
    48
    def init(text_area: JEditTextArea): Completion_Popup.Text_Area =
wenzelm@53274
    49
    {
wenzelm@53274
    50
      exit(text_area)
wenzelm@53274
    51
      val text_area_completion = new Text_Area(text_area)
wenzelm@53274
    52
      text_area.putClientProperty(key, text_area_completion)
wenzelm@53274
    53
      text_area_completion.activate()
wenzelm@53274
    54
      text_area_completion
wenzelm@53274
    55
    }
wenzelm@53274
    56
wenzelm@53274
    57
    def dismissed(text_area: JEditTextArea): Boolean =
wenzelm@53274
    58
    {
wenzelm@53274
    59
      Swing_Thread.require()
wenzelm@53274
    60
      apply(text_area) match {
wenzelm@53274
    61
        case Some(text_area_completion) => text_area_completion.dismissed()
wenzelm@53274
    62
        case None => false
wenzelm@53274
    63
      }
wenzelm@53274
    64
    }
wenzelm@53272
    65
  }
wenzelm@53272
    66
wenzelm@53274
    67
  class Text_Area private(text_area: JEditTextArea)
wenzelm@53272
    68
  {
wenzelm@53272
    69
    private var completion_popup: Option[Completion_Popup] = None
wenzelm@53272
    70
wenzelm@53272
    71
wenzelm@53292
    72
    /* completion action */
wenzelm@53273
    73
wenzelm@53275
    74
    private def insert(item: Completion.Item)
wenzelm@53242
    75
    {
wenzelm@53242
    76
      Swing_Thread.require()
wenzelm@53023
    77
wenzelm@53242
    78
      val buffer = text_area.getBuffer
wenzelm@53242
    79
      val len = item.original.length
wenzelm@53242
    80
      if (buffer.isEditable && len > 0) {
wenzelm@53242
    81
        JEdit_Lib.buffer_edit(buffer) {
wenzelm@53242
    82
          val caret = text_area.getCaretPosition
wenzelm@53242
    83
          JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
wenzelm@53242
    84
            case Some(text) if text == item.original =>
wenzelm@53242
    85
              buffer.remove(caret - len, len)
wenzelm@53242
    86
              buffer.insert(caret - len, item.replacement)
wenzelm@53242
    87
            case _ =>
wenzelm@53242
    88
          }
wenzelm@53233
    89
        }
wenzelm@53233
    90
      }
wenzelm@53233
    91
    }
wenzelm@53242
    92
wenzelm@53292
    93
    def action(immediate: Boolean)
wenzelm@53292
    94
    {
wenzelm@53292
    95
      val view = text_area.getView
wenzelm@53292
    96
      val layered = view.getLayeredPane
wenzelm@53292
    97
      val buffer = text_area.getBuffer
wenzelm@53292
    98
      val painter = text_area.getPainter
wenzelm@53273
    99
wenzelm@53292
   100
      if (buffer.isEditable) {
wenzelm@53292
   101
        Isabelle.mode_syntax(JEdit_Lib.buffer_mode(buffer)) match {
wenzelm@53292
   102
          case Some(syntax) =>
wenzelm@53292
   103
            val caret = text_area.getCaretPosition
wenzelm@53292
   104
            val line = buffer.getLineOfOffset(caret)
wenzelm@53292
   105
            val start = buffer.getLineStartOffset(line)
wenzelm@53292
   106
            val text = buffer.getSegment(start, caret - start)
wenzelm@53292
   107
wenzelm@53292
   108
            syntax.completion.complete(Isabelle_Encoding.is_active(buffer), text) match {
wenzelm@53296
   109
              case Some((_, List(item))) if item.immediate && immediate =>
wenzelm@53292
   110
                insert(item)
wenzelm@53292
   111
wenzelm@53292
   112
              case Some((original, items)) =>
wenzelm@53292
   113
                val font =
wenzelm@53292
   114
                  painter.getFont.deriveFont(Rendering.font_size("jedit_popup_font_scale"))
wenzelm@53292
   115
wenzelm@53292
   116
                val loc1 = text_area.offsetToXY(caret - original.length)
wenzelm@53292
   117
                if (loc1 != null) {
wenzelm@53292
   118
                  val loc2 =
wenzelm@53292
   119
                    SwingUtilities.convertPoint(painter,
wenzelm@53292
   120
                      loc1.x, loc1.y + painter.getFontMetrics.getHeight, layered)
wenzelm@53292
   121
wenzelm@53292
   122
                  val completion =
wenzelm@53292
   123
                    new Completion_Popup(layered, loc2, font, items) {
wenzelm@53292
   124
                      override def complete(item: Completion.Item) { insert(item) }
wenzelm@53292
   125
                      override def propagate(evt: KeyEvent) {
wenzelm@53292
   126
                        JEdit_Lib.propagate_key(view, evt)
wenzelm@53292
   127
                        input(evt)
wenzelm@53292
   128
                      }
wenzelm@53292
   129
                      override def refocus() { text_area.requestFocus }
wenzelm@53292
   130
                    }
wenzelm@53292
   131
                  completion_popup = Some(completion)
wenzelm@53292
   132
                  completion.show_popup()
wenzelm@53292
   133
                }
wenzelm@53292
   134
              case None =>
wenzelm@53292
   135
            }
wenzelm@53292
   136
          case None =>
wenzelm@53292
   137
        }
wenzelm@53292
   138
      }
wenzelm@53292
   139
    }
wenzelm@53292
   140
wenzelm@53292
   141
wenzelm@53292
   142
    /* input key events */
wenzelm@53273
   143
wenzelm@53272
   144
    def input(evt: KeyEvent)
wenzelm@53242
   145
    {
wenzelm@53242
   146
      Swing_Thread.require()
wenzelm@53233
   147
wenzelm@53273
   148
      if (PIDE.options.bool("jedit_completion")) {
wenzelm@53273
   149
        if (!evt.isConsumed) {
wenzelm@53273
   150
          dismissed()
wenzelm@53296
   151
wenzelm@53296
   152
          val mod = evt.getModifiers
wenzelm@53296
   153
          val special =
wenzelm@53296
   154
            evt.getKeyChar == '\b' ||
wenzelm@53296
   155
            // cf. 5.1.0/jEdit/org/gjt/sp/jedit/gui/KeyEventWorkaround.java
wenzelm@53296
   156
            (mod & InputEvent.CTRL_MASK) != 0 && (mod & InputEvent.ALT_MASK) == 0 ||
wenzelm@53296
   157
            (mod & InputEvent.CTRL_MASK) == 0 && (mod & InputEvent.ALT_MASK) != 0 &&
wenzelm@53296
   158
              !Debug.ALT_KEY_PRESSED_DISABLED ||
wenzelm@53296
   159
            (mod & InputEvent.META_MASK) != 0
wenzelm@53296
   160
wenzelm@53296
   161
          if (PIDE.options.seconds("jedit_completion_delay").is_zero && !special) {
wenzelm@53292
   162
            input_delay.revoke()
wenzelm@53292
   163
            action(PIDE.options.bool("jedit_completion_immediate"))
wenzelm@53292
   164
          }
wenzelm@53292
   165
          else input_delay.invoke()
wenzelm@53242
   166
        }
wenzelm@53228
   167
      }
wenzelm@53228
   168
    }
wenzelm@53273
   169
wenzelm@53273
   170
    private val input_delay =
wenzelm@53292
   171
      Swing_Thread.delay_last(PIDE.options.seconds("jedit_completion_delay")) {
wenzelm@53292
   172
        action(false)
wenzelm@53292
   173
      }
wenzelm@53273
   174
wenzelm@53273
   175
wenzelm@53292
   176
    /* dismiss popup */
wenzelm@53273
   177
wenzelm@53292
   178
    def dismissed(): Boolean =
wenzelm@53292
   179
    {
wenzelm@53292
   180
      Swing_Thread.require()
wenzelm@53273
   181
wenzelm@53292
   182
      completion_popup match {
wenzelm@53292
   183
        case Some(completion) =>
wenzelm@53292
   184
          completion.hide_popup()
wenzelm@53292
   185
          completion_popup = None
wenzelm@53292
   186
          true
wenzelm@53292
   187
        case None =>
wenzelm@53292
   188
          false
wenzelm@53273
   189
      }
wenzelm@53292
   190
    }
wenzelm@53274
   191
wenzelm@53274
   192
wenzelm@53274
   193
    /* activation */
wenzelm@53274
   194
wenzelm@53274
   195
    private val outer_key_listener =
wenzelm@53274
   196
      JEdit_Lib.key_listener(key_typed = input _)
wenzelm@53274
   197
wenzelm@53274
   198
    private def activate()
wenzelm@53274
   199
    {
wenzelm@53274
   200
      text_area.addKeyListener(outer_key_listener)
wenzelm@53274
   201
    }
wenzelm@53274
   202
wenzelm@53274
   203
    private def deactivate()
wenzelm@53274
   204
    {
wenzelm@53274
   205
      dismissed()
wenzelm@53274
   206
      text_area.removeKeyListener(outer_key_listener)
wenzelm@53274
   207
    }
wenzelm@53228
   208
  }
wenzelm@53023
   209
}
wenzelm@53023
   210
wenzelm@53233
   211
wenzelm@53023
   212
class Completion_Popup private(
wenzelm@53246
   213
  layered: JLayeredPane,
wenzelm@53246
   214
  location: Point,
wenzelm@53272
   215
  font: Font,
wenzelm@53275
   216
  items: List[Completion.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   217
{
wenzelm@53023
   218
  completion =>
wenzelm@53023
   219
wenzelm@53023
   220
  Swing_Thread.require()
wenzelm@53232
   221
  require(!items.isEmpty)
wenzelm@53232
   222
wenzelm@53232
   223
wenzelm@53232
   224
  /* actions */
wenzelm@53232
   225
wenzelm@53275
   226
  def complete(item: Completion.Item) { }
wenzelm@53232
   227
  def propagate(evt: KeyEvent) { }
wenzelm@53234
   228
  def refocus() { }
wenzelm@53023
   229
wenzelm@53023
   230
wenzelm@53023
   231
  /* list view */
wenzelm@53023
   232
wenzelm@53023
   233
  private val list_view = new ListView(items)
wenzelm@53272
   234
  list_view.font = font
wenzelm@53023
   235
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   236
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   237
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   238
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   239
wenzelm@53023
   240
  private def complete_selected(): Boolean =
wenzelm@53023
   241
  {
wenzelm@53023
   242
    list_view.selection.items.toList match {
wenzelm@53023
   243
      case item :: _ => complete(item); true
wenzelm@53023
   244
      case _ => false
wenzelm@53023
   245
    }
wenzelm@53023
   246
  }
wenzelm@53023
   247
wenzelm@53023
   248
  private def move_items(n: Int)
wenzelm@53023
   249
  {
wenzelm@53023
   250
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   251
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   252
    if (i != j) {
wenzelm@53023
   253
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   254
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   255
    }
wenzelm@53023
   256
  }
wenzelm@53023
   257
wenzelm@53023
   258
  private def move_pages(n: Int)
wenzelm@53023
   259
  {
wenzelm@53023
   260
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   261
    move_items(page_size * n)
wenzelm@53023
   262
  }
wenzelm@53023
   263
wenzelm@53023
   264
wenzelm@53023
   265
  /* event handling */
wenzelm@53023
   266
wenzelm@53274
   267
  private val inner_key_listener =
wenzelm@53226
   268
    JEdit_Lib.key_listener(
wenzelm@53226
   269
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   270
        {
wenzelm@53226
   271
          if (!e.isConsumed) {
wenzelm@53226
   272
            e.getKeyCode match {
wenzelm@53226
   273
              case KeyEvent.VK_TAB =>
wenzelm@53226
   274
                if (complete_selected()) e.consume
wenzelm@53226
   275
                hide_popup()
wenzelm@53226
   276
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   277
                hide_popup()
wenzelm@53226
   278
                e.consume
wenzelm@53226
   279
              case KeyEvent.VK_UP => move_items(-1); e.consume
wenzelm@53226
   280
              case KeyEvent.VK_DOWN => move_items(1); e.consume
wenzelm@53226
   281
              case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
wenzelm@53226
   282
              case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
wenzelm@53226
   283
              case _ =>
wenzelm@53226
   284
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   285
                  hide_popup()
wenzelm@53226
   286
            }
wenzelm@53226
   287
          }
wenzelm@53232
   288
          propagate(e)
wenzelm@53226
   289
        },
wenzelm@53232
   290
      key_typed = propagate _
wenzelm@53226
   291
    )
wenzelm@53023
   292
wenzelm@53274
   293
  list_view.peer.addKeyListener(inner_key_listener)
wenzelm@53023
   294
wenzelm@53023
   295
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   296
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   297
      if (complete_selected()) e.consume
wenzelm@53023
   298
      hide_popup()
wenzelm@53023
   299
    }
wenzelm@53023
   300
  })
wenzelm@53023
   301
wenzelm@53023
   302
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   303
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   304
  })
wenzelm@53023
   305
wenzelm@53023
   306
wenzelm@53023
   307
  /* main content */
wenzelm@53023
   308
wenzelm@53023
   309
  override def getFocusTraversalKeysEnabled = false
wenzelm@53023
   310
wenzelm@53023
   311
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   312
wenzelm@53023
   313
wenzelm@53023
   314
  /* popup */
wenzelm@53023
   315
wenzelm@53023
   316
  private val popup =
wenzelm@53023
   317
  {
wenzelm@53247
   318
    val screen = JEdit_Lib.screen_location(layered, location)
wenzelm@53247
   319
    val size =
wenzelm@53230
   320
    {
wenzelm@53230
   321
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   322
      val bounds = Rendering.popup_bounds
wenzelm@53247
   323
      val w = geometry.width min (screen.bounds.width * bounds).toInt min layered.getWidth
wenzelm@53247
   324
      val h = geometry.height min (screen.bounds.height * bounds).toInt min layered.getHeight
wenzelm@53247
   325
      new Dimension(w, h)
wenzelm@53230
   326
    }
wenzelm@53247
   327
    new Popup(layered, completion, screen.relative(layered, size), size)
wenzelm@53246
   328
  }
wenzelm@53246
   329
wenzelm@53246
   330
  private def show_popup()
wenzelm@53246
   331
  {
wenzelm@53246
   332
    popup.show
wenzelm@53246
   333
    list_view.requestFocus
wenzelm@53023
   334
  }
wenzelm@53023
   335
wenzelm@53232
   336
  private def hide_popup()
wenzelm@53023
   337
  {
wenzelm@53234
   338
    val had_focus = list_view.peer.isFocusOwner
wenzelm@53229
   339
    popup.hide
wenzelm@53234
   340
    if (had_focus) refocus()
wenzelm@53023
   341
  }
wenzelm@53023
   342
}
wenzelm@53023
   343