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