1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! A graph representation for computing tree diffs.
use rpds::Stack;
use std::{
cmp::min,
fmt,
hash::{Hash, Hasher},
};
use strsim::normalized_levenshtein;
use crate::syntax::{AtomKind, ChangeKind, Syntax};
use Edge::*;
/// A vertex in a directed acyclic graph that represents a diff.
///
/// Each vertex represents two pointers: one to the next unmatched LHS
/// syntax, and one to the next unmatched RHS syntax.
///
/// For example, suppose we have `X A` on the LHS and `A` on the
/// RHS. Our start vertex looks like this.
///
/// ```text
/// LHS: X A RHS: A
/// ^ ^
/// ```
///
/// From this vertex, we could take [`Edge::NovelAtomLHS`], bringing
/// us to this vertex.
///
/// ```text
/// LHS: X A RHS: A
/// ^ ^
/// ```
///
/// Alternatively, we could take the [`Edge::NovelAtomRHS`], bringing us
/// to this vertex.
///
/// ```text
/// LHS: X A RHS: A
/// ^ ^
/// ```
#[derive(Debug, Clone)]
pub struct Vertex<'a> {
pub lhs_syntax: Option<&'a Syntax<'a>>,
pub rhs_syntax: Option<&'a Syntax<'a>>,
parents: Stack<EnteredDelimiter<'a>>,
lhs_parent_id: Option<u32>,
rhs_parent_id: Option<u32>,
}
impl<'a> PartialEq for Vertex<'a> {
fn eq(&self, other: &Self) -> bool {
self.lhs_syntax.map(|node| node.id()) == other.lhs_syntax.map(|node| node.id())
&& self.rhs_syntax.map(|node| node.id()) == other.rhs_syntax.map(|node| node.id())
// Strictly speaking, we should compare the whole
// EnteredDelimiter stack, not just the immediate
// parents. By taking the immediate parent, we have
// vertices with different stacks that are 'equal'.
//
// This makes the graph traversal path dependent: the
// first vertex we see 'wins', and we use it for deciding
// how we can pop.
//
// In practice this seems to work well. The first vertex
// has the lowest cost, so has the most PopBoth
// occurrences, which is the best outcome.
//
// Handling this properly would require considering many
// more vertices to be distinct, exponentially increasing
// the graph size relative to tree depth.
&& self.lhs_parent_id == other.lhs_parent_id
&& self.rhs_parent_id == other.rhs_parent_id
}
}
impl<'a> Eq for Vertex<'a> {}
impl<'a> Hash for Vertex<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.lhs_syntax.map(|node| node.id()).hash(state);
self.rhs_syntax.map(|node| node.id()).hash(state);
self.lhs_parent_id.hash(state);
self.rhs_parent_id.hash(state);
}
}
/// Tracks entering syntax List nodes.
#[derive(Clone)]
enum EnteredDelimiter<'a> {
/// If we've entered the LHS or RHS separately, we can pop either
/// side independently.
///
/// Assumes that at least one stack is non-empty.
PopEither((Stack<&'a Syntax<'a>>, Stack<&'a Syntax<'a>>)),
/// If we've entered the LHS and RHS together, we must pop both
/// sides together too. Otherwise we'd consider the following case to have no changes.
///
/// ```text
/// Old: (a b c)
/// New: (a b) c
/// ```
PopBoth((&'a Syntax<'a>, &'a Syntax<'a>)),
}
impl<'a> fmt::Debug for EnteredDelimiter<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let desc = match self {
EnteredDelimiter::PopEither((lhs_delims, rhs_delims)) => {
format!("PopEither({}, {})", lhs_delims.size(), rhs_delims.size())
}
EnteredDelimiter::PopBoth(_) => "PopBoth".to_string(),
};
f.write_str(&desc)
}
}
fn push_both_delimiters<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
lhs_delim: &'a Syntax<'a>,
rhs_delim: &'a Syntax<'a>,
) -> Stack<EnteredDelimiter<'a>> {
entered.push(EnteredDelimiter::PopBoth((lhs_delim, rhs_delim)))
}
fn try_pop_both<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
) -> Option<(&'a Syntax<'a>, &'a Syntax<'a>, Stack<EnteredDelimiter<'a>>)> {
match entered.peek() {
Some(EnteredDelimiter::PopBoth((lhs_delim, rhs_delim))) => {
Some((lhs_delim, rhs_delim, entered.pop().unwrap()))
}
_ => None,
}
}
fn try_pop_lhs<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
) -> Option<(&'a Syntax<'a>, Stack<EnteredDelimiter<'a>>)> {
match entered.peek() {
Some(EnteredDelimiter::PopEither((lhs_delims, rhs_delims))) => match lhs_delims.peek() {
Some(lhs_delim) => {
let mut entered = entered.pop().unwrap();
let new_lhs_delims = lhs_delims.pop().unwrap();
if !new_lhs_delims.is_empty() || !rhs_delims.is_empty() {
entered = entered.push(EnteredDelimiter::PopEither((
new_lhs_delims,
rhs_delims.clone(),
)));
}
Some((lhs_delim, entered))
}
None => None,
},
_ => None,
}
}
fn try_pop_rhs<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
) -> Option<(&'a Syntax<'a>, Stack<EnteredDelimiter<'a>>)> {
match entered.peek() {
Some(EnteredDelimiter::PopEither((lhs_delims, rhs_delims))) => match rhs_delims.peek() {
Some(rhs_delim) => {
let mut entered = entered.pop().unwrap();
let new_rhs_delims = rhs_delims.pop().unwrap();
if !lhs_delims.is_empty() || !new_rhs_delims.is_empty() {
entered = entered.push(EnteredDelimiter::PopEither((
lhs_delims.clone(),
new_rhs_delims,
)));
}
Some((rhs_delim, entered))
}
None => None,
},
_ => None,
}
}
fn push_lhs_delimiter<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
delimiter: &'a Syntax<'a>,
) -> Stack<EnteredDelimiter<'a>> {
let mut modifying_head = false;
let (mut lhs_delims, rhs_delims) = match entered.peek() {
Some(EnteredDelimiter::PopEither((lhs_delims, rhs_delims))) => {
modifying_head = true;
(lhs_delims.clone(), rhs_delims.clone())
}
_ => (Stack::new(), Stack::new()),
};
lhs_delims = lhs_delims.push(delimiter);
let entered = if modifying_head {
entered.pop().unwrap()
} else {
entered.clone()
};
entered.push(EnteredDelimiter::PopEither((lhs_delims, rhs_delims)))
}
fn push_rhs_delimiter<'a>(
entered: &Stack<EnteredDelimiter<'a>>,
delimiter: &'a Syntax<'a>,
) -> Stack<EnteredDelimiter<'a>> {
let mut modifying_head = false;
let (lhs_delims, mut rhs_delims) = match entered.peek() {
Some(EnteredDelimiter::PopEither((lhs_delims, rhs_delims))) => {
modifying_head = true;
(lhs_delims.clone(), rhs_delims.clone())
}
_ => (Stack::new(), Stack::new()),
};
rhs_delims = rhs_delims.push(delimiter);
let entered = if modifying_head {
entered.pop().unwrap()
} else {
entered.clone()
};
entered.push(EnteredDelimiter::PopEither((lhs_delims, rhs_delims)))
}
impl<'a> Vertex<'a> {
pub fn is_end(&self) -> bool {
self.lhs_syntax.is_none() && self.rhs_syntax.is_none() && self.parents.is_empty()
}
pub fn new(lhs_syntax: Option<&'a Syntax<'a>>, rhs_syntax: Option<&'a Syntax<'a>>) -> Self {
let parents = Stack::new();
Vertex {
lhs_syntax,
rhs_syntax,
parents,
lhs_parent_id: None,
rhs_parent_id: None,
}
}
}
/// An edge in our graph, with an associated [`cost`](Edge::cost).
///
/// A syntax node can always be marked as novel, so a vertex will have
/// at least a NovelFoo edge. Depending on the syntax nodes of the
/// current [`Vertex`], other edges may also be available.
///
/// See [`neighbours`] for all the edges available for a given `Vertex`.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Edge {
UnchangedNode { depth_difference: u32 },
EnterUnchangedDelimiter { depth_difference: u32 },
ReplacedComment { levenshtein_pct: u8 },
NovelAtomLHS { contiguous: bool },
NovelAtomRHS { contiguous: bool },
// TODO: An EnterNovelDelimiterBoth edge might help performance
// rather doing LHS and RHS separately.
EnterNovelDelimiterLHS { contiguous: bool },
EnterNovelDelimiterRHS { contiguous: bool },
NovelTreeLHS { num_descendants: u32 },
NovelTreeRHS { num_descendants: u32 },
ExitDelimiterLHS,
ExitDelimiterRHS,
ExitDelimiterBoth,
}
impl Edge {
pub fn cost(&self) -> u64 {
match self {
ExitDelimiterBoth => 1,
ExitDelimiterLHS | ExitDelimiterRHS => 2,
// Matching nodes is always best.
// TODO: now we model parents correctly, do we need to track depth difference?
UnchangedNode { depth_difference } => min(40, *depth_difference as u64 + 1),
// Matching an outer delimiter is good.
EnterUnchangedDelimiter { depth_difference } => 100 + min(40, *depth_difference as u64),
// Replacing a comment is better than treating it as novel.
ReplacedComment { levenshtein_pct } => 150 + u64::from(100 - levenshtein_pct),
// Otherwise, we've added/removed a node.
NovelAtomLHS { contiguous }
| NovelAtomRHS { contiguous }
| EnterNovelDelimiterLHS { contiguous }
| EnterNovelDelimiterRHS { contiguous } => {
if *contiguous {
300
} else {
// This needs to be more than 40 greater than the
// contiguous case. Otherwise, we end up choosing
// a badly positioned unchanged delimiter just
// because it has a better depth difference.
//
// TODO: write a test for this case.
350
}
}
// For large trees, it's better to mark the whole tree as
// novel rather than marking 90% of the children as
// novel. This stops us matching up completely unrelated trees.
NovelTreeLHS { num_descendants } | NovelTreeRHS { num_descendants } => {
300 + (*num_descendants as u64 - 10) * NovelAtomLHS { contiguous: false }.cost()
}
}
}
}
const NOVEL_TREE_THRESHOLD: u32 = 20;
/// Calculate all the neighbours from `v` and write them to `buf`.
pub fn neighbours<'a>(v: &Vertex<'a>, buf: &mut [Option<(Edge, Vertex<'a>)>]) {
for item in &mut *buf {
*item = None;
}
let mut i = 0;
if v.lhs_syntax.is_none() && v.rhs_syntax.is_none() {
if let Some((lhs_parent, rhs_parent, parents_next)) = try_pop_both(&v.parents) {
// We have exhausted all the nodes on both lists, so we can
// move up to the parent node.
// Continue from sibling of parent.
buf[i] = Some((
ExitDelimiterBoth,
Vertex {
lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: rhs_parent.next_sibling(),
parents: parents_next,
lhs_parent_id: lhs_parent.parent().map(Syntax::id),
rhs_parent_id: rhs_parent.parent().map(Syntax::id),
},
));
i += 1;
}
}
if v.lhs_syntax.is_none() {
if let Some((lhs_parent, parents_next)) = try_pop_lhs(&v.parents) {
// Move to next after LHS parent.
// Continue from sibling of parent.
buf[i] = Some((
ExitDelimiterLHS,
Vertex {
lhs_syntax: lhs_parent.next_sibling(),
rhs_syntax: v.rhs_syntax,
parents: parents_next,
lhs_parent_id: lhs_parent.parent().map(Syntax::id),
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
}
if v.rhs_syntax.is_none() {
if let Some((rhs_parent, parents_next)) = try_pop_rhs(&v.parents) {
// Move to next after RHS parent.
// Continue from sibling of parent.
buf[i] = Some((
ExitDelimiterRHS,
Vertex {
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_parent.next_sibling(),
parents: parents_next,
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: rhs_parent.parent().map(Syntax::id),
},
));
i += 1;
}
}
if let (Some(lhs_syntax), Some(rhs_syntax)) = (&v.lhs_syntax, &v.rhs_syntax) {
if lhs_syntax == rhs_syntax {
let depth_difference = (lhs_syntax.num_ancestors() as i32
- rhs_syntax.num_ancestors() as i32)
.abs() as u32;
// Both nodes are equal, the happy case.
buf[i] = Some((
UnchangedNode { depth_difference },
Vertex {
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
if let (
Syntax::List {
open_content: lhs_open_content,
close_content: lhs_close_content,
children: lhs_children,
..
},
Syntax::List {
open_content: rhs_open_content,
close_content: rhs_close_content,
children: rhs_children,
..
},
) = (lhs_syntax, rhs_syntax)
{
// The list delimiters are equal, but children may not be.
if lhs_open_content == rhs_open_content && lhs_close_content == rhs_close_content {
let lhs_next = lhs_children.get(0).copied();
let rhs_next = rhs_children.get(0).copied();
// TODO: be consistent between parents_next and next_parents.
let parents_next = push_both_delimiters(&v.parents, lhs_syntax, rhs_syntax);
let depth_difference = (lhs_syntax.num_ancestors() as i32
- rhs_syntax.num_ancestors() as i32)
.abs() as u32;
buf[i] = Some((
EnterUnchangedDelimiter { depth_difference },
Vertex {
lhs_syntax: lhs_next,
rhs_syntax: rhs_next,
parents: parents_next,
lhs_parent_id: Some(lhs_syntax.id()),
rhs_parent_id: Some(rhs_syntax.id()),
},
));
i += 1;
}
}
if let (
Syntax::Atom {
content: lhs_content,
kind: AtomKind::Comment,
..
},
Syntax::Atom {
content: rhs_content,
kind: AtomKind::Comment,
..
},
) = (lhs_syntax, rhs_syntax)
{
// Both sides are comments and their content is reasonably
// similar.
if lhs_content != rhs_content {
let levenshtein_pct =
(normalized_levenshtein(lhs_content, rhs_content) * 100.0).round() as u8;
buf[i] = Some((
ReplacedComment { levenshtein_pct },
Vertex {
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
}
}
if let Some(lhs_syntax) = &v.lhs_syntax {
match lhs_syntax {
// Step over this novel atom.
Syntax::Atom { .. } => {
buf[i] = Some((
NovelAtomLHS {
// TODO: should this apply if prev is a parent
// node rather than a sibling?
contiguous: lhs_syntax.prev_is_contiguous(),
},
Vertex {
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: v.rhs_syntax,
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
// Step into this partially/fully novel list.
Syntax::List {
children,
num_descendants,
..
} => {
let lhs_next = children.get(0).copied();
let parents_next = push_lhs_delimiter(&v.parents, lhs_syntax);
buf[i] = Some((
EnterNovelDelimiterLHS {
contiguous: lhs_syntax.prev_is_contiguous(),
},
Vertex {
lhs_syntax: lhs_next,
rhs_syntax: v.rhs_syntax,
parents: parents_next,
lhs_parent_id: Some(lhs_syntax.id()),
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
if *num_descendants > NOVEL_TREE_THRESHOLD && lhs_syntax.parent().is_none() {
buf[i] = Some((
NovelTreeLHS {
num_descendants: *num_descendants,
},
Vertex {
lhs_syntax: lhs_syntax.next_sibling(),
rhs_syntax: v.rhs_syntax,
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
}
}
}
if let Some(rhs_syntax) = &v.rhs_syntax {
match rhs_syntax {
// Step over this novel atom.
Syntax::Atom { .. } => {
buf[i] = Some((
NovelAtomRHS {
contiguous: rhs_syntax.prev_is_contiguous(),
},
Vertex {
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
// Step into this partially/fully novel list.
Syntax::List {
children,
num_descendants,
..
} => {
let rhs_next = children.get(0).copied();
let parents_next = push_rhs_delimiter(&v.parents, rhs_syntax);
buf[i] = Some((
EnterNovelDelimiterRHS {
contiguous: rhs_syntax.prev_is_contiguous(),
},
Vertex {
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_next,
parents: parents_next,
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: Some(rhs_syntax.id()),
},
));
i += 1;
if *num_descendants > NOVEL_TREE_THRESHOLD && rhs_syntax.parent().is_none() {
buf[i] = Some((
NovelTreeRHS {
num_descendants: *num_descendants,
},
Vertex {
lhs_syntax: v.lhs_syntax,
rhs_syntax: rhs_syntax.next_sibling(),
parents: v.parents.clone(),
lhs_parent_id: v.lhs_parent_id,
rhs_parent_id: v.rhs_parent_id,
},
));
i += 1;
}
}
}
}
assert!(
i > 0,
"Must always find some next steps if node is not the end"
);
}
pub fn mark_route(route: &[(Edge, Vertex)]) {
for (e, v) in route {
match e {
ExitDelimiterBoth | ExitDelimiterLHS | ExitDelimiterRHS => {
// Nothing to do: we have already marked this node when we entered it.
}
UnchangedNode { .. } => {
// No change on this node or its children.
let lhs = v.lhs_syntax.unwrap();
let rhs = v.rhs_syntax.unwrap();
lhs.set_change_deep(ChangeKind::Unchanged(rhs));
rhs.set_change_deep(ChangeKind::Unchanged(lhs));
}
EnterUnchangedDelimiter { .. } => {
// No change on the outer delimiter, but children may
// have changed.
let lhs = v.lhs_syntax.unwrap();
let rhs = v.rhs_syntax.unwrap();
lhs.set_change(ChangeKind::Unchanged(rhs));
rhs.set_change(ChangeKind::Unchanged(lhs));
}
ReplacedComment { levenshtein_pct } => {
let lhs = v.lhs_syntax.unwrap();
let rhs = v.rhs_syntax.unwrap();
if *levenshtein_pct > 40 {
lhs.set_change(ChangeKind::ReplacedComment(lhs, rhs));
rhs.set_change(ChangeKind::ReplacedComment(rhs, lhs));
} else {
lhs.set_change(ChangeKind::Novel);
rhs.set_change(ChangeKind::Novel);
}
}
NovelAtomLHS { .. } | EnterNovelDelimiterLHS { .. } => {
let lhs = v.lhs_syntax.unwrap();
lhs.set_change(ChangeKind::Novel);
}
NovelAtomRHS { .. } | EnterNovelDelimiterRHS { .. } => {
let rhs = v.rhs_syntax.unwrap();
rhs.set_change(ChangeKind::Novel);
}
NovelTreeLHS { .. } => {
let lhs = v.lhs_syntax.unwrap();
lhs.set_change_deep(ChangeKind::Novel);
}
NovelTreeRHS { .. } => {
let rhs = v.rhs_syntax.unwrap();
rhs.set_change_deep(ChangeKind::Novel);
}
}
}
}