src/Tools/jEdit/src/document_model.scala
author wenzelm
Mon Aug 12 12:06:48 2013 +0200 (2013-08-12)
changeset 52974 908e8a36e975
parent 52973 d5f7fa1498b7
child 52977 15254e32d299
permissions -rw-r--r--
tuned signature;
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@52973
    49
  def init(session: Session, buffer: Buffer, node_name: Document.Node.Name): Document_Model =
wenzelm@43397
    50
  {
wenzelm@47058
    51
    Swing_Thread.require()
wenzelm@47058
    52
    apply(buffer).map(_.deactivate)
wenzelm@52973
    53
    val model = new Document_Model(session, buffer, node_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@52973
    62
class Document_Model(val session: Session, val buffer: Buffer, val node_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@52973
    71
        PIDE.thy_load.check_thy_text(node_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@52849
    80
  /* overlays */
wenzelm@52849
    81
wenzelm@52887
    82
  private var overlays = Document.Node.Overlays.empty
wenzelm@52849
    83
wenzelm@52862
    84
  def insert_overlay(command: Command, name: String, args: List[String]): Unit =
wenzelm@52862
    85
    Swing_Thread.require { overlays = overlays.insert(command, (name, args)) }
wenzelm@52849
    86
wenzelm@52858
    87
  def remove_overlay(command: Command, name: String, args: List[String]): Unit =
wenzelm@52862
    88
    Swing_Thread.require { overlays = overlays.remove(command, (name, args)) }
wenzelm@52849
    89
wenzelm@52849
    90
wenzelm@44385
    91
  /* perspective */
wenzelm@44385
    92
wenzelm@52849
    93
  // owned by Swing thread
wenzelm@52816
    94
  private var _node_required = false
wenzelm@52816
    95
  def node_required: Boolean = _node_required
wenzelm@52816
    96
  def node_required_=(b: Boolean)
wenzelm@52816
    97
  {
wenzelm@52816
    98
    Swing_Thread.require()
wenzelm@52816
    99
    if (_node_required != b) {
wenzelm@52816
   100
      _node_required = b
wenzelm@52816
   101
      PIDE.options_changed()
wenzelm@52974
   102
      PIDE.editor.flush()
wenzelm@52816
   103
    }
wenzelm@52816
   104
  }
wenzelm@52808
   105
wenzelm@52849
   106
  val empty_perspective: Document.Node.Perspective_Text =
wenzelm@52887
   107
    Document.Node.Perspective(false, Text.Perspective.empty, Document.Node.Overlays.empty)
wenzelm@52849
   108
wenzelm@52808
   109
  def node_perspective(): Document.Node.Perspective_Text =
wenzelm@44385
   110
  {
wenzelm@44385
   111
    Swing_Thread.require()
wenzelm@52759
   112
wenzelm@52849
   113
    if (Isabelle.continuous_checking) {
wenzelm@52849
   114
      Document.Node.Perspective(node_required, Text.Perspective(
wenzelm@52849
   115
        for {
wenzelm@52849
   116
          doc_view <- PIDE.document_views(buffer)
wenzelm@52849
   117
          range <- doc_view.perspective().ranges
wenzelm@52849
   118
        } yield range), overlays)
wenzelm@52849
   119
    }
wenzelm@52849
   120
    else empty_perspective
wenzelm@44385
   121
  }
wenzelm@44385
   122
wenzelm@44385
   123
wenzelm@50363
   124
  /* edits */
wenzelm@50344
   125
wenzelm@50344
   126
  def init_edits(): List[Document.Edit_Text] =
wenzelm@50344
   127
  {
wenzelm@50344
   128
    Swing_Thread.require()
wenzelm@52815
   129
wenzelm@50344
   130
    val header = node_header()
wenzelm@50344
   131
    val text = JEdit_Lib.buffer_text(buffer)
wenzelm@50344
   132
    val perspective = node_perspective()
wenzelm@50344
   133
wenzelm@52973
   134
    List(session.header_edit(node_name, header),
wenzelm@52973
   135
      node_name -> Document.Node.Clear(),
wenzelm@52973
   136
      node_name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
wenzelm@52973
   137
      node_name -> perspective)
wenzelm@50344
   138
  }
wenzelm@50344
   139
wenzelm@52808
   140
  def node_edits(perspective: Document.Node.Perspective_Text, text_edits: List[Text.Edit])
wenzelm@50363
   141
    : List[Document.Edit_Text] =
wenzelm@50363
   142
  {
wenzelm@50363
   143
    Swing_Thread.require()
wenzelm@52815
   144
wenzelm@50363
   145
    val header = node_header()
wenzelm@50363
   146
wenzelm@52973
   147
    List(session.header_edit(node_name, header),
wenzelm@52973
   148
      node_name -> Document.Node.Edits(text_edits),
wenzelm@52973
   149
      node_name -> perspective)
wenzelm@50363
   150
  }
wenzelm@50363
   151
wenzelm@50344
   152
wenzelm@50344
   153
  /* pending edits */
wenzelm@43648
   154
wenzelm@43644
   155
  private object pending_edits  // owned by Swing thread
wenzelm@38224
   156
  {
wenzelm@38425
   157
    private val pending = new mutable.ListBuffer[Text.Edit]
wenzelm@52849
   158
    private var last_perspective = empty_perspective
wenzelm@44438
   159
wenzelm@38425
   160
    def snapshot(): List[Text.Edit] = pending.toList
wenzelm@38224
   161
wenzelm@52759
   162
    def flushed_edits(): List[Document.Edit_Text] =
wenzelm@38224
   163
    {
wenzelm@38224
   164
      Swing_Thread.require()
wenzelm@44438
   165
wenzelm@47393
   166
      val edits = snapshot()
wenzelm@50344
   167
      val new_perspective = node_perspective()
wenzelm@47393
   168
      if (!edits.isEmpty || last_perspective != new_perspective) {
wenzelm@47393
   169
        pending.clear
wenzelm@47393
   170
        last_perspective = new_perspective
wenzelm@52759
   171
        node_edits(new_perspective, edits)
wenzelm@43648
   172
      }
wenzelm@52759
   173
      else Nil
wenzelm@38224
   174
    }
wenzelm@38224
   175
wenzelm@52759
   176
    def flush(): Unit = session.update(flushed_edits())
wenzelm@52759
   177
wenzelm@52767
   178
    val delay_flush =
wenzelm@50207
   179
      Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
wenzelm@40478
   180
wenzelm@38425
   181
    def +=(edit: Text.Edit)
wenzelm@38224
   182
    {
wenzelm@38224
   183
      Swing_Thread.require()
wenzelm@38224
   184
      pending += edit
wenzelm@49195
   185
      delay_flush.invoke()
wenzelm@38222
   186
    }
wenzelm@44436
   187
wenzelm@46740
   188
    def init()
wenzelm@46740
   189
    {
wenzelm@46740
   190
      flush()
wenzelm@50363
   191
      session.update(init_edits())
wenzelm@46740
   192
    }
wenzelm@46740
   193
wenzelm@46740
   194
    def exit()
wenzelm@46740
   195
    {
wenzelm@49195
   196
      delay_flush.revoke()
wenzelm@46740
   197
      flush()
wenzelm@44436
   198
    }
wenzelm@44436
   199
  }
wenzelm@44436
   200
wenzelm@52759
   201
  def flushed_edits(): List[Document.Edit_Text] = pending_edits.flushed_edits()
wenzelm@52767
   202
wenzelm@52767
   203
  def update_perspective(): Unit = pending_edits.delay_flush.invoke()
wenzelm@44776
   204
wenzelm@38152
   205
wenzelm@38152
   206
  /* snapshot */
wenzelm@34731
   207
wenzelm@38417
   208
  def snapshot(): Document.Snapshot =
wenzelm@38417
   209
  {
wenzelm@38223
   210
    Swing_Thread.require()
wenzelm@52973
   211
    session.snapshot(node_name, pending_edits.snapshot())
wenzelm@38223
   212
  }
wenzelm@34828
   213
wenzelm@34828
   214
wenzelm@34828
   215
  /* buffer listener */
wenzelm@34828
   216
wenzelm@34828
   217
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   218
  {
wenzelm@40478
   219
    override def bufferLoaded(buffer: JEditBuffer)
wenzelm@40478
   220
    {
wenzelm@40478
   221
      pending_edits.init()
wenzelm@40478
   222
    }
wenzelm@40478
   223
wenzelm@34828
   224
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   225
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   226
    {
wenzelm@40478
   227
      if (!buffer.isLoading)
wenzelm@40478
   228
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
wenzelm@34828
   229
    }
wenzelm@34828
   230
wenzelm@34828
   231
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@38426
   232
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   233
    {
wenzelm@40478
   234
      if (!buffer.isLoading)
wenzelm@40478
   235
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
wenzelm@34828
   236
    }
wenzelm@34828
   237
  }
wenzelm@34828
   238
wenzelm@34828
   239
wenzelm@38158
   240
  /* activation */
wenzelm@37557
   241
wenzelm@43512
   242
  private def activate()
wenzelm@34784
   243
  {
wenzelm@34784
   244
    buffer.addBufferListener(buffer_listener)
wenzelm@50344
   245
    pending_edits.flush()
wenzelm@44358
   246
    Token_Markup.refresh_buffer(buffer)
immler@34680
   247
  }
immler@34680
   248
wenzelm@43512
   249
  private def deactivate()
immler@34680
   250
  {
wenzelm@46740
   251
    pending_edits.exit()
wenzelm@34784
   252
    buffer.removeBufferListener(buffer_listener)
wenzelm@44358
   253
    Token_Markup.refresh_buffer(buffer)
immler@34680
   254
  }
wenzelm@34447
   255
}