author | wenzelm |
Mon, 04 Jan 2010 19:08:10 +0100 | |
changeset 34835 | 67733fd0e3fa |
parent 34832 | d785f72ef388 |
child 34838 | 08a72dc4868e |
permissions | -rw-r--r-- |
34407 | 1 |
/* |
34485 | 2 |
* Document as list of commands, consisting of lists of tokens |
34407 | 3 |
* |
4 |
* @author Johannes Hölzl, TU Munich |
|
34532 | 5 |
* @author Fabian Immler, TU Munich |
34485 | 6 |
* @author Makarius |
34407 | 7 |
*/ |
8 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
package isabelle.proofdocument |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
10 |
|
34760 | 11 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
12 |
import scala.actors.Actor._ |
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
13 |
import scala.collection.mutable |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
14 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
15 |
import java.util.regex.Pattern |
34703 | 16 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
17 |
|
34823 | 18 |
object Document |
34483 | 19 |
{ |
34582 | 20 |
// Be careful when changing this regex. Not only must it handle the |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
21 |
// spurious end of a token but also: |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
22 |
// Bug ID: 5050507 Pattern.matches throws StackOverflow Error |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
23 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507 |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
24 |
|
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
25 |
val token_pattern = |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
26 |
Pattern.compile( |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
27 |
"\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
28 |
"\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" + |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
29 |
"(\\?'?|')[A-Za-z_0-9.]*|" + |
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
30 |
"[A-Za-z_0-9.]+|" + |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
31 |
"[!#$%&*+-/<=>?@^_|~]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
32 |
"\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
33 |
"`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
34 |
"[()\\[\\]{}:;]", Pattern.MULTILINE) |
34485 | 35 |
|
34823 | 36 |
def empty(id: Isar_Document.Document_ID): Document = |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
37 |
{ |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
38 |
val doc = new Document(id, Linear_Set(), Map(), Linear_Set(), Map()) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
39 |
doc.assign_states(Nil) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
40 |
doc |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
41 |
} |
34660 | 42 |
|
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
43 |
type Structure_Edit = (Option[Command], Option[Command]) |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
44 |
type Structure_Change = List[Structure_Edit] |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
45 |
type Result = (Document, List[Structure_Edit]) |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
46 |
|
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
47 |
def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID, |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
48 |
edits: List[Edit]): Result = |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
49 |
{ |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
50 |
val changes = new mutable.ListBuffer[Structure_Edit] |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
51 |
val new_doc = (old_doc /: edits)((doc1: Document, edit: Edit) => |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
52 |
{ |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
53 |
val (doc2, chgs) = doc1.text_edit(session, edit, new_id) // FIXME odd multiple use of id |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
54 |
changes ++ chgs |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
55 |
doc2 |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
56 |
}) |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
57 |
(new_doc, changes.toList) |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
58 |
} |
34778 | 59 |
} |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
60 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
61 |
|
34823 | 62 |
class Document( |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
63 |
val id: Isar_Document.Document_ID, |
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
64 |
val tokens: Linear_Set[Token], // FIXME plain List, inside Command |
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
65 |
val token_start: Map[Token, Int], // FIXME eliminate |
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
66 |
val commands: Linear_Set[Command], |
34832
d785f72ef388
more explicit treatment of command/document state;
wenzelm
parents:
34826
diff
changeset
|
67 |
old_states: Map[Command, Command]) |
34483 | 68 |
{ |
34582 | 69 |
def content = Token.string_from_tokens(Nil ++ tokens, token_start) |
34657 | 70 |
|
71 |
||
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
72 |
/* command/state assignment */ |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
73 |
|
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
74 |
val assignment = Future.promise[Map[Command, Command]] |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
75 |
def is_assigned = assignment.is_finished |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
76 |
|
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
77 |
@volatile private var tmp_states = old_states |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
78 |
|
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
79 |
def assign_states(new_states: List[(Command, Command)]) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
80 |
{ |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
81 |
assignment.fulfill(tmp_states ++ new_states) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
82 |
tmp_states = Map() |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
83 |
} |
34832
d785f72ef388
more explicit treatment of command/document state;
wenzelm
parents:
34826
diff
changeset
|
84 |
|
d785f72ef388
more explicit treatment of command/document state;
wenzelm
parents:
34826
diff
changeset
|
85 |
def current_state(cmd: Command): State = |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
86 |
{ |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
87 |
require(assignment.is_finished) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
88 |
(assignment.join)(cmd).current_state |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
89 |
} |
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
90 |
|
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
91 |
|
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
92 |
|
34485 | 93 |
/** token view **/ |
94 |
||
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
95 |
def text_edit(session: Session, e: Edit, id: String): Document.Result = |
34660 | 96 |
{ |
97 |
case class TextChange(start: Int, added: String, removed: String) |
|
98 |
val change = e match { |
|
99 |
case Insert(s, a) => TextChange(s, a, "") |
|
100 |
case Remove(s, r) => TextChange(s, "", r) |
|
101 |
} |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
102 |
//indices of tokens |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
103 |
var start: Map[Token, Int] = token_start |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
104 |
def stop(t: Token) = start(t) + t.length |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
105 |
// split old token lists |
34582 | 106 |
val tokens = Nil ++ this.tokens |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
107 |
val (begin, remaining) = tokens.span(stop(_) < change.start) |
34648 | 108 |
val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
109 |
// update indices |
34582 | 110 |
start = end.foldLeft(start)((s, t) => |
34648 | 111 |
s + (t -> (s(t) + change.added.length - change.removed.length))) |
34485 | 112 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
113 |
val split_begin = removed.takeWhile(start(_) < change.start). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
114 |
map (t => { |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
115 |
val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
116 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
117 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
118 |
}) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
119 |
|
34648 | 120 |
val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
121 |
map (t => { |
34582 | 122 |
val split_tok = |
34648 | 123 |
new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
124 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
125 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
126 |
}) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
127 |
// update indices |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
128 |
start = removed.foldLeft (start) ((s, t) => s - t) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
129 |
start = split_end.foldLeft (start) ((s, t) => |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
130 |
s + (t -> (change.start + change.added.length))) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
131 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
132 |
val ins = new Token(change.added, Token.Kind.OTHER) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
133 |
start += (ins -> change.start) |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
134 |
|
34582 | 135 |
var invalid_tokens = split_begin ::: ins :: split_end ::: end |
136 |
var new_tokens: List[Token] = Nil |
|
137 |
var old_suffix: List[Token] = Nil |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
138 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
139 |
val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0) |
34582 | 140 |
val matcher = |
34823 | 141 |
Document.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start)) |
34526 | 142 |
|
143 |
while (matcher.find() && invalid_tokens != Nil) { |
|
34485 | 144 |
val kind = |
34819 | 145 |
if (session.current_syntax.is_command(matcher.group)) |
34485 | 146 |
Token.Kind.COMMAND_START |
34494 | 147 |
else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*") |
34485 | 148 |
Token.Kind.COMMENT |
149 |
else |
|
150 |
Token.Kind.OTHER |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
151 |
val new_token = new Token(matcher.group, kind) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
152 |
start += (new_token -> (match_start + matcher.start)) |
34526 | 153 |
new_tokens ::= new_token |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
154 |
|
34660 | 155 |
invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token)) |
34526 | 156 |
invalid_tokens match { |
34582 | 157 |
case t :: ts => |
158 |
if (start(t) == start(new_token) && |
|
159 |
start(t) > change.start + change.added.length) { |
|
34597 | 160 |
old_suffix = t :: ts |
34592 | 161 |
new_tokens = new_tokens.tail |
34526 | 162 |
invalid_tokens = Nil |
163 |
} |
|
164 |
case _ => |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
165 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
166 |
} |
34526 | 167 |
val insert = new_tokens.reverse |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
168 |
val new_token_list = begin ::: insert ::: old_suffix |
34778 | 169 |
token_changed(session, id, begin.lastOption, insert, |
34597 | 170 |
old_suffix.firstOption, new_token_list, start) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
171 |
} |
34582 | 172 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
173 |
|
34485 | 174 |
/** command view **/ |
175 |
||
34582 | 176 |
private def token_changed( |
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
177 |
session: Session, |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
178 |
new_id: String, |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
179 |
before_change: Option[Token], |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
180 |
inserted_tokens: List[Token], |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
181 |
after_change: Option[Token], |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
182 |
new_tokens: List[Token], |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
183 |
new_token_start: Map[Token, Int]): |
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
184 |
Document.Result = |
34485 | 185 |
{ |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
186 |
require(assignment.is_finished) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
187 |
|
34689 | 188 |
val new_tokenset = Linear_Set[Token]() ++ new_tokens |
34593 | 189 |
val cmd_before_change = before_change match { |
190 |
case None => None |
|
191 |
case Some(bc) => |
|
192 |
val cmd_with_bc = commands.find(_.contains(bc)).get |
|
193 |
if (cmd_with_bc.tokens.last == bc) { |
|
194 |
if (new_tokenset.next(bc).map(_.is_start).getOrElse(true)) |
|
195 |
Some(cmd_with_bc) |
|
196 |
else commands.prev(cmd_with_bc) |
|
197 |
} |
|
198 |
else commands.prev(cmd_with_bc) |
|
199 |
} |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
200 |
|
34593 | 201 |
val cmd_after_change = after_change match { |
202 |
case None => None |
|
203 |
case Some(ac) => |
|
204 |
val cmd_with_ac = commands.find(_.contains(ac)).get |
|
205 |
if (ac.is_start) |
|
206 |
Some(cmd_with_ac) |
|
207 |
else |
|
208 |
commands.next(cmd_with_ac) |
|
209 |
} |
|
34485 | 210 |
|
34593 | 211 |
val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
212 |
takeWhile(Some(_) != cmd_after_change) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
213 |
|
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
214 |
// calculate inserted commands |
34526 | 215 |
def tokens_to_commands(tokens: List[Token]): List[Command]= { |
216 |
tokens match { |
|
217 |
case Nil => Nil |
|
34582 | 218 |
case t :: ts => |
219 |
val (cmd, rest) = |
|
220 |
ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT) |
|
34778 | 221 |
new Command(session.create_id(), t :: cmd, new_token_start) :: tokens_to_commands(rest) |
34485 | 222 |
} |
223 |
} |
|
224 |
||
34593 | 225 |
val split_begin = |
226 |
if (before_change.isDefined) { |
|
227 |
val changed = |
|
228 |
if (cmd_before_change.isDefined) |
|
34595 | 229 |
new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1) |
34593 | 230 |
else new_tokenset |
231 |
if (changed.exists(_ == before_change.get)) |
|
34597 | 232 |
changed.takeWhile(_ != before_change.get).toList ::: |
233 |
List(before_change.get) |
|
34593 | 234 |
else Nil |
235 |
} else Nil |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
236 |
|
34593 | 237 |
val split_end = |
34667 | 238 |
if (after_change.isDefined) { |
34595 | 239 |
val unchanged = new_tokens.dropWhile(_ != after_change.get) |
34667 | 240 |
if(cmd_after_change.isDefined) { |
241 |
if (unchanged.exists(_ == cmd_after_change.get.tokens.first)) |
|
242 |
unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList |
|
243 |
else Nil |
|
244 |
} else { |
|
245 |
unchanged |
|
246 |
} |
|
34593 | 247 |
} else Nil |
248 |
||
34597 | 249 |
val rescan_begin = |
250 |
split_begin ::: |
|
251 |
before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens) |
|
34582 | 252 |
val rescanning_tokens = |
34597 | 253 |
after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) ::: |
254 |
split_end |
|
34593 | 255 |
val inserted_commands = tokens_to_commands(rescanning_tokens.toList) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
256 |
|
34550
171c8c6e5707
prepared proofdocument for only needed changes
immler@in.tum.de
parents:
34544
diff
changeset
|
257 |
// build new document |
34739 | 258 |
val new_commandset = commands. |
259 |
delete_between(cmd_before_change, cmd_after_change). |
|
260 |
append_after(cmd_before_change, inserted_commands) |
|
261 |
||
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
262 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
263 |
val doc = |
34823 | 264 |
new Document(new_id, new_tokenset, new_token_start, new_commandset, |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
265 |
assignment.join -- removed_commands) |
34660 | 266 |
|
267 |
val removes = |
|
268 |
for (cmd <- removed_commands) yield (cmd_before_change -> None) |
|
269 |
val inserts = |
|
270 |
for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd)) |
|
271 |
||
272 |
return (doc, removes.toList ++ inserts) |
|
34485 | 273 |
} |
34596 | 274 |
|
275 |
val commands_offsets = { |
|
276 |
var last_stop = 0 |
|
277 |
(for (c <- commands) yield { |
|
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
278 |
val r = c -> (last_stop, c.stop(this)) |
34596 | 279 |
last_stop = c.stop(this) |
280 |
r |
|
281 |
}).toArray |
|
282 |
} |
|
283 |
||
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
284 |
def command_at(pos: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
285 |
find_command(pos, 0, commands_offsets.length) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
286 |
|
34596 | 287 |
// use a binary search to find commands for a given offset |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
288 |
private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
289 |
{ |
34596 | 290 |
val middle_index = (array_start + array_stop) / 2 |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
291 |
if (middle_index >= commands_offsets.length) return None |
34596 | 292 |
val (middle, (start, stop)) = commands_offsets(middle_index) |
293 |
// does middle contain pos? |
|
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
294 |
if (start <= pos && pos < stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
295 |
Some(middle) |
34596 | 296 |
else if (start > pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
297 |
find_command(pos, array_start, middle_index) |
34596 | 298 |
else if (stop <= pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
299 |
find_command(pos, middle_index + 1, array_stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
300 |
else error("impossible") |
34596 | 301 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
302 |
} |