src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Apr 03 21:08:00 2014 +0200 (2014-04-03)
changeset 56394 bbf4d512f395
parent 56393 22f533e6a049
child 56473 5b5c750e9763
permissions -rw-r--r--
clarified Version.syntax -- avoid guessing initial situation;
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@56373
   144
        (if (overlays.is_empty) node.command_iterator(perspective.range)
wenzelm@56373
   145
         else node.command_iterator()).toStream
wenzelm@56373
   146
      check_ranges(perspective.ranges, commands)
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@56394
   156
    resources: Resources,
wenzelm@46946
   157
    previous: Document.Version,
wenzelm@55134
   158
    edits: List[Document.Edit_Text]):
wenzelm@56393
   159
    (Prover.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@56394
   183
      previous.syntax match {
wenzelm@56394
   184
        case Some(syntax) if !updated_keywords =>
wenzelm@56394
   185
          (syntax, false)
wenzelm@56394
   186
        case _ =>
wenzelm@56394
   187
          val syntax =
wenzelm@56394
   188
            (resources.base_syntax /: nodes.iterator) {
wenzelm@56394
   189
              case (syn, (_, node)) => syn.add_keywords(node.header.keywords)
wenzelm@56394
   190
            }
wenzelm@56394
   191
          (syntax, true)
wenzelm@55134
   192
      }
wenzelm@46946
   193
wenzelm@46946
   194
    val reparse =
wenzelm@47987
   195
      if (updated_imports || updated_keywords)
wenzelm@47987
   196
        nodes.descendants(doc_edits.iterator.map(_._1).toList)
wenzelm@46946
   197
      else Nil
wenzelm@46946
   198
wenzelm@56316
   199
    (syntax, syntax_changed, updated_imports, reparse, nodes, doc_edits.toList)
wenzelm@46946
   200
  }
wenzelm@46946
   201
wenzelm@46946
   202
wenzelm@46946
   203
wenzelm@38374
   204
  /** text edits **/
wenzelm@38374
   205
wenzelm@48755
   206
  /* edit individual command source */
wenzelm@46946
   207
wenzelm@50761
   208
  @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   209
  {
wenzelm@46946
   210
    eds match {
wenzelm@46946
   211
      case e :: es =>
wenzelm@52901
   212
        Document.Node.Commands.starts(commands.iterator).find {
wenzelm@46946
   213
          case (cmd, cmd_start) =>
wenzelm@46946
   214
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   215
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   216
        } match {
wenzelm@46946
   217
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   218
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   219
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   220
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   221
wenzelm@46946
   222
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   223
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   224
wenzelm@46946
   225
          case None =>
wenzelm@46946
   226
            require(e.is_insert && e.start == 0)
wenzelm@46946
   227
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   228
        }
wenzelm@46946
   229
      case Nil => commands
wenzelm@46946
   230
    }
wenzelm@46946
   231
  }
wenzelm@46946
   232
wenzelm@46946
   233
wenzelm@54513
   234
  /* inlined files */
wenzelm@54513
   235
wenzelm@54513
   236
  private def find_file(tokens: List[Token]): Option[String] =
wenzelm@54513
   237
  {
wenzelm@54513
   238
    def clean(toks: List[Token]): List[Token] =
wenzelm@54513
   239
      toks match {
wenzelm@54513
   240
        case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
wenzelm@54513
   241
        case t :: ts => t :: clean(ts)
wenzelm@54513
   242
        case Nil => Nil
wenzelm@54513
   243
      }
wenzelm@55118
   244
    clean(tokens.filter(_.is_proper)) match {
wenzelm@55118
   245
      case tok :: toks if tok.is_command => toks.find(_.is_name).map(_.content)
wenzelm@55118
   246
      case _ => None
wenzelm@55118
   247
    }
wenzelm@54513
   248
  }
wenzelm@54513
   249
wenzelm@56393
   250
  def span_files(syntax: Prover.Syntax, span: List[Token]): List[String] =
wenzelm@56314
   251
    syntax.load(span) match {
wenzelm@54513
   252
      case Some(exts) =>
wenzelm@54513
   253
        find_file(span) match {
wenzelm@54513
   254
          case Some(file) =>
wenzelm@54513
   255
            if (exts.isEmpty) List(file)
wenzelm@54513
   256
            else exts.map(ext => file + "." + ext)
wenzelm@54513
   257
          case None => Nil
wenzelm@54513
   258
        }
wenzelm@54513
   259
      case None => Nil
wenzelm@54513
   260
    }
wenzelm@54513
   261
wenzelm@54513
   262
  def resolve_files(
wenzelm@56208
   263
      resources: Resources,
wenzelm@56393
   264
      syntax: Prover.Syntax,
wenzelm@54517
   265
      node_name: Document.Node.Name,
wenzelm@54517
   266
      span: List[Token],
wenzelm@56336
   267
      get_blob: Document.Node.Name => Option[Document.Blob])
wenzelm@54519
   268
    : List[Command.Blob] =
wenzelm@54513
   269
  {
wenzelm@55431
   270
    span_files(syntax, span).map(file_name =>
wenzelm@54517
   271
      Exn.capture {
wenzelm@54517
   272
        val name =
wenzelm@56208
   273
          Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file_name)))
wenzelm@56336
   274
        val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.file)))
wenzelm@55431
   275
        (name, blob)
wenzelm@55783
   276
      })
wenzelm@54513
   277
  }
wenzelm@54513
   278
wenzelm@54513
   279
wenzelm@48755
   280
  /* reparse range of command spans */
wenzelm@46946
   281
wenzelm@48748
   282
  @tailrec private def chop_common(
wenzelm@55779
   283
      cmds: List[Command],
wenzelm@55779
   284
      blobs_spans: List[(List[Command.Blob], List[Token])])
wenzelm@55779
   285
    : (List[Command], List[(List[Command.Blob], List[Token])]) =
wenzelm@55779
   286
  {
wenzelm@55779
   287
    (cmds, blobs_spans) match {
wenzelm@55779
   288
      case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
wenzelm@55779
   289
        chop_common(cmds, rest)
wenzelm@55779
   290
      case _ => (cmds, blobs_spans)
wenzelm@48748
   291
    }
wenzelm@55779
   292
  }
wenzelm@48748
   293
wenzelm@48754
   294
  private def reparse_spans(
wenzelm@56208
   295
    resources: Resources,
wenzelm@56393
   296
    syntax: Prover.Syntax,
wenzelm@56336
   297
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   298
    name: Document.Node.Name,
wenzelm@48754
   299
    commands: Linear_Set[Command],
wenzelm@48754
   300
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   301
  {
wenzelm@48754
   302
    val cmds0 = commands.iterator(first, last).toList
wenzelm@55779
   303
    val blobs_spans0 =
wenzelm@54513
   304
      parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
wenzelm@56336
   305
        map(span => (resolve_files(resources, syntax, name, span, get_blob), span))
wenzelm@48754
   306
wenzelm@55779
   307
    val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
wenzelm@48754
   308
wenzelm@55779
   309
    val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
wenzelm@48754
   310
    val cmds2 = rev_cmds2.reverse
wenzelm@55779
   311
    val blobs_spans2 = rev_blobs_spans2.reverse
wenzelm@48754
   312
wenzelm@48754
   313
    cmds2 match {
wenzelm@48754
   314
      case Nil =>
wenzelm@55779
   315
        assert(blobs_spans2.isEmpty)
wenzelm@48754
   316
        commands
wenzelm@48754
   317
      case cmd :: _ =>
wenzelm@48754
   318
        val hook = commands.prev(cmd)
wenzelm@54462
   319
        val inserted =
wenzelm@55779
   320
          blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
wenzelm@48754
   321
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   322
    }
wenzelm@48748
   323
  }
wenzelm@48748
   324
wenzelm@48754
   325
wenzelm@48755
   326
  /* recover command spans after edits */
wenzelm@48754
   327
wenzelm@48755
   328
  // FIXME somewhat slow
wenzelm@48746
   329
  private def recover_spans(
wenzelm@56208
   330
    resources: Resources,
wenzelm@56393
   331
    syntax: Prover.Syntax,
wenzelm@56336
   332
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   333
    name: Document.Node.Name,
wenzelm@48748
   334
    perspective: Command.Perspective,
wenzelm@48754
   335
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   336
  {
wenzelm@48754
   337
    val visible = perspective.commands.toSet
wenzelm@48748
   338
wenzelm@48754
   339
    def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@48754
   340
      cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
wenzelm@48754
   341
        .find(_.is_command) getOrElse cmds.last
wenzelm@48746
   342
wenzelm@48754
   343
    @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@48754
   344
      cmds.find(_.is_unparsed) match {
wenzelm@48754
   345
        case Some(first_unparsed) =>
wenzelm@48754
   346
          val first = next_invisible_command(cmds.reverse, first_unparsed)
wenzelm@48754
   347
          val last = next_invisible_command(cmds, first_unparsed)
wenzelm@56336
   348
          recover(reparse_spans(resources, syntax, get_blob, name, cmds, first, last))
wenzelm@48754
   349
        case None => cmds
wenzelm@48746
   350
      }
wenzelm@48754
   351
    recover(commands)
wenzelm@46946
   352
  }
wenzelm@46946
   353
wenzelm@46946
   354
wenzelm@48755
   355
  /* consolidate unfinished spans */
wenzelm@46946
   356
wenzelm@48754
   357
  private def consolidate_spans(
wenzelm@56208
   358
    resources: Resources,
wenzelm@56393
   359
    syntax: Prover.Syntax,
wenzelm@56336
   360
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@49524
   361
    reparse_limit: Int,
wenzelm@48754
   362
    name: Document.Node.Name,
wenzelm@48754
   363
    perspective: Command.Perspective,
wenzelm@46946
   364
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   365
  {
wenzelm@48754
   366
    if (perspective.commands.isEmpty) commands
wenzelm@48754
   367
    else {
wenzelm@48754
   368
      commands.find(_.is_unfinished) match {
wenzelm@48754
   369
        case Some(first_unfinished) =>
wenzelm@48754
   370
          val visible = perspective.commands.toSet
wenzelm@48754
   371
          commands.reverse.find(visible) match {
wenzelm@48754
   372
            case Some(last_visible) =>
wenzelm@49524
   373
              val it = commands.iterator(last_visible)
wenzelm@49524
   374
              var last = last_visible
wenzelm@49524
   375
              var i = 0
wenzelm@49524
   376
              while (i < reparse_limit && it.hasNext) {
wenzelm@49524
   377
                last = it.next
wenzelm@49524
   378
                i += last.length
wenzelm@49524
   379
              }
wenzelm@56336
   380
              reparse_spans(resources, syntax, get_blob, name, commands, first_unfinished, last)
wenzelm@48754
   381
            case None => commands
wenzelm@48754
   382
          }
wenzelm@48754
   383
        case None => commands
wenzelm@48754
   384
      }
wenzelm@48754
   385
    }
wenzelm@46946
   386
  }
wenzelm@46946
   387
wenzelm@46946
   388
wenzelm@48755
   389
  /* main */
wenzelm@46946
   390
wenzelm@50761
   391
  def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@52849
   392
    : List[Command.Edit] =
wenzelm@48754
   393
  {
wenzelm@48754
   394
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   395
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   396
wenzelm@48754
   397
    removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
wenzelm@48754
   398
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   399
  }
wenzelm@48754
   400
wenzelm@54513
   401
  private def text_edit(
wenzelm@56208
   402
    resources: Resources,
wenzelm@56393
   403
    syntax: Prover.Syntax,
wenzelm@56336
   404
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@54513
   405
    reparse_limit: Int,
wenzelm@48755
   406
    node: Document.Node, edit: Document.Edit_Text): Document.Node =
wenzelm@48755
   407
  {
wenzelm@48755
   408
    edit match {
wenzelm@48755
   409
      case (_, Document.Node.Clear()) => node.clear
wenzelm@48755
   410
wenzelm@56335
   411
      case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
wenzelm@54562
   412
wenzelm@48755
   413
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@56335
   414
        if (name.is_theory) {
wenzelm@55435
   415
          val commands0 = node.commands
wenzelm@55435
   416
          val commands1 = edit_text(text_edits, commands0)
wenzelm@55435
   417
          val commands2 =
wenzelm@56336
   418
            recover_spans(resources, syntax, get_blob, name, node.perspective.visible, commands1)
wenzelm@55435
   419
          node.update_commands(commands2)
wenzelm@55435
   420
        }
wenzelm@56335
   421
        else node
wenzelm@48755
   422
wenzelm@48755
   423
      case (_, Document.Node.Deps(_)) => node
wenzelm@48755
   424
wenzelm@52849
   425
      case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
wenzelm@52861
   426
        val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
wenzelm@52849
   427
        val perspective: Document.Node.Perspective_Command =
wenzelm@52861
   428
          Document.Node.Perspective(required, visible_overlay, overlays)
wenzelm@52808
   429
        if (node.same_perspective(perspective)) node
wenzelm@48755
   430
        else
wenzelm@49524
   431
          node.update_perspective(perspective).update_commands(
wenzelm@56336
   432
            consolidate_spans(resources, syntax, get_blob, reparse_limit,
wenzelm@54517
   433
              name, visible, node.commands))
wenzelm@48755
   434
    }
wenzelm@48755
   435
  }
wenzelm@48755
   436
wenzelm@56316
   437
  def parse_change(
wenzelm@56208
   438
      resources: Resources,
wenzelm@49524
   439
      reparse_limit: Int,
wenzelm@43722
   440
      previous: Document.Version,
wenzelm@54521
   441
      doc_blobs: Document.Blobs,
wenzelm@56315
   442
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@38374
   443
  {
wenzelm@56336
   444
    def get_blob(name: Document.Node.Name) =
wenzelm@56336
   445
      doc_blobs.get(name) orElse previous.nodes(name).get_blob
wenzelm@56336
   446
wenzelm@56316
   447
    val (syntax, syntax_changed, deps_changed, reparse0, nodes0, doc_edits0) =
wenzelm@56394
   448
      header_edits(resources, previous, edits)
wenzelm@54518
   449
wenzelm@56316
   450
    val (doc_edits, version) =
wenzelm@56394
   451
      if (edits.isEmpty) (Nil, Document.Version.make(Some(syntax), previous.nodes))
wenzelm@56316
   452
      else {
wenzelm@56316
   453
        val reparse =
wenzelm@56372
   454
          (reparse0 /: nodes0.iterator)({
wenzelm@56316
   455
            case (reparse, (name, node)) =>
wenzelm@56316
   456
              if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
wenzelm@56316
   457
                name :: reparse
wenzelm@56316
   458
              else reparse
wenzelm@56316
   459
            })
wenzelm@56316
   460
        val reparse_set = reparse.toSet
wenzelm@46946
   461
wenzelm@56316
   462
        var nodes = nodes0
wenzelm@56316
   463
        val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@54521
   464
wenzelm@56316
   465
        val node_edits =
wenzelm@56316
   466
          (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
wenzelm@56316
   467
            .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
wenzelm@38374
   468
wenzelm@56316
   469
        node_edits foreach {
wenzelm@56316
   470
          case (name, edits) =>
wenzelm@56316
   471
            val node = nodes(name)
wenzelm@56316
   472
            val commands = node.commands
wenzelm@48754
   473
wenzelm@56316
   474
            val node1 =
wenzelm@56316
   475
              if (reparse_set(name) && !commands.isEmpty)
wenzelm@56316
   476
                node.update_commands(
wenzelm@56336
   477
                  reparse_spans(resources, syntax, get_blob,
wenzelm@56316
   478
                    name, commands, commands.head, commands.last))
wenzelm@56316
   479
              else node
wenzelm@56316
   480
            val node2 =
wenzelm@56336
   481
              (node1 /: edits)(text_edit(resources, syntax, get_blob, reparse_limit, _, _))
wenzelm@54521
   482
wenzelm@56316
   483
            if (!(node.same_perspective(node2.perspective)))
wenzelm@56316
   484
              doc_edits += (name -> node2.perspective)
wenzelm@54521
   485
wenzelm@56316
   486
            doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
wenzelm@54521
   487
wenzelm@56316
   488
            nodes += (name -> node2)
wenzelm@56316
   489
        }
wenzelm@56394
   490
        (doc_edits.toList, Document.Version.make(Some(syntax), nodes))
wenzelm@54513
   491
      }
wenzelm@54513
   492
wenzelm@56316
   493
    Session.Change(previous, doc_blobs, syntax_changed, deps_changed, doc_edits, version)
wenzelm@38374
   494
  }
wenzelm@34268
   495
}