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 services.src.correction.util; | 5 library services.src.correction.util; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 import 'package:analysis_server/plugin/protocol/protocol.dart' | 9 import 'package:analysis_server/plugin/protocol/protocol.dart' |
10 show SourceChange, SourceEdit; | 10 show SourceChange, SourceEdit; |
11 import 'package:analysis_server/src/protocol_server.dart' | 11 import 'package:analysis_server/src/protocol_server.dart' |
12 show doSourceChange_addElementEdit; | 12 show doSourceChange_addElementEdit; |
13 import 'package:analysis_server/src/services/correction/source_range.dart'; | 13 import 'package:analysis_server/src/services/correction/source_range.dart'; |
14 import 'package:analysis_server/src/services/correction/strings.dart'; | 14 import 'package:analysis_server/src/services/correction/strings.dart'; |
15 import 'package:analysis_server/src/services/search/element_visitors.dart'; | |
16 import 'package:analyzer/dart/element/element.dart'; | 15 import 'package:analyzer/dart/element/element.dart'; |
17 import 'package:analyzer/dart/element/type.dart'; | 16 import 'package:analyzer/dart/element/type.dart'; |
18 import 'package:analyzer/src/generated/ast.dart'; | 17 import 'package:analyzer/src/generated/ast.dart'; |
19 import 'package:analyzer/src/generated/engine.dart'; | 18 import 'package:analyzer/src/generated/engine.dart'; |
20 import 'package:analyzer/src/generated/resolver.dart'; | 19 import 'package:analyzer/src/generated/resolver.dart'; |
21 import 'package:analyzer/src/generated/scanner.dart'; | 20 import 'package:analyzer/src/generated/scanner.dart'; |
22 import 'package:analyzer/src/generated/source.dart'; | 21 import 'package:analyzer/src/generated/source.dart'; |
23 import 'package:path/path.dart'; | 22 import 'package:path/path.dart'; |
24 | 23 |
25 /** | 24 /** |
(...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 | 660 |
662 /** | 661 /** |
663 * Returns names of elements that might conflict with a new local variable | 662 * Returns names of elements that might conflict with a new local variable |
664 * declared at [offset]. | 663 * declared at [offset]. |
665 */ | 664 */ |
666 Set<String> findPossibleLocalVariableConflicts(int offset) { | 665 Set<String> findPossibleLocalVariableConflicts(int offset) { |
667 Set<String> conflicts = new Set<String>(); | 666 Set<String> conflicts = new Set<String>(); |
668 AstNode enclosingNode = findNode(offset); | 667 AstNode enclosingNode = findNode(offset); |
669 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); | 668 Block enclosingBlock = enclosingNode.getAncestor((node) => node is Block); |
670 if (enclosingBlock != null) { | 669 if (enclosingBlock != null) { |
671 SourceRange newRange = rangeStartEnd(offset, enclosingBlock.end); | 670 _CollectReferencedUnprefixedNames visitor = new _CollectReferencedUnprefix
edNames(); |
672 ExecutableElement enclosingExecutable = | 671 enclosingBlock.accept(visitor); |
673 getEnclosingExecutableElement(enclosingNode); | 672 return visitor.names; |
674 if (enclosingExecutable != null) { | |
675 visitChildren(enclosingExecutable, (Element element) { | |
676 if (element is LocalElement) { | |
677 SourceRange elementRange = element.visibleRange; | |
678 if (elementRange != null && elementRange.intersects(newRange)) { | |
679 conflicts.add(element.displayName); | |
680 } | |
681 } | |
682 return true; | |
683 }); | |
684 } | |
685 } | 673 } |
686 return conflicts; | 674 return conflicts; |
687 } | 675 } |
688 | 676 |
689 /** | 677 /** |
690 * Returns the actual type source of the given [Expression], may be `null` | 678 * Returns the actual type source of the given [Expression], may be `null` |
691 * if can not be resolved, should be treated as the `dynamic` type. | 679 * if can not be resolved, should be treated as the `dynamic` type. |
692 */ | 680 */ |
693 String getExpressionTypeSource( | 681 String getExpressionTypeSource( |
694 Expression expression, Set<LibraryElement> librariesToImport) { | 682 Expression expression, Set<LibraryElement> librariesToImport) { |
(...skipping 735 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1430 } | 1418 } |
1431 | 1419 |
1432 /** | 1420 /** |
1433 * @return <code>true</code> if given [Token]s contain only single [Token] wit
h given | 1421 * @return <code>true</code> if given [Token]s contain only single [Token] wit
h given |
1434 * [TokenType]. | 1422 * [TokenType]. |
1435 */ | 1423 */ |
1436 static bool hasOnly(List<Token> tokens, TokenType type) => | 1424 static bool hasOnly(List<Token> tokens, TokenType type) => |
1437 tokens.length == 1 && tokens[0].type == type; | 1425 tokens.length == 1 && tokens[0].type == type; |
1438 } | 1426 } |
1439 | 1427 |
| 1428 class _CollectReferencedUnprefixedNames extends RecursiveAstVisitor { |
| 1429 final Set<String> names = new Set<String>(); |
| 1430 |
| 1431 void visitSimpleIdentifier(SimpleIdentifier node) { |
| 1432 if (!_isPrefixed(node)) { |
| 1433 names.add(node.name); |
| 1434 } |
| 1435 } |
| 1436 |
| 1437 static bool _isPrefixed(SimpleIdentifier node) { |
| 1438 AstNode parent = node.parent; |
| 1439 return parent is ConstructorName && parent.name == node || |
| 1440 parent is MethodInvocation && |
| 1441 parent.methodName == node && |
| 1442 parent.realTarget != null || |
| 1443 parent is PrefixedIdentifier && parent.identifier == node || |
| 1444 parent is PropertyAccess && parent.target == node; |
| 1445 } |
| 1446 } |
| 1447 |
1440 /** | 1448 /** |
1441 * A container with a source and its precedence. | 1449 * A container with a source and its precedence. |
1442 */ | 1450 */ |
1443 class _InvertedCondition { | 1451 class _InvertedCondition { |
1444 final int _precedence; | 1452 final int _precedence; |
1445 | 1453 |
1446 final String _source; | 1454 final String _source; |
1447 | 1455 |
1448 _InvertedCondition(this._precedence, this._source); | 1456 _InvertedCondition(this._precedence, this._source); |
1449 | 1457 |
(...skipping 20 matching lines...) Expand all Loading... |
1470 _InvertedCondition expr, int newOperatorPrecedence) { | 1478 _InvertedCondition expr, int newOperatorPrecedence) { |
1471 if (expr._precedence < newOperatorPrecedence) { | 1479 if (expr._precedence < newOperatorPrecedence) { |
1472 return "(${expr._source})"; | 1480 return "(${expr._source})"; |
1473 } | 1481 } |
1474 return expr._source; | 1482 return expr._source; |
1475 } | 1483 } |
1476 | 1484 |
1477 static _InvertedCondition _simple(String source) => | 1485 static _InvertedCondition _simple(String source) => |
1478 new _InvertedCondition(2147483647, source); | 1486 new _InvertedCondition(2147483647, source); |
1479 } | 1487 } |
OLD | NEW |