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