| 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/services/lint.dart'; | 10 import 'package:analyzer/src/services/lint.dart'; |
| (...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1182 int endOffsetOld = math.max(firstOffsetOld, lastOffsetOld); | 1182 int endOffsetOld = math.max(firstOffsetOld, lastOffsetOld); |
| 1183 int beginOffsetNew = math.min(firstOffsetNew, lastOffsetNew); | 1183 int beginOffsetNew = math.min(firstOffsetNew, lastOffsetNew); |
| 1184 int endOffsetNew = math.max(firstOffsetNew, lastOffsetNew); | 1184 int endOffsetNew = math.max(firstOffsetNew, lastOffsetNew); |
| 1185 // check for a whitespace only change | 1185 // check for a whitespace only change |
| 1186 if (identical(lastPair.oldToken, firstPair.oldToken) && | 1186 if (identical(lastPair.oldToken, firstPair.oldToken) && |
| 1187 identical(lastPair.newToken, firstPair.newToken)) { | 1187 identical(lastPair.newToken, firstPair.newToken)) { |
| 1188 _updateOffset = beginOffsetOld - 1; | 1188 _updateOffset = beginOffsetOld - 1; |
| 1189 _updateEndOld = endOffsetOld; | 1189 _updateEndOld = endOffsetOld; |
| 1190 _updateEndNew = endOffsetNew; | 1190 _updateEndNew = endOffsetNew; |
| 1191 _updateDelta = newUnit.length - _oldUnit.length; | 1191 _updateDelta = newUnit.length - _oldUnit.length; |
| 1192 // A comment change. | |
| 1193 if (firstPair.kind == _TokenDifferenceKind.COMMENT) { | |
| 1194 bool success = _resolveComment(newUnit, firstPair); | |
| 1195 logger.log('Comment change: $success'); | |
| 1196 return success; | |
| 1197 } | |
| 1198 // A Dart documentation comment change. | 1192 // A Dart documentation comment change. |
| 1199 if (firstPair.kind == _TokenDifferenceKind.COMMENT_DOC) { | 1193 if (firstPair.kind == _TokenDifferenceKind.COMMENT_DOC) { |
| 1200 bool success = _resolveCommentDoc(newUnit, firstPair); | 1194 bool success = _resolveCommentDoc(newUnit, firstPair); |
| 1201 logger.log('Documentation comment resolved: $success'); | 1195 logger.log('Documentation comment resolved: $success'); |
| 1202 return success; | 1196 return success; |
| 1203 } | 1197 } |
| 1204 // A pure whitespace change. | 1198 // A pure whitespace change. |
| 1205 if (firstPair.kind == _TokenDifferenceKind.OFFSET) { | 1199 if (firstPair.kind == _TokenDifferenceKind.OFFSET) { |
| 1206 logger.log('Whitespace change.'); | 1200 logger.log('Whitespace change.'); |
| 1207 _shiftTokens(firstPair.oldToken); | 1201 _shiftTokens(firstPair.oldToken); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1247 oldNode = oldParent; | 1241 oldNode = oldParent; |
| 1248 newNode = newParent; | 1242 newNode = newParent; |
| 1249 found = true; | 1243 found = true; |
| 1250 break; | 1244 break; |
| 1251 } | 1245 } |
| 1252 } | 1246 } |
| 1253 if (!found) { | 1247 if (!found) { |
| 1254 logger.log('Failure: no enclosing function body or executable.'); | 1248 logger.log('Failure: no enclosing function body or executable.'); |
| 1255 return false; | 1249 return false; |
| 1256 } | 1250 } |
| 1251 // fail if a comment change outside the bodies |
| 1252 if (firstPair.kind == _TokenDifferenceKind.COMMENT) { |
| 1253 if (beginOffsetOld <= oldNode.offset || beginOffsetNew <= newNode.of
fset) { |
| 1254 logger.log('Failure: comment outside a function body.'); |
| 1255 return false; |
| 1256 } |
| 1257 } |
| 1257 } | 1258 } |
| 1258 logger.log(() => 'oldNode: $oldNode'); | 1259 logger.log(() => 'oldNode: $oldNode'); |
| 1259 logger.log(() => 'newNode: $newNode'); | 1260 logger.log(() => 'newNode: $newNode'); |
| 1260 // prepare update range | 1261 // prepare update range |
| 1261 _updateOffset = oldNode.offset; | 1262 _updateOffset = oldNode.offset; |
| 1262 _updateEndOld = oldNode.end; | 1263 _updateEndOld = oldNode.end; |
| 1263 _updateEndNew = newNode.end; | 1264 _updateEndNew = newNode.end; |
| 1264 _updateDelta = _updateEndNew - _updateEndOld; | 1265 _updateDelta = _updateEndNew - _updateEndOld; |
| 1265 // replace node | 1266 // replace node |
| 1266 NodeReplacer.replace(oldNode, newNode); | 1267 NodeReplacer.replace(oldNode, newNode); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1308 Parser parser = new Parser(_unitSource, errorListener); | 1309 Parser parser = new Parser(_unitSource, errorListener); |
| 1309 CompilationUnit unit = parser.parseCompilationUnit(token); | 1310 CompilationUnit unit = parser.parseCompilationUnit(token); |
| 1310 _newParseErrors = errorListener.errors; | 1311 _newParseErrors = errorListener.errors; |
| 1311 return unit; | 1312 return unit; |
| 1312 } finally { | 1313 } finally { |
| 1313 timer.stop('parse'); | 1314 timer.stop('parse'); |
| 1314 } | 1315 } |
| 1315 } | 1316 } |
| 1316 | 1317 |
| 1317 /** | 1318 /** |
| 1318 * Attempts to resolve a comment change. | |
| 1319 * Returns `true` if success. | |
| 1320 */ | |
| 1321 bool _resolveComment(CompilationUnit newUnit, _TokenPair firstPair) { | |
| 1322 Token oldToken = firstPair.oldToken; | |
| 1323 Token newToken = firstPair.newToken; | |
| 1324 CommentToken newComments = newToken.precedingComments; | |
| 1325 // update token references | |
| 1326 _updateOffset = oldToken.offset - 1; | |
| 1327 _shiftTokens(firstPair.oldToken); | |
| 1328 _setPrecedingComments(oldToken, newComments); | |
| 1329 // update elements | |
| 1330 IncrementalResolver incrementalResolver = new IncrementalResolver( | |
| 1331 _unitElement, _updateOffset, _updateEndOld, _updateEndNew); | |
| 1332 incrementalResolver._updateElementNameOffsets(); | |
| 1333 incrementalResolver._shiftEntryErrors(); | |
| 1334 _updateEntry(); | |
| 1335 // OK | |
| 1336 return true; | |
| 1337 } | |
| 1338 | |
| 1339 /** | |
| 1340 * Attempts to resolve a documentation comment change. | 1319 * Attempts to resolve a documentation comment change. |
| 1341 * Returns `true` if success. | 1320 * Returns `true` if success. |
| 1342 */ | 1321 */ |
| 1343 bool _resolveCommentDoc(CompilationUnit newUnit, _TokenPair firstPair) { | 1322 bool _resolveCommentDoc(CompilationUnit newUnit, _TokenPair firstPair) { |
| 1344 Token oldToken = firstPair.oldToken; | 1323 Token oldToken = firstPair.oldToken; |
| 1345 Token newToken = firstPair.newToken; | 1324 Token newToken = firstPair.newToken; |
| 1346 CommentToken oldComments = oldToken.precedingComments; | 1325 CommentToken oldComments = oldToken.precedingComments; |
| 1347 CommentToken newComments = newToken.precedingComments; | 1326 CommentToken newComments = newToken.precedingComments; |
| 1348 if (oldComments == null || newComments == null) { | 1327 if (oldComments == null || newComments == null) { |
| 1349 return false; | 1328 return false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1383 } | 1362 } |
| 1384 | 1363 |
| 1385 /** | 1364 /** |
| 1386 * Set the given [comment] as a "precedingComments" for [token]. | 1365 * Set the given [comment] as a "precedingComments" for [token]. |
| 1387 */ | 1366 */ |
| 1388 void _setPrecedingComments(Token token, CommentToken comment) { | 1367 void _setPrecedingComments(Token token, CommentToken comment) { |
| 1389 if (token is BeginTokenWithComment) { | 1368 if (token is BeginTokenWithComment) { |
| 1390 token.precedingComments = comment; | 1369 token.precedingComments = comment; |
| 1391 } else if (token is KeywordTokenWithComment) { | 1370 } else if (token is KeywordTokenWithComment) { |
| 1392 token.precedingComments = comment; | 1371 token.precedingComments = comment; |
| 1393 } else if (token is KeywordToken) { | |
| 1394 KeywordTokenWithComment newToken = | |
| 1395 new KeywordTokenWithComment(token.keyword, token.offset, comment); | |
| 1396 token.previous.setNext(newToken); | |
| 1397 newToken.setNext(token.next); | |
| 1398 if (_oldUnit.beginToken == token) { | |
| 1399 _oldUnit.beginToken = newToken; | |
| 1400 } | |
| 1401 } else if (token is StringTokenWithComment) { | 1372 } else if (token is StringTokenWithComment) { |
| 1402 token.precedingComments = comment; | 1373 token.precedingComments = comment; |
| 1403 } else if (token is StringToken) { | |
| 1404 StringTokenWithComment newToken = new StringTokenWithComment( | |
| 1405 token.type, token.value(), token.offset, comment); | |
| 1406 token.previous.setNext(newToken); | |
| 1407 newToken.setNext(token.next); | |
| 1408 if (_oldUnit.beginToken == token) { | |
| 1409 _oldUnit.beginToken = newToken; | |
| 1410 } | |
| 1411 } else if (token is TokenWithComment) { | 1374 } else if (token is TokenWithComment) { |
| 1412 token.precedingComments = comment; | 1375 token.precedingComments = comment; |
| 1413 } else { | 1376 } else { |
| 1414 Type parentType = token != null ? token.runtimeType : null; | 1377 Type parentType = token != null ? token.runtimeType : null; |
| 1415 throw new AnalysisException('Uknown parent token type: $parentType'); | 1378 throw new AnalysisException('Uknown parent token type: $parentType'); |
| 1416 } | 1379 } |
| 1417 } | 1380 } |
| 1418 | 1381 |
| 1419 void _shiftTokens(Token token) { | 1382 void _shiftTokens(Token token) { |
| 1420 while (token != null) { | 1383 while (token != null) { |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1842 @override | 1805 @override |
| 1843 String toString() => name; | 1806 String toString() => name; |
| 1844 } | 1807 } |
| 1845 | 1808 |
| 1846 class _TokenPair { | 1809 class _TokenPair { |
| 1847 final _TokenDifferenceKind kind; | 1810 final _TokenDifferenceKind kind; |
| 1848 final Token oldToken; | 1811 final Token oldToken; |
| 1849 final Token newToken; | 1812 final Token newToken; |
| 1850 _TokenPair(this.kind, this.oldToken, this.newToken); | 1813 _TokenPair(this.kind, this.oldToken, this.newToken); |
| 1851 } | 1814 } |
| OLD | NEW |