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 '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 |
| 29 AbstractFieldElement, |
| 30 ClassElement, |
| 31 CompilationUnitElement, |
| 32 Element, |
| 33 ElementCategory, |
| 34 FunctionElement, |
| 35 LibraryElement, |
| 36 ScopeContainerElement; |
| 37 |
| 38 import 'package:compiler/implementation/elements/modelx.dart' as modelx; |
| 39 |
| 40 import 'package:compiler/implementation/dart_types.dart' show |
| 41 DartType; |
| 42 |
| 43 import 'package:compiler/implementation/scanner/scannerlib.dart' show |
| 44 EOF_TOKEN, |
| 45 ErrorToken, |
| 46 IDENTIFIER_TOKEN, |
| 47 KEYWORD_TOKEN, |
| 48 PartialClassElement, |
| 49 PartialElement, |
| 50 Token; |
| 51 |
| 52 import 'package:compiler/implementation/source_file.dart' show |
| 53 StringSourceFile; |
| 54 |
| 55 class Difference { |
| 56 final Element before; |
| 57 final Element after; |
| 58 Token token; |
| 59 |
| 60 Difference(this.before, this.after); |
| 61 |
| 62 String toString() { |
| 63 if (before == null) return 'Added($after)'; |
| 64 if (after == null) return 'Removed($before)'; |
| 65 return 'Modified($after -> $before)'; |
| 66 } |
| 67 } |
| 68 |
| 69 List<Difference> computeDifference( |
| 70 ScopeContainerElement before, |
| 71 ScopeContainerElement after) { |
| 72 Map<String, Element> beforeMap = <String, Element>{}; |
| 73 before.forEachLocalMember((Element element) { |
| 74 beforeMap[element.name] = element; |
| 75 }); |
| 76 List<Difference> modifications = <Difference>[]; |
| 77 List<Difference> potentiallyChanged = <Difference>[]; |
| 78 after.forEachLocalMember((Element element) { |
| 79 Element existing = beforeMap.remove(element.name); |
| 80 if (existing == null) { |
| 81 modifications.add(new Difference(null, element)); |
| 82 } else { |
| 83 potentiallyChanged.add(new Difference(existing, element)); |
| 84 } |
| 85 }); |
| 86 |
| 87 modifications.addAll( |
| 88 beforeMap.values.map((Element element) => new Difference(element, null))); |
| 89 |
| 90 modifications.addAll( |
| 91 potentiallyChanged.where(areDifferentElements)); |
| 92 |
| 93 return modifications; |
| 94 } |
| 95 |
| 96 bool areDifferentElements(Difference diff) { |
| 97 Element beforeElement = diff.before; |
| 98 Element afterElement = 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) { |
| 104 Token beforeToken = before.beginToken; |
| 105 Token afterToken = after.beginToken; |
| 106 Token stop = before.endToken; |
| 107 int beforeKind = beforeToken.kind; |
| 108 int afterKind = afterToken.kind; |
| 109 while (beforeKind != EOF_TOKEN && afterKind != EOF_TOKEN) { |
| 110 |
| 111 if (beforeKind != afterKind) { |
| 112 diff.token = afterToken; |
| 113 return true; |
| 114 } |
| 115 |
| 116 if (beforeToken is! ErrorToken && afterToken is! ErrorToken) { |
| 117 if (beforeToken.value != afterToken.value) { |
| 118 diff.token = afterToken; |
| 119 return true; |
| 120 } |
| 121 } |
| 122 |
| 123 if (beforeToken == stop) return false; |
| 124 |
| 125 beforeToken = beforeToken.next; |
| 126 afterToken = afterToken.next; |
| 127 beforeKind = beforeToken.kind; |
| 128 afterKind = afterToken.kind; |
| 129 } |
| 130 return beforeKind != afterKind; |
| 131 } |
| 132 print("$before isn't a PartialElement"); |
| 133 return true; |
| 134 } |
OLD | NEW |