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