src/Tools/jEdit/src/document_model.scala
author wenzelm
Sun Dec 16 18:02:28 2012 +0100 (2012-12-16)
changeset 50565 b00ea974613c
parent 50363 2f8dc9e65401
child 52759 a20631db9c8a
permissions -rw-r--r--
tuned property name;
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@50565
    26
  private val key = "PIDE.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@48707
    66
  def node_header(): Document.Node.Header =
wenzelm@46920
    67
  {
wenzelm@46920
    68
    Swing_Thread.require()
wenzelm@49406
    69
    JEdit_Lib.buffer_lock(buffer) {
wenzelm@46748
    70
      Exn.capture {
wenzelm@50205
    71
        PIDE.thy_load.check_thy_text(name, buffer.getSegment(0, buffer.getLength))
wenzelm@48707
    72
      } match {
wenzelm@48707
    73
        case Exn.Res(header) => header
wenzelm@48707
    74
        case Exn.Exn(exn) => Document.Node.bad_header(Exn.message(exn))
wenzelm@46748
    75
      }
wenzelm@46748
    76
    }
wenzelm@46920
    77
  }
wenzelm@44385
    78
wenzelm@44385
    79
wenzelm@44385
    80
  /* perspective */
wenzelm@44385
    81
wenzelm@50344
    82
  def node_perspective(): Text.Perspective =
wenzelm@44385
    83
  {
wenzelm@44385
    84
    Swing_Thread.require()
wenzelm@44473
    85
    Text.Perspective(
wenzelm@44385
    86
      for {
wenzelm@50205
    87
        doc_view <- PIDE.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@50363
    93
  /* edits */
wenzelm@50344
    94
wenzelm@50344
    95
  def init_edits(): List[Document.Edit_Text] =
wenzelm@50344
    96
  {
wenzelm@50344
    97
    Swing_Thread.require()
wenzelm@50344
    98
    val header = node_header()
wenzelm@50344
    99
    val text = JEdit_Lib.buffer_text(buffer)
wenzelm@50344
   100
    val perspective = node_perspective()
wenzelm@50344
   101
wenzelm@50344
   102
    List(session.header_edit(name, header),
wenzelm@50344
   103
      name -> Document.Node.Clear(),
wenzelm@50344
   104
      name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@50344
   105
      name -> Document.Node.Perspective(perspective))
wenzelm@50344
   106
  }
wenzelm@50344
   107
wenzelm@50363
   108
  def node_edits(perspective: Text.Perspective, text_edits: List[Text.Edit])
wenzelm@50363
   109
    : List[Document.Edit_Text] =
wenzelm@50363
   110
  {
wenzelm@50363
   111
    Swing_Thread.require()
wenzelm@50363
   112
    val header = node_header()
wenzelm@50363
   113
wenzelm@50363
   114
    List(session.header_edit(name, header),
wenzelm@50363
   115
      name -> Document.Node.Edits(text_edits),
wenzelm@50363
   116
      name -> Document.Node.Perspective(perspective))
wenzelm@50363
   117
  }
wenzelm@50363
   118
wenzelm@50344
   119
wenzelm@50344
   120
  /* pending edits */
wenzelm@43648
   121
wenzelm@43644
   122
  private object pending_edits  // owned by Swing thread
wenzelm@38224
   123
  {
wenzelm@38425
   124
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@44473
   125
    private var last_perspective: Text.Perspective = Text.Perspective.empty
wenzelm@44438
   126
wenzelm@38425
   127
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
   128
wenzelm@43648
   129
    def flush()
wenzelm@38224
   130
    {
wenzelm@38224
   131
      Swing_Thread.require()
wenzelm@44438
   132
wenzelm@47393
   133
      val edits = snapshot()
wenzelm@50344
   134
      val new_perspective = node_perspective()
wenzelm@47393
   135
      if (!edits.isEmpty || last_perspective != new_perspective) {
wenzelm@47393
   136
        pending.clear
wenzelm@47393
   137
        last_perspective = new_perspective
wenzelm@50363
   138
        session.update(node_edits(new_perspective, edits))
wenzelm@43648
   139
      }
wenzelm@38224
   140
    }
wenzelm@38224
   141
wenzelm@40478
   142
    private val delay_flush =
wenzelm@50207
   143
      Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
wenzelm@40478
   144
wenzelm@38425
   145
    def +=(edit: Text.Edit)
wenzelm@38224
   146
    {
wenzelm@38224
   147
      Swing_Thread.require()
wenzelm@38224
   148
      pending += edit
wenzelm@49195
   149
      delay_flush.invoke()
wenzelm@38222
   150
    }
wenzelm@44436
   151
wenzelm@44436
   152
    def update_perspective()
wenzelm@44436
   153
    {
wenzelm@49195
   154
      delay_flush.invoke()
wenzelm@46740
   155
    }
wenzelm@46740
   156
wenzelm@46740
   157
    def init()
wenzelm@46740
   158
    {
wenzelm@46740
   159
      flush()
wenzelm@50363
   160
      session.update(init_edits())
wenzelm@46740
   161
    }
wenzelm@46740
   162
wenzelm@46740
   163
    def exit()
wenzelm@46740
   164
    {
wenzelm@49195
   165
      delay_flush.revoke()
wenzelm@46740
   166
      flush()
wenzelm@44436
   167
    }
wenzelm@44436
   168
  }
wenzelm@44436
   169
wenzelm@44436
   170
  def update_perspective()
wenzelm@44436
   171
  {
wenzelm@44436
   172
    Swing_Thread.require()
wenzelm@44436
   173
    pending_edits.update_perspective()
wenzelm@38222
   174
  }
wenzelm@38152
   175
wenzelm@44776
   176
  def full_perspective()
wenzelm@44776
   177
  {
wenzelm@44776
   178
    pending_edits.flush()
wenzelm@50363
   179
    session.update(node_edits(Text.Perspective(List(JEdit_Lib.buffer_range(buffer))), Nil))
wenzelm@44776
   180
  }
wenzelm@44776
   181
wenzelm@38152
   182
wenzelm@38152
   183
  /* snapshot */
wenzelm@34731
   184
wenzelm@38417
   185
  def snapshot(): Document.Snapshot =
wenzelm@38417
   186
  {
wenzelm@38223
   187
    Swing_Thread.require()
wenzelm@44615
   188
    session.snapshot(name, pending_edits.snapshot())
wenzelm@38223
   189
  }
wenzelm@34828
   190
wenzelm@34828
   191
wenzelm@34828
   192
  /* buffer listener */
wenzelm@34828
   193
wenzelm@34828
   194
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   195
  {
wenzelm@40478
   196
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   197
    {
wenzelm@40478
   198
      pending_edits.init()
wenzelm@40478
   199
    }
wenzelm@40478
   200
wenzelm@34828
   201
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   202
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   203
    {
wenzelm@40478
   204
      if (!buffer.isLoading)
wenzelm@40478
   205
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   206
    }
wenzelm@34828
   207
wenzelm@34828
   208
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   209
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   210
    {
wenzelm@40478
   211
      if (!buffer.isLoading)
wenzelm@40478
   212
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   213
    }
wenzelm@34828
   214
  }
wenzelm@34828
   215
wenzelm@34828
   216
wenzelm@38158
   217
  /* activation */
wenzelm@37557
   218
wenzelm@43512
   219
  private def activate()
wenzelm@34784
   220
  {
wenzelm@34784
   221
    buffer.addBufferListener(buffer_listener)
wenzelm@50344
   222
    pending_edits.flush()
wenzelm@44358
   223
    Token_Markup.refresh_buffer(buffer)
immler@34680
   224
  }
immler@34680
   225
wenzelm@43512
   226
  private def deactivate()
immler@34680
   227
  {
wenzelm@46740
   228
    pending_edits.exit()
wenzelm@34784
   229
    buffer.removeBufferListener(buffer_listener)
wenzelm@44358
   230
    Token_Markup.refresh_buffer(buffer)
immler@34680
   231
  }
wenzelm@34447
   232
}