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