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