src/Pure/PIDE/command.scala
author wenzelm
Sun Mar 15 20:35:47 2015 +0100 (2015-03-15 ago)
changeset 59707 740a0ca7e09b
parent 59704 58dfaa369c11
child 59708 bf6ca55aae13
permissions -rw-r--r--
clarified span position;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/command.scala
wenzelm@36676
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     3
    Author:     Makarius
wenzelm@36676
     4
wenzelm@52536
     5
Prover commands with accumulated results from execution.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@45644
    11
import scala.collection.mutable
wenzelm@38872
    12
import scala.collection.immutable.SortedMap
wenzelm@59707
    13
import scala.util.parsing.input.CharSequenceReader
wenzelm@38872
    14
wenzelm@38872
    15
wenzelm@34637
    16
object Command
wenzelm@34637
    17
{
wenzelm@52849
    18
  type Edit = (Option[Command], Option[Command])
wenzelm@59704
    19
wenzelm@56746
    20
  type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Symbol.Text_Chunk)])]
wenzelm@59704
    21
  type Blobs_Info = (List[Blob], Int)
wenzelm@59704
    22
  val no_blobs: Blobs_Info = (Nil, -1)
wenzelm@52849
    23
wenzelm@52849
    24
wenzelm@38361
    25
  /** accumulated results from prover **/
wenzelm@38361
    26
wenzelm@50507
    27
  /* results */
wenzelm@50507
    28
wenzelm@50507
    29
  object Results
wenzelm@50507
    30
  {
wenzelm@51496
    31
    type Entry = (Long, XML.Tree)
wenzelm@50507
    32
    val empty = new Results(SortedMap.empty)
wenzelm@59072
    33
    def make(args: TraversableOnce[Results.Entry]): Results = (empty /: args)(_ + _)
wenzelm@59072
    34
    def merge(args: TraversableOnce[Results]): Results = (empty /: args)(_ ++ _)
wenzelm@50507
    35
  }
wenzelm@50507
    36
wenzelm@51494
    37
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    38
  {
wenzelm@50507
    39
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    40
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@56372
    41
    def iterator: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    42
wenzelm@51496
    43
    def + (entry: Results.Entry): Results =
wenzelm@50508
    44
      if (defined(entry._1)) this
wenzelm@50508
    45
      else new Results(rep + entry)
wenzelm@50508
    46
wenzelm@50508
    47
    def ++ (other: Results): Results =
wenzelm@50508
    48
      if (this eq other) this
wenzelm@50508
    49
      else if (rep.isEmpty) other
wenzelm@56372
    50
      else (this /: other.iterator)(_ + _)
wenzelm@50540
    51
wenzelm@51494
    52
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    53
    override def equals(that: Any): Boolean =
wenzelm@51494
    54
      that match {
wenzelm@51494
    55
        case other: Results => rep == other.rep
wenzelm@51494
    56
        case _ => false
wenzelm@51494
    57
      }
wenzelm@56372
    58
    override def toString: String = iterator.mkString("Results(", ", ", ")")
wenzelm@50507
    59
  }
wenzelm@50507
    60
wenzelm@50507
    61
wenzelm@55649
    62
  /* markup */
wenzelm@55649
    63
wenzelm@55649
    64
  object Markup_Index
wenzelm@55649
    65
  {
wenzelm@56746
    66
    val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
wenzelm@55649
    67
  }
wenzelm@55649
    68
wenzelm@56746
    69
  sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
wenzelm@55649
    70
wenzelm@55649
    71
  object Markups
wenzelm@55649
    72
  {
wenzelm@55649
    73
    val empty: Markups = new Markups(Map.empty)
wenzelm@55649
    74
wenzelm@55649
    75
    def init(markup: Markup_Tree): Markups =
wenzelm@55649
    76
      new Markups(Map(Markup_Index.markup -> markup))
wenzelm@55649
    77
  }
wenzelm@55649
    78
wenzelm@55649
    79
  final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
wenzelm@55649
    80
  {
wenzelm@56489
    81
    def is_empty: Boolean = rep.isEmpty
wenzelm@56489
    82
wenzelm@55649
    83
    def apply(index: Markup_Index): Markup_Tree =
wenzelm@55649
    84
      rep.getOrElse(index, Markup_Tree.empty)
wenzelm@55649
    85
wenzelm@55649
    86
    def add(index: Markup_Index, markup: Text.Markup): Markups =
wenzelm@55649
    87
      new Markups(rep + (index -> (this(index) + markup)))
wenzelm@55649
    88
wenzelm@56475
    89
    def redirection_iterator: Iterator[Document_ID.Generic] =
wenzelm@56746
    90
      for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
wenzelm@56475
    91
        yield id
wenzelm@56470
    92
wenzelm@56475
    93
    def redirect(other_id: Document_ID.Generic): Markups =
wenzelm@56489
    94
    {
wenzelm@56489
    95
      val rep1 =
wenzelm@56470
    96
        (for {
wenzelm@56746
    97
          (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
wenzelm@56470
    98
          if other_id == id
wenzelm@56746
    99
        } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
wenzelm@56489
   100
      if (rep1.isEmpty) Markups.empty else new Markups(rep1)
wenzelm@56489
   101
    }
wenzelm@56470
   102
wenzelm@55649
   103
    override def hashCode: Int = rep.hashCode
wenzelm@55649
   104
    override def equals(that: Any): Boolean =
wenzelm@55649
   105
      that match {
wenzelm@55649
   106
        case other: Markups => rep == other.rep
wenzelm@55649
   107
        case _ => false
wenzelm@55649
   108
      }
wenzelm@55649
   109
    override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
wenzelm@55649
   110
  }
wenzelm@55649
   111
wenzelm@55649
   112
wenzelm@50507
   113
  /* state */
wenzelm@50501
   114
wenzelm@56299
   115
  object State
wenzelm@56299
   116
  {
wenzelm@56299
   117
    def merge_results(states: List[State]): Command.Results =
wenzelm@56299
   118
      Results.merge(states.map(_.results))
wenzelm@56299
   119
wenzelm@56301
   120
    def merge_markup(states: List[State], index: Markup_Index,
wenzelm@56743
   121
        range: Text.Range, elements: Markup.Elements): Markup_Tree =
wenzelm@56301
   122
      Markup_Tree.merge(states.map(_.markup(index)), range, elements)
wenzelm@56299
   123
  }
wenzelm@56299
   124
wenzelm@43714
   125
  sealed case class State(
wenzelm@50501
   126
    command: Command,
wenzelm@50501
   127
    status: List[Markup] = Nil,
wenzelm@50507
   128
    results: Results = Results.empty,
wenzelm@55649
   129
    markups: Markups = Markups.empty)
wenzelm@38361
   130
  {
wenzelm@56395
   131
    lazy val protocol_status: Protocol.Status =
wenzelm@56395
   132
    {
wenzelm@56395
   133
      val warnings =
wenzelm@59203
   134
        if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
wenzelm@56395
   135
          List(Markup(Markup.WARNING, Nil))
wenzelm@56395
   136
        else Nil
wenzelm@56395
   137
      val errors =
wenzelm@56395
   138
        if (results.iterator.exists(p => Protocol.is_error(p._2)))
wenzelm@56395
   139
          List(Markup(Markup.ERROR, Nil))
wenzelm@56395
   140
        else Nil
wenzelm@56395
   141
      Protocol.Status.make((warnings ::: errors ::: status).iterator)
wenzelm@56395
   142
    }
wenzelm@55432
   143
wenzelm@55650
   144
    def markup(index: Markup_Index): Markup_Tree = markups(index)
wenzelm@55432
   145
wenzelm@56489
   146
    def redirect(other_command: Command): Option[State] =
wenzelm@56489
   147
    {
wenzelm@56489
   148
      val markups1 = markups.redirect(other_command.id)
wenzelm@56489
   149
      if (markups1.is_empty) None
wenzelm@56489
   150
      else Some(new State(other_command, Nil, Results.empty, markups1))
wenzelm@56489
   151
    }
wenzelm@49614
   152
wenzelm@51494
   153
    def eq_content(other: State): Boolean =
wenzelm@51494
   154
      command.source == other.command.source &&
wenzelm@51494
   155
      status == other.status &&
wenzelm@51494
   156
      results == other.results &&
wenzelm@55434
   157
      markups == other.markups
wenzelm@38361
   158
wenzelm@55649
   159
    private def add_status(st: Markup): State =
wenzelm@55649
   160
      copy(status = st :: status)
wenzelm@55432
   161
wenzelm@56746
   162
    private def add_markup(
wenzelm@56746
   163
      status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
wenzelm@55649
   164
    {
wenzelm@55649
   165
      val markups1 =
wenzelm@56395
   166
        if (status || Protocol.liberal_status_elements(m.info.name))
wenzelm@56462
   167
          markups.add(Markup_Index(true, chunk_name), m)
wenzelm@55649
   168
        else markups
wenzelm@56462
   169
      copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
wenzelm@55649
   170
    }
wenzelm@38361
   171
wenzelm@56470
   172
    def accumulate(
wenzelm@56470
   173
        self_id: Document_ID.Generic => Boolean,
wenzelm@56746
   174
        other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
wenzelm@56470
   175
        message: XML.Elem): State =
wenzelm@38361
   176
      message match {
wenzelm@50201
   177
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
   178
          (this /: msgs)((state, msg) =>
wenzelm@38714
   179
            msg match {
wenzelm@46152
   180
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@55649
   181
                state.
wenzelm@55649
   182
                  add_status(markup).
wenzelm@56746
   183
                  add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
wenzelm@52536
   184
              case _ =>
wenzelm@56782
   185
                Output.warning("Ignored status message: " + msg)
wenzelm@52536
   186
                state
wenzelm@38714
   187
            })
wenzelm@38581
   188
wenzelm@50201
   189
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
   190
          (this /: msgs)((state, msg) =>
wenzelm@55432
   191
            {
wenzelm@56782
   192
              def bad(): Unit = Output.warning("Ignored report message: " + msg)
wenzelm@55432
   193
wenzelm@55432
   194
              msg match {
wenzelm@57911
   195
                case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
wenzelm@56470
   196
wenzelm@56470
   197
                  val target =
wenzelm@56470
   198
                    if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
wenzelm@56470
   199
                      Some((chunk_name, command.chunks(chunk_name)))
wenzelm@56746
   200
                    else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
wenzelm@56470
   201
                    else None
wenzelm@56470
   202
wenzelm@57911
   203
                  (target, atts) match {
wenzelm@57911
   204
                    case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
wenzelm@56470
   205
                      target_chunk.incorporate(symbol_range) match {
wenzelm@55550
   206
                        case Some(range) =>
wenzelm@55822
   207
                          val props = Position.purge(atts)
wenzelm@55822
   208
                          val info = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56470
   209
                          state.add_markup(false, target_name, info)
wenzelm@55550
   210
                        case None => bad(); state
wenzelm@55432
   211
                      }
wenzelm@57911
   212
                    case _ =>
wenzelm@56514
   213
                      // silently ignore excessive reports
wenzelm@56476
   214
                      state
wenzelm@55432
   215
                  }
wenzelm@55432
   216
wenzelm@55432
   217
                case XML.Elem(Markup(name, atts), args)
wenzelm@55432
   218
                if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@55432
   219
                  val range = command.proper_range
wenzelm@55432
   220
                  val props = Position.purge(atts)
wenzelm@55432
   221
                  val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@56746
   222
                  state.add_markup(false, Symbol.Text_Chunk.Default, info)
wenzelm@55432
   223
wenzelm@56470
   224
                case _ => bad(); state
wenzelm@55432
   225
              }
wenzelm@38361
   226
            })
wenzelm@52930
   227
        case XML.Elem(Markup(name, props), body) =>
wenzelm@52930
   228
          props match {
wenzelm@50201
   229
            case Markup.Serial(i) =>
wenzelm@50201
   230
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   231
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   232
wenzelm@55433
   233
              var st = copy(results = results + (i -> message1))
wenzelm@55433
   234
              if (Protocol.is_inlined(message)) {
wenzelm@55433
   235
                for {
wenzelm@56469
   236
                  (chunk_name, chunk) <- command.chunks.iterator
wenzelm@57911
   237
                  range <- Protocol.message_positions(
wenzelm@57911
   238
                    self_id, command.position, chunk_name, chunk, message)
wenzelm@56462
   239
                } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
wenzelm@55433
   240
              }
wenzelm@55433
   241
              st
wenzelm@55432
   242
wenzelm@52536
   243
            case _ =>
wenzelm@56782
   244
              Output.warning("Ignored message without serial number: " + message)
wenzelm@52536
   245
              this
wenzelm@38872
   246
          }
wenzelm@56295
   247
    }
wenzelm@38361
   248
  }
wenzelm@38367
   249
wenzelm@38367
   250
wenzelm@55431
   251
wenzelm@55431
   252
  /** static content **/
wenzelm@55431
   253
wenzelm@45644
   254
  /* make commands */
wenzelm@45644
   255
wenzelm@55648
   256
  def apply(
wenzelm@55648
   257
    id: Document_ID.Command,
wenzelm@55648
   258
    node_name: Document.Node.Name,
wenzelm@59704
   259
    blobs_info: Blobs_Info,
wenzelm@57905
   260
    span: Command_Span.Span): Command =
wenzelm@55648
   261
  {
wenzelm@57901
   262
    val (source, span1) = span.compact_source
wenzelm@59704
   263
    new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
wenzelm@55648
   264
  }
wenzelm@49414
   265
wenzelm@57901
   266
  val empty: Command =
wenzelm@59704
   267
    Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
wenzelm@55648
   268
wenzelm@55648
   269
  def unparsed(
wenzelm@55648
   270
    id: Document_ID.Command,
wenzelm@55648
   271
    source: String,
wenzelm@55648
   272
    results: Results,
wenzelm@55648
   273
    markup: Markup_Tree): Command =
wenzelm@55648
   274
  {
wenzelm@57905
   275
    val (source1, span1) = Command_Span.unparsed(source).compact_source
wenzelm@59704
   276
    new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
wenzelm@55648
   277
  }
wenzelm@49414
   278
wenzelm@50501
   279
  def unparsed(source: String): Command =
wenzelm@52530
   280
    unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   281
wenzelm@52530
   282
  def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
wenzelm@49414
   283
  {
wenzelm@49466
   284
    val text = XML.content(body)
wenzelm@49466
   285
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   286
    unparsed(id, text, results, markup)
wenzelm@49414
   287
  }
wenzelm@49359
   288
wenzelm@44384
   289
wenzelm@44384
   290
  /* perspective */
wenzelm@44384
   291
wenzelm@44474
   292
  object Perspective
wenzelm@44474
   293
  {
wenzelm@44474
   294
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   295
  }
wenzelm@44385
   296
wenzelm@44474
   297
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   298
  {
wenzelm@57615
   299
    def is_empty: Boolean = commands.isEmpty
wenzelm@57615
   300
wenzelm@44474
   301
    def same(that: Perspective): Boolean =
wenzelm@44474
   302
    {
wenzelm@44474
   303
      val cmds1 = this.commands
wenzelm@44474
   304
      val cmds2 = that.commands
wenzelm@48754
   305
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   306
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   307
      cmds1.length == cmds2.length &&
wenzelm@44474
   308
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   309
    }
wenzelm@44385
   310
  }
wenzelm@59689
   311
wenzelm@59689
   312
wenzelm@59704
   313
  /* blobs: inlined errors and auxiliary files */
wenzelm@59689
   314
wenzelm@59689
   315
  private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
wenzelm@59689
   316
  {
wenzelm@59689
   317
    def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
wenzelm@59689
   318
      toks match {
wenzelm@59689
   319
        case (t1, i1) :: (t2, i2) :: rest =>
wenzelm@59689
   320
          if (t1.is_keyword && (t1.source == "%" || t1.source == "--")) clean(rest)
wenzelm@59689
   321
          else (t1, i1) :: clean((t2, i2) :: rest)
wenzelm@59689
   322
        case _ => toks
wenzelm@59689
   323
      }
wenzelm@59689
   324
    clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
wenzelm@59689
   325
  }
wenzelm@59689
   326
wenzelm@59689
   327
  private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
wenzelm@59689
   328
    tokens match {
wenzelm@59689
   329
      case (tok, _) :: toks =>
wenzelm@59689
   330
        if (tok.is_command)
wenzelm@59689
   331
          toks.collectFirst({ case (t, i) if t.is_name => (t.content, i) })
wenzelm@59689
   332
        else None
wenzelm@59689
   333
      case Nil => None
wenzelm@59689
   334
    }
wenzelm@59689
   335
wenzelm@59689
   336
  def span_files(syntax: Prover.Syntax, span: Command_Span.Span): (List[String], Int) =
wenzelm@59689
   337
    span.kind match {
wenzelm@59689
   338
      case Command_Span.Command_Span(name, _) =>
wenzelm@59689
   339
        syntax.load_command(name) match {
wenzelm@59689
   340
          case Some(exts) =>
wenzelm@59689
   341
            find_file(clean_tokens(span.content)) match {
wenzelm@59689
   342
              case Some((file, i)) =>
wenzelm@59689
   343
                if (exts.isEmpty) (List(file), i)
wenzelm@59689
   344
                else (exts.map(ext => file + "." + ext), i)
wenzelm@59689
   345
              case None => (Nil, -1)
wenzelm@59689
   346
            }
wenzelm@59689
   347
          case None => (Nil, -1)
wenzelm@59689
   348
        }
wenzelm@59689
   349
      case _ => (Nil, -1)
wenzelm@59689
   350
    }
wenzelm@59689
   351
wenzelm@59704
   352
  def blobs_info(
wenzelm@59701
   353
    resources: Resources,
wenzelm@59701
   354
    syntax: Prover.Syntax,
wenzelm@59701
   355
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59704
   356
    can_import: Document.Node.Name => Boolean,
wenzelm@59701
   357
    node_name: Document.Node.Name,
wenzelm@59704
   358
    span: Command_Span.Span): Blobs_Info =
wenzelm@59689
   359
  {
wenzelm@59704
   360
    span.kind match {
wenzelm@59704
   361
      // inlined errors
wenzelm@59704
   362
      case Command_Span.Command_Span(name, _) if syntax.is_theory_begin(name) =>
wenzelm@59707
   363
        val header =
wenzelm@59707
   364
          resources.check_thy_reader("", node_name,
wenzelm@59707
   365
            new CharSequenceReader(span.source), Token.Pos.offset)
wenzelm@59704
   366
        val import_errors =
wenzelm@59704
   367
          for ((imp, pos) <- header.imports if !can_import(imp))
wenzelm@59704
   368
            yield "Bad theory import " + quote(imp.node) + Position.here(pos)
wenzelm@59704
   369
        ((header.errors ::: import_errors).map(msg => Exn.Exn(ERROR(msg)): Command.Blob), -1)
wenzelm@59704
   370
wenzelm@59704
   371
      // auxiliary files
wenzelm@59704
   372
      case _ =>
wenzelm@59704
   373
        val (files, index) = span_files(syntax, span)
wenzelm@59704
   374
        val blobs =
wenzelm@59704
   375
          files.map(file =>
wenzelm@59704
   376
            (Exn.capture {
wenzelm@59704
   377
              val name =
wenzelm@59704
   378
                Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file)))
wenzelm@59704
   379
              val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
wenzelm@59704
   380
              (name, blob)
wenzelm@59704
   381
            }).user_error)
wenzelm@59704
   382
        (blobs, index)
wenzelm@59704
   383
    }
wenzelm@59689
   384
  }
wenzelm@34318
   385
}
wenzelm@34318
   386
wenzelm@38361
   387
wenzelm@46712
   388
final class Command private(
wenzelm@52530
   389
    val id: Document_ID.Command,
wenzelm@44615
   390
    val node_name: Document.Node.Name,
wenzelm@59704
   391
    val blobs_info: Command.Blobs_Info,
wenzelm@57905
   392
    val span: Command_Span.Span,
wenzelm@49414
   393
    val source: String,
wenzelm@50501
   394
    val init_results: Command.Results,
wenzelm@49414
   395
    val init_markup: Markup_Tree)
wenzelm@34451
   396
{
wenzelm@57910
   397
  /* name */
wenzelm@34859
   398
wenzelm@57901
   399
  def name: String =
wenzelm@57910
   400
    span.kind match { case Command_Span.Command_Span(name, _) => name case _ => "" }
wenzelm@57910
   401
wenzelm@57910
   402
  def position: Position.T =
wenzelm@57910
   403
    span.kind match { case Command_Span.Command_Span(_, pos) => pos case _ => Position.none }
wenzelm@47012
   404
wenzelm@57912
   405
  override def toString: String = id + "/" + span.kind.toString
wenzelm@34495
   406
wenzelm@57910
   407
wenzelm@57910
   408
  /* classification */
wenzelm@57910
   409
wenzelm@57905
   410
  def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
wenzelm@57905
   411
  def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
wenzelm@57904
   412
wenzelm@57904
   413
  def is_undefined: Boolean = id == Document_ID.none
wenzelm@57904
   414
  val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
wenzelm@57904
   415
  val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
wenzelm@57904
   416
wenzelm@34859
   417
wenzelm@54519
   418
  /* blobs */
wenzelm@54519
   419
wenzelm@59704
   420
  def blobs: List[Command.Blob] = blobs_info._1
wenzelm@59704
   421
  def blobs_index: Int = blobs_info._2
wenzelm@59704
   422
wenzelm@54530
   423
  def blobs_names: List[Document.Node.Name] =
wenzelm@54530
   424
    for (Exn.Res((name, _)) <- blobs) yield name
wenzelm@54530
   425
wenzelm@57842
   426
  def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
wenzelm@57842
   427
    for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
wenzelm@57842
   428
wenzelm@57842
   429
  def blobs_changed(doc_blobs: Document.Blobs): Boolean =
wenzelm@57842
   430
    blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
wenzelm@54519
   431
wenzelm@55432
   432
wenzelm@56462
   433
  /* source chunks */
wenzelm@55431
   434
wenzelm@56746
   435
  val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
wenzelm@56473
   436
wenzelm@56746
   437
  val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
wenzelm@56746
   438
    ((Symbol.Text_Chunk.Default -> chunk) ::
wenzelm@56473
   439
      (for (Exn.Res((name, Some((_, file)))) <- blobs)
wenzelm@56746
   440
        yield (Symbol.Text_Chunk.File(name.node) -> file))).toMap
wenzelm@56473
   441
wenzelm@46813
   442
  def length: Int = source.length
wenzelm@56473
   443
  def range: Text.Range = chunk.range
wenzelm@46813
   444
wenzelm@46813
   445
  val proper_range: Text.Range =
wenzelm@57901
   446
    Text.Range(0,
wenzelm@57901
   447
      (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   448
wenzelm@38426
   449
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   450
wenzelm@38370
   451
wenzelm@38370
   452
  /* accumulated results */
wenzelm@38370
   453
wenzelm@50501
   454
  val init_state: Command.State =
wenzelm@55649
   455
    Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
wenzelm@52527
   456
wenzelm@52527
   457
  val empty_state: Command.State = Command.State(this)
immler@34676
   458
}