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