src/Tools/jEdit/src/document_model.scala
author wenzelm
Mon, 12 Aug 2013 12:06:48 +0200
changeset 52974 908e8a36e975
parent 52973 d5f7fa1498b7
child 52977 15254e32d299
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43282
5d294220ca43 moved sources -- eliminated Netbeans artifact of jedit package directory;
wenzelm
parents: 40851
diff changeset
     1
/*  Title:      Tools/jEdit/src/document_model.scala
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     2
    Author:     Fabian Immler, TU Munich
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     3
    Author:     Makarius
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     4
38222
dac5fa0ac971 replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents: 38158
diff changeset
     5
Document model connected to jEdit buffer -- single node in theory graph.
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     6
*/
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     7
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
     8
package isabelle.jedit
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
     9
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    10
36015
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    11
import isabelle._
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    12
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
    13
import scala.collection.mutable
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    14
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    15
import org.gjt.sp.jedit.Buffer
34783
cb95d6bbf5f1 clarified BufferListener: use adapter, listen to contentInserted instead of preContentInserted;
wenzelm
parents: 34778
diff changeset
    16
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer}
38158
8aaa21db41f3 Document_Model: include token marker here;
wenzelm
parents: 38152
diff changeset
    17
import org.gjt.sp.jedit.textarea.TextArea
8aaa21db41f3 Document_Model: include token marker here;
wenzelm
parents: 38152
diff changeset
    18
8aaa21db41f3 Document_Model: include token marker here;
wenzelm
parents: 38152
diff changeset
    19
import java.awt.font.TextAttribute
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    20
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    21
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    22
object Document_Model
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    23
{
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    24
  /* document model of buffer */
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    25
50565
b00ea974613c tuned property name;
wenzelm
parents: 50363
diff changeset
    26
  private val key = "PIDE.document_model"
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    27
34788
3779c54a2d21 direct apply for Document_Model and Document_View;
wenzelm
parents: 34784
diff changeset
    28
  def apply(buffer: Buffer): Option[Document_Model] =
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    29
  {
38223
2a368e8e0a80 more explicit treatment of Swing thread context;
wenzelm
parents: 38222
diff changeset
    30
    Swing_Thread.require()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    31
    buffer.getProperty(key) match {
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    32
      case model: Document_Model => Some(model)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    33
      case _ => None
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    34
    }
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    35
  }
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    36
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    37
  def exit(buffer: Buffer)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    38
  {
38223
2a368e8e0a80 more explicit treatment of Swing thread context;
wenzelm
parents: 38222
diff changeset
    39
    Swing_Thread.require()
34788
3779c54a2d21 direct apply for Document_Model and Document_View;
wenzelm
parents: 34784
diff changeset
    40
    apply(buffer) match {
39636
610dc743932c permissive exit;
wenzelm
parents: 39179
diff changeset
    41
      case None =>
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    42
      case Some(model) =>
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    43
        model.deactivate()
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    44
        buffer.unsetProperty(key)
47058
34761733526c refined init_model: allow change of buffer name as caused by "Save as", for example;
wenzelm
parents: 46920
diff changeset
    45
        buffer.propertiesChanged
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    46
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    47
  }
43397
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    48
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
    49
  def init(session: Session, buffer: Buffer, node_name: Document.Node.Name): Document_Model =
43397
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    50
  {
47058
34761733526c refined init_model: allow change of buffer name as caused by "Save as", for example;
wenzelm
parents: 46920
diff changeset
    51
    Swing_Thread.require()
34761733526c refined init_model: allow change of buffer name as caused by "Save as", for example;
wenzelm
parents: 46920
diff changeset
    52
    apply(buffer).map(_.deactivate)
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
    53
    val model = new Document_Model(session, buffer, node_name)
43397
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    54
    buffer.setProperty(key, model)
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    55
    model.activate()
47058
34761733526c refined init_model: allow change of buffer name as caused by "Save as", for example;
wenzelm
parents: 46920
diff changeset
    56
    buffer.propertiesChanged
43397
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    57
    model
dba359c0ae3b more robust init;
wenzelm
parents: 43394
diff changeset
    58
  }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    59
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    60
38151
2837c952ca31 explicit Change.Snapshot and Document.Node;
wenzelm
parents: 38150
diff changeset
    61
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
    62
class Document_Model(val session: Session, val buffer: Buffer, val node_name: Document.Node.Name)
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    63
{
44385
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
    64
  /* header */
38222
dac5fa0ac971 replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents: 38158
diff changeset
    65
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 47393
diff changeset
    66
  def node_header(): Document.Node.Header =
46920
5f44c8bea84e more explicit indication of swing thread context;
wenzelm
parents: 46750
diff changeset
    67
  {
5f44c8bea84e more explicit indication of swing thread context;
wenzelm
parents: 46750
diff changeset
    68
    Swing_Thread.require()
49406
38db4832b210 somewhat more general JEdit_Lib;
wenzelm
parents: 49357
diff changeset
    69
    JEdit_Lib.buffer_lock(buffer) {
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46740
diff changeset
    70
      Exn.capture {
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
    71
        PIDE.thy_load.check_thy_text(node_name, buffer.getSegment(0, buffer.getLength))
48707
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 47393
diff changeset
    72
      } match {
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 47393
diff changeset
    73
        case Exn.Res(header) => header
ba531af91148 simplified Document.Node.Header -- internalized errors;
wenzelm
parents: 47393
diff changeset
    74
        case Exn.Exn(exn) => Document.Node.bad_header(Exn.message(exn))
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46740
diff changeset
    75
      }
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46740
diff changeset
    76
    }
46920
5f44c8bea84e more explicit indication of swing thread context;
wenzelm
parents: 46750
diff changeset
    77
  }
44385
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
    78
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
    79
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    80
  /* overlays */
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    81
52887
98eac7b7eec3 tuned signature;
wenzelm
parents: 52862
diff changeset
    82
  private var overlays = Document.Node.Overlays.empty
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    83
52862
930ce8eacb87 tuned signature -- more uniform treatment of overlays as command mapping;
wenzelm
parents: 52859
diff changeset
    84
  def insert_overlay(command: Command, name: String, args: List[String]): Unit =
930ce8eacb87 tuned signature -- more uniform treatment of overlays as command mapping;
wenzelm
parents: 52859
diff changeset
    85
    Swing_Thread.require { overlays = overlays.insert(command, (name, args)) }
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    86
52858
863581a704a6 avoid repeated PIDE.flush_buffers when manipulating overlays;
wenzelm
parents: 52849
diff changeset
    87
  def remove_overlay(command: Command, name: String, args: List[String]): Unit =
52862
930ce8eacb87 tuned signature -- more uniform treatment of overlays as command mapping;
wenzelm
parents: 52859
diff changeset
    88
    Swing_Thread.require { overlays = overlays.remove(command, (name, args)) }
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    89
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    90
44385
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
    91
  /* perspective */
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
    92
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
    93
  // owned by Swing thread
52816
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    94
  private var _node_required = false
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    95
  def node_required: Boolean = _node_required
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    96
  def node_required_=(b: Boolean)
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    97
  {
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    98
    Swing_Thread.require()
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
    99
    if (_node_required != b) {
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
   100
      _node_required = b
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
   101
      PIDE.options_changed()
52974
908e8a36e975 tuned signature;
wenzelm
parents: 52973
diff changeset
   102
      PIDE.editor.flush()
52816
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
   103
    }
c608e0ade554 home-grown mouse handling to pretend that the painted checkbox is actually a Swing component;
wenzelm
parents: 52815
diff changeset
   104
  }
52808
143f225e50f5 allow explicit indication of required node: full eval, no prints;
wenzelm
parents: 52807
diff changeset
   105
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   106
  val empty_perspective: Document.Node.Perspective_Text =
52887
98eac7b7eec3 tuned signature;
wenzelm
parents: 52862
diff changeset
   107
    Document.Node.Perspective(false, Text.Perspective.empty, Document.Node.Overlays.empty)
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   108
52808
143f225e50f5 allow explicit indication of required node: full eval, no prints;
wenzelm
parents: 52807
diff changeset
   109
  def node_perspective(): Document.Node.Perspective_Text =
44385
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
   110
  {
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
   111
    Swing_Thread.require()
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   112
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   113
    if (Isabelle.continuous_checking) {
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   114
      Document.Node.Perspective(node_required, Text.Perspective(
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   115
        for {
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   116
          doc_view <- PIDE.document_views(buffer)
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   117
          range <- doc_view.perspective().ranges
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   118
        } yield range), overlays)
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   119
    }
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   120
    else empty_perspective
44385
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
   121
  }
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
   122
e7fdb008aa7d propagate editor perspective through document model;
wenzelm
parents: 44379
diff changeset
   123
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   124
  /* edits */
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   125
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   126
  def init_edits(): List[Document.Edit_Text] =
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   127
  {
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   128
    Swing_Thread.require()
52815
eaad5fe7bb1b actions and shortcuts to change node_required status, with painter that looks like CheckBox (non-clickable);
wenzelm
parents: 52808
diff changeset
   129
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   130
    val header = node_header()
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   131
    val text = JEdit_Lib.buffer_text(buffer)
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   132
    val perspective = node_perspective()
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   133
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   134
    List(session.header_edit(node_name, header),
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   135
      node_name -> Document.Node.Clear(),
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   136
      node_name -> Document.Node.Edits(List(Text.Edit.insert(0, text))),
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   137
      node_name -> perspective)
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   138
  }
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   139
52808
143f225e50f5 allow explicit indication of required node: full eval, no prints;
wenzelm
parents: 52807
diff changeset
   140
  def node_edits(perspective: Document.Node.Perspective_Text, text_edits: List[Text.Edit])
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   141
    : List[Document.Edit_Text] =
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   142
  {
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   143
    Swing_Thread.require()
52815
eaad5fe7bb1b actions and shortcuts to change node_required status, with painter that looks like CheckBox (non-clickable);
wenzelm
parents: 52808
diff changeset
   144
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   145
    val header = node_header()
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   146
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   147
    List(session.header_edit(node_name, header),
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   148
      node_name -> Document.Node.Edits(text_edits),
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   149
      node_name -> perspective)
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   150
  }
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   151
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   152
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   153
  /* pending edits */
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43644
diff changeset
   154
43644
ea08ce1c314b tuned signature;
wenzelm
parents: 43512
diff changeset
   155
  private object pending_edits  // owned by Swing thread
38224
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   156
  {
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38417
diff changeset
   157
    private val pending = new mutable.ListBuffer[Text.Edit]
52849
199e9fa5a5c2 maintain overlays within node perspective;
wenzelm
parents: 52816
diff changeset
   158
    private var last_perspective = empty_perspective
44438
0fc38897248a early filtering of unchanged perspective;
wenzelm
parents: 44436
diff changeset
   159
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38417
diff changeset
   160
    def snapshot(): List[Text.Edit] = pending.toList
38224
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   161
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   162
    def flushed_edits(): List[Document.Edit_Text] =
38224
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   163
    {
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   164
      Swing_Thread.require()
44438
0fc38897248a early filtering of unchanged perspective;
wenzelm
parents: 44436
diff changeset
   165
47393
d760049b2d18 more robust update_perspective, e.g. required after reload of buffer that is not at start position;
wenzelm
parents: 47058
diff changeset
   166
      val edits = snapshot()
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   167
      val new_perspective = node_perspective()
47393
d760049b2d18 more robust update_perspective, e.g. required after reload of buffer that is not at start position;
wenzelm
parents: 47058
diff changeset
   168
      if (!edits.isEmpty || last_perspective != new_perspective) {
d760049b2d18 more robust update_perspective, e.g. required after reload of buffer that is not at start position;
wenzelm
parents: 47058
diff changeset
   169
        pending.clear
d760049b2d18 more robust update_perspective, e.g. required after reload of buffer that is not at start position;
wenzelm
parents: 47058
diff changeset
   170
        last_perspective = new_perspective
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   171
        node_edits(new_perspective, edits)
43648
e32de528b5ef more explicit edit_node vs. init_node;
wenzelm
parents: 43644
diff changeset
   172
      }
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   173
      else Nil
38224
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   174
    }
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   175
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   176
    def flush(): Unit = session.update(flushed_edits())
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   177
52767
9c28559e5fff back to model.update_perspective with delay (cf. a20631db9c8a);
wenzelm
parents: 52759
diff changeset
   178
    val delay_flush =
50207
54be125d8cdc tuned signature;
wenzelm
parents: 50205
diff changeset
   179
      Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
40478
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   180
38425
e467db701d78 moved Text_Edit to Text.Edit;
wenzelm
parents: 38417
diff changeset
   181
    def +=(edit: Text.Edit)
38224
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   182
    {
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   183
      Swing_Thread.require()
809578d7f6af more explicit model of pending text edits;
wenzelm
parents: 38223
diff changeset
   184
      pending += edit
49195
9d10bd85c1be more explicit Delay operations;
wenzelm
parents: 48885
diff changeset
   185
      delay_flush.invoke()
38222
dac5fa0ac971 replaced individual Document_Model history by all-inclusive one in Session;
wenzelm
parents: 38158
diff changeset
   186
    }
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   187
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   188
    def init()
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   189
    {
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   190
      flush()
50363
2f8dc9e65401 tuned signature in accordance to document operations;
wenzelm
parents: 50344
diff changeset
   191
      session.update(init_edits())
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   192
    }
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   193
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   194
    def exit()
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   195
    {
49195
9d10bd85c1be more explicit Delay operations;
wenzelm
parents: 48885
diff changeset
   196
      delay_flush.revoke()
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   197
      flush()
44436
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   198
    }
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   199
  }
546adfa8a6fc update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents: 44385
diff changeset
   200
52759
a20631db9c8a support declarative editor_execution_range, instead of old-style check/cancel buttons;
wenzelm
parents: 50565
diff changeset
   201
  def flushed_edits(): List[Document.Edit_Text] = pending_edits.flushed_edits()
52767
9c28559e5fff back to model.update_perspective with delay (cf. a20631db9c8a);
wenzelm
parents: 52759
diff changeset
   202
9c28559e5fff back to model.update_perspective with delay (cf. a20631db9c8a);
wenzelm
parents: 52759
diff changeset
   203
  def update_perspective(): Unit = pending_edits.delay_flush.invoke()
44776
47e8c8daccae added "check" button: adhoc change to full buffer perspective;
wenzelm
parents: 44615
diff changeset
   204
38152
eab0d1c2e46e Change.Snapshot: include from_current/to_current here, with precomputed changes;
wenzelm
parents: 38151
diff changeset
   205
eab0d1c2e46e Change.Snapshot: include from_current/to_current here, with precomputed changes;
wenzelm
parents: 38151
diff changeset
   206
  /* snapshot */
34731
c0cb6bd10eec keep BufferListener and TextAreaExtension private;
wenzelm
parents: 34724
diff changeset
   207
38417
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38359
diff changeset
   208
  def snapshot(): Document.Snapshot =
b8922ae21111 renamed class Document to Document.Version etc.;
wenzelm
parents: 38359
diff changeset
   209
  {
38223
2a368e8e0a80 more explicit treatment of Swing thread context;
wenzelm
parents: 38222
diff changeset
   210
    Swing_Thread.require()
52973
d5f7fa1498b7 tuned signature;
wenzelm
parents: 52887
diff changeset
   211
    session.snapshot(node_name, pending_edits.snapshot())
38223
2a368e8e0a80 more explicit treatment of Swing thread context;
wenzelm
parents: 38222
diff changeset
   212
  }
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   213
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   214
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   215
  /* buffer listener */
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   216
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   217
  private val buffer_listener: BufferListener = new BufferAdapter
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   218
  {
40478
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   219
    override def bufferLoaded(buffer: JEditBuffer)
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   220
    {
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   221
      pending_edits.init()
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   222
    }
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   223
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   224
    override def contentInserted(buffer: JEditBuffer,
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   225
      start_line: Int, offset: Int, num_lines: Int, length: Int)
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   226
    {
40478
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   227
      if (!buffer.isLoading)
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   228
        pending_edits += Text.Edit.insert(offset, buffer.getText(offset, length))
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   229
    }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   230
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   231
    override def preContentRemoved(buffer: JEditBuffer,
38426
2858ec7b6dd8 specific types Text.Offset and Text.Range;
wenzelm
parents: 38425
diff changeset
   232
      start_line: Int, offset: Int, num_lines: Int, removed_length: Int)
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   233
    {
40478
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   234
      if (!buffer.isLoading)
4bae781b8f7c replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents: 40474
diff changeset
   235
        pending_edits += Text.Edit.remove(offset, buffer.getText(offset, removed_length))
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   236
    }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   237
  }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   238
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   239
38158
8aaa21db41f3 Document_Model: include token marker here;
wenzelm
parents: 38152
diff changeset
   240
  /* activation */
37557
1ae272fd4082 refresh Isabelle token marker after buffer properties changed, e.g. when fold mode is switched;
wenzelm
parents: 37555
diff changeset
   241
43512
270ce5ff2086 clarified init/exit procedure;
wenzelm
parents: 43452
diff changeset
   242
  private def activate()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   243
  {
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   244
    buffer.addBufferListener(buffer_listener)
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50207
diff changeset
   245
    pending_edits.flush()
44358
2a2df4de1bbe more robust initialization of token marker and line context wrt. session startup;
wenzelm
parents: 44182
diff changeset
   246
    Token_Markup.refresh_buffer(buffer)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   247
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   248
43512
270ce5ff2086 clarified init/exit procedure;
wenzelm
parents: 43452
diff changeset
   249
  private def deactivate()
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   250
  {
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46739
diff changeset
   251
    pending_edits.exit()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   252
    buffer.removeBufferListener(buffer_listener)
44358
2a2df4de1bbe more robust initialization of token marker and line context wrt. session startup;
wenzelm
parents: 44182
diff changeset
   253
    Token_Markup.refresh_buffer(buffer)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   254
  }
34447
wenzelm
parents: 34446
diff changeset
   255
}