src/Tools/jEdit/src/document_model.scala
author wenzelm
Wed Jun 08 17:42:07 2011 +0200 (2011-06-08)
changeset 43282 5d294220ca43
parent 40851 src/Tools/jEdit/src/jedit/document_model.scala@58d25beedcea
child 43390 7ee98a3802af
permissions -rw-r--r--
moved sources -- eliminated Netbeans artifact of jedit package directory;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/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@40851
    66
      override def equals(that: Any): Boolean =
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@40478
   119
    def flush(more_edits: Option[List[Text.Edit]]*)
wenzelm@38224
   120
    {
wenzelm@38224
   121
      Swing_Thread.require()
wenzelm@38224
   122
      val edits = snapshot()
wenzelm@38224
   123
      pending.clear
wenzelm@40478
   124
wenzelm@40481
   125
      val all_edits =
wenzelm@40481
   126
        if (edits.isEmpty) more_edits.toList
wenzelm@40481
   127
        else Some(edits) :: more_edits.toList
wenzelm@40481
   128
      if (!all_edits.isEmpty) session.edit_version(all_edits.map((thy_name, _)))
wenzelm@38224
   129
    }
wenzelm@38224
   130
wenzelm@40478
   131
    def init()
wenzelm@40478
   132
    {
wenzelm@40478
   133
      Swing_Thread.require()
wenzelm@40478
   134
      flush(None, Some(List(Text.Edit.insert(0, Isabelle.buffer_text(buffer)))))
wenzelm@40478
   135
    }
wenzelm@40478
   136
wenzelm@40478
   137
    private val delay_flush =
wenzelm@40478
   138
      Swing_Thread.delay_last(session.input_delay) { flush() }
wenzelm@40478
   139
wenzelm@38425
   140
    def +=(edit: Text.Edit)
wenzelm@38224
   141
    {
wenzelm@38224
   142
      Swing_Thread.require()
wenzelm@38224
   143
      pending += edit
wenzelm@38224
   144
      delay_flush()
wenzelm@38222
   145
    }
wenzelm@38222
   146
  }
wenzelm@38152
   147
wenzelm@38152
   148
wenzelm@38152
   149
  /* snapshot */
wenzelm@34731
   150
wenzelm@38417
   151
  def snapshot(): Document.Snapshot =
wenzelm@38417
   152
  {
wenzelm@38223
   153
    Swing_Thread.require()
wenzelm@38359
   154
    session.snapshot(thy_name, pending_edits.snapshot())
wenzelm@38223
   155
  }
wenzelm@34828
   156
wenzelm@34828
   157
wenzelm@34828
   158
  /* buffer listener */
wenzelm@34828
   159
wenzelm@34828
   160
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   161
  {
wenzelm@40478
   162
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   163
    {
wenzelm@40478
   164
      pending_edits.init()
wenzelm@40478
   165
    }
wenzelm@40478
   166
wenzelm@34828
   167
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   168
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   169
    {
wenzelm@40478
   170
      if (!buffer.isLoading)
wenzelm@40478
   171
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   172
    }
wenzelm@34828
   173
wenzelm@34828
   174
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   175
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   176
    {
wenzelm@40478
   177
      if (!buffer.isLoading)
wenzelm@40478
   178
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   179
    }
wenzelm@34828
   180
  }
wenzelm@34828
   181
wenzelm@34828
   182
wenzelm@38158
   183
  /* semantic token marker */
wenzelm@38158
   184
wenzelm@38158
   185
  private val token_marker = new TokenMarker
wenzelm@38158
   186
  {
wenzelm@38158
   187
    override def markTokens(prev: TokenMarker.LineContext,
wenzelm@38158
   188
        handler: TokenHandler, line_segment: Segment): TokenMarker.LineContext =
wenzelm@38158
   189
    {
wenzelm@38843
   190
      Isabelle.swing_buffer_lock(buffer) {
wenzelm@38843
   191
        val snapshot = Document_Model.this.snapshot()
wenzelm@38572
   192
wenzelm@38852
   193
        val previous = prev.asInstanceOf[Document_Model.Token_Markup.Line_Context]
wenzelm@38641
   194
        val line = if (prev == null) 0 else previous.line + 1
wenzelm@38852
   195
        val context = new Document_Model.Token_Markup.Line_Context(line, previous)
wenzelm@38572
   196
wenzelm@38641
   197
        val start = buffer.getLineStartOffset(line)
wenzelm@38641
   198
        val stop = start + line_segment.count
wenzelm@38158
   199
wenzelm@38641
   200
        /* FIXME
wenzelm@38641
   201
        for (text_area <- Isabelle.jedit_text_areas(buffer)
wenzelm@38641
   202
              if Document_View(text_area).isDefined)
wenzelm@38641
   203
          Document_View(text_area).get.set_styles()
wenzelm@38641
   204
        */
wenzelm@38158
   205
wenzelm@38641
   206
        def handle_token(style: Byte, offset: Text.Offset, length: Int) =
wenzelm@38641
   207
          handler.handleToken(line_segment, style, offset, length, context)
immler@34680
   208
wenzelm@38641
   209
        val syntax = session.current_syntax()
wenzelm@39179
   210
        val tokens = snapshot.select_markup(Text.Range(start, stop))(Isabelle_Markup.tokens(syntax))
wenzelm@38572
   211
wenzelm@38846
   212
        var last = start
wenzelm@39179
   213
        for (token <- tokens.iterator) {
wenzelm@38846
   214
          val Text.Range(token_start, token_stop) = token.range
wenzelm@38846
   215
          if (last < token_start)
wenzelm@38846
   216
            handle_token(Token.COMMENT1, last - start, token_start - last)
wenzelm@39177
   217
          handle_token(token.info getOrElse Token.NULL,
wenzelm@39177
   218
            token_start - start, token_stop - token_start)
wenzelm@38846
   219
          last = token_stop
wenzelm@38641
   220
        }
wenzelm@39179
   221
        if (last < stop) handle_token(Token.COMMENT1, last - start, stop - last)
wenzelm@38641
   222
wenzelm@38641
   223
        handle_token(Token.END, line_segment.count, 0)
wenzelm@38641
   224
        handler.setLineContext(context)
wenzelm@38641
   225
        context
wenzelm@38572
   226
      }
wenzelm@38158
   227
    }
wenzelm@38158
   228
  }
wenzelm@38158
   229
wenzelm@38158
   230
wenzelm@38158
   231
  /* activation */
wenzelm@37557
   232
wenzelm@34784
   233
  def activate()
wenzelm@34784
   234
  {
wenzelm@37557
   235
    buffer.setTokenMarker(token_marker)
wenzelm@34784
   236
    buffer.addBufferListener(buffer_listener)
wenzelm@34784
   237
    buffer.propertiesChanged()
wenzelm@40478
   238
    pending_edits.init()
immler@34680
   239
  }
immler@34680
   240
wenzelm@37557
   241
  def refresh()
wenzelm@37557
   242
  {
wenzelm@37557
   243
    buffer.setTokenMarker(token_marker)
wenzelm@37557
   244
  }
wenzelm@37557
   245
wenzelm@34784
   246
  def deactivate()
immler@34680
   247
  {
wenzelm@40478
   248
    pending_edits.flush()
wenzelm@34784
   249
    buffer.setTokenMarker(buffer.getMode.getTokenMarker)
wenzelm@34784
   250
    buffer.removeBufferListener(buffer_listener)
immler@34680
   251
  }
wenzelm@34447
   252
}