Chromium Code Reviews| 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; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 import 'package:analyzer/src/generated/resolver.dart'; | 24 import 'package:analyzer/src/generated/resolver.dart'; |
| 25 import 'package:analyzer/src/generated/source.dart'; | 25 import 'package:analyzer/src/generated/source.dart'; |
| 26 import 'package:path/path.dart'; | 26 import 'package:path/path.dart'; |
| 27 | 27 |
| 28 /** | 28 /** |
| 29 * Adds edits to the given [change] that ensure that all the [libraries] are | 29 * Adds edits to the given [change] that ensure that all the [libraries] are |
| 30 * imported into the given [targetLibrary]. | 30 * imported into the given [targetLibrary]. |
| 31 */ | 31 */ |
| 32 void addLibraryImports(SourceChange change, LibraryElement targetLibrary, | 32 void addLibraryImports(SourceChange change, LibraryElement targetLibrary, |
| 33 Set<LibraryElement> libraries) { | 33 Set<LibraryElement> libraries) { |
| 34 CompilationUnitElement libUnitElement = targetLibrary.definingCompilationUnit; | 34 CorrectionUtils libUtils; |
| 35 CompilationUnit libUnit = getParsedUnit(libUnitElement); | 35 try { |
| 36 CorrectionUtils libUtils = new CorrectionUtils(libUnit); | 36 CompilationUnitElement unitElement = targetLibrary.definingCompilationUnit; |
| 37 CompilationUnit unitAst = getParsedUnit(unitElement); | |
| 38 libUtils = new CorrectionUtils(unitAst); | |
| 39 } catch (e) { | |
| 40 throw new CancelCorrectionException(exception: e); | |
| 41 } | |
| 37 String eol = libUtils.endOfLine; | 42 String eol = libUtils.endOfLine; |
| 38 // Prepare information about existing imports. | 43 // Prepare information about existing imports. |
| 39 LibraryDirective libraryDirective; | 44 LibraryDirective libraryDirective; |
| 40 List<_ImportDirectiveInfo> importDirectives = <_ImportDirectiveInfo>[]; | 45 List<_ImportDirectiveInfo> importDirectives = <_ImportDirectiveInfo>[]; |
| 41 for (Directive directive in libUnit.directives) { | 46 for (Directive directive in libUtils.unit.directives) { |
| 42 if (directive is LibraryDirective) { | 47 if (directive is LibraryDirective) { |
| 43 libraryDirective = directive; | 48 libraryDirective = directive; |
| 44 } else if (directive is ImportDirective) { | 49 } else if (directive is ImportDirective) { |
| 45 importDirectives.add(new _ImportDirectiveInfo( | 50 importDirectives.add(new _ImportDirectiveInfo( |
| 46 directive.uriContent, directive.offset, directive.end)); | 51 directive.uriContent, directive.offset, directive.end)); |
| 47 } | 52 } |
| 48 } | 53 } |
| 49 | 54 |
| 50 // Prepare all URIs to import. | 55 // Prepare all URIs to import. |
| 51 List<String> uriList = libraries | 56 List<String> uriList = libraries |
| (...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 if (expression != null) { | 662 if (expression != null) { |
| 658 AstNode parent = expression.parent; | 663 AstNode parent = expression.parent; |
| 659 if (parent is NamedExpression && parent.expression == expression) { | 664 if (parent is NamedExpression && parent.expression == expression) { |
| 660 return parent; | 665 return parent; |
| 661 } | 666 } |
| 662 } | 667 } |
| 663 return expression; | 668 return expression; |
| 664 } | 669 } |
| 665 | 670 |
| 666 /** | 671 /** |
| 672 * This exception is thrown to cancel the current correction operation, | |
| 673 * such as quick assist or quick fix because an inconsistency was detected. | |
| 674 * These inconsistencies may happen as a part of normal workflow, e.g. because | |
| 675 * a resource was deleted, or an analysis result was invalidated. | |
| 676 */ | |
| 677 class CancelCorrectionException { | |
| 678 final Object exception; | |
| 679 CancelCorrectionException({this.exception}); | |
| 680 } | |
| 681 | |
| 682 /** | |
| 667 * Describes the location for a newly created [ClassMember]. | 683 * Describes the location for a newly created [ClassMember]. |
| 668 */ | 684 */ |
| 669 class ClassMemberLocation { | 685 class ClassMemberLocation { |
| 670 final String prefix; | 686 final String prefix; |
| 671 final int offset; | 687 final int offset; |
| 672 final String suffix; | 688 final String suffix; |
| 673 | 689 |
| 674 ClassMemberLocation(this.prefix, this.offset, this.suffix); | 690 ClassMemberLocation(this.prefix, this.offset, this.suffix); |
| 675 } | 691 } |
| 676 | 692 |
| 677 class CorrectionUtils { | 693 class CorrectionUtils { |
| 678 final CompilationUnit unit; | 694 final CompilationUnit unit; |
| 679 | 695 |
| 680 /** | 696 /** |
| 681 * The [ClassElement] the generated code is inserted to, so we can decide if | 697 * The [ClassElement] the generated code is inserted to, so we can decide if |
| 682 * a type parameter may or may not be used. | 698 * a type parameter may or may not be used. |
| 683 */ | 699 */ |
| 684 ClassElement targetClassElement; | 700 ClassElement targetClassElement; |
| 685 | 701 |
| 686 LibraryElement _library; | 702 LibraryElement _library; |
| 687 String _buffer; | 703 String _buffer; |
| 688 String _endOfLine; | 704 String _endOfLine; |
| 689 | 705 |
| 690 CorrectionUtils(this.unit) { | 706 CorrectionUtils(this.unit) { |
| 691 CompilationUnitElement unitElement = unit.element; | 707 CompilationUnitElement unitElement = unit.element; |
| 708 AnalysisContext context = unitElement.context; | |
| 709 if (context == null) { | |
| 710 throw new CancelCorrectionException(); | |
|
Brian Wilkerson
2016/10/05 13:52:20
How often does this occur? Should we log the issue
| |
| 711 } | |
| 692 this._library = unitElement.library; | 712 this._library = unitElement.library; |
| 693 this._buffer = unitElement.context.getContents(unitElement.source).data; | 713 this._buffer = context.getContents(unitElement.source).data; |
| 694 } | 714 } |
| 695 | 715 |
| 696 /** | 716 /** |
| 697 * Returns the EOL to use for this [CompilationUnit]. | 717 * Returns the EOL to use for this [CompilationUnit]. |
| 698 */ | 718 */ |
| 699 String get endOfLine { | 719 String get endOfLine { |
| 700 if (_endOfLine == null) { | 720 if (_endOfLine == null) { |
| 701 if (_buffer.contains("\r\n")) { | 721 if (_buffer.contains("\r\n")) { |
| 702 _endOfLine = "\r\n"; | 722 _endOfLine = "\r\n"; |
| 703 } else { | 723 } else { |
| (...skipping 927 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1631 _InvertedCondition expr, int newOperatorPrecedence) { | 1651 _InvertedCondition expr, int newOperatorPrecedence) { |
| 1632 if (expr._precedence < newOperatorPrecedence) { | 1652 if (expr._precedence < newOperatorPrecedence) { |
| 1633 return "(${expr._source})"; | 1653 return "(${expr._source})"; |
| 1634 } | 1654 } |
| 1635 return expr._source; | 1655 return expr._source; |
| 1636 } | 1656 } |
| 1637 | 1657 |
| 1638 static _InvertedCondition _simple(String source) => | 1658 static _InvertedCondition _simple(String source) => |
| 1639 new _InvertedCondition(2147483647, source); | 1659 new _InvertedCondition(2147483647, source); |
| 1640 } | 1660 } |
| OLD | NEW |