src/Tools/jEdit/src/document_model.scala
author wenzelm
Sat Apr 07 17:55:35 2012 +0200 (2012-04-07)
changeset 47393 d760049b2d18
parent 47058 34761733526c
child 48707 ba531af91148
permissions -rw-r--r--
more robust update_perspective, e.g. required after reload of buffer that is not at start position;
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)
wenzelm@47058
    45
        buffer.propertiesChanged
immler@34653
    46
    }
wenzelm@34318
    47
  }
wenzelm@43397
    48
wenzelm@44615
    49
  def init(session: Session, buffer: Buffer, name: Document.Node.Name): Document_Model =
wenzelm@43397
    50
  {
wenzelm@47058
    51
    Swing_Thread.require()
wenzelm@47058
    52
    apply(buffer).map(_.deactivate)
wenzelm@44615
    53
    val model = new Document_Model(session, buffer, name)
wenzelm@43397
    54
    buffer.setProperty(key, model)
wenzelm@43397
    55
    model.activate()
wenzelm@47058
    56
    buffer.propertiesChanged
wenzelm@43397
    57
    model
wenzelm@43397
    58
  }
wenzelm@34318
    59
}
wenzelm@34318
    60
wenzelm@38151
    61
wenzelm@44615
    62
class Document_Model(val session: Session, val buffer: Buffer, val name: Document.Node.Name)
wenzelm@34588
    63
{
wenzelm@44385
    64
  /* header */
wenzelm@38222
    65
wenzelm@46737
    66
  def node_header(): Document.Node_Header =
wenzelm@46920
    67
  {
wenzelm@46920
    68
    Swing_Thread.require()
wenzelm@46920
    69
    Isabelle.buffer_lock(buffer) {
wenzelm@46748
    70
      Exn.capture {
wenzelm@46748
    71
        Isabelle.thy_load.check_header(name,
wenzelm@46748
    72
          Thy_Header.read(buffer.getSegment(0, buffer.getLength)))
wenzelm@46748
    73
      }
wenzelm@46748
    74
    }
wenzelm@46920
    75
  }
wenzelm@44385
    76
wenzelm@44385
    77
wenzelm@44385
    78
  /* perspective */
wenzelm@44385
    79
wenzelm@44776
    80
  def buffer_range(): Text.Range = Text.Range(0, (buffer.getLength - 1) max 0)
wenzelm@44776
    81
wenzelm@44385
    82
  def perspective(): Text.Perspective =
wenzelm@44385
    83
  {
wenzelm@44385
    84
    Swing_Thread.require()
wenzelm@44473
    85
    Text.Perspective(
wenzelm@44385
    86
      for {
wenzelm@44385
    87
        doc_view <- Isabelle.document_views(buffer)
wenzelm@44473
    88
        range <- doc_view.perspective().ranges
wenzelm@44385
    89
      } yield range)
wenzelm@44385
    90
  }
wenzelm@44385
    91
wenzelm@44385
    92
wenzelm@44385
    93
  /* pending text edits */
wenzelm@43648
    94
wenzelm@43644
    95
  private object pending_edits  // owned by Swing thread
wenzelm@38224
    96
  {
wenzelm@38425
    97
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@44473
    98
    private var last_perspective: Text.Perspective = Text.Perspective.empty
wenzelm@44438
    99
wenzelm@38425
   100
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
   101
wenzelm@43648
   102
    def flush()
wenzelm@38224
   103
    {
wenzelm@38224
   104
      Swing_Thread.require()
wenzelm@44438
   105
wenzelm@47393
   106
      val edits = snapshot()
wenzelm@47393
   107
      val new_perspective = perspective()
wenzelm@47393
   108
      if (!edits.isEmpty || last_perspective != new_perspective) {
wenzelm@47393
   109
        pending.clear
wenzelm@47393
   110
        last_perspective = new_perspective
wenzelm@47393
   111
        session.edit_node(name, node_header(), new_perspective, edits)
wenzelm@43648
   112
      }
wenzelm@38224
   113
    }
wenzelm@38224
   114
wenzelm@40478
   115
    private val delay_flush =
wenzelm@40478
   116
      Swing_Thread.delay_last(session.input_delay) { flush() }
wenzelm@40478
   117
wenzelm@38425
   118
    def +=(edit: Text.Edit)
wenzelm@38224
   119
    {
wenzelm@38224
   120
      Swing_Thread.require()
wenzelm@38224
   121
      pending += edit
wenzelm@46740
   122
      delay_flush(true)
wenzelm@38222
   123
    }
wenzelm@44436
   124
wenzelm@44436
   125
    def update_perspective()
wenzelm@44436
   126
    {
wenzelm@46740
   127
      delay_flush(true)
wenzelm@46740
   128
    }
wenzelm@46740
   129
wenzelm@46740
   130
    def init()
wenzelm@46740
   131
    {
wenzelm@46740
   132
      flush()
wenzelm@46740
   133
      session.init_node(name, node_header(), perspective(), Isabelle.buffer_text(buffer))
wenzelm@46740
   134
    }
wenzelm@46740
   135
wenzelm@46740
   136
    def exit()
wenzelm@46740
   137
    {
wenzelm@46740
   138
      delay_flush(false)
wenzelm@46740
   139
      flush()
wenzelm@44436
   140
    }
wenzelm@44436
   141
  }
wenzelm@44436
   142
wenzelm@44436
   143
  def update_perspective()
wenzelm@44436
   144
  {
wenzelm@44436
   145
    Swing_Thread.require()
wenzelm@44436
   146
    pending_edits.update_perspective()
wenzelm@38222
   147
  }
wenzelm@38152
   148
wenzelm@44776
   149
  def full_perspective()
wenzelm@44776
   150
  {
wenzelm@44776
   151
    pending_edits.flush()
wenzelm@44776
   152
    session.edit_node(name, node_header(), Text.Perspective(List(buffer_range())), Nil)
wenzelm@44776
   153
  }
wenzelm@44776
   154
wenzelm@38152
   155
wenzelm@38152
   156
  /* snapshot */
wenzelm@34731
   157
wenzelm@38417
   158
  def snapshot(): Document.Snapshot =
wenzelm@38417
   159
  {
wenzelm@38223
   160
    Swing_Thread.require()
wenzelm@44615
   161
    session.snapshot(name, pending_edits.snapshot())
wenzelm@38223
   162
  }
wenzelm@34828
   163
wenzelm@34828
   164
wenzelm@34828
   165
  /* buffer listener */
wenzelm@34828
   166
wenzelm@34828
   167
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   168
  {
wenzelm@40478
   169
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   170
    {
wenzelm@40478
   171
      pending_edits.init()
wenzelm@40478
   172
    }
wenzelm@40478
   173
wenzelm@34828
   174
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   175
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   176
    {
wenzelm@40478
   177
      if (!buffer.isLoading)
wenzelm@40478
   178
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   179
    }
wenzelm@34828
   180
wenzelm@34828
   181
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   182
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   183
    {
wenzelm@40478
   184
      if (!buffer.isLoading)
wenzelm@40478
   185
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   186
    }
wenzelm@34828
   187
  }
wenzelm@34828
   188
wenzelm@34828
   189
wenzelm@38158
   190
  /* activation */
wenzelm@37557
   191
wenzelm@43512
   192
  private def activate()
wenzelm@34784
   193
  {
wenzelm@34784
   194
    buffer.addBufferListener(buffer_listener)
wenzelm@43512
   195
    pending_edits.init()
wenzelm@44358
   196
    Token_Markup.refresh_buffer(buffer)
immler@34680
   197
  }
immler@34680
   198
wenzelm@43512
   199
  private def deactivate()
immler@34680
   200
  {
wenzelm@46740
   201
    pending_edits.exit()
wenzelm@34784
   202
    buffer.removeBufferListener(buffer_listener)
wenzelm@44358
   203
    Token_Markup.refresh_buffer(buffer)
immler@34680
   204
  }
wenzelm@34447
   205
}