author | immler@in.tum.de |
Wed, 08 Jul 2009 13:29:42 +0200 | |
changeset 34648 | 8213a350fd45 |
parent 34603 | 83a37e3b8c9c |
child 34649 | 70759ca6bb87 |
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 |
|
34526 | 11 |
import scala.collection.mutable.ListBuffer |
34532 | 12 |
import scala.actors.Actor |
13 |
import scala.actors.Actor._ |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
14 |
import java.util.regex.Pattern |
34485 | 15 |
import isabelle.prover.{Prover, Command} |
34531 | 16 |
import isabelle.utils.LinearSet |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
17 |
|
34494 | 18 |
case class StructureChange( |
34526 | 19 |
val before_change: Option[Command], |
34494 | 20 |
val added_commands: List[Command], |
21 |
val removed_commands: List[Command]) |
|
34485 | 22 |
|
34483 | 23 |
object ProofDocument |
24 |
{ |
|
34582 | 25 |
// Be careful when changing this regex. Not only must it handle the |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
26 |
// spurious end of a token but also: |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
27 |
// 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
|
28 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507 |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
29 |
|
34483 | 30 |
val token_pattern = |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
31 |
Pattern.compile( |
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 |
"(\\?'?|')[A-Za-z_0-9.]*|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
35 |
"[A-Za-z_0-9.]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
36 |
"[!#$%&*+-/<=>?@^_|~]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
37 |
"\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
38 |
"`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
39 |
"[()\\[\\]{}:;]", Pattern.MULTILINE) |
34485 | 40 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
41 |
val empty = |
34603
83a37e3b8c9c
produce ids via Isabelle.system (http://isabelle.in.tum.de/repos/isabelle/rev/c23663825e23);
wenzelm
parents:
34597
diff
changeset
|
42 |
new ProofDocument(isabelle.jedit.Isabelle.system.id(), |
34582 | 43 |
LinearSet(), Map(), LinearSet(), false, _ => false) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
44 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
45 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
46 |
|
34582 | 47 |
class ProofDocument( |
48 |
val id: String, |
|
49 |
val tokens: LinearSet[Token], |
|
50 |
val token_start: Map[Token, Int], |
|
51 |
val commands: LinearSet[Command], |
|
52 |
val active: Boolean, |
|
53 |
is_command_keyword: String => Boolean) |
|
34483 | 54 |
{ |
34485 | 55 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
56 |
def mark_active: ProofDocument = |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
57 |
new ProofDocument(id, tokens, token_start, commands, true, is_command_keyword) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
58 |
def activate: (ProofDocument, StructureChange) = { |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
59 |
val (doc, change) = |
34648 | 60 |
text_changed(new Text.Change(isabelle.jedit.Isabelle.system.id(), 0, content, content)) |
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
61 |
return (doc.mark_active, change) |
34456 | 62 |
} |
34532 | 63 |
def set_command_keyword(f: String => Boolean): ProofDocument = |
34603
83a37e3b8c9c
produce ids via Isabelle.system (http://isabelle.in.tum.de/repos/isabelle/rev/c23663825e23);
wenzelm
parents:
34597
diff
changeset
|
64 |
new ProofDocument(isabelle.jedit.Isabelle.system.id(), tokens, token_start, commands, active, f) |
34485 | 65 |
|
34582 | 66 |
def content = Token.string_from_tokens(Nil ++ tokens, token_start) |
34485 | 67 |
/** token view **/ |
68 |
||
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
69 |
def text_changed(change: Text.Change): (ProofDocument, StructureChange) = |
34485 | 70 |
{ |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
71 |
//indices of tokens |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
72 |
var start: Map[Token, Int] = token_start |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
73 |
def stop(t: Token) = start(t) + t.length |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
74 |
// split old token lists |
34582 | 75 |
val tokens = Nil ++ this.tokens |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
76 |
val (begin, remaining) = tokens.span(stop(_) < change.start) |
34648 | 77 |
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
|
78 |
// update indices |
34582 | 79 |
start = end.foldLeft(start)((s, t) => |
34648 | 80 |
s + (t -> (s(t) + change.added.length - change.removed.length))) |
34485 | 81 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
82 |
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
|
83 |
map (t => { |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
84 |
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
|
85 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
86 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
87 |
}) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
88 |
|
34648 | 89 |
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
|
90 |
map (t => { |
34582 | 91 |
val split_tok = |
34648 | 92 |
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
|
93 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
94 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
95 |
}) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
96 |
// update indices |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
97 |
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
|
98 |
start = split_end.foldLeft (start) ((s, t) => |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
99 |
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
|
100 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
101 |
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
|
102 |
start += (ins -> change.start) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
103 |
|
34582 | 104 |
var invalid_tokens = split_begin ::: ins :: split_end ::: end |
105 |
var new_tokens: List[Token] = Nil |
|
106 |
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
|
107 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
108 |
val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0) |
34582 | 109 |
val matcher = |
110 |
ProofDocument.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start)) |
|
34526 | 111 |
|
112 |
while (matcher.find() && invalid_tokens != Nil) { |
|
34485 | 113 |
val kind = |
34505
87f4f70d61bb
ProofDocument: pass is_command_keyword directly, not via full-blown Prover object;
wenzelm
parents:
34496
diff
changeset
|
114 |
if (is_command_keyword(matcher.group)) |
34485 | 115 |
Token.Kind.COMMAND_START |
34494 | 116 |
else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*") |
34485 | 117 |
Token.Kind.COMMENT |
118 |
else |
|
119 |
Token.Kind.OTHER |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
120 |
val new_token = new Token(matcher.group, kind) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
121 |
start += (new_token -> (match_start + matcher.start)) |
34526 | 122 |
new_tokens ::= new_token |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
123 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
124 |
invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token)) |
34526 | 125 |
invalid_tokens match { |
34582 | 126 |
case t :: ts => |
127 |
if (start(t) == start(new_token) && |
|
128 |
start(t) > change.start + change.added.length) { |
|
34597 | 129 |
old_suffix = t :: ts |
34592 | 130 |
new_tokens = new_tokens.tail |
34526 | 131 |
invalid_tokens = Nil |
132 |
} |
|
133 |
case _ => |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
134 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
135 |
} |
34526 | 136 |
val insert = new_tokens.reverse |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
137 |
val new_token_list = begin ::: insert ::: old_suffix |
34582 | 138 |
token_changed(change.id, begin.lastOption, insert, |
34597 | 139 |
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
|
140 |
} |
34582 | 141 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
142 |
|
34485 | 143 |
/** command view **/ |
144 |
||
34582 | 145 |
private def token_changed( |
146 |
new_id: String, |
|
147 |
before_change: Option[Token], |
|
148 |
inserted_tokens: List[Token], |
|
149 |
after_change: Option[Token], |
|
34597 | 150 |
new_tokens: List[Token], |
34582 | 151 |
new_token_start: Map[Token, Int]): (ProofDocument, StructureChange) = |
34485 | 152 |
{ |
34595 | 153 |
val new_tokenset = (LinearSet() ++ new_tokens).asInstanceOf[LinearSet[Token]] |
34593 | 154 |
val cmd_before_change = before_change match { |
155 |
case None => None |
|
156 |
case Some(bc) => |
|
157 |
val cmd_with_bc = commands.find(_.contains(bc)).get |
|
158 |
if (cmd_with_bc.tokens.last == bc) { |
|
159 |
if (new_tokenset.next(bc).map(_.is_start).getOrElse(true)) |
|
160 |
Some(cmd_with_bc) |
|
161 |
else commands.prev(cmd_with_bc) |
|
162 |
} |
|
163 |
else commands.prev(cmd_with_bc) |
|
164 |
} |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
165 |
|
34593 | 166 |
val cmd_after_change = after_change match { |
167 |
case None => None |
|
168 |
case Some(ac) => |
|
169 |
val cmd_with_ac = commands.find(_.contains(ac)).get |
|
170 |
if (ac.is_start) |
|
171 |
Some(cmd_with_ac) |
|
172 |
else |
|
173 |
commands.next(cmd_with_ac) |
|
174 |
} |
|
34485 | 175 |
|
34593 | 176 |
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
|
177 |
takeWhile(Some(_) != cmd_after_change) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
178 |
|
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
179 |
// calculate inserted commands |
34526 | 180 |
def tokens_to_commands(tokens: List[Token]): List[Command]= { |
181 |
tokens match { |
|
182 |
case Nil => Nil |
|
34582 | 183 |
case t :: ts => |
184 |
val (cmd, rest) = |
|
185 |
ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT) |
|
186 |
new Command(t :: cmd, new_token_start) :: tokens_to_commands(rest) |
|
34485 | 187 |
} |
188 |
} |
|
189 |
||
34593 | 190 |
val split_begin = |
191 |
if (before_change.isDefined) { |
|
192 |
val changed = |
|
193 |
if (cmd_before_change.isDefined) |
|
34595 | 194 |
new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1) |
34593 | 195 |
else new_tokenset |
196 |
if (changed.exists(_ == before_change.get)) |
|
34597 | 197 |
changed.takeWhile(_ != before_change.get).toList ::: |
198 |
List(before_change.get) |
|
34593 | 199 |
else Nil |
200 |
} else Nil |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
201 |
|
34593 | 202 |
val split_end = |
203 |
if (after_change.isDefined && cmd_after_change.isDefined) { |
|
34595 | 204 |
val unchanged = new_tokens.dropWhile(_ != after_change.get) |
34593 | 205 |
if (unchanged.exists(_ == cmd_after_change.get.tokens.first)) |
206 |
unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList |
|
207 |
else Nil |
|
208 |
} else Nil |
|
209 |
||
34597 | 210 |
val rescan_begin = |
211 |
split_begin ::: |
|
212 |
before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens) |
|
34582 | 213 |
val rescanning_tokens = |
34597 | 214 |
after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) ::: |
215 |
split_end |
|
34593 | 216 |
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
|
217 |
|
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
218 |
val change = new StructureChange(cmd_before_change, inserted_commands, removed_commands.toList) |
34550
171c8c6e5707
prepared proofdocument for only needed changes
immler@in.tum.de
parents:
34544
diff
changeset
|
219 |
// build new document |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
220 |
val new_commandset = commands.delete_between(cmd_before_change, cmd_after_change). |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
221 |
insert_after(cmd_before_change, inserted_commands) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
222 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
223 |
val doc = |
34582 | 224 |
new ProofDocument(new_id, new_tokenset, new_token_start, |
225 |
new_commandset, active, is_command_keyword) |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
226 |
return (doc, change) |
34485 | 227 |
} |
34596 | 228 |
|
229 |
val commands_offsets = { |
|
230 |
var last_stop = 0 |
|
231 |
(for (c <- commands) yield { |
|
232 |
val r = c -> (last_stop,c.stop(this)) |
|
233 |
last_stop = c.stop(this) |
|
234 |
r |
|
235 |
}).toArray |
|
236 |
} |
|
237 |
||
238 |
// use a binary search to find commands for a given offset |
|
239 |
def find_command_at(pos: Int): Command = find_command_at(pos, 0, commands_offsets.length) |
|
240 |
private def find_command_at(pos: Int, array_start: Int, array_stop: Int): Command = { |
|
241 |
val middle_index = (array_start + array_stop) / 2 |
|
242 |
if (middle_index >= commands_offsets.length) return null |
|
243 |
val (middle, (start, stop)) = commands_offsets(middle_index) |
|
244 |
// does middle contain pos? |
|
245 |
if (start <= pos && stop > pos) |
|
246 |
middle |
|
247 |
else if (start > pos) |
|
248 |
find_command_at(pos, array_start, middle_index) |
|
249 |
else if (stop <= pos) |
|
250 |
find_command_at(pos, middle_index + 1, array_stop) |
|
251 |
else error("can't be") |
|
252 |
} |
|
253 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
254 |
} |