src/Tools/jEdit/src/document_model.scala
author wenzelm
Sun Aug 21 21:24:42 2011 +0200 (2011-08-21)
changeset 44358 2a2df4de1bbe
parent 44182 ecb51b457064
child 44379 1079ab6b342b
permissions -rw-r--r--
more robust initialization of token marker and line context wrt. session startup;
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.textarea.TextArea
wenzelm@38158
    18
wenzelm@38158
    19
import java.awt.font.TextAttribute
wenzelm@34318
    20
wenzelm@34760
    21
wenzelm@34784
    22
object Document_Model
wenzelm@34588
    23
{
wenzelm@34784
    24
  /* document model of buffer */
wenzelm@34784
    25
wenzelm@34784
    26
  private val key = "isabelle.document_model"
wenzelm@34784
    27
wenzelm@34788
    28
  def apply(buffer: Buffer): Option[Document_Model] =
wenzelm@34784
    29
  {
wenzelm@38223
    30
    Swing_Thread.require()
wenzelm@34784
    31
    buffer.getProperty(key) match {
wenzelm@34784
    32
      case model: Document_Model => Some(model)
wenzelm@34784
    33
      case _ => None
wenzelm@34784
    34
    }
wenzelm@34784
    35
  }
wenzelm@34784
    36
wenzelm@34784
    37
  def exit(buffer: Buffer)
wenzelm@34784
    38
  {
wenzelm@38223
    39
    Swing_Thread.require()
wenzelm@34788
    40
    apply(buffer) match {
wenzelm@39636
    41
      case None =>
wenzelm@34784
    42
      case Some(model) =>
wenzelm@34784
    43
        model.deactivate()
wenzelm@34784
    44
        buffer.unsetProperty(key)
immler@34653
    45
    }
wenzelm@34318
    46
  }
wenzelm@43397
    47
wenzelm@44160
    48
  def init(session: Session, buffer: Buffer,
wenzelm@44160
    49
    master_dir: String, node_name: String, thy_name: String): Document_Model =
wenzelm@43397
    50
  {
wenzelm@43397
    51
    exit(buffer)
wenzelm@44160
    52
    val model = new Document_Model(session, buffer, master_dir, node_name, thy_name)
wenzelm@43397
    53
    buffer.setProperty(key, model)
wenzelm@43397
    54
    model.activate()
wenzelm@43397
    55
    model
wenzelm@43397
    56
  }
wenzelm@34318
    57
}
wenzelm@34318
    58
wenzelm@38151
    59
wenzelm@44160
    60
class Document_Model(val session: Session, val buffer: Buffer,
wenzelm@44160
    61
  val master_dir: String, val node_name: String, val thy_name: String)
wenzelm@34588
    62
{
wenzelm@38224
    63
  /* pending text edits */
wenzelm@38222
    64
wenzelm@44182
    65
  def node_header(): Exn.Result[Thy_Header] =
wenzelm@44182
    66
    Exn.capture { Thy_Header.check(thy_name, buffer.getSegment(0, buffer.getLength)) }
wenzelm@43648
    67
wenzelm@43644
    68
  private object pending_edits  // owned by Swing thread
wenzelm@38224
    69
  {
wenzelm@38425
    70
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@38425
    71
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
    72
wenzelm@43648
    73
    def flush()
wenzelm@38224
    74
    {
wenzelm@38224
    75
      Swing_Thread.require()
wenzelm@43648
    76
      snapshot() match {
wenzelm@43648
    77
        case Nil =>
wenzelm@43648
    78
        case edits =>
wenzelm@43648
    79
          pending.clear
wenzelm@44182
    80
          session.edit_node(node_name, master_dir, node_header(), edits)
wenzelm@43648
    81
      }
wenzelm@38224
    82
    }
wenzelm@38224
    83
wenzelm@40478
    84
    def init()
wenzelm@40478
    85
    {
wenzelm@43648
    86
      flush()
wenzelm@44182
    87
      session.init_node(node_name, master_dir, node_header(), Isabelle.buffer_text(buffer))
wenzelm@40478
    88
    }
wenzelm@40478
    89
wenzelm@40478
    90
    private val delay_flush =
wenzelm@40478
    91
      Swing_Thread.delay_last(session.input_delay) { flush() }
wenzelm@40478
    92
wenzelm@38425
    93
    def +=(edit: Text.Edit)
wenzelm@38224
    94
    {
wenzelm@38224
    95
      Swing_Thread.require()
wenzelm@38224
    96
      pending += edit
wenzelm@38224
    97
      delay_flush()
wenzelm@38222
    98
    }
wenzelm@38222
    99
  }
wenzelm@38152
   100
wenzelm@38152
   101
wenzelm@38152
   102
  /* snapshot */
wenzelm@34731
   103
wenzelm@38417
   104
  def snapshot(): Document.Snapshot =
wenzelm@38417
   105
  {
wenzelm@38223
   106
    Swing_Thread.require()
wenzelm@43697
   107
    session.snapshot(node_name, pending_edits.snapshot())
wenzelm@38223
   108
  }
wenzelm@34828
   109
wenzelm@34828
   110
wenzelm@34828
   111
  /* buffer listener */
wenzelm@34828
   112
wenzelm@34828
   113
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   114
  {
wenzelm@40478
   115
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   116
    {
wenzelm@40478
   117
      pending_edits.init()
wenzelm@40478
   118
    }
wenzelm@40478
   119
wenzelm@34828
   120
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   121
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   122
    {
wenzelm@40478
   123
      if (!buffer.isLoading)
wenzelm@40478
   124
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   125
    }
wenzelm@34828
   126
wenzelm@34828
   127
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   128
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   129
    {
wenzelm@40478
   130
      if (!buffer.isLoading)
wenzelm@40478
   131
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   132
    }
wenzelm@34828
   133
  }
wenzelm@34828
   134
wenzelm@34828
   135
wenzelm@38158
   136
  /* activation */
wenzelm@37557
   137
wenzelm@43512
   138
  private def activate()
wenzelm@34784
   139
  {
wenzelm@34784
   140
    buffer.addBufferListener(buffer_listener)
wenzelm@43512
   141
    pending_edits.init()
wenzelm@44358
   142
    Token_Markup.refresh_buffer(buffer)
immler@34680
   143
  }
immler@34680
   144
wenzelm@43512
   145
  private def deactivate()
immler@34680
   146
  {
wenzelm@40478
   147
    pending_edits.flush()
wenzelm@34784
   148
    buffer.removeBufferListener(buffer_listener)
wenzelm@44358
   149
    Token_Markup.refresh_buffer(buffer)
immler@34680
   150
  }
wenzelm@34447
   151
}