src/Tools/jEdit/src/jedit/document_model.scala
author wenzelm
Fri Sep 24 14:12:33 2010 +0200 (2010-09-24)
changeset 39636 610dc743932c
parent 39179 591bbab9ef59
child 40474 576b88b1dce9
permissions -rw-r--r--
permissive exit;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/document_model.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
    Author:     Makarius
wenzelm@36760
     4
wenzelm@38222
     5
Document model connected to jEdit buffer -- single node in theory graph.
wenzelm@36760
     6
*/
wenzelm@34407
     7
wenzelm@34784
     8
package isabelle.jedit
wenzelm@34760
     9
wenzelm@34318
    10
wenzelm@36015
    11
import isabelle._
wenzelm@36015
    12
wenzelm@34693
    13
import scala.collection.mutable
wenzelm@34446
    14
wenzelm@34784
    15
import org.gjt.sp.jedit.Buffer
wenzelm@34783
    16
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer}
wenzelm@38158
    17
import org.gjt.sp.jedit.syntax.{SyntaxStyle, Token, TokenMarker, TokenHandler, ParserRuleSet}
wenzelm@38158
    18
import org.gjt.sp.jedit.textarea.TextArea
wenzelm@38158
    19
wenzelm@38158
    20
import java.awt.font.TextAttribute
wenzelm@38158
    21
import javax.swing.text.Segment;
wenzelm@34318
    22
wenzelm@34760
    23
wenzelm@34784
    24
object Document_Model
wenzelm@34588
    25
{
wenzelm@38158
    26
  object Token_Markup
wenzelm@38158
    27
  {
wenzelm@38158
    28
    /* extended token styles */
wenzelm@38158
    29
wenzelm@38158
    30
    private val plain_range: Int = Token.ID_COUNT
wenzelm@38158
    31
    private val full_range: Int = 3 * plain_range
wenzelm@38158
    32
    private def check_range(i: Int) { require(0 <= i && i < plain_range) }
wenzelm@38158
    33
wenzelm@38158
    34
    def subscript(i: Byte): Byte = { check_range(i); (i + plain_range).toByte }
wenzelm@38158
    35
    def superscript(i: Byte): Byte = { check_range(i); (i + 2 * plain_range).toByte }
wenzelm@38158
    36
wenzelm@38158
    37
    private def script_style(style: SyntaxStyle, i: Int): SyntaxStyle =
wenzelm@38158
    38
    {
wenzelm@38158
    39
      import scala.collection.JavaConversions._
wenzelm@38158
    40
      val script_font =
wenzelm@38158
    41
        style.getFont.deriveFont(Map(TextAttribute.SUPERSCRIPT -> new java.lang.Integer(i)))
wenzelm@38158
    42
      new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, script_font)
wenzelm@38158
    43
    }
wenzelm@38158
    44
wenzelm@38158
    45
    def extend_styles(styles: Array[SyntaxStyle]): Array[SyntaxStyle] =
wenzelm@38158
    46
    {
wenzelm@38158
    47
      val new_styles = new Array[SyntaxStyle](full_range)
wenzelm@38158
    48
      for (i <- 0 until plain_range) {
wenzelm@38158
    49
        val style = styles(i)
wenzelm@38158
    50
        new_styles(i) = style
wenzelm@38158
    51
        new_styles(subscript(i.toByte)) = script_style(style, -1)
wenzelm@38158
    52
        new_styles(superscript(i.toByte)) = script_style(style, 1)
wenzelm@38158
    53
      }
wenzelm@38158
    54
      new_styles
wenzelm@38158
    55
    }
wenzelm@38158
    56
wenzelm@38158
    57
wenzelm@38158
    58
    /* line context */
wenzelm@38158
    59
wenzelm@38852
    60
    private val dummy_rules = new ParserRuleSet("isabelle", "MAIN")
wenzelm@38852
    61
wenzelm@38852
    62
    class Line_Context(val line: Int, prev: Line_Context)
wenzelm@38852
    63
      extends TokenMarker.LineContext(dummy_rules, prev)
wenzelm@38852
    64
    {
wenzelm@38852
    65
      override def hashCode: Int = line
wenzelm@38852
    66
      override def equals(that: Any) =
wenzelm@38852
    67
        that match {
wenzelm@38852
    68
          case other: Line_Context => line == other.line
wenzelm@38852
    69
          case _ => false
wenzelm@38852
    70
        }
wenzelm@38852
    71
    }
wenzelm@38158
    72
  }
wenzelm@38158
    73
wenzelm@38158
    74
wenzelm@34784
    75
  /* document model of buffer */
wenzelm@34784
    76
wenzelm@34784
    77
  private val key = "isabelle.document_model"
wenzelm@34784
    78
wenzelm@38222
    79
  def init(session: Session, buffer: Buffer, thy_name: String): Document_Model =
wenzelm@34716
    80
  {
wenzelm@38223
    81
    Swing_Thread.require()
wenzelm@38222
    82
    val model = new Document_Model(session, buffer, thy_name)
wenzelm@34784
    83
    buffer.setProperty(key, model)
wenzelm@34784
    84
    model.activate()
wenzelm@34784
    85
    model
wenzelm@34784
    86
  }
wenzelm@34784
    87
wenzelm@34788
    88
  def apply(buffer: Buffer): Option[Document_Model] =
wenzelm@34784
    89
  {
wenzelm@38223
    90
    Swing_Thread.require()
wenzelm@34784
    91
    buffer.getProperty(key) match {
wenzelm@34784
    92
      case model: Document_Model => Some(model)
wenzelm@34784
    93
      case _ => None
wenzelm@34784
    94
    }
wenzelm@34784
    95
  }
wenzelm@34784
    96
wenzelm@34784
    97
  def exit(buffer: Buffer)
wenzelm@34784
    98
  {
wenzelm@38223
    99
    Swing_Thread.require()
wenzelm@34788
   100
    apply(buffer) match {
wenzelm@39636
   101
      case None =>
wenzelm@34784
   102
      case Some(model) =>
wenzelm@34784
   103
        model.deactivate()
wenzelm@34784
   104
        buffer.unsetProperty(key)
immler@34653
   105
    }
wenzelm@34318
   106
  }
wenzelm@34318
   107
}
wenzelm@34318
   108
wenzelm@38151
   109
wenzelm@38222
   110
class Document_Model(val session: Session, val buffer: Buffer, val thy_name: String)
wenzelm@34588
   111
{
wenzelm@38224
   112
  /* pending text edits */
wenzelm@38222
   113
wenzelm@38224
   114
  object pending_edits  // owned by Swing thread
wenzelm@38224
   115
  {
wenzelm@38425
   116
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@38425
   117
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
   118
wenzelm@38224
   119
    private val delay_flush = Swing_Thread.delay_last(session.input_delay) {
wenzelm@38417
   120
      if (!pending.isEmpty) session.edit_version(List((thy_name, flush())))
wenzelm@38224
   121
    }
wenzelm@38152
   122
wenzelm@38425
   123
    def flush(): List[Text.Edit] =
wenzelm@38224
   124
    {
wenzelm@38224
   125
      Swing_Thread.require()
wenzelm@38224
   126
      val edits = snapshot()
wenzelm@38224
   127
      pending.clear
wenzelm@38224
   128
      edits
wenzelm@38224
   129
    }
wenzelm@38224
   130
wenzelm@38425
   131
    def +=(edit: Text.Edit)
wenzelm@38224
   132
    {
wenzelm@38224
   133
      Swing_Thread.require()
wenzelm@38224
   134
      pending += edit
wenzelm@38224
   135
      delay_flush()
wenzelm@38222
   136
    }
wenzelm@38222
   137
  }
wenzelm@38152
   138
wenzelm@38152
   139
wenzelm@38152
   140
  /* snapshot */
wenzelm@34731
   141
wenzelm@38417
   142
  def snapshot(): Document.Snapshot =
wenzelm@38417
   143
  {
wenzelm@38223
   144
    Swing_Thread.require()
wenzelm@38359
   145
    session.snapshot(thy_name, pending_edits.snapshot())
wenzelm@38223
   146
  }
wenzelm@34828
   147
wenzelm@34828
   148
wenzelm@34828
   149
  /* buffer listener */
wenzelm@34828
   150
wenzelm@34828
   151
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   152
  {
wenzelm@34828
   153
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   154
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   155
    {
wenzelm@38425
   156
      pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   157
    }
wenzelm@34828
   158
wenzelm@34828
   159
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   160
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   161
    {
wenzelm@38426
   162
      pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   163
    }
wenzelm@34828
   164
  }
wenzelm@34828
   165
wenzelm@34828
   166
wenzelm@38158
   167
  /* semantic token marker */
wenzelm@38158
   168
wenzelm@38158
   169
  private val token_marker = new TokenMarker
wenzelm@38158
   170
  {
wenzelm@38158
   171
    override def markTokens(prev: TokenMarker.LineContext,
wenzelm@38158
   172
        handler: TokenHandler, line_segment: Segment): TokenMarker.LineContext =
wenzelm@38158
   173
    {
wenzelm@38843
   174
      Isabelle.swing_buffer_lock(buffer) {
wenzelm@38843
   175
        val snapshot = Document_Model.this.snapshot()
wenzelm@38572
   176
wenzelm@38852
   177
        val previous = prev.asInstanceOf[Document_Model.Token_Markup.Line_Context]
wenzelm@38641
   178
        val line = if (prev == null) 0 else previous.line + 1
wenzelm@38852
   179
        val context = new Document_Model.Token_Markup.Line_Context(line, previous)
wenzelm@38572
   180
wenzelm@38641
   181
        val start = buffer.getLineStartOffset(line)
wenzelm@38641
   182
        val stop = start + line_segment.count
wenzelm@38158
   183
wenzelm@38641
   184
        /* FIXME
wenzelm@38641
   185
        for (text_area <- Isabelle.jedit_text_areas(buffer)
wenzelm@38641
   186
              if Document_View(text_area).isDefined)
wenzelm@38641
   187
          Document_View(text_area).get.set_styles()
wenzelm@38641
   188
        */
wenzelm@38158
   189
wenzelm@38641
   190
        def handle_token(style: Byte, offset: Text.Offset, length: Int) =
wenzelm@38641
   191
          handler.handleToken(line_segment, style, offset, length, context)
immler@34680
   192
wenzelm@38641
   193
        val syntax = session.current_syntax()
wenzelm@39179
   194
        val tokens = snapshot.select_markup(Text.Range(start, stop))(Isabelle_Markup.tokens(syntax))
wenzelm@38572
   195
wenzelm@38846
   196
        var last = start
wenzelm@39179
   197
        for (token <- tokens.iterator) {
wenzelm@38846
   198
          val Text.Range(token_start, token_stop) = token.range
wenzelm@38846
   199
          if (last < token_start)
wenzelm@38846
   200
            handle_token(Token.COMMENT1, last - start, token_start - last)
wenzelm@39177
   201
          handle_token(token.info getOrElse Token.NULL,
wenzelm@39177
   202
            token_start - start, token_stop - token_start)
wenzelm@38846
   203
          last = token_stop
wenzelm@38641
   204
        }
wenzelm@39179
   205
        if (last < stop) handle_token(Token.COMMENT1, last - start, stop - last)
wenzelm@38641
   206
wenzelm@38641
   207
        handle_token(Token.END, line_segment.count, 0)
wenzelm@38641
   208
        handler.setLineContext(context)
wenzelm@38641
   209
        context
wenzelm@38572
   210
      }
wenzelm@38158
   211
    }
wenzelm@38158
   212
  }
wenzelm@38158
   213
wenzelm@38158
   214
wenzelm@38158
   215
  /* activation */
wenzelm@37557
   216
wenzelm@34784
   217
  def activate()
wenzelm@34784
   218
  {
wenzelm@37557
   219
    buffer.setTokenMarker(token_marker)
wenzelm@34784
   220
    buffer.addBufferListener(buffer_listener)
wenzelm@34784
   221
    buffer.propertiesChanged()
wenzelm@38425
   222
    pending_edits += Text.Edit.insert(0, buffer.getText(0, buffer.getLength))
immler@34680
   223
  }
immler@34680
   224
wenzelm@37557
   225
  def refresh()
wenzelm@37557
   226
  {
wenzelm@37557
   227
    buffer.setTokenMarker(token_marker)
wenzelm@37557
   228
  }
wenzelm@37557
   229
wenzelm@34784
   230
  def deactivate()
immler@34680
   231
  {
wenzelm@34784
   232
    buffer.setTokenMarker(buffer.getMode.getTokenMarker)
wenzelm@34784
   233
    buffer.removeBufferListener(buffer_listener)
immler@34680
   234
  }
wenzelm@34447
   235
}