src/Tools/jEdit/src/jedit/TheoryView.scala
author immler@in.tum.de
Thu, 27 Aug 2009 10:51:09 +0200
changeset 34682 bcae80cc4170
parent 34681 74cc10b5ba51
child 34685 93f4978fe2a8
permissions -rw-r--r--
must unapply edits in reverse order
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     1
/*
34408
ad7b6c4813c8 added some headers and comments;
wenzelm
parents: 34407
diff changeset
     2
 * jEdit text area as document text source
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
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     5
 * @author Johannes Hölzl, TU Munich
34447
wenzelm
parents: 34446
diff changeset
     6
 * @author Makarius
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     7
 */
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     8
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     9
package isabelle.jedit
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    10
34532
aaafe9c4180b ProofDocument without state
immler@in.tum.de
parents: 34526
diff changeset
    11
import scala.actors.Actor
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34574
diff changeset
    12
import scala.actors.Actor._
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    13
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
    14
import isabelle.proofdocument.{ProofDocument, Change, Edit, Insert, Remove}
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    15
import isabelle.prover.{Prover, ProverEvents, Command}
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    16
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    17
import java.awt.Graphics2D
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    18
import java.awt.event.{ActionEvent, ActionListener}
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    19
import java.awt.Color
34447
wenzelm
parents: 34446
diff changeset
    20
import javax.swing.Timer
wenzelm
parents: 34446
diff changeset
    21
import javax.swing.event.{CaretListener, CaretEvent}
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    22
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    23
import org.gjt.sp.jedit.buffer.{BufferListener, JEditBuffer}
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    24
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextAreaExtension, TextAreaPainter}
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    25
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
    26
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    27
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    28
object TheoryView
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    29
{
34574
d68352286c91 split large changes for faster responses of prover
immler@in.tum.de
parents: 34568
diff changeset
    30
  
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    31
  def choose_color(cmd: Command, doc: ProofDocument): Color = {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    32
    cmd.status(doc) match {
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    33
      case Command.Status.UNPROCESSED => new Color(255, 228, 225)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    34
      case Command.Status.FINISHED => new Color(234, 248, 255)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    35
      case Command.Status.FAILED => new Color(255, 192, 192)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    36
      case _ => Color.red
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    37
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    38
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    39
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    40
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    41
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    42
class TheoryView (text_area: JEditTextArea)
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    43
    extends TextAreaExtension with BufferListener
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    44
{
34541
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34539
diff changeset
    45
  
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    46
  val buffer = text_area.getBuffer
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    47
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    48
  // start prover
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    49
  val prover: Prover = new Prover(Isabelle.system, Isabelle.default_logic, change_receiver)
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    50
  prover.start() // start actor
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    51
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    52
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    53
  /* activation */
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    54
34566
28fa2f219f01 fixed duplicate activation
immler@in.tum.de
parents: 34565
diff changeset
    55
  private val phase_overview = new PhaseOverviewPanel(prover, text_area, to_current)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    56
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    57
  private val selected_state_controller = new CaretListener {
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    58
    override def caretUpdate(e: CaretEvent) = {
34650
d7ba607bf684 current version in theoryview/buffer
immler@in.tum.de
parents: 34649
diff changeset
    59
      val doc = current_document()
34554
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    60
      val cmd = doc.find_command_at(e.getDot)
7dc6c231da40 abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents: 34549
diff changeset
    61
      if (cmd != null && doc.token_start(cmd.tokens.first) <= e.getDot &&
34669
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34660
diff changeset
    62
          Isabelle.plugin.selected_state != cmd)
73727c7eec64 state_update global in Plugin
immler@in.tum.de
parents: 34660
diff changeset
    63
        Isabelle.plugin.selected_state = cmd
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    64
    }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    65
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    66
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34574
diff changeset
    67
  def activate() {
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    68
    text_area.addCaretListener(selected_state_controller)
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    69
    text_area.addLeftOfScrollBar(phase_overview)
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    70
    text_area.getPainter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, this)
34538
20bfcca24658 Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents: 34532
diff changeset
    71
    buffer.setTokenMarker(new DynamicTokenMarker(buffer, prover))
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    72
    buffer.addBufferListener(this)
34671
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    73
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    74
    val dockable =
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    75
      text_area.getView.getDockableWindowManager.getDockable("isabelle-output")
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    76
    if (dockable != null) {
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    77
      val output_dockable = dockable.asInstanceOf[OutputDockable]
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    78
      val output_text_view = prover.output_text_view
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    79
      output_dockable.set_text(output_text_view)
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    80
    }
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    81
d179fcb04cbc output_info specific to prover
immler@in.tum.de
parents: 34670
diff changeset
    82
    buffer.propertiesChanged()
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    83
  }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    84
34582
5d5d253c7c29 superficial tuning;
wenzelm
parents: 34574
diff changeset
    85
  def deactivate() {
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    86
    buffer.setTokenMarker(buffer.getMode.getTokenMarker)
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    87
    buffer.removeBufferListener(this)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    88
    text_area.getPainter.removeExtension(this)
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    89
    text_area.removeLeftOfScrollBar(phase_overview)
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    90
    text_area.removeCaretListener(selected_state_controller)
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    91
  }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    92
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    93
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
    94
  /* history of changes - TODO: seperate class?*/
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
    95
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    96
  private val change_0 = new Change(prover.document_0.id, None, Nil)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    97
  private var _changes = List(change_0)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    98
  def changes = _changes
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
    99
  private var current_change = change_0
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   100
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   101
  private def doc_or_pred(c: Change): ProofDocument =
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   102
    prover.document(c.id).getOrElse(doc_or_pred(c.parent.get))
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   103
  def current_document() = doc_or_pred(current_change)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   104
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   105
  /* update to desired version */
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   106
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   107
  def set_version(goal: Change) {
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   108
    // changes in buffer must be ignored
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   109
    buffer.removeBufferListener(this)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   110
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   111
    def apply(c: Change) = c.map {
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   112
      case Insert(start, added) => buffer.insert(start, added)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   113
      case Remove(start, removed) => buffer.remove(start, removed.length)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   114
    }
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   115
34682
bcae80cc4170 must unapply edits in reverse order
immler@in.tum.de
parents: 34681
diff changeset
   116
    def unapply(c: Change) = c.toList.reverse.map {
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   117
      case Insert(start, added) => buffer.remove(start, added.length)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   118
      case Remove(start, removed) => buffer.insert(start, removed)
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   119
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   120
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   121
    // undo/redo changes
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   122
    val ancs_current = current_change.ancestors
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   123
    val ancs_goal = goal.ancestors
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   124
    val paired = ancs_current.reverse zip ancs_goal.reverse
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   125
    def last_common[A](xs: List[(A, A)]): Option[A] = {
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   126
      xs match {
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   127
        case (x, y) :: xs =>
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   128
          if (x == y)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   129
            xs match {
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   130
              case (a, b) :: ys =>
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   131
                if (a == b) last_common(xs)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   132
                else Some(x)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   133
              case _ => Some(x)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   134
            }
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   135
          else None
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   136
        case _ => None
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   137
      }
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   138
    }
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   139
    val common_anc = last_common(paired).get
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   140
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   141
    ancs_current.takeWhile(_ != common_anc) map unapply
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   142
    ancs_goal.takeWhile(_ != common_anc).reverse map apply
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   143
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   144
    current_change = goal
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   145
    // invoke repaint
34679
8be4cdbbe3a7 improved repainting
immler@in.tum.de
parents: 34673
diff changeset
   146
    buffer.propertiesChanged()
8be4cdbbe3a7 improved repainting
immler@in.tum.de
parents: 34673
diff changeset
   147
    invalidate_all()
8be4cdbbe3a7 improved repainting
immler@in.tum.de
parents: 34673
diff changeset
   148
    phase_overview.repaint()
34654
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   149
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   150
    //track changes in buffer
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   151
    buffer.addBufferListener(this)
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   152
  }
30f588245884 arbitrary history
immler@in.tum.de
parents: 34653
diff changeset
   153
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   154
  /* sending edits to prover */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   155
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   156
  private var edits: List[Edit] = Nil
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   157
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   158
  private val col_timer = new Timer(300, new ActionListener() {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   159
    override def actionPerformed(e: ActionEvent) = commit
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   160
  })
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   161
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   162
  col_timer.stop
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   163
  col_timer.setRepeats(true)
34545
b928628742ed implemented to_current and from_current in dependancy of document-versions
immler@in.tum.de
parents: 34544
diff changeset
   164
34547
68a5e91ac3a3 fixed shifting;
immler@in.tum.de
parents: 34546
diff changeset
   165
  private def commit: Unit = synchronized {
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   166
    if (!edits.isEmpty) {
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   167
      val change = new Change(Isabelle.system.id(), Some(current_change), edits)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   168
      _changes ::= change
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
   169
      prover ! change
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   170
      current_change = change
34545
b928628742ed implemented to_current and from_current in dependancy of document-versions
immler@in.tum.de
parents: 34544
diff changeset
   171
    }
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   172
    edits = Nil
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   173
    if (col_timer.isRunning())
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   174
      col_timer.stop()
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   175
  }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   176
34515
3be515f1379d use FontMetrics.getMaxAdvance if available; tuned
immler@in.tum.de
parents: 34514
diff changeset
   177
  private def delay_commit {
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   178
    if (col_timer.isRunning())
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   179
      col_timer.restart()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   180
    else
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   181
      col_timer.start()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   182
  }
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   183
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   184
  /* BufferListener methods */
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   185
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   186
  override def contentInserted(buffer: JEditBuffer,
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   187
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   188
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   189
  override def contentRemoved(buffer: JEditBuffer,
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   190
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   191
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   192
  override def preContentInserted(buffer: JEditBuffer,
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
   193
    start_line: Int, offset: Int, num_lines: Int, length: Int)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   194
  {
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   195
    edits ::= Insert(offset, buffer.getText(offset, length))
34515
3be515f1379d use FontMetrics.getMaxAdvance if available; tuned
immler@in.tum.de
parents: 34514
diff changeset
   196
    delay_commit
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   197
  }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   198
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   199
  override def preContentRemoved(buffer: JEditBuffer,
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34643
diff changeset
   200
    start_line: Int, start: Int, num_lines: Int, removed_length: Int)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   201
  {
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   202
    edits ::= Remove(start, buffer.getText(start, removed_length))
34515
3be515f1379d use FontMetrics.getMaxAdvance if available; tuned
immler@in.tum.de
parents: 34514
diff changeset
   203
    delay_commit
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   204
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   205
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   206
  override def bufferLoaded(buffer: JEditBuffer) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   207
  override def foldHandlerChanged(buffer: JEditBuffer) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   208
  override def foldLevelChanged(buffer: JEditBuffer, start_line: Int, end_line: Int) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   209
  override def transactionComplete(buffer: JEditBuffer) { }
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
   210
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   211
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   212
  /* transforming offsets */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   213
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   214
  private def changes_to(doc: ProofDocument) =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   215
    edits ::: current_change.ancestors(_.id == doc.id).flatten(_.toList)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   216
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   217
  def from_current(doc: ProofDocument, pos: Int) =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   218
    (pos /: changes_to(doc)) ((p, c) => c from_where p)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   219
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   220
  def to_current(doc: ProofDocument, pos: Int) =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   221
    (pos /: changes_to(doc).reverse) ((p, c) => c where_to p)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   222
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   223
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   224
  private def lines_of_command(cmd: Command) =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   225
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   226
    val document = current_document()
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   227
    (buffer.getLineOfOffset(to_current(document, cmd.start(document))),
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   228
     buffer.getLineOfOffset(to_current(document, cmd.stop(document))))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   229
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   230
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   231
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   232
  /* (re)painting */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   233
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   234
  private val update_delay = Swing_Thread.delay(500){ buffer.propertiesChanged() }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   235
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   236
  private def update_syntax(cmd: Command) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   237
    val (line1, line2) = lines_of_command(cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   238
    if (line2 >= text_area.getFirstLine &&
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   239
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   240
        update_delay()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   241
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   242
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   243
  private def invalidate_line(cmd: Command) =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   244
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   245
    val (start, stop) = lines_of_command(cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   246
    text_area.invalidateLineRange(start, stop)
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
    if (Isabelle.plugin.selected_state == cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   249
        Isabelle.plugin.selected_state = cmd  // update State view
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   250
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   251
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   252
  private def invalidate_all() =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   253
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   254
      text_area.getLastPhysicalLine)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   255
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   256
  private def encolor(gfx: Graphics2D,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   257
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   258
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   259
    val start = text_area.offsetToXY(begin)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   260
    val stop =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   261
      if (finish < buffer.getLength) text_area.offsetToXY(finish)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   262
      else {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   263
        val p = text_area.offsetToXY(finish - 1)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   264
        val metrics = text_area.getPainter.getFontMetrics
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   265
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   266
        p
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   267
      }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   268
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   269
    if (start != null && stop != null) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   270
      gfx.setColor(color)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   271
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   272
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   273
    }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   274
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   275
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   276
  /* TextAreaExtension methods */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   277
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   278
  override def paintValidLine(gfx: Graphics2D,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   279
    screen_line: Int, physical_line: Int, start: Int, end: Int, y: Int)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   280
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   281
    val document = current_document()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   282
    def from_current(pos: Int) = this.from_current(document, pos)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   283
    def to_current(pos: Int) = this.to_current(document, pos)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   284
    val saved_color = gfx.getColor
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   285
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   286
    val metrics = text_area.getPainter.getFontMetrics
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   287
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   288
    // encolor phase
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   289
    var e = document.find_command_at(from_current(start))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   290
    while (e != null && e.start(document) < end) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   291
      val begin = start max to_current(e.start(document))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   292
      val finish = end - 1 min to_current(e.stop(document))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   293
      encolor(gfx, y, metrics.getHeight, begin, finish,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   294
        TheoryView.choose_color(e, document), true)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   295
      e = document.commands.next(e).getOrElse(null)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   296
    }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   297
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   298
    gfx.setColor(saved_color)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   299
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   300
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   301
  override def getToolTipText(x: Int, y: Int) = {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   302
    val document = current_document()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   303
    val offset = from_current(document, text_area.xyToOffset(x, y))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   304
    val cmd = document.find_command_at(offset)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   305
    if (cmd != null) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   306
      document.token_start(cmd.tokens.first)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   307
      cmd.type_at(document, offset - cmd.start(document))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   308
    } else null
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   309
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   310
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   311
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   312
  /* receiving from prover */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   313
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   314
  lazy val change_receiver: Actor = actor {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   315
    loop {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   316
      react {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   317
        case ProverEvents.Activate =>
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   318
          edits = List(Insert(0, buffer.getText(0, buffer.getLength)))
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   319
          commit
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   320
        case c: Command =>
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   321
          if(current_document().commands.contains(c))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   322
          Swing_Thread.later {
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   323
            // repaint if buffer is active
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   324
            if(text_area.getBuffer == buffer) {
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   325
              update_syntax(c)
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   326
              invalidate_line(c)
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   327
              phase_overview.repaint()
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   328
            }
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   329
          }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   330
        case x => System.err.println("warning: change_receiver ignored " + x)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   331
      }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   332
    }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   333
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   334
34447
wenzelm
parents: 34446
diff changeset
   335
}