src/Tools/jEdit/src/jedit/TheoryView.scala
author wenzelm
Sun, 06 Sep 2009 16:21:01 +0200
changeset 34716 b8f2b44529fd
parent 34712 4f0ee5ab0380
child 34717 3f32e08bbb6c
permissions -rw-r--r--
tuned color (PG 4.0);
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
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
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
34703
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
    17
import java.awt.{Color, Graphics2D}
34447
wenzelm
parents: 34446
diff changeset
    18
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
    19
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    20
import org.gjt.sp.jedit.buffer.{BufferListener, JEditBuffer}
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    21
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextAreaExtension, TextAreaPainter}
34649
70759ca6bb87 activation
immler@in.tum.de
parents: 34648
diff changeset
    22
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
    23
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    24
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    25
object TheoryView
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    26
{
34716
b8f2b44529fd tuned color (PG 4.0);
wenzelm
parents: 34712
diff changeset
    27
  def choose_color(command: Command, doc: ProofDocument): Color =
b8f2b44529fd tuned color (PG 4.0);
wenzelm
parents: 34712
diff changeset
    28
  {
b8f2b44529fd tuned color (PG 4.0);
wenzelm
parents: 34712
diff changeset
    29
    command.status(doc) match {
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    30
      case Command.Status.UNPROCESSED => new Color(255, 228, 225)
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    31
      case Command.Status.FINISHED => new Color(234, 248, 255)
34716
b8f2b44529fd tuned color (PG 4.0);
wenzelm
parents: 34712
diff changeset
    32
      case Command.Status.FAILED => new Color(255, 106, 106)
34653
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    33
      case _ => Color.red
2e033aaf128e commands carrying state-information
immler@in.tum.de
parents: 34652
diff changeset
    34
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    35
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    36
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    37
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    38
34696
wenzelm
parents: 34695
diff changeset
    39
class TheoryView(text_area: JEditTextArea)
wenzelm
parents: 34695
diff changeset
    40
  extends TextAreaExtension with BufferListener
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
    41
{
34541
e3ca0658fb6a changes of text with unique id
immler@in.tum.de
parents: 34539
diff changeset
    42
  
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    43
  val buffer = text_area.getBuffer
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    44
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    45
  // start prover
34696
wenzelm
parents: 34695
diff changeset
    46
  val prover: Prover = new Prover(Isabelle.system, Isabelle.default_logic(), change_receiver)
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
    47
  prover.start() // start actor
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    48
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    49
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
    50
  /* activation */
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    51
34566
28fa2f219f01 fixed duplicate activation
immler@in.tum.de
parents: 34565
diff changeset
    52
  private val phase_overview = new PhaseOverviewPanel(prover, text_area, to_current)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    53
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    54
  private val selected_state_controller = new CaretListener {
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
    55
    override def caretUpdate(e: CaretEvent) = {
34650
d7ba607bf684 current version in theoryview/buffer
immler@in.tum.de
parents: 34649
diff changeset
    56
      val doc = current_document()
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    57
      doc.command_at(e.getDot) match {
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    58
        case Some(cmd)
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    59
          if (doc.token_start(cmd.tokens.first) <= e.getDot &&
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    60
            Isabelle.plugin.selected_state != cmd) =>
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    61
          Isabelle.plugin.selected_state = cmd
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    62
        case _ =>
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
    63
      }
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)
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
    97
  private var _changes = List(change_0)   // owned by Swing/AWT thread
34680
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
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   111
    def apply(change: Change): Unit = change.edits.foreach {
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   112
      case Insert(start, text) => buffer.insert(start, text)
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   113
      case Remove(start, text) => buffer.remove(start, text.length)
34660
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
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   116
    def unapply(change: Change): Unit = change.edits.reverse.foreach {
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   117
      case Insert(start, text) => buffer.remove(start, text.length)
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   118
      case Remove(start, text) => buffer.insert(start, text)
34660
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
34703
ff037c17332a minor tuning;
wenzelm
parents: 34696
diff changeset
   150
    // track changes in buffer
34654
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
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   154
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   155
  /* sending edits to prover */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   156
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   157
  private val edits = new mutable.ListBuffer[Edit]   // owned by Swing/AWT thread
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   158
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
   159
  private val edits_delay = Swing_Thread.delay_last(300) {
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   160
    if (!edits.isEmpty) {
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   161
      val change = new Change(Isabelle.system.id(), Some(current_change), edits.toList)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   162
      _changes ::= change
34672
20e8dcd29a8b TheoryView starts Prover
immler@in.tum.de
parents: 34671
diff changeset
   163
      prover ! change
34660
e0561943bfc9 Change consisting of a list of Edits
immler@in.tum.de
parents: 34654
diff changeset
   164
      current_change = change
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   165
      edits.clear
34545
b928628742ed implemented to_current and from_current in dependancy of document-versions
immler@in.tum.de
parents: 34544
diff changeset
   166
    }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   167
  }
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   168
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   169
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   170
  /* BufferListener methods */
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   171
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   172
  override def preContentInserted(buffer: JEditBuffer,
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
   173
    start_line: Int, offset: Int, num_lines: Int, length: Int)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   174
  {
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   175
    edits += Insert(offset, buffer.getText(offset, length))
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
   176
    edits_delay()
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   177
  }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   178
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   179
  override def preContentRemoved(buffer: JEditBuffer,
34648
8213a350fd45 remember removed text
immler@in.tum.de
parents: 34643
diff changeset
   180
    start_line: Int, start: Int, num_lines: Int, removed_length: Int)
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   181
  {
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   182
    edits += Remove(start, buffer.getText(start, removed_length))
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
   183
    edits_delay()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   184
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   185
34695
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   186
  override def contentInserted(buffer: JEditBuffer,
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   187
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   188
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   189
  override def contentRemoved(buffer: JEditBuffer,
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   190
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   191
34446
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   192
  override def bufferLoaded(buffer: JEditBuffer) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   193
  override def foldHandlerChanged(buffer: JEditBuffer) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   194
  override def foldLevelChanged(buffer: JEditBuffer, start_line: Int, end_line: Int) { }
5c79f97ec1d1 superficial tuning;
wenzelm
parents: 34445
diff changeset
   195
  override def transactionComplete(buffer: JEditBuffer) { }
34588
e8ac8794971f superficial tuning;
wenzelm
parents: 34583
diff changeset
   196
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   197
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   198
  /* transforming offsets */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   199
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   200
  private def changes_to(doc: ProofDocument): List[Edit] =
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   201
    edits.toList ::: List.flatten(current_change.ancestors(_.id == doc.id).map(_.edits))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   202
34695
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   203
  def from_current(doc: ProofDocument, offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   204
    (offset /: changes_to(doc)) ((i, change) => change before i)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   205
34695
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   206
  def to_current(doc: ProofDocument, offset: Int): Int =
e799546c6928 minor tuning;
wenzelm
parents: 34694
diff changeset
   207
    (offset /: changes_to(doc).reverse) ((i, change) => change after i)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   208
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   209
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   210
  private def lines_of_command(cmd: Command) =
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
    val document = current_document()
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   213
    (buffer.getLineOfOffset(to_current(document, cmd.start(document))),
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   214
     buffer.getLineOfOffset(to_current(document, cmd.stop(document))))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   215
  }
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
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   218
  /* (re)painting */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   219
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
   220
  private val update_delay = Swing_Thread.delay_first(500) { buffer.propertiesChanged() }
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   221
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   222
  private def update_syntax(cmd: Command) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   223
    val (line1, line2) = lines_of_command(cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   224
    if (line2 >= text_area.getFirstLine &&
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   225
      line1 <= text_area.getFirstLine + text_area.getVisibleLines)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   226
        update_delay()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   227
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   228
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   229
  private def invalidate_line(cmd: Command) =
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
    val (start, stop) = lines_of_command(cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   232
    text_area.invalidateLineRange(start, stop)
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
    if (Isabelle.plugin.selected_state == cmd)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   235
        Isabelle.plugin.selected_state = cmd  // update State view
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   236
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   237
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   238
  private def invalidate_all() =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   239
    text_area.invalidateLineRange(text_area.getFirstPhysicalLine,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   240
      text_area.getLastPhysicalLine)
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
  private def encolor(gfx: Graphics2D,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   243
    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
   244
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   245
    val start = text_area.offsetToXY(begin)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   246
    val stop =
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   247
      if (finish < buffer.getLength) text_area.offsetToXY(finish)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   248
      else {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   249
        val p = text_area.offsetToXY(finish - 1)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   250
        val metrics = text_area.getPainter.getFontMetrics
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   251
        p.x = p.x + (metrics.charWidth(' ') max metrics.getMaxAdvance)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   252
        p
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   253
      }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   254
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   255
    if (start != null && stop != null) {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   256
      gfx.setColor(color)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   257
      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
   258
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   259
    }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   260
  }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   261
34696
wenzelm
parents: 34695
diff changeset
   262
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   263
  /* TextAreaExtension methods */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   264
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   265
  override def paintValidLine(gfx: Graphics2D,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   266
    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
   267
  {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   268
    val document = current_document()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   269
    def from_current(pos: Int) = this.from_current(document, pos)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   270
    def to_current(pos: Int) = this.to_current(document, pos)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   271
    val saved_color = gfx.getColor
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   272
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   273
    val metrics = text_area.getPainter.getFontMetrics
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
    // encolor phase
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   276
    var cmd = document.command_at(from_current(start))
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   277
    while (cmd.isDefined && cmd.get.start(document) < end) {
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   278
      val e = cmd.get
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   279
      val begin = start max to_current(e.start(document))
34709
2f0c18f9b6c7 minor tuning;
wenzelm
parents: 34703
diff changeset
   280
      val finish = (end - 1) min to_current(e.stop(document))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   281
      encolor(gfx, y, metrics.getHeight, begin, finish,
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   282
        TheoryView.choose_color(e, document), true)
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   283
      cmd = document.commands.next(e)
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   284
    }
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
    gfx.setColor(saved_color)
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
34696
wenzelm
parents: 34695
diff changeset
   289
  override def getToolTipText(x: Int, y: Int): String =
wenzelm
parents: 34695
diff changeset
   290
  {
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   291
    val document = current_document()
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   292
    val offset = from_current(document, text_area.xyToOffset(x, y))
34712
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   293
    document.command_at(offset) match {
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   294
      case Some(cmd) =>
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   295
        document.token_start(cmd.tokens.first)
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   296
        cmd.type_at(document, offset - cmd.start(document))
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   297
      case None => null
4f0ee5ab0380 replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents: 34709
diff changeset
   298
    }
34680
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
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   302
  /* receiving from prover */
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   303
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   304
  lazy val change_receiver: Actor = actor {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   305
    loop {
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   306
      react {
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   307
        case ProverEvents.Activate =>   // FIXME !?
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   308
          Swing_Thread.now {
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   309
            edits.clear
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   310
            edits += Insert(0, buffer.getText(0, buffer.getLength))
34694
51f9011c777b unified Swing_Thread.delay_first/last;
wenzelm
parents: 34693
diff changeset
   311
            edits_delay()
34693
3e995f100ad2 sealed Edit;
wenzelm
parents: 34685
diff changeset
   312
          }
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   313
        case c: Command =>
34685
93f4978fe2a8 global event buses for changes concerning commands and document edits
immler@in.tum.de
parents: 34682
diff changeset
   314
          actor{Isabelle.plugin.command_change.event(c)}
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   315
          if(current_document().commands.contains(c))
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   316
          Swing_Thread.later {
34681
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   317
            // repaint if buffer is active
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   318
            if(text_area.getBuffer == buffer) {
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   319
              update_syntax(c)
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   320
              invalidate_line(c)
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   321
              phase_overview.repaint()
74cc10b5ba51 fixed some issues with multiple active buffers
immler@in.tum.de
parents: 34680
diff changeset
   322
            }
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   323
          }
34685
93f4978fe2a8 global event buses for changes concerning commands and document edits
immler@in.tum.de
parents: 34682
diff changeset
   324
        case d: ProofDocument =>
93f4978fe2a8 global event buses for changes concerning commands and document edits
immler@in.tum.de
parents: 34682
diff changeset
   325
          actor{Isabelle.plugin.document_change.event(d)}
34680
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   326
        case x => System.err.println("warning: change_receiver ignored " + x)
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   327
      }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   328
    }
1f1f6c95de64 clarified structure of TheoryView
immler@in.tum.de
parents: 34679
diff changeset
   329
  }
34447
wenzelm
parents: 34446
diff changeset
   330
}