src/Tools/jEdit/src/jedit_lib.scala
author wenzelm
Sat Nov 24 16:24:39 2012 +0100 (2012-11-24)
changeset 50186 f83cab68c788
parent 50116 88b971fca902
child 50195 863b1dfc396c
permissions -rw-r--r--
recovered some tooltip wrapping from e2762f962042, with multi-line support via HTML.encode;
     1 /*  Title:      Tools/jEdit/src/jedit_lib.scala
     2     Author:     Makarius
     3 
     4 Misc library functions for jEdit.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.awt.{Component, Container, Frame, Window}
    13 
    14 import scala.annotation.tailrec
    15 
    16 import org.gjt.sp.jedit.{jEdit, Buffer, View}
    17 import org.gjt.sp.jedit.buffer.JEditBuffer
    18 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
    19 
    20 
    21 object JEdit_Lib
    22 {
    23   /* GUI components */
    24 
    25   def get_parent(component: Component): Option[Container] =
    26     component.getParent match {
    27       case null => None
    28       case parent => Some(parent)
    29     }
    30 
    31   def ancestors(component: Component): Iterator[Container] = new Iterator[Container] {
    32     private var next_elem = get_parent(component)
    33     def hasNext(): Boolean = next_elem.isDefined
    34     def next(): Container =
    35       next_elem match {
    36         case Some(parent) =>
    37           next_elem = get_parent(parent)
    38           parent
    39         case None => Iterator.empty.next()
    40       }
    41   }
    42 
    43   def parent_window(component: Component): Option[Window] =
    44     ancestors(component).find(_.isInstanceOf[Window]).map(_.asInstanceOf[Window])
    45 
    46   def parent_frame(component: Component): Option[Frame] =
    47     ancestors(component).find(_.isInstanceOf[Frame]).map(_.asInstanceOf[Frame])
    48 
    49 
    50   /* basic tooltips, with multi-line support */
    51 
    52   def wrap_tooltip(text: String): String =
    53     if (text == "") null
    54     else "<html><pre>" + HTML.encode(text) + "</pre></html>"
    55 
    56 
    57   /* buffers */
    58 
    59   def swing_buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
    60     Swing_Thread.now { buffer_lock(buffer) { body } }
    61 
    62   def buffer_text(buffer: JEditBuffer): String =
    63     buffer_lock(buffer) { buffer.getText(0, buffer.getLength) }
    64 
    65   def buffer_name(buffer: Buffer): String = buffer.getSymlinkPath
    66 
    67   def buffer_node_dummy(buffer: Buffer): Option[Document.Node.Name] =
    68     Some(Document.Node.Name(buffer_name(buffer), buffer.getDirectory, buffer.getName))
    69 
    70   def buffer_node_name(buffer: Buffer): Option[Document.Node.Name] =
    71   {
    72     val name = buffer_name(buffer)
    73     Thy_Header.thy_name(name).map(theory => Document.Node.Name(name, buffer.getDirectory, theory))
    74   }
    75 
    76 
    77   /* main jEdit components */
    78 
    79   def jedit_buffers(): Iterator[Buffer] = jEdit.getBuffers().iterator
    80 
    81   def jedit_buffer(name: String): Option[Buffer] =
    82     jedit_buffers().find(buffer => buffer_name(buffer) == name)
    83 
    84   def jedit_views(): Iterator[View] = jEdit.getViews().iterator
    85 
    86   def jedit_text_areas(view: View): Iterator[JEditTextArea] =
    87     view.getEditPanes().iterator.map(_.getTextArea)
    88 
    89   def jedit_text_areas(): Iterator[JEditTextArea] =
    90     jedit_views().flatMap(jedit_text_areas(_))
    91 
    92   def jedit_text_areas(buffer: JEditBuffer): Iterator[JEditTextArea] =
    93     jedit_text_areas().filter(_.getBuffer == buffer)
    94 
    95   def buffer_lock[A](buffer: JEditBuffer)(body: => A): A =
    96   {
    97     try { buffer.readLock(); body }
    98     finally { buffer.readUnlock() }
    99   }
   100 
   101 
   102   /* point range */
   103 
   104   def point_range(buffer: JEditBuffer, offset: Text.Offset): Text.Range =
   105     buffer_lock(buffer) {
   106       def text(i: Text.Offset): Char = buffer.getText(i, 1).charAt(0)
   107       try {
   108         val c = text(offset)
   109         if (Character.isHighSurrogate(c) && Character.isLowSurrogate(text(offset + 1)))
   110           Text.Range(offset, offset + 2)
   111         else if (Character.isLowSurrogate(c) && Character.isHighSurrogate(text(offset - 1)))
   112           Text.Range(offset - 1, offset + 1)
   113         else Text.Range(offset, offset + 1)
   114       }
   115       catch { case _: ArrayIndexOutOfBoundsException => Text.Range(offset, offset + 1) }
   116     }
   117 
   118 
   119   /* visible text range */
   120 
   121   def visible_range(text_area: TextArea): Option[Text.Range] =
   122   {
   123     val buffer = text_area.getBuffer
   124     val n = text_area.getVisibleLines
   125     if (n > 0) {
   126       val start = text_area.getScreenLineStartOffset(0)
   127       val raw_end = text_area.getScreenLineEndOffset(n - 1)
   128       val end = if (raw_end >= 0) raw_end min buffer.getLength else buffer.getLength
   129       Some(Text.Range(start, end))
   130     }
   131     else None
   132   }
   133 
   134   def invalidate_range(text_area: TextArea, range: Text.Range)
   135   {
   136     val buffer = text_area.getBuffer
   137     text_area.invalidateLineRange(
   138       buffer.getLineOfOffset(range.start),
   139       buffer.getLineOfOffset(range.stop))
   140   }
   141 
   142 
   143   /* char width */
   144 
   145   def char_width(text_area: TextArea): Int =
   146   {
   147     val painter = text_area.getPainter
   148     val font = painter.getFont
   149     val font_context = painter.getFontRenderContext
   150     font.getStringBounds(" ", font_context).getWidth.round.toInt
   151   }
   152 
   153 
   154   /* graphics range */
   155 
   156   case class Gfx_Range(val x: Int, val y: Int, val length: Int)
   157 
   158   // NB: jEdit always normalizes \r\n and \r to \n
   159   // NB: last line lacks \n
   160   def gfx_range(text_area: TextArea, range: Text.Range): Option[Gfx_Range] =
   161   {
   162     val buffer = text_area.getBuffer
   163 
   164     val p = text_area.offsetToXY(range.start)
   165 
   166     val end = buffer.getLength
   167     val stop = range.stop
   168     val (q, r) =
   169       if (stop >= end) (text_area.offsetToXY(end), char_width(text_area) * (stop - end))
   170       else if (stop > 0 && buffer.getText(stop - 1, 1) == "\n")
   171         (text_area.offsetToXY(stop - 1), char_width(text_area))
   172       else (text_area.offsetToXY(stop), 0)
   173 
   174     if (p != null && q != null && p.x < q.x + r && p.y == q.y)
   175       Some(Gfx_Range(p.x, p.y, q.x + r - p.x))
   176     else None
   177   }
   178 
   179 
   180   /* pixel range */
   181 
   182   def pixel_range(text_area: TextArea, x: Int, y: Int): Option[Text.Range] =
   183   {
   184     val range = point_range(text_area.getBuffer, text_area.xyToOffset(x, y, false))
   185     gfx_range(text_area, range) match {
   186       case Some(g) if (g.x <= x && x < g.x + g.length) => Some(range)
   187       case _ => None
   188     }
   189   }
   190 }
   191