src/Tools/jEdit/src/prover/Command.scala
author immler@in.tum.de
Tue Jun 02 19:00:58 2009 +0200 (2009-06-02 ago)
changeset 34593 cf37a9f988bf
parent 34577 aef72e071725
child 34597 a0c84b0edb9a
permissions -rw-r--r--
ignore unchanged commands
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Prover commands with semantic state
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34318
     8
package isabelle.prover
wenzelm@34318
     9
wenzelm@34451
    10
immler@34393
    11
import javax.swing.text.Position
immler@34393
    12
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34318
    13
wenzelm@34497
    14
import scala.collection.mutable
wenzelm@34486
    15
wenzelm@34491
    16
import isabelle.proofdocument.{Text, Token, ProofDocument}
wenzelm@34451
    17
import isabelle.jedit.{Isabelle, Plugin}
wenzelm@34476
    18
import isabelle.XML
wenzelm@34451
    19
wenzelm@34451
    20
import sidekick.{SideKickParsedData, IAsset}
wenzelm@34451
    21
wenzelm@34451
    22
wenzelm@34318
    23
object Command {
wenzelm@34458
    24
  object Status extends Enumeration {
wenzelm@34318
    25
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    26
    val FINISHED = Value("FINISHED")
wenzelm@34318
    27
    val FAILED = Value("FAILED")
wenzelm@34318
    28
  }
wenzelm@34318
    29
}
wenzelm@34318
    30
wenzelm@34451
    31
immler@34554
    32
class Command(val tokens: List[Token], val starts: Map[Token, Int])
wenzelm@34451
    33
{
wenzelm@34451
    34
  val id = Isabelle.plugin.id()
wenzelm@34500
    35
wenzelm@34500
    36
  /* content */
wenzelm@34500
    37
wenzelm@34495
    38
  override def toString = name
wenzelm@34495
    39
immler@34526
    40
  val name = tokens.head.content
immler@34575
    41
  val content:String = Token.string_from_tokens(tokens, starts)
wenzelm@34451
    42
immler@34554
    43
  def start(doc: ProofDocument) = doc.token_start(tokens.first)
immler@34554
    44
  def stop(doc: ProofDocument) = doc.token_start(tokens.last) + tokens.last.length
wenzelm@34451
    45
immler@34593
    46
  def contains(p: Token) = tokens.contains(p)
immler@34593
    47
wenzelm@34500
    48
  /* command status */
wenzelm@34500
    49
wenzelm@34508
    50
  var state_id: IsarDocument.State_ID = null
wenzelm@34508
    51
wenzelm@34500
    52
  private var _status = Command.Status.UNPROCESSED
wenzelm@34500
    53
  def status = _status
wenzelm@34500
    54
  def status_=(st: Command.Status.Value) {
wenzelm@34500
    55
    if (st == Command.Status.UNPROCESSED) {
wenzelm@34533
    56
      state_results.clear
wenzelm@34500
    57
      // delete markup
immler@34577
    58
      markup_root = markup_root.filter(_.info match {
immler@34564
    59
          case RootInfo() | OuterInfo(_) => true
immler@34560
    60
          case _ => false
immler@34577
    61
        }).head
wenzelm@34500
    62
    }
wenzelm@34500
    63
    _status = st
wenzelm@34500
    64
  }
wenzelm@34500
    65
wenzelm@34500
    66
wenzelm@34500
    67
  /* results */
wenzelm@34500
    68
wenzelm@34500
    69
  private val results = new mutable.ListBuffer[XML.Tree]
wenzelm@34500
    70
  private val state_results = new mutable.ListBuffer[XML.Tree]
wenzelm@34533
    71
  def add_result(running: Boolean, tree: XML.Tree) = synchronized {
wenzelm@34508
    72
    (if (running) state_results else results) += tree
wenzelm@34508
    73
  }
wenzelm@34500
    74
wenzelm@34500
    75
  def result_document = XML.document(
wenzelm@34500
    76
    results.toList ::: state_results.toList match {
wenzelm@34500
    77
      case Nil => XML.Elem("message", Nil, Nil)
wenzelm@34500
    78
      case List(elem) => elem
wenzelm@34500
    79
      case elems => XML.Elem("messages", Nil, elems)
wenzelm@34500
    80
    }, "style")
wenzelm@34500
    81
wenzelm@34500
    82
wenzelm@34500
    83
  /* markup */
wenzelm@34451
    84
immler@34557
    85
  val empty_root_node =
immler@34560
    86
    new MarkupNode(this, 0, starts(tokens.last) - starts(tokens.first) + tokens.last.length, Nil,
immler@34564
    87
                   id, content, RootInfo())
immler@34557
    88
immler@34560
    89
  var markup_root = empty_root_node
wenzelm@34451
    90
immler@34560
    91
  def highlight_node: MarkupNode = {
immler@34560
    92
    import MarkupNode._
immler@34564
    93
    markup_root.filter(_.info match {
immler@34564
    94
      case RootInfo() | OuterInfo(_) | HighlightInfo(_) => true
immler@34560
    95
      case _ => false
immler@34560
    96
    }).head
immler@34560
    97
  }
immler@34556
    98
immler@34564
    99
  def markup_node(begin: Int, end: Int, info: MarkupInfo) =
immler@34557
   100
    new MarkupNode(this, begin, end, Nil, id,
immler@34557
   101
                   if (end <= content.length && begin >= 0) content.substring(begin, end) else "wrong indices??",
immler@34564
   102
                   info)
immler@34556
   103
immler@34562
   104
  def type_at(pos: Int): String = {
immler@34564
   105
    val types = markup_root.filter(_.info match {case TypeInfo(_) => true case _ => false})
immler@34562
   106
    types.flatten(_.flatten).
immler@34562
   107
      find(t => t.start <= pos && t.stop > pos).
immler@34564
   108
      map(t => "\"" + t.content + "\" : " + (t.info match {case TypeInfo(i) => i case _ => ""})).
immler@34562
   109
      getOrElse(null)
immler@34562
   110
  }
immler@34568
   111
immler@34568
   112
  def ref_at(pos: Int): Option[MarkupNode] =
immler@34568
   113
    markup_root.filter(_.info match {case RefInfo(_,_,_,_) => true case _ => false}).
immler@34568
   114
      flatten(_.flatten).
immler@34568
   115
      find(t => t.start <= pos && t.stop > pos)
wenzelm@34451
   116
}