OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library trydart.poi.diff; | 5 library trydart.poi.diff; |
6 | 6 |
7 import 'dart:async' show | |
8 Completer, | |
9 Future, | |
10 Stream; | |
11 | |
12 import 'dart:convert' show | |
13 LineSplitter, | |
14 UTF8; | |
15 | |
16 import 'package:compiler/compiler.dart' as api; | |
17 | |
18 import 'package:compiler/implementation/dart2jslib.dart' show | |
19 Compiler, | |
20 Enqueuer, | |
21 QueueFilter, | |
22 Script, | |
23 WorkItem; | |
24 | |
25 import 'package:compiler/implementation/elements/visitor.dart' show | |
26 ElementVisitor; | |
27 | |
28 import 'package:compiler/implementation/elements/elements.dart' show | 7 import 'package:compiler/implementation/elements/elements.dart' show |
29 AbstractFieldElement, | 8 AbstractFieldElement, |
30 ClassElement, | 9 ClassElement, |
31 CompilationUnitElement, | 10 CompilationUnitElement, |
32 Element, | 11 Element, |
33 ElementCategory, | 12 ElementCategory, |
34 FunctionElement, | 13 FunctionElement, |
35 LibraryElement, | 14 LibraryElement, |
36 ScopeContainerElement; | 15 ScopeContainerElement; |
37 | 16 |
38 import 'package:compiler/implementation/elements/modelx.dart' as modelx; | 17 import 'package:compiler/implementation/elements/modelx.dart' as modelx; |
39 | 18 |
40 import 'package:compiler/implementation/dart_types.dart' show | 19 import 'package:compiler/implementation/elements/modelx.dart' show |
41 DartType; | 20 DeclarationSite; |
42 | 21 |
43 import 'package:compiler/implementation/scanner/scannerlib.dart' show | 22 import 'package:compiler/implementation/scanner/scannerlib.dart' show |
44 EOF_TOKEN, | 23 EOF_TOKEN, |
45 ErrorToken, | 24 ErrorToken, |
46 IDENTIFIER_TOKEN, | 25 IDENTIFIER_TOKEN, |
47 KEYWORD_TOKEN, | 26 KEYWORD_TOKEN, |
48 PartialClassElement, | 27 PartialClassElement, |
49 PartialElement, | 28 PartialElement, |
50 Token; | 29 Token; |
51 | 30 |
52 import 'package:compiler/implementation/source_file.dart' show | |
53 StringSourceFile; | |
54 | |
55 class Difference { | 31 class Difference { |
56 final Element before; | 32 final DeclarationSite before; |
57 final Element after; | 33 final DeclarationSite after; |
58 Token token; | 34 Token token; |
59 | 35 |
60 Difference(this.before, this.after); | 36 Difference(this.before, this.after); |
61 | 37 |
62 String toString() { | 38 String toString() { |
63 if (before == null) return 'Added($after)'; | 39 if (before == null) return 'Added($after)'; |
64 if (after == null) return 'Removed($before)'; | 40 if (after == null) return 'Removed($before)'; |
65 return 'Modified($after -> $before)'; | 41 return 'Modified($after -> $before)'; |
66 } | 42 } |
67 } | 43 } |
68 | 44 |
69 List<Difference> computeDifference( | 45 List<Difference> computeDifference( |
70 ScopeContainerElement before, | 46 ScopeContainerElement before, |
71 ScopeContainerElement after) { | 47 ScopeContainerElement after) { |
72 Map<String, Element> beforeMap = <String, Element>{}; | 48 Map<String, DeclarationSite> beforeMap = <String, DeclarationSite>{}; |
73 before.forEachLocalMember((Element element) { | 49 before.forEachLocalMember((modelx.ElementX element) { |
74 beforeMap[element.name] = element; | 50 DeclarationSite site = element.declarationSite; |
| 51 assert(site != null); |
| 52 beforeMap[element.name] = site; |
75 }); | 53 }); |
76 List<Difference> modifications = <Difference>[]; | 54 List<Difference> modifications = <Difference>[]; |
77 List<Difference> potentiallyChanged = <Difference>[]; | 55 List<Difference> potentiallyChanged = <Difference>[]; |
78 after.forEachLocalMember((Element element) { | 56 after.forEachLocalMember((modelx.ElementX element) { |
79 Element existing = beforeMap.remove(element.name); | 57 DeclarationSite existing = beforeMap.remove(element.name); |
80 if (existing == null) { | 58 if (existing == null) { |
81 modifications.add(new Difference(null, element)); | 59 modifications.add(new Difference(null, element.declarationSite)); |
82 } else { | 60 } else { |
83 potentiallyChanged.add(new Difference(existing, element)); | 61 potentiallyChanged.add(new Difference(existing, element.declarationSite)); |
84 } | 62 } |
85 }); | 63 }); |
86 | 64 |
87 modifications.addAll( | 65 modifications.addAll( |
88 beforeMap.values.map((Element element) => new Difference(element, null))); | 66 beforeMap.values.map( |
| 67 (DeclarationSite site) => new Difference(site, null))); |
89 | 68 |
90 modifications.addAll( | 69 modifications.addAll( |
91 potentiallyChanged.where(areDifferentElements)); | 70 potentiallyChanged.where(areDifferentElements)); |
92 | 71 |
93 return modifications; | 72 return modifications; |
94 } | 73 } |
95 | 74 |
96 bool areDifferentElements(Difference diff) { | 75 bool areDifferentElements(Difference diff) { |
97 Element beforeElement = diff.before; | 76 DeclarationSite before = diff.before; |
98 Element afterElement = diff.after; | 77 DeclarationSite after = diff.after; |
99 var before = (beforeElement is modelx.VariableElementX) | |
100 ? beforeElement.variables : beforeElement; | |
101 var after = (afterElement is modelx.VariableElementX) | |
102 ? afterElement.variables : afterElement; | |
103 if (before is PartialElement && after is PartialElement) { | 78 if (before is PartialElement && after is PartialElement) { |
104 Token beforeToken = before.beginToken; | 79 Token beforeToken = before.beginToken; |
105 Token afterToken = after.beginToken; | 80 Token afterToken = after.beginToken; |
106 Token stop = before.endToken; | 81 Token stop = before.endToken; |
107 int beforeKind = beforeToken.kind; | 82 int beforeKind = beforeToken.kind; |
108 int afterKind = afterToken.kind; | 83 int afterKind = afterToken.kind; |
109 while (beforeKind != EOF_TOKEN && afterKind != EOF_TOKEN) { | 84 while (beforeKind != EOF_TOKEN && afterKind != EOF_TOKEN) { |
110 | 85 |
111 if (beforeKind != afterKind) { | 86 if (beforeKind != afterKind) { |
112 diff.token = afterToken; | 87 diff.token = afterToken; |
(...skipping 12 matching lines...) Expand all Loading... |
125 beforeToken = beforeToken.next; | 100 beforeToken = beforeToken.next; |
126 afterToken = afterToken.next; | 101 afterToken = afterToken.next; |
127 beforeKind = beforeToken.kind; | 102 beforeKind = beforeToken.kind; |
128 afterKind = afterToken.kind; | 103 afterKind = afterToken.kind; |
129 } | 104 } |
130 return beforeKind != afterKind; | 105 return beforeKind != afterKind; |
131 } | 106 } |
132 print("$before isn't a PartialElement"); | 107 print("$before isn't a PartialElement"); |
133 return true; | 108 return true; |
134 } | 109 } |
OLD | NEW |