src/Tools/jEdit/src/jedit/document_model.scala
author wenzelm
Tue Mar 30 00:13:27 2010 +0200 (2010-03-30)
changeset 36015 6111de7c916a
parent 34871 e596a0b71f3c
child 36760 b82a698ef6c9
permissions -rw-r--r--
adapted to Scala 2.8.0 Beta 1;
wenzelm@34407
     1
/*
wenzelm@34784
     2
 * Document model connected to jEdit buffer
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Fabian Immler, TU Munich
wenzelm@34447
     5
 * @author Makarius
wenzelm@34407
     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@34703
    13
import scala.actors.Actor, Actor._
wenzelm@34693
    14
import scala.collection.mutable
wenzelm@34446
    15
wenzelm@34784
    16
import org.gjt.sp.jedit.Buffer
wenzelm@34783
    17
import org.gjt.sp.jedit.buffer.{BufferAdapter, BufferListener, JEditBuffer}
immler@34649
    18
import org.gjt.sp.jedit.syntax.{ModeProvider, SyntaxStyle}
wenzelm@34318
    19
wenzelm@34760
    20
wenzelm@34784
    21
object Document_Model
wenzelm@34588
    22
{
wenzelm@34784
    23
  /* document model of buffer */
wenzelm@34784
    24
wenzelm@34784
    25
  private val key = "isabelle.document_model"
wenzelm@34784
    26
wenzelm@34784
    27
  def init(session: Session, buffer: Buffer): Document_Model =
wenzelm@34716
    28
  {
wenzelm@34789
    29
    Swing_Thread.assert()
wenzelm@34784
    30
    val model = new Document_Model(session, buffer)
wenzelm@34784
    31
    buffer.setProperty(key, model)
wenzelm@34784
    32
    model.activate()
wenzelm@34784
    33
    model
wenzelm@34784
    34
  }
wenzelm@34784
    35
wenzelm@34788
    36
  def apply(buffer: Buffer): Option[Document_Model] =
wenzelm@34784
    37
  {
wenzelm@34789
    38
    Swing_Thread.assert()
wenzelm@34784
    39
    buffer.getProperty(key) match {
wenzelm@34784
    40
      case model: Document_Model => Some(model)
wenzelm@34784
    41
      case _ => None
wenzelm@34784
    42
    }
wenzelm@34784
    43
  }
wenzelm@34784
    44
wenzelm@34784
    45
  def exit(buffer: Buffer)
wenzelm@34784
    46
  {
wenzelm@34789
    47
    Swing_Thread.assert()
wenzelm@34788
    48
    apply(buffer) match {
wenzelm@34784
    49
      case None => error("No document model for buffer: " + buffer)
wenzelm@34784
    50
      case Some(model) =>
wenzelm@34784
    51
        model.deactivate()
wenzelm@34784
    52
        buffer.unsetProperty(key)
immler@34653
    53
    }
wenzelm@34318
    54
  }
wenzelm@34318
    55
}
wenzelm@34318
    56
wenzelm@34784
    57
class Document_Model(val session: Session, val buffer: Buffer)
wenzelm@34588
    58
{
wenzelm@34828
    59
  /* history */
wenzelm@34731
    60
wenzelm@34833
    61
  private val document_0 = session.begin_document(buffer.getName)
wenzelm@34833
    62
wenzelm@34830
    63
  @volatile private var history =  // owned by Swing thread
wenzelm@34853
    64
    new Change(document_0.id, None, Nil, Future.value(Nil, document_0))
wenzelm@34731
    65
wenzelm@34830
    66
  def current_change(): Change = history
wenzelm@34854
    67
  def recent_document(): Document = current_change().ancestors.find(_.is_assigned).get.join_document
immler@34654
    68
wenzelm@34731
    69
immler@34680
    70
  /* transforming offsets */
immler@34680
    71
wenzelm@34838
    72
  private def changes_from(doc: Document): List[Text_Edit] =
wenzelm@34828
    73
  {
wenzelm@34828
    74
    Swing_Thread.assert()
wenzelm@34833
    75
    (edits_buffer.toList /:
wenzelm@34833
    76
      current_change.ancestors.takeWhile(_.id != doc.id))((edits, change) => change.edits ::: edits)
wenzelm@34828
    77
  }
immler@34680
    78
wenzelm@34823
    79
  def from_current(doc: Document, offset: Int): Int =
immler@34718
    80
    (offset /: changes_from(doc).reverse) ((i, change) => change before i)
immler@34680
    81
wenzelm@34823
    82
  def to_current(doc: Document, offset: Int): Int =
immler@34718
    83
    (offset /: changes_from(doc)) ((i, change) => change after i)
immler@34680
    84
wenzelm@34834
    85
  def lines_of_command(doc: Document, cmd: Command): (Int, Int) =
immler@34680
    86
  {
wenzelm@34858
    87
    val start = doc.command_start(cmd).get  // FIXME total?
wenzelm@34856
    88
    val stop = start + cmd.length
wenzelm@34856
    89
    (buffer.getLineOfOffset(to_current(doc, start)),
wenzelm@34856
    90
     buffer.getLineOfOffset(to_current(doc, stop)))
immler@34680
    91
  }
immler@34680
    92
immler@34680
    93
wenzelm@34828
    94
  /* text edits */
wenzelm@34828
    95
wenzelm@34838
    96
  private val edits_buffer = new mutable.ListBuffer[Text_Edit]   // owned by Swing thread
wenzelm@34828
    97
wenzelm@34828
    98
  private val edits_delay = Swing_Thread.delay_last(300) {
wenzelm@34828
    99
    if (!edits_buffer.isEmpty) {
wenzelm@34853
   100
      val new_change = current_change().edit(session, edits_buffer.toList)
wenzelm@34828
   101
      edits_buffer.clear
wenzelm@34853
   102
      history = new_change
wenzelm@34853
   103
      new_change.result.map(_ => session.input(new_change))
wenzelm@34828
   104
    }
wenzelm@34828
   105
  }
wenzelm@34828
   106
wenzelm@34828
   107
wenzelm@34828
   108
  /* buffer listener */
wenzelm@34828
   109
wenzelm@34828
   110
  private val buffer_listener: BufferListener = new BufferAdapter
wenzelm@34828
   111
  {
wenzelm@34828
   112
    override def contentInserted(buffer: JEditBuffer,
wenzelm@34828
   113
      start_line: Int, offset: Int, num_lines: Int, length: Int)
wenzelm@34828
   114
    {
wenzelm@34864
   115
      edits_buffer += new Text_Edit(true, offset, buffer.getText(offset, length))
wenzelm@34828
   116
      edits_delay()
wenzelm@34828
   117
    }
wenzelm@34828
   118
wenzelm@34828
   119
    override def preContentRemoved(buffer: JEditBuffer,
wenzelm@34828
   120
      start_line: Int, start: Int, num_lines: Int, removed_length: Int)
wenzelm@34828
   121
    {
wenzelm@34864
   122
      edits_buffer += new Text_Edit(false, start, buffer.getText(start, removed_length))
wenzelm@34828
   123
      edits_delay()
wenzelm@34828
   124
    }
wenzelm@34828
   125
  }
wenzelm@34828
   126
wenzelm@34828
   127
wenzelm@34784
   128
  /* activation */
immler@34680
   129
wenzelm@34784
   130
  def activate()
wenzelm@34784
   131
  {
wenzelm@34828
   132
    buffer.setTokenMarker(new Isabelle_Token_Marker(this))
wenzelm@34784
   133
    buffer.addBufferListener(buffer_listener)
wenzelm@34784
   134
    buffer.propertiesChanged()
immler@34680
   135
wenzelm@34864
   136
    edits_buffer += new Text_Edit(true, 0, buffer.getText(0, buffer.getLength))
wenzelm@34784
   137
    edits_delay()
immler@34680
   138
  }
immler@34680
   139
wenzelm@34784
   140
  def deactivate()
immler@34680
   141
  {
wenzelm@34784
   142
    buffer.setTokenMarker(buffer.getMode.getTokenMarker)
wenzelm@34784
   143
    buffer.removeBufferListener(buffer_listener)
immler@34680
   144
  }
wenzelm@34447
   145
}