src/Pure/Thy/thy_syntax.scala
author wenzelm
Wed Apr 02 20:22:12 2014 +0200 (2014-04-02)
changeset 56372 fadb0fef09d7
parent 56336 60434514ec0a
child 56373 0605d90be6fc
permissions -rw-r--r--
more explicit iterator terminology, in accordance to Scala 2.8 library;
clarified Graph.keys_iterator vs. Graph.keys, with subtle change of semantics;
tuned output;
wenzelm@34268
     1
/*  Title:      Pure/Thy/thy_syntax.scala
wenzelm@34268
     2
    Author:     Makarius
wenzelm@34268
     3
wenzelm@38374
     4
Superficial theory syntax: tokens and spans.
wenzelm@34268
     5
*/
wenzelm@34268
     6
wenzelm@34268
     7
package isabelle
wenzelm@34268
     8
wenzelm@34268
     9
wenzelm@38239
    10
import scala.collection.mutable
wenzelm@38374
    11
import scala.annotation.tailrec
wenzelm@38239
    12
wenzelm@38239
    13
wenzelm@34303
    14
object Thy_Syntax
wenzelm@34268
    15
{
wenzelm@40454
    16
  /** nested structure **/
wenzelm@40454
    17
wenzelm@40454
    18
  object Structure
wenzelm@40454
    19
  {
wenzelm@40478
    20
    sealed abstract class Entry { def length: Int }
wenzelm@40454
    21
    case class Block(val name: String, val body: List[Entry]) extends Entry
wenzelm@40454
    22
    {
wenzelm@40454
    23
      val length: Int = (0 /: body)(_ + _.length)
wenzelm@40454
    24
    }
wenzelm@40454
    25
    case class Atom(val command: Command) extends Entry
wenzelm@40454
    26
    {
wenzelm@40454
    27
      def length: Int = command.length
wenzelm@40454
    28
    }
wenzelm@40454
    29
wenzelm@46811
    30
    def parse(syntax: Outer_Syntax, node_name: Document.Node.Name, text: CharSequence): Entry =
wenzelm@40454
    31
    {
wenzelm@40454
    32
      /* stack operations */
wenzelm@40454
    33
wenzelm@40454
    34
      def buffer(): mutable.ListBuffer[Entry] = new mutable.ListBuffer[Entry]
wenzelm@44615
    35
      var stack: List[(Int, String, mutable.ListBuffer[Entry])] =
wenzelm@54515
    36
        List((0, node_name.toString, buffer()))
wenzelm@40454
    37
wenzelm@40454
    38
      @tailrec def close(level: Int => Boolean)
wenzelm@40454
    39
      {
wenzelm@40454
    40
        stack match {
wenzelm@40454
    41
          case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@40454
    42
            body2 += Block(name, body.toList)
wenzelm@40454
    43
            stack = stack.tail
wenzelm@40454
    44
            close(level)
wenzelm@40454
    45
          case _ =>
wenzelm@40454
    46
        }
wenzelm@40454
    47
      }
wenzelm@40454
    48
wenzelm@40454
    49
      def result(): Entry =
wenzelm@40454
    50
      {
wenzelm@40454
    51
        close(_ => true)
wenzelm@40454
    52
        val (_, name, body) = stack.head
wenzelm@40454
    53
        Block(name, body.toList)
wenzelm@40454
    54
      }
wenzelm@40454
    55
wenzelm@40454
    56
      def add(command: Command)
wenzelm@40454
    57
      {
wenzelm@40454
    58
        syntax.heading_level(command) match {
wenzelm@40454
    59
          case Some(i) =>
wenzelm@46969
    60
            close(_ > i)
wenzelm@46969
    61
            stack = (i + 1, command.source, buffer()) :: stack
wenzelm@40454
    62
          case None =>
wenzelm@40454
    63
        }
wenzelm@40454
    64
        stack.head._3 += Atom(command)
wenzelm@40454
    65
      }
wenzelm@40454
    66
wenzelm@40454
    67
wenzelm@40454
    68
      /* result structure */
wenzelm@40454
    69
wenzelm@40454
    70
      val spans = parse_spans(syntax.scan(text))
wenzelm@54519
    71
      spans.foreach(span => add(Command(Document_ID.none, node_name, Nil, span)))
wenzelm@40454
    72
      result()
wenzelm@40454
    73
    }
wenzelm@40454
    74
  }
wenzelm@40454
    75
wenzelm@40454
    76
wenzelm@40454
    77
wenzelm@38374
    78
  /** parse spans **/
wenzelm@38374
    79
wenzelm@52535
    80
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    81
  {
wenzelm@52535
    82
    val result = new mutable.ListBuffer[List[Token]]
wenzelm@38239
    83
    val span = new mutable.ListBuffer[Token]
wenzelm@53843
    84
    val improper = new mutable.ListBuffer[Token]
wenzelm@34268
    85
wenzelm@53843
    86
    def flush()
wenzelm@53843
    87
    {
wenzelm@53843
    88
      if (!span.isEmpty) { result += span.toList; span.clear }
wenzelm@53843
    89
      if (!improper.isEmpty) { result += improper.toList; improper.clear }
wenzelm@53843
    90
    }
wenzelm@53843
    91
    for (tok <- toks) {
wenzelm@53843
    92
      if (tok.is_command) { flush(); span += tok }
wenzelm@53843
    93
      else if (tok.is_improper) improper += tok
wenzelm@53843
    94
      else { span ++= improper; improper.clear; span += tok }
wenzelm@53843
    95
    }
wenzelm@46811
    96
    flush()
wenzelm@53843
    97
wenzelm@38239
    98
    result.toList
wenzelm@34268
    99
  }
wenzelm@38374
   100
wenzelm@38374
   101
wenzelm@38374
   102
wenzelm@44436
   103
  /** perspective **/
wenzelm@44388
   104
wenzelm@52861
   105
  def command_perspective(
wenzelm@52861
   106
      node: Document.Node,
wenzelm@52861
   107
      perspective: Text.Perspective,
wenzelm@52887
   108
      overlays: Document.Node.Overlays): (Command.Perspective, Command.Perspective) =
wenzelm@44388
   109
  {
wenzelm@52861
   110
    if (perspective.is_empty && overlays.is_empty)
wenzelm@52861
   111
      (Command.Perspective.empty, Command.Perspective.empty)
wenzelm@44388
   112
    else {
wenzelm@52861
   113
      val has_overlay = overlays.commands
wenzelm@52861
   114
      val visible = new mutable.ListBuffer[Command]
wenzelm@52861
   115
      val visible_overlay = new mutable.ListBuffer[Command]
wenzelm@44388
   116
      @tailrec
wenzelm@44388
   117
      def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
wenzelm@44388
   118
      {
wenzelm@44388
   119
        (ranges, commands) match {
wenzelm@44388
   120
          case (range :: more_ranges, (command, offset) #:: more_commands) =>
wenzelm@44388
   121
            val command_range = command.range + offset
wenzelm@44388
   122
            range compare command_range match {
wenzelm@44388
   123
              case 0 =>
wenzelm@52861
   124
                visible += command
wenzelm@52861
   125
                visible_overlay += command
wenzelm@44388
   126
                check_ranges(ranges, more_commands)
wenzelm@52861
   127
              case c =>
wenzelm@52861
   128
                if (has_overlay(command)) visible_overlay += command
wenzelm@52861
   129
wenzelm@52861
   130
                if (c < 0) check_ranges(more_ranges, commands)
wenzelm@52861
   131
                else check_ranges(ranges, more_commands)
wenzelm@44388
   132
            }
wenzelm@52861
   133
wenzelm@52861
   134
          case (Nil, (command, _) #:: more_commands) =>
wenzelm@52861
   135
            if (has_overlay(command)) visible_overlay += command
wenzelm@52861
   136
wenzelm@52861
   137
            check_ranges(Nil, more_commands)
wenzelm@52861
   138
wenzelm@44388
   139
          case _ =>
wenzelm@44388
   140
        }
wenzelm@44388
   141
      }
wenzelm@52861
   142
wenzelm@52861
   143
      val commands =
wenzelm@52861
   144
        if (overlays.is_empty) node.command_range(perspective.range)
wenzelm@52861
   145
        else node.command_range()
wenzelm@52861
   146
      check_ranges(perspective.ranges, commands.toStream)
wenzelm@52861
   147
      (Command.Perspective(visible.toList), Command.Perspective(visible_overlay.toList))
wenzelm@44388
   148
    }
wenzelm@44388
   149
  }
wenzelm@44388
   150
wenzelm@44388
   151
wenzelm@44388
   152
wenzelm@46946
   153
  /** header edits: structure and outer syntax **/
wenzelm@46946
   154
wenzelm@46946
   155
  private def header_edits(
wenzelm@46946
   156
    base_syntax: Outer_Syntax,
wenzelm@46946
   157
    previous: Document.Version,
wenzelm@55134
   158
    edits: List[Document.Edit_Text]):
wenzelm@56316
   159
    (Outer_Syntax, Boolean, Boolean, List[Document.Node.Name], Document.Nodes,
wenzelm@56316
   160
      List[Document.Edit_Command]) =
wenzelm@46946
   161
  {
wenzelm@47987
   162
    var updated_imports = false
wenzelm@47987
   163
    var updated_keywords = false
wenzelm@46946
   164
    var nodes = previous.nodes
wenzelm@46946
   165
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@46946
   166
wenzelm@46946
   167
    edits foreach {
wenzelm@48707
   168
      case (name, Document.Node.Deps(header)) =>
wenzelm@46946
   169
        val node = nodes(name)
wenzelm@46946
   170
        val update_header =
wenzelm@48707
   171
          !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
wenzelm@46946
   172
        if (update_header) {
wenzelm@46946
   173
          val node1 = node.update_header(header)
wenzelm@48707
   174
          updated_imports = updated_imports || (node.header.imports != node1.header.imports)
wenzelm@48707
   175
          updated_keywords = updated_keywords || (node.header.keywords != node1.header.keywords)
wenzelm@46946
   176
          nodes += (name -> node1)
wenzelm@48707
   177
          doc_edits += (name -> Document.Node.Deps(header))
wenzelm@46946
   178
        }
wenzelm@46946
   179
      case _ =>
wenzelm@46946
   180
    }
wenzelm@46946
   181
wenzelm@56316
   182
    val (syntax, syntax_changed) =
wenzelm@55134
   183
      if (previous.is_init || updated_keywords) {
wenzelm@55134
   184
        val syntax =
wenzelm@56372
   185
          (base_syntax /: nodes.iterator) {
wenzelm@55134
   186
            case (syn, (_, node)) => syn.add_keywords(node.header.keywords)
wenzelm@55134
   187
          }
wenzelm@55134
   188
        (syntax, true)
wenzelm@55134
   189
      }
wenzelm@55134
   190
      else (previous.syntax, false)
wenzelm@46946
   191
wenzelm@46946
   192
    val reparse =
wenzelm@47987
   193
      if (updated_imports || updated_keywords)
wenzelm@47987
   194
        nodes.descendants(doc_edits.iterator.map(_._1).toList)
wenzelm@46946
   195
      else Nil
wenzelm@46946
   196
wenzelm@56316
   197
    (syntax, syntax_changed, updated_imports, reparse, nodes, doc_edits.toList)
wenzelm@46946
   198
  }
wenzelm@46946
   199
wenzelm@46946
   200
wenzelm@46946
   201
wenzelm@38374
   202
  /** text edits **/
wenzelm@38374
   203
wenzelm@48755
   204
  /* edit individual command source */
wenzelm@46946
   205
wenzelm@50761
   206
  @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   207
  {
wenzelm@46946
   208
    eds match {
wenzelm@46946
   209
      case e :: es =>
wenzelm@52901
   210
        Document.Node.Commands.starts(commands.iterator).find {
wenzelm@46946
   211
          case (cmd, cmd_start) =>
wenzelm@46946
   212
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   213
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   214
        } match {
wenzelm@46946
   215
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   216
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   217
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   218
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   219
wenzelm@46946
   220
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   221
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   222
wenzelm@46946
   223
          case None =>
wenzelm@46946
   224
            require(e.is_insert && e.start == 0)
wenzelm@46946
   225
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   226
        }
wenzelm@46946
   227
      case Nil => commands
wenzelm@46946
   228
    }
wenzelm@46946
   229
  }
wenzelm@46946
   230
wenzelm@46946
   231
wenzelm@54513
   232
  /* inlined files */
wenzelm@54513
   233
wenzelm@54513
   234
  private def find_file(tokens: List[Token]): Option[String] =
wenzelm@54513
   235
  {
wenzelm@54513
   236
    def clean(toks: List[Token]): List[Token] =
wenzelm@54513
   237
      toks match {
wenzelm@54513
   238
        case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
wenzelm@54513
   239
        case t :: ts => t :: clean(ts)
wenzelm@54513
   240
        case Nil => Nil
wenzelm@54513
   241
      }
wenzelm@55118
   242
    clean(tokens.filter(_.is_proper)) match {
wenzelm@55118
   243
      case tok :: toks if tok.is_command => toks.find(_.is_name).map(_.content)
wenzelm@55118
   244
      case _ => None
wenzelm@55118
   245
    }
wenzelm@54513
   246
  }
wenzelm@54513
   247
wenzelm@54513
   248
  def span_files(syntax: Outer_Syntax, span: List[Token]): List[String] =
wenzelm@56314
   249
    syntax.load(span) match {
wenzelm@54513
   250
      case Some(exts) =>
wenzelm@54513
   251
        find_file(span) match {
wenzelm@54513
   252
          case Some(file) =>
wenzelm@54513
   253
            if (exts.isEmpty) List(file)
wenzelm@54513
   254
            else exts.map(ext => file + "." + ext)
wenzelm@54513
   255
          case None => Nil
wenzelm@54513
   256
        }
wenzelm@54513
   257
      case None => Nil
wenzelm@54513
   258
    }
wenzelm@54513
   259
wenzelm@54513
   260
  def resolve_files(
wenzelm@56208
   261
      resources: Resources,
wenzelm@54513
   262
      syntax: Outer_Syntax,
wenzelm@54517
   263
      node_name: Document.Node.Name,
wenzelm@54517
   264
      span: List[Token],
wenzelm@56336
   265
      get_blob: Document.Node.Name => Option[Document.Blob])
wenzelm@54519
   266
    : List[Command.Blob] =
wenzelm@54513
   267
  {
wenzelm@55431
   268
    span_files(syntax, span).map(file_name =>
wenzelm@54517
   269
      Exn.capture {
wenzelm@54517
   270
        val name =
wenzelm@56208
   271
          Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file_name)))
wenzelm@56336
   272
        val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.file)))
wenzelm@55431
   273
        (name, blob)
wenzelm@55783
   274
      })
wenzelm@54513
   275
  }
wenzelm@54513
   276
wenzelm@54513
   277
wenzelm@48755
   278
  /* reparse range of command spans */
wenzelm@46946
   279
wenzelm@48748
   280
  @tailrec private def chop_common(
wenzelm@55779
   281
      cmds: List[Command],
wenzelm@55779
   282
      blobs_spans: List[(List[Command.Blob], List[Token])])
wenzelm@55779
   283
    : (List[Command], List[(List[Command.Blob], List[Token])]) =
wenzelm@55779
   284
  {
wenzelm@55779
   285
    (cmds, blobs_spans) match {
wenzelm@55779
   286
      case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
wenzelm@55779
   287
        chop_common(cmds, rest)
wenzelm@55779
   288
      case _ => (cmds, blobs_spans)
wenzelm@48748
   289
    }
wenzelm@55779
   290
  }
wenzelm@48748
   291
wenzelm@48754
   292
  private def reparse_spans(
wenzelm@56208
   293
    resources: Resources,
wenzelm@48754
   294
    syntax: Outer_Syntax,
wenzelm@56336
   295
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   296
    name: Document.Node.Name,
wenzelm@48754
   297
    commands: Linear_Set[Command],
wenzelm@48754
   298
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   299
  {
wenzelm@48754
   300
    val cmds0 = commands.iterator(first, last).toList
wenzelm@55779
   301
    val blobs_spans0 =
wenzelm@54513
   302
      parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
wenzelm@56336
   303
        map(span => (resolve_files(resources, syntax, name, span, get_blob), span))
wenzelm@48754
   304
wenzelm@55779
   305
    val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
wenzelm@48754
   306
wenzelm@55779
   307
    val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
wenzelm@48754
   308
    val cmds2 = rev_cmds2.reverse
wenzelm@55779
   309
    val blobs_spans2 = rev_blobs_spans2.reverse
wenzelm@48754
   310
wenzelm@48754
   311
    cmds2 match {
wenzelm@48754
   312
      case Nil =>
wenzelm@55779
   313
        assert(blobs_spans2.isEmpty)
wenzelm@48754
   314
        commands
wenzelm@48754
   315
      case cmd :: _ =>
wenzelm@48754
   316
        val hook = commands.prev(cmd)
wenzelm@54462
   317
        val inserted =
wenzelm@55779
   318
          blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
wenzelm@48754
   319
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   320
    }
wenzelm@48748
   321
  }
wenzelm@48748
   322
wenzelm@48754
   323
wenzelm@48755
   324
  /* recover command spans after edits */
wenzelm@48754
   325
wenzelm@48755
   326
  // FIXME somewhat slow
wenzelm@48746
   327
  private def recover_spans(
wenzelm@56208
   328
    resources: Resources,
wenzelm@46946
   329
    syntax: Outer_Syntax,
wenzelm@56336
   330
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   331
    name: Document.Node.Name,
wenzelm@48748
   332
    perspective: Command.Perspective,
wenzelm@48754
   333
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   334
  {
wenzelm@48754
   335
    val visible = perspective.commands.toSet
wenzelm@48748
   336
wenzelm@48754
   337
    def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@48754
   338
      cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
wenzelm@48754
   339
        .find(_.is_command) getOrElse cmds.last
wenzelm@48746
   340
wenzelm@48754
   341
    @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@48754
   342
      cmds.find(_.is_unparsed) match {
wenzelm@48754
   343
        case Some(first_unparsed) =>
wenzelm@48754
   344
          val first = next_invisible_command(cmds.reverse, first_unparsed)
wenzelm@48754
   345
          val last = next_invisible_command(cmds, first_unparsed)
wenzelm@56336
   346
          recover(reparse_spans(resources, syntax, get_blob, name, cmds, first, last))
wenzelm@48754
   347
        case None => cmds
wenzelm@48746
   348
      }
wenzelm@48754
   349
    recover(commands)
wenzelm@46946
   350
  }
wenzelm@46946
   351
wenzelm@46946
   352
wenzelm@48755
   353
  /* consolidate unfinished spans */
wenzelm@46946
   354
wenzelm@48754
   355
  private def consolidate_spans(
wenzelm@56208
   356
    resources: Resources,
wenzelm@46946
   357
    syntax: Outer_Syntax,
wenzelm@56336
   358
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@49524
   359
    reparse_limit: Int,
wenzelm@48754
   360
    name: Document.Node.Name,
wenzelm@48754
   361
    perspective: Command.Perspective,
wenzelm@46946
   362
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   363
  {
wenzelm@48754
   364
    if (perspective.commands.isEmpty) commands
wenzelm@48754
   365
    else {
wenzelm@48754
   366
      commands.find(_.is_unfinished) match {
wenzelm@48754
   367
        case Some(first_unfinished) =>
wenzelm@48754
   368
          val visible = perspective.commands.toSet
wenzelm@48754
   369
          commands.reverse.find(visible) match {
wenzelm@48754
   370
            case Some(last_visible) =>
wenzelm@49524
   371
              val it = commands.iterator(last_visible)
wenzelm@49524
   372
              var last = last_visible
wenzelm@49524
   373
              var i = 0
wenzelm@49524
   374
              while (i < reparse_limit && it.hasNext) {
wenzelm@49524
   375
                last = it.next
wenzelm@49524
   376
                i += last.length
wenzelm@49524
   377
              }
wenzelm@56336
   378
              reparse_spans(resources, syntax, get_blob, name, commands, first_unfinished, last)
wenzelm@48754
   379
            case None => commands
wenzelm@48754
   380
          }
wenzelm@48754
   381
        case None => commands
wenzelm@48754
   382
      }
wenzelm@48754
   383
    }
wenzelm@46946
   384
  }
wenzelm@46946
   385
wenzelm@46946
   386
wenzelm@48755
   387
  /* main */
wenzelm@46946
   388
wenzelm@50761
   389
  def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@52849
   390
    : List[Command.Edit] =
wenzelm@48754
   391
  {
wenzelm@48754
   392
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   393
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   394
wenzelm@48754
   395
    removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
wenzelm@48754
   396
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   397
  }
wenzelm@48754
   398
wenzelm@54513
   399
  private def text_edit(
wenzelm@56208
   400
    resources: Resources,
wenzelm@54513
   401
    syntax: Outer_Syntax,
wenzelm@56336
   402
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@54513
   403
    reparse_limit: Int,
wenzelm@48755
   404
    node: Document.Node, edit: Document.Edit_Text): Document.Node =
wenzelm@48755
   405
  {
wenzelm@48755
   406
    edit match {
wenzelm@48755
   407
      case (_, Document.Node.Clear()) => node.clear
wenzelm@48755
   408
wenzelm@56335
   409
      case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
wenzelm@54562
   410
wenzelm@48755
   411
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@56335
   412
        if (name.is_theory) {
wenzelm@55435
   413
          val commands0 = node.commands
wenzelm@55435
   414
          val commands1 = edit_text(text_edits, commands0)
wenzelm@55435
   415
          val commands2 =
wenzelm@56336
   416
            recover_spans(resources, syntax, get_blob, name, node.perspective.visible, commands1)
wenzelm@55435
   417
          node.update_commands(commands2)
wenzelm@55435
   418
        }
wenzelm@56335
   419
        else node
wenzelm@48755
   420
wenzelm@48755
   421
      case (_, Document.Node.Deps(_)) => node
wenzelm@48755
   422
wenzelm@52849
   423
      case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
wenzelm@52861
   424
        val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
wenzelm@52849
   425
        val perspective: Document.Node.Perspective_Command =
wenzelm@52861
   426
          Document.Node.Perspective(required, visible_overlay, overlays)
wenzelm@52808
   427
        if (node.same_perspective(perspective)) node
wenzelm@48755
   428
        else
wenzelm@49524
   429
          node.update_perspective(perspective).update_commands(
wenzelm@56336
   430
            consolidate_spans(resources, syntax, get_blob, reparse_limit,
wenzelm@54517
   431
              name, visible, node.commands))
wenzelm@48755
   432
    }
wenzelm@48755
   433
  }
wenzelm@48755
   434
wenzelm@56316
   435
  def parse_change(
wenzelm@56208
   436
      resources: Resources,
wenzelm@49524
   437
      reparse_limit: Int,
wenzelm@43722
   438
      previous: Document.Version,
wenzelm@54521
   439
      doc_blobs: Document.Blobs,
wenzelm@56315
   440
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@38374
   441
  {
wenzelm@56336
   442
    def get_blob(name: Document.Node.Name) =
wenzelm@56336
   443
      doc_blobs.get(name) orElse previous.nodes(name).get_blob
wenzelm@56336
   444
wenzelm@56316
   445
    val (syntax, syntax_changed, deps_changed, reparse0, nodes0, doc_edits0) =
wenzelm@56208
   446
      header_edits(resources.base_syntax, previous, edits)
wenzelm@54518
   447
wenzelm@56316
   448
    val (doc_edits, version) =
wenzelm@56316
   449
      if (edits.isEmpty) (Nil, Document.Version.make(syntax, previous.nodes))
wenzelm@56316
   450
      else {
wenzelm@56316
   451
        val reparse =
wenzelm@56372
   452
          (reparse0 /: nodes0.iterator)({
wenzelm@56316
   453
            case (reparse, (name, node)) =>
wenzelm@56316
   454
              if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
wenzelm@56316
   455
                name :: reparse
wenzelm@56316
   456
              else reparse
wenzelm@56316
   457
            })
wenzelm@56316
   458
        val reparse_set = reparse.toSet
wenzelm@46946
   459
wenzelm@56316
   460
        var nodes = nodes0
wenzelm@56316
   461
        val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@54521
   462
wenzelm@56316
   463
        val node_edits =
wenzelm@56316
   464
          (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
wenzelm@56316
   465
            .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
wenzelm@38374
   466
wenzelm@56316
   467
        node_edits foreach {
wenzelm@56316
   468
          case (name, edits) =>
wenzelm@56316
   469
            val node = nodes(name)
wenzelm@56316
   470
            val commands = node.commands
wenzelm@48754
   471
wenzelm@56316
   472
            val node1 =
wenzelm@56316
   473
              if (reparse_set(name) && !commands.isEmpty)
wenzelm@56316
   474
                node.update_commands(
wenzelm@56336
   475
                  reparse_spans(resources, syntax, get_blob,
wenzelm@56316
   476
                    name, commands, commands.head, commands.last))
wenzelm@56316
   477
              else node
wenzelm@56316
   478
            val node2 =
wenzelm@56336
   479
              (node1 /: edits)(text_edit(resources, syntax, get_blob, reparse_limit, _, _))
wenzelm@54521
   480
wenzelm@56316
   481
            if (!(node.same_perspective(node2.perspective)))
wenzelm@56316
   482
              doc_edits += (name -> node2.perspective)
wenzelm@54521
   483
wenzelm@56316
   484
            doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
wenzelm@54521
   485
wenzelm@56316
   486
            nodes += (name -> node2)
wenzelm@56316
   487
        }
wenzelm@56316
   488
        (doc_edits.toList, Document.Version.make(syntax, nodes))
wenzelm@54513
   489
      }
wenzelm@54513
   490
wenzelm@56316
   491
    Session.Change(previous, doc_blobs, syntax_changed, deps_changed, doc_edits, version)
wenzelm@38374
   492
  }
wenzelm@34268
   493
}