src/Tools/jEdit/src/jedit/document_model.scala
author wenzelm
Mon, 11 Jan 2010 23:00:05 +0100
changeset 34871 e596a0b71f3c
parent 34864 fd6801e87944
child 36015 6111de7c916a
permissions -rw-r--r--
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     1
/*
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
     2
 * Document model connected to jEdit buffer
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     3
 *
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     4
 * @author Fabian Immler, TU Munich
34447
wenzelm
parents: 34446
diff changeset
     5
 * @author Makarius
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     6
 */
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
34703
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
    11
import scala.actors.Actor, Actor._
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
    12
import scala.collection.mutable
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    13
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    14
import org.gjt.sp.jedit.Buffer
34783
cb95d6bbf5f1 clarified BufferListener: use adapter, listen to contentInserted instead of preContentInserted;
wenzelm
parents: 34778
diff changeset
    15
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer}
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    16
import org.gjt.sp.jedit.syntax.{ModeProvider, SyntaxStyle}
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    17
34760
dc7f5e0d9d27 misc modernization of names;
wenzelm
parents: 34759
diff changeset
    18
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    19
object Document_Model
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    20
{
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    21
  /* 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
    22
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    23
  private val key = "isabelle.document_model"
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    24
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    25
  def init(session: Session, buffer: Buffer): Document_Model =
34716
b8f2b44529fd tuned color (PG 4.0);
wenzelm
parents: 34712
diff changeset
    26
  {
34789
30528e68f774 some explicit Swing_Thread guards;
wenzelm
parents: 34788
diff changeset
    27
    Swing_Thread.assert()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    28
    val model = new Document_Model(session, buffer)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    29
    buffer.setProperty(key, model)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    30
    model.activate()
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    31
    model
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    32
  }
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    33
34788
3779c54a2d21 direct apply for Document_Model and Document_View;
wenzelm
parents: 34784
diff changeset
    34
  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
    35
  {
34789
30528e68f774 some explicit Swing_Thread guards;
wenzelm
parents: 34788
diff changeset
    36
    Swing_Thread.assert()
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    37
    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
    38
      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
    39
      case _ => None
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    40
    }
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    41
  }
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    42
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    43
  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
    44
  {
34789
30528e68f774 some explicit Swing_Thread guards;
wenzelm
parents: 34788
diff changeset
    45
    Swing_Thread.assert()
34788
3779c54a2d21 direct apply for Document_Model and Document_View;
wenzelm
parents: 34784
diff changeset
    46
    apply(buffer) match {
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    47
      case None => error("No document model for buffer: " + buffer)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    48
      case Some(model) =>
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    49
        model.deactivate()
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    50
        buffer.unsetProperty(key)
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    51
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    52
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    53
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    54
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
    55
class Document_Model(val session: Session, val buffer: Buffer)
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    56
{
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    57
  /* history */
34731
c0cb6bd10eec keep BufferListener and TextAreaExtension private;
wenzelm
parents: 34724
diff changeset
    58
34833
683ddf358698 recovered explicit Changed.id (copy of future document) -- avoids premature joining of result;
wenzelm
parents: 34830
diff changeset
    59
  private val document_0 = session.begin_document(buffer.getName)
683ddf358698 recovered explicit Changed.id (copy of future document) -- avoids premature joining of result;
wenzelm
parents: 34830
diff changeset
    60
34830
wenzelm
parents: 34829
diff changeset
    61
  @volatile private var history =  // owned by Swing thread
34853
32b49207ca20 misc tuning and clarification of Document/Change;
wenzelm
parents: 34840
diff changeset
    62
    new Change(document_0.id, None, Nil, Future.value(Nil, document_0))
34731
c0cb6bd10eec keep BufferListener and TextAreaExtension private;
wenzelm
parents: 34724
diff changeset
    63
34830
wenzelm
parents: 34829
diff changeset
    64
  def current_change(): Change = history
34854
64c2eb92df9f tuned document changes;
wenzelm
parents: 34853
diff changeset
    65
  def recent_document(): Document = current_change().ancestors.find(_.is_assigned).get.join_document
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
    66
34731
c0cb6bd10eec keep BufferListener and TextAreaExtension private;
wenzelm
parents: 34724
diff changeset
    67
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    68
  /* transforming offsets */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    69
34838
08a72dc4868e use Text_Edit provided by Isabelle;
wenzelm
parents: 34835
diff changeset
    70
  private def changes_from(doc: Document): List[Text_Edit] =
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    71
  {
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    72
    Swing_Thread.assert()
34833
683ddf358698 recovered explicit Changed.id (copy of future document) -- avoids premature joining of result;
wenzelm
parents: 34830
diff changeset
    73
    (edits_buffer.toList /:
683ddf358698 recovered explicit Changed.id (copy of future document) -- avoids premature joining of result;
wenzelm
parents: 34830
diff changeset
    74
      current_change.ancestors.takeWhile(_.id != doc.id))((edits, change) => change.edits ::: edits)
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    75
  }
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    76
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34814
diff changeset
    77
  def from_current(doc: Document, offset: Int): Int =
34718
39e3039645ae corrected order
immler@in.tum.de
parents: 34717
diff changeset
    78
    (offset /: changes_from(doc).reverse) ((i, change) => change before i)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    79
34823
2f3ea37c5958 renamed Proof_Document to Document;
wenzelm
parents: 34814
diff changeset
    80
  def to_current(doc: Document, offset: Int): Int =
34718
39e3039645ae corrected order
immler@in.tum.de
parents: 34717
diff changeset
    81
    (offset /: changes_from(doc)) ((i, change) => change after i)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    82
34834
df9af932e418 slightly more uniform/robust handling of visible document;
wenzelm
parents: 34833
diff changeset
    83
  def lines_of_command(doc: Document, cmd: Command): (Int, Int) =
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    84
  {
34858
ad486bd8abf3 further tuning of command_start;
wenzelm
parents: 34856
diff changeset
    85
    val start = doc.command_start(cmd).get  // FIXME total?
34856
aa9e22d9f9a7 eliminated Command.stop, which tends to case duplicate traversal of commands;
wenzelm
parents: 34854
diff changeset
    86
    val stop = start + cmd.length
aa9e22d9f9a7 eliminated Command.stop, which tends to case duplicate traversal of commands;
wenzelm
parents: 34854
diff changeset
    87
    (buffer.getLineOfOffset(to_current(doc, start)),
aa9e22d9f9a7 eliminated Command.stop, which tends to case duplicate traversal of commands;
wenzelm
parents: 34854
diff changeset
    88
     buffer.getLineOfOffset(to_current(doc, stop)))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    89
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    90
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    91
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    92
  /* text edits */
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    93
34838
08a72dc4868e use Text_Edit provided by Isabelle;
wenzelm
parents: 34835
diff changeset
    94
  private val edits_buffer = new mutable.ListBuffer[Text_Edit]   // owned by Swing thread
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    95
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    96
  private val edits_delay = Swing_Thread.delay_last(300) {
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    97
    if (!edits_buffer.isEmpty) {
34853
32b49207ca20 misc tuning and clarification of Document/Change;
wenzelm
parents: 34840
diff changeset
    98
      val new_change = current_change().edit(session, edits_buffer.toList)
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
    99
      edits_buffer.clear
34853
32b49207ca20 misc tuning and clarification of Document/Change;
wenzelm
parents: 34840
diff changeset
   100
      history = new_change
32b49207ca20 misc tuning and clarification of Document/Change;
wenzelm
parents: 34840
diff changeset
   101
      new_change.result.map(_ => session.input(new_change))
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   102
    }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   103
  }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   104
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   105
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   106
  /* buffer listener */
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   107
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   108
  private val buffer_listener: BufferListener = new BufferAdapter
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   109
  {
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   110
    override def contentInserted(buffer: JEditBuffer,
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   111
      start_line: Int, offset: Int, num_lines: Int, length: Int)
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   112
    {
34864
fd6801e87944 simplified Text_Edit;
wenzelm
parents: 34858
diff changeset
   113
      edits_buffer += new Text_Edit(true, offset, buffer.getText(offset, length))
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   114
      edits_delay()
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   115
    }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   116
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   117
    override def preContentRemoved(buffer: JEditBuffer,
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   118
      start_line: Int, start: Int, num_lines: Int, removed_length: Int)
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   119
    {
34864
fd6801e87944 simplified Text_Edit;
wenzelm
parents: 34858
diff changeset
   120
      edits_buffer += new Text_Edit(false, start, buffer.getText(start, removed_length))
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   121
      edits_delay()
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   122
    }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   123
  }
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   124
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   125
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   126
  /* activation */
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   127
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   128
  def activate()
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   129
  {
34828
c2308b317244 misc tuning;
wenzelm
parents: 34827
diff changeset
   130
    buffer.setTokenMarker(new Isabelle_Token_Marker(this))
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   131
    buffer.addBufferListener(buffer_listener)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   132
    buffer.propertiesChanged()
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   133
34864
fd6801e87944 simplified Text_Edit;
wenzelm
parents: 34858
diff changeset
   134
    edits_buffer += new Text_Edit(true, 0, buffer.getText(0, buffer.getLength))
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   135
    edits_delay()
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   136
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   137
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   138
  def deactivate()
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   139
  {
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   140
    buffer.setTokenMarker(buffer.getMode.getTokenMarker)
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34783
diff changeset
   141
    buffer.removeBufferListener(buffer_listener)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   142
  }
34447
wenzelm
parents: 34446
diff changeset
   143
}