OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library trydart.poi.diff; | |
6 | |
7 import 'package:compiler/implementation/elements/elements.dart' show | |
8 AbstractFieldElement, | |
9 ClassElement, | |
10 CompilationUnitElement, | |
11 Element, | |
12 ElementCategory, | |
13 FunctionElement, | |
14 LibraryElement, | |
15 ScopeContainerElement; | |
16 | |
17 import 'package:compiler/implementation/elements/modelx.dart' as modelx; | |
18 | |
19 import 'package:compiler/implementation/elements/modelx.dart' show | |
20 DeclarationSite; | |
21 | |
22 import 'package:compiler/implementation/scanner/scannerlib.dart' show | |
23 EOF_TOKEN, | |
24 ErrorToken, | |
25 IDENTIFIER_TOKEN, | |
26 KEYWORD_TOKEN, | |
27 PartialClassElement, | |
28 PartialElement, | |
29 Token; | |
30 | |
31 class Difference { | |
32 final DeclarationSite before; | |
33 final DeclarationSite after; | |
34 Token token; | |
35 | |
36 Difference(this.before, this.after); | |
37 | |
38 String toString() { | |
39 if (before == null) return 'Added($after)'; | |
40 if (after == null) return 'Removed($before)'; | |
41 return 'Modified($after -> $before)'; | |
42 } | |
43 } | |
44 | |
45 List<Difference> computeDifference( | |
46 ScopeContainerElement before, | |
47 ScopeContainerElement after) { | |
48 Map<String, DeclarationSite> beforeMap = <String, DeclarationSite>{}; | |
49 before.forEachLocalMember((modelx.ElementX element) { | |
50 DeclarationSite site = element.declarationSite; | |
51 assert(site != null); | |
52 beforeMap[element.name] = site; | |
53 }); | |
54 List<Difference> modifications = <Difference>[]; | |
55 List<Difference> potentiallyChanged = <Difference>[]; | |
56 after.forEachLocalMember((modelx.ElementX element) { | |
57 DeclarationSite existing = beforeMap.remove(element.name); | |
58 if (existing == null) { | |
59 modifications.add(new Difference(null, element.declarationSite)); | |
60 } else { | |
61 potentiallyChanged.add(new Difference(existing, element.declarationSite)); | |
62 } | |
63 }); | |
64 | |
65 modifications.addAll( | |
66 beforeMap.values.map( | |
67 (DeclarationSite site) => new Difference(site, null))); | |
68 | |
69 modifications.addAll( | |
70 potentiallyChanged.where(areDifferentElements)); | |
71 | |
72 return modifications; | |
73 } | |
74 | |
75 bool areDifferentElements(Difference diff) { | |
76 DeclarationSite before = diff.before; | |
77 DeclarationSite after = diff.after; | |
78 if (before is PartialElement && after is PartialElement) { | |
79 Token beforeToken = before.beginToken; | |
80 Token afterToken = after.beginToken; | |
81 Token stop = before.endToken; | |
82 int beforeKind = beforeToken.kind; | |
83 int afterKind = afterToken.kind; | |
84 while (beforeKind != EOF_TOKEN && afterKind != EOF_TOKEN) { | |
85 | |
86 if (beforeKind != afterKind) { | |
87 diff.token = afterToken; | |
88 return true; | |
89 } | |
90 | |
91 if (beforeToken is! ErrorToken && afterToken is! ErrorToken) { | |
92 if (beforeToken.value != afterToken.value) { | |
93 diff.token = afterToken; | |
94 return true; | |
95 } | |
96 } | |
97 | |
98 if (beforeToken == stop) return false; | |
99 | |
100 beforeToken = beforeToken.next; | |
101 afterToken = afterToken.next; | |
102 beforeKind = beforeToken.kind; | |
103 afterKind = afterToken.kind; | |
104 } | |
105 return beforeKind != afterKind; | |
106 } | |
107 print("$before isn't a PartialElement"); | |
108 return true; | |
109 } | |
OLD | NEW |