src/Tools/jEdit/src/completion_popup.scala
author wenzelm
Tue Aug 27 20:45:02 2013 +0200 (2013-08-27 ago)
changeset 53233 4b422e5d64fd
parent 53232 4a3762693452
child 53234 ea4abbbe1702
permissions -rw-r--r--
some actual completion via outer syntax;
disable old SideKick completion for "isabelle" mode;
wenzelm@53023
     1
/*  Title:      Tools/jEdit/src/completion_popup.scala
wenzelm@53023
     2
    Author:     Makarius
wenzelm@53023
     3
wenzelm@53023
     4
Completion popup based on javax.swing.PopupFactory.
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@53228
    14
import javax.swing.{JPanel, JComponent, PopupFactory, 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@53233
    25
  private sealed case class Item(original: String, replacement: String, description: String)
wenzelm@53233
    26
  { override def toString: String = description }
wenzelm@53233
    27
wenzelm@53233
    28
  private def complete_item(text_area: JEditTextArea, item: Item)
wenzelm@53233
    29
  {
wenzelm@53233
    30
    Swing_Thread.require()
wenzelm@53023
    31
wenzelm@53233
    32
    val buffer = text_area.getBuffer
wenzelm@53233
    33
    val len = item.original.length
wenzelm@53233
    34
    if (buffer.isEditable && len > 0) {
wenzelm@53233
    35
      JEdit_Lib.buffer_edit(buffer) {
wenzelm@53233
    36
        val caret = text_area.getCaretPosition
wenzelm@53233
    37
        JEdit_Lib.try_get_text(buffer, Text.Range(caret - len, caret)) match {
wenzelm@53233
    38
          case Some(text) if text == item.original =>
wenzelm@53233
    39
            buffer.remove(caret - len, len)
wenzelm@53233
    40
            buffer.insert(caret - len, item.replacement)
wenzelm@53233
    41
          case _ =>
wenzelm@53233
    42
        }
wenzelm@53233
    43
      }
wenzelm@53233
    44
    }
wenzelm@53233
    45
  }
wenzelm@53233
    46
wenzelm@53233
    47
  def input_text_area(text_area: JEditTextArea, get_syntax: => Option[Outer_Syntax], evt: KeyEvent)
wenzelm@53228
    48
  {
wenzelm@53228
    49
    Swing_Thread.require()
wenzelm@53228
    50
wenzelm@53228
    51
    val buffer = text_area.getBuffer
wenzelm@53228
    52
    if (buffer.isEditable) {
wenzelm@53233
    53
      get_syntax match {
wenzelm@53233
    54
        case None =>
wenzelm@53233
    55
        case Some(syntax) =>
wenzelm@53233
    56
          val view = text_area.getView
wenzelm@53233
    57
          val painter = text_area.getPainter
wenzelm@53228
    58
wenzelm@53233
    59
          val caret = text_area.getCaretPosition
wenzelm@53233
    60
          val completion =
wenzelm@53233
    61
          {
wenzelm@53233
    62
            val line = buffer.getLineOfOffset(caret)
wenzelm@53233
    63
            val start = buffer.getLineStartOffset(line)
wenzelm@53233
    64
            val text = buffer.getSegment(start, caret - start)
wenzelm@53228
    65
wenzelm@53233
    66
            syntax.completion.complete(text) match {
wenzelm@53233
    67
              case None => None
wenzelm@53233
    68
              case Some((word, cs)) =>
wenzelm@53233
    69
                val ds =
wenzelm@53233
    70
                  (if (Isabelle_Encoding.is_active(buffer))
wenzelm@53233
    71
                    cs.map(Symbol.decode(_)).sorted
wenzelm@53233
    72
                   else cs).filter(_ != word)
wenzelm@53233
    73
                if (ds.isEmpty) None
wenzelm@53233
    74
                else Some((word, ds.map(s => Item(word, s, s))))
wenzelm@53233
    75
            }
wenzelm@53233
    76
          }
wenzelm@53233
    77
          completion match {
wenzelm@53233
    78
            case None =>
wenzelm@53233
    79
            case Some((original, items)) =>
wenzelm@53233
    80
              val popup_font =
wenzelm@53233
    81
                painter.getFont.deriveFont(
wenzelm@53233
    82
                  (painter.getFont.getSize2D * PIDE.options.real("jedit_popup_font_scale")).toFloat
wenzelm@53233
    83
                    max 10.0f)
wenzelm@53229
    84
wenzelm@53233
    85
              val location = text_area.offsetToXY(caret - original.length)
wenzelm@53233
    86
              if (location != null) {
wenzelm@53233
    87
                location.y = location.y + painter.getFontMetrics.getHeight
wenzelm@53233
    88
                SwingUtilities.convertPointToScreen(location, painter)
wenzelm@53233
    89
                new Completion_Popup(view.getRootPane, popup_font, location, items) {
wenzelm@53233
    90
                  override def complete(item: Item) { complete_item(text_area, item) }
wenzelm@53233
    91
                  override def propagate(evt: KeyEvent) { JEdit_Lib.propagate_key(view, evt) }
wenzelm@53233
    92
                  override def hidden() { text_area.requestFocus }
wenzelm@53233
    93
                }
wenzelm@53233
    94
              }
wenzelm@53232
    95
          }
wenzelm@53228
    96
      }
wenzelm@53228
    97
    }
wenzelm@53228
    98
  }
wenzelm@53023
    99
}
wenzelm@53023
   100
wenzelm@53233
   101
wenzelm@53023
   102
class Completion_Popup private(
wenzelm@53229
   103
  root: JComponent,
wenzelm@53229
   104
  popup_font: Font,
wenzelm@53023
   105
  screen_point: Point,
wenzelm@53232
   106
  items: List[Completion_Popup.Item]) extends JPanel(new BorderLayout)
wenzelm@53023
   107
{
wenzelm@53023
   108
  completion =>
wenzelm@53023
   109
wenzelm@53023
   110
  Swing_Thread.require()
wenzelm@53232
   111
  require(!items.isEmpty)
wenzelm@53232
   112
wenzelm@53232
   113
wenzelm@53232
   114
  /* actions */
wenzelm@53232
   115
wenzelm@53232
   116
  def complete(item: Completion_Popup.Item) { }
wenzelm@53232
   117
  def propagate(evt: KeyEvent) { }
wenzelm@53232
   118
  def hidden() { }
wenzelm@53023
   119
wenzelm@53023
   120
wenzelm@53023
   121
  /* list view */
wenzelm@53023
   122
wenzelm@53023
   123
  private val list_view = new ListView(items)
wenzelm@53023
   124
  {
wenzelm@53229
   125
    font = popup_font
wenzelm@53023
   126
  }
wenzelm@53023
   127
  list_view.selection.intervalMode = ListView.IntervalMode.Single
wenzelm@53023
   128
  list_view.peer.setFocusTraversalKeysEnabled(false)
wenzelm@53023
   129
  list_view.peer.setVisibleRowCount(items.length min 8)
wenzelm@53023
   130
  list_view.peer.setSelectedIndex(0)
wenzelm@53023
   131
wenzelm@53023
   132
  private def complete_selected(): Boolean =
wenzelm@53023
   133
  {
wenzelm@53023
   134
    list_view.selection.items.toList match {
wenzelm@53023
   135
      case item :: _ => complete(item); true
wenzelm@53023
   136
      case _ => false
wenzelm@53023
   137
    }
wenzelm@53023
   138
  }
wenzelm@53023
   139
wenzelm@53023
   140
  private def move_items(n: Int)
wenzelm@53023
   141
  {
wenzelm@53023
   142
    val i = list_view.peer.getSelectedIndex
wenzelm@53023
   143
    val j = ((i + n) min (items.length - 1)) max 0
wenzelm@53023
   144
    if (i != j) {
wenzelm@53023
   145
      list_view.peer.setSelectedIndex(j)
wenzelm@53023
   146
      list_view.peer.ensureIndexIsVisible(j)
wenzelm@53023
   147
    }
wenzelm@53023
   148
  }
wenzelm@53023
   149
wenzelm@53023
   150
  private def move_pages(n: Int)
wenzelm@53023
   151
  {
wenzelm@53023
   152
    val page_size = list_view.peer.getVisibleRowCount - 1
wenzelm@53023
   153
    move_items(page_size * n)
wenzelm@53023
   154
  }
wenzelm@53023
   155
wenzelm@53023
   156
wenzelm@53023
   157
  /* event handling */
wenzelm@53023
   158
wenzelm@53226
   159
  private val key_listener =
wenzelm@53226
   160
    JEdit_Lib.key_listener(
wenzelm@53226
   161
      workaround = false,
wenzelm@53226
   162
      key_pressed = (e: KeyEvent) =>
wenzelm@53226
   163
        {
wenzelm@53226
   164
          if (!e.isConsumed) {
wenzelm@53226
   165
            e.getKeyCode match {
wenzelm@53226
   166
              case KeyEvent.VK_TAB =>
wenzelm@53226
   167
                if (complete_selected()) e.consume
wenzelm@53226
   168
                hide_popup()
wenzelm@53226
   169
              case KeyEvent.VK_ESCAPE =>
wenzelm@53226
   170
                hide_popup()
wenzelm@53226
   171
                e.consume
wenzelm@53226
   172
              case KeyEvent.VK_UP => move_items(-1); e.consume
wenzelm@53226
   173
              case KeyEvent.VK_DOWN => move_items(1); e.consume
wenzelm@53226
   174
              case KeyEvent.VK_PAGE_UP => move_pages(-1); e.consume
wenzelm@53226
   175
              case KeyEvent.VK_PAGE_DOWN => move_pages(1); e.consume
wenzelm@53226
   176
              case _ =>
wenzelm@53226
   177
                if (e.isActionKey || e.isAltDown || e.isMetaDown || e.isControlDown)
wenzelm@53226
   178
                  hide_popup()
wenzelm@53226
   179
            }
wenzelm@53226
   180
          }
wenzelm@53232
   181
          propagate(e)
wenzelm@53226
   182
        },
wenzelm@53232
   183
      key_typed = propagate _
wenzelm@53226
   184
    )
wenzelm@53023
   185
wenzelm@53226
   186
  list_view.peer.addKeyListener(key_listener)
wenzelm@53023
   187
wenzelm@53023
   188
  list_view.peer.addMouseListener(new MouseAdapter {
wenzelm@53023
   189
    override def mouseClicked(e: MouseEvent) {
wenzelm@53023
   190
      if (complete_selected()) e.consume
wenzelm@53023
   191
      hide_popup()
wenzelm@53023
   192
    }
wenzelm@53023
   193
  })
wenzelm@53023
   194
wenzelm@53023
   195
  list_view.peer.addFocusListener(new FocusAdapter {
wenzelm@53023
   196
    override def focusLost(e: FocusEvent) { hide_popup() }
wenzelm@53023
   197
  })
wenzelm@53023
   198
wenzelm@53023
   199
wenzelm@53023
   200
  /* main content */
wenzelm@53023
   201
wenzelm@53023
   202
  override def getFocusTraversalKeysEnabled = false
wenzelm@53023
   203
wenzelm@53023
   204
  completion.add((new ScrollPane(list_view)).peer.asInstanceOf[JComponent])
wenzelm@53023
   205
wenzelm@53023
   206
wenzelm@53023
   207
  /* popup */
wenzelm@53023
   208
wenzelm@53023
   209
  private val popup =
wenzelm@53023
   210
  {
wenzelm@53023
   211
    val screen_bounds = JEdit_Lib.screen_bounds(screen_point)
wenzelm@53023
   212
wenzelm@53230
   213
    val x0 = root.getLocationOnScreen.x
wenzelm@53230
   214
    val y0 = root.getLocationOnScreen.y
wenzelm@53230
   215
    val w0 = root.getWidth
wenzelm@53230
   216
    val h0 = root.getHeight
wenzelm@53023
   217
wenzelm@53230
   218
    val (w, h) =
wenzelm@53230
   219
    {
wenzelm@53230
   220
      val geometry = JEdit_Lib.window_geometry(completion, completion)
wenzelm@53230
   221
      val bounds = Rendering.popup_bounds
wenzelm@53230
   222
      val w = geometry.width min (screen_bounds.width * bounds).toInt min w0
wenzelm@53230
   223
      val h = geometry.height min (screen_bounds.height * bounds).toInt min h0
wenzelm@53230
   224
      (w, h)
wenzelm@53230
   225
    }
wenzelm@53230
   226
wenzelm@53230
   227
    val (x, y) =
wenzelm@53230
   228
    {
wenzelm@53230
   229
      val x1 = x0 + w0 - w
wenzelm@53230
   230
      val y1 = y0 + h0 - h
wenzelm@53230
   231
      val x2 = screen_point.x min (screen_bounds.x + screen_bounds.width - w)
wenzelm@53230
   232
      val y2 = screen_point.y min (screen_bounds.y + screen_bounds.height - h)
wenzelm@53230
   233
      ((x2 min x1) max x0, (y2 min y1) max y0)
wenzelm@53230
   234
    }
wenzelm@53023
   235
wenzelm@53023
   236
    completion.setSize(new Dimension(w, h))
wenzelm@53023
   237
    completion.setPreferredSize(new Dimension(w, h))
wenzelm@53229
   238
    PopupFactory.getSharedInstance.getPopup(root, completion, x, y)
wenzelm@53023
   239
  }
wenzelm@53023
   240
wenzelm@53232
   241
  private def hide_popup()
wenzelm@53023
   242
  {
wenzelm@53229
   243
    popup.hide
wenzelm@53229
   244
    hidden()
wenzelm@53023
   245
  }
wenzelm@53232
   246
wenzelm@53232
   247
  popup.show
wenzelm@53232
   248
  list_view.requestFocus
wenzelm@53023
   249
}
wenzelm@53023
   250