| 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 engine.incremental_resolver; | 5 library engine.incremental_resolver; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 | 9 |
| 10 import 'package:analyzer/src/generated/error_verifier.dart'; | 10 import 'package:analyzer/src/generated/error_verifier.dart'; |
| (...skipping 923 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 934 int beginOffsetNew = math.min(firstOffsetNew, lastOffsetNew); | 934 int beginOffsetNew = math.min(firstOffsetNew, lastOffsetNew); |
| 935 int endOffsetNew = math.max(firstOffsetNew, lastOffsetNew); | 935 int endOffsetNew = math.max(firstOffsetNew, lastOffsetNew); |
| 936 // check for a whitespace only change | 936 // check for a whitespace only change |
| 937 if (identical(lastPair.oldToken, firstPair.oldToken) && | 937 if (identical(lastPair.oldToken, firstPair.oldToken) && |
| 938 identical(lastPair.newToken, firstPair.newToken)) { | 938 identical(lastPair.newToken, firstPair.newToken)) { |
| 939 _updateOffset = beginOffsetOld - 1; | 939 _updateOffset = beginOffsetOld - 1; |
| 940 _updateEndOld = endOffsetOld; | 940 _updateEndOld = endOffsetOld; |
| 941 _updateDelta = newUnit.length - oldUnit.length; | 941 _updateDelta = newUnit.length - oldUnit.length; |
| 942 if (firstPair.atComment && lastPair.atComment) { | 942 if (firstPair.atComment && lastPair.atComment) { |
| 943 _resolveComment(oldUnit, newUnit, firstPair); | 943 _resolveComment(oldUnit, newUnit, firstPair); |
| 944 } else { |
| 945 _shiftTokens(firstPair.oldToken); |
| 946 IncrementalResolver._updateElementNameOffsets( |
| 947 oldUnit.element, |
| 948 _updateOffset, |
| 949 _updateDelta); |
| 950 _updateEntry(); |
| 944 } | 951 } |
| 945 _shiftTokens(firstPair.oldToken, _updateDelta); | |
| 946 IncrementalResolver._updateElementNameOffsets( | |
| 947 oldUnit.element, | |
| 948 _updateOffset, | |
| 949 _updateDelta); | |
| 950 _updateEntry(); | |
| 951 return true; | 952 return true; |
| 952 } | 953 } |
| 953 // Find nodes covering the "old" and "new" token ranges. | 954 // Find nodes covering the "old" and "new" token ranges. |
| 954 AstNode oldNode = | 955 AstNode oldNode = |
| 955 _findNodeCovering(oldUnit, beginOffsetOld, endOffsetOld); | 956 _findNodeCovering(oldUnit, beginOffsetOld, endOffsetOld); |
| 956 AstNode newNode = | 957 AstNode newNode = |
| 957 _findNodeCovering(newUnit, beginOffsetNew, endOffsetNew); | 958 _findNodeCovering(newUnit, beginOffsetNew, endOffsetNew); |
| 958 // print('oldNode: $oldNode'); | 959 // print('oldNode: $oldNode'); |
| 959 // print('newNode: $newNode'); | 960 // print('newNode: $newNode'); |
| 960 // Try to find the smallest common node, a FunctionBody currently. | 961 // Try to find the smallest common node, a FunctionBody currently. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 // update token references | 998 // update token references |
| 998 { | 999 { |
| 999 Token oldBeginToken = _getBeginTokenNotComment(oldNode); | 1000 Token oldBeginToken = _getBeginTokenNotComment(oldNode); |
| 1000 Token newBeginToken = _getBeginTokenNotComment(newNode); | 1001 Token newBeginToken = _getBeginTokenNotComment(newNode); |
| 1001 if (oldBeginToken.previous.type == TokenType.EOF) { | 1002 if (oldBeginToken.previous.type == TokenType.EOF) { |
| 1002 oldUnit.beginToken = newBeginToken; | 1003 oldUnit.beginToken = newBeginToken; |
| 1003 } else { | 1004 } else { |
| 1004 oldBeginToken.previous.setNext(newBeginToken); | 1005 oldBeginToken.previous.setNext(newBeginToken); |
| 1005 } | 1006 } |
| 1006 newNode.endToken.setNext(oldNode.endToken.next); | 1007 newNode.endToken.setNext(oldNode.endToken.next); |
| 1007 _shiftTokens(oldNode.endToken.next, _updateDelta); | 1008 _shiftTokens(oldNode.endToken.next); |
| 1008 } | 1009 } |
| 1009 // perform incremental resolution | 1010 // perform incremental resolution |
| 1010 CompilationUnitElement oldUnitElement = oldUnit.element; | 1011 CompilationUnitElement oldUnitElement = oldUnit.element; |
| 1011 IncrementalResolver incrementalResolver = new IncrementalResolver( | 1012 IncrementalResolver incrementalResolver = new IncrementalResolver( |
| 1012 _typeProvider, | 1013 _typeProvider, |
| 1013 oldUnitElement, | 1014 oldUnitElement, |
| 1014 _updateOffset, | 1015 _updateOffset, |
| 1015 oldNode.length, | 1016 oldNode.length, |
| 1016 newNode.length); | 1017 newNode.length); |
| 1017 incrementalResolver.resolve(newNode); | 1018 incrementalResolver.resolve(newNode); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1038 _newParseErrors = errorListener.errors; | 1039 _newParseErrors = errorListener.errors; |
| 1039 return unit; | 1040 return unit; |
| 1040 } | 1041 } |
| 1041 | 1042 |
| 1042 void _resolveComment(CompilationUnit oldUnit, CompilationUnit newUnit, | 1043 void _resolveComment(CompilationUnit oldUnit, CompilationUnit newUnit, |
| 1043 _TokenPair firstPair) { | 1044 _TokenPair firstPair) { |
| 1044 Token oldToken = firstPair.oldToken; | 1045 Token oldToken = firstPair.oldToken; |
| 1045 int offset = oldToken.precedingComments.offset; | 1046 int offset = oldToken.precedingComments.offset; |
| 1046 Comment oldComment = _findNodeCovering(oldUnit, offset, offset); | 1047 Comment oldComment = _findNodeCovering(oldUnit, offset, offset); |
| 1047 Comment newComment = _findNodeCovering(newUnit, offset, offset); | 1048 Comment newComment = _findNodeCovering(newUnit, offset, offset); |
| 1048 _updateOffset = offset + 1; | 1049 _updateOffset = oldToken.offset - 1; |
| 1050 // update token references |
| 1051 _shiftTokens(firstPair.oldToken); |
| 1052 _setPrecedingComments(oldToken, newComment.tokens.first); |
| 1049 // replace node | 1053 // replace node |
| 1050 NodeReplacer.replace(oldComment, newComment); | 1054 NodeReplacer.replace(oldComment, newComment); |
| 1051 // update token references | 1055 // update elements |
| 1052 _setPrecedingComments(oldToken, newComment.tokens.first); | 1056 IncrementalResolver._updateElementNameOffsets( |
| 1057 oldUnit.element, |
| 1058 _updateOffset, |
| 1059 _updateDelta); |
| 1060 _updateEntry(); |
| 1053 // resolve references in the comment | 1061 // resolve references in the comment |
| 1054 CompilationUnitElement oldUnitElement = oldUnit.element; | 1062 CompilationUnitElement oldUnitElement = oldUnit.element; |
| 1055 IncrementalResolver incrementalResolver = | 1063 IncrementalResolver incrementalResolver = |
| 1056 new IncrementalResolver(_typeProvider, oldUnitElement, _updateOffset, 0,
0); | 1064 new IncrementalResolver(_typeProvider, oldUnitElement, _updateOffset, 0,
0); |
| 1057 incrementalResolver._resolveReferences(newComment); | 1065 incrementalResolver._resolveReferences(newComment); |
| 1058 } | 1066 } |
| 1059 | 1067 |
| 1060 Token _scan(String code) { | 1068 Token _scan(String code) { |
| 1061 RecordingErrorListener errorListener = new RecordingErrorListener(); | 1069 RecordingErrorListener errorListener = new RecordingErrorListener(); |
| 1062 CharSequenceReader reader = new CharSequenceReader(code); | 1070 CharSequenceReader reader = new CharSequenceReader(code); |
| 1063 Scanner scanner = new Scanner(_unitSource, reader, errorListener); | 1071 Scanner scanner = new Scanner(_unitSource, reader, errorListener); |
| 1064 Token token = scanner.tokenize(); | 1072 Token token = scanner.tokenize(); |
| 1065 _newScanErrors = errorListener.errors; | 1073 _newScanErrors = errorListener.errors; |
| 1066 return token; | 1074 return token; |
| 1067 } | 1075 } |
| 1068 | 1076 |
| 1069 void _shiftTokens(Token token, int delta) { | 1077 void _shiftTokens(Token token) { |
| 1070 while (token != null) { | 1078 while (token != null) { |
| 1071 if (token.offset > _updateOffset) { | 1079 if (token.offset > _updateOffset) { |
| 1072 token.offset += delta; | 1080 token.offset += _updateDelta; |
| 1073 } | 1081 } |
| 1074 _shiftTokens(token.precedingComments, delta); | 1082 // comments |
| 1083 _shiftTokens(token.precedingComments); |
| 1084 if (token is CommentToken) { |
| 1085 for (Token reference in token.references) { |
| 1086 _shiftTokens(reference); |
| 1087 } |
| 1088 } |
| 1089 // next |
| 1075 if (token.type == TokenType.EOF) { | 1090 if (token.type == TokenType.EOF) { |
| 1076 break; | 1091 break; |
| 1077 } | 1092 } |
| 1078 token = token.next; | 1093 token = token.next; |
| 1079 } | 1094 } |
| 1080 } | 1095 } |
| 1081 | 1096 |
| 1082 void _updateEntry() { | 1097 void _updateEntry() { |
| 1083 _entry.setValue(DartEntry.SCAN_ERRORS, _newScanErrors); | 1098 _entry.setValue(DartEntry.SCAN_ERRORS, _newScanErrors); |
| 1084 _entry.setValue(DartEntry.PARSE_ERRORS, _newParseErrors); | 1099 _entry.setValue(DartEntry.PARSE_ERRORS, _newParseErrors); |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1529 } | 1544 } |
| 1530 } | 1545 } |
| 1531 | 1546 |
| 1532 | 1547 |
| 1533 class _TokenPair { | 1548 class _TokenPair { |
| 1534 final Token oldToken; | 1549 final Token oldToken; |
| 1535 final Token newToken; | 1550 final Token newToken; |
| 1536 final bool atComment; | 1551 final bool atComment; |
| 1537 _TokenPair(this.oldToken, this.newToken, [this.atComment = false]); | 1552 _TokenPair(this.oldToken, this.newToken, [this.atComment = false]); |
| 1538 } | 1553 } |
| OLD | NEW |