| Index: pkg/analysis_server/lib/src/services/correction/util.dart
|
| diff --git a/pkg/analysis_server/lib/src/services/correction/util.dart b/pkg/analysis_server/lib/src/services/correction/util.dart
|
| index c19a4c8e55b3f03c656f8960fde9b28b8acc74db..c40907dbf2d3753af8eda528a26adbdd39ede252 100644
|
| --- a/pkg/analysis_server/lib/src/services/correction/util.dart
|
| +++ b/pkg/analysis_server/lib/src/services/correction/util.dart
|
| @@ -27,7 +27,8 @@ import 'package:path/path.dart';
|
| */
|
| void addLibraryImports(SourceChange change, LibraryElement targetLibrary,
|
| Set<LibraryElement> libraries) {
|
| - CompilationUnit libUnit = targetLibrary.definingCompilationUnit.node;
|
| + CompilationUnitElement libUnitElement = targetLibrary.definingCompilationUnit;
|
| + CompilationUnit libUnit = getParsedUnit(libUnitElement);
|
| // prepare new import location
|
| int offset = 0;
|
| String prefix;
|
| @@ -113,6 +114,16 @@ String findAbsoluteUri(AnalysisContext context, String path) {
|
| }
|
|
|
| /**
|
| + * Returns the EOL to use for the given [code].
|
| + */
|
| +String getCodeEndOfLine(String code) {
|
| + if (code.contains('\r\n')) {
|
| + return '\r\n';
|
| + }
|
| + return '\n';
|
| +}
|
| +
|
| +/**
|
| * TODO(scheglov) replace with nodes once there will be [CompilationUnit.getComments].
|
| *
|
| * Returns [SourceRange]s of all comments in [unit].
|
| @@ -419,6 +430,41 @@ List<AstNode> getParents(AstNode node) {
|
| }
|
|
|
| /**
|
| + * Returns a parsed [AstNode] for the given [classElement].
|
| + *
|
| + * The resulting AST structure may or may not be resolved.
|
| + */
|
| +AstNode getParsedClassElementNode(ClassElement classElement) {
|
| + CompilationUnitElement unitElement =
|
| + classElement.getAncestor((e) => e is CompilationUnitElement);
|
| + CompilationUnit unit = getParsedUnit(unitElement);
|
| + int offset = classElement.nameOffset;
|
| + AstNode classNameNode = new NodeLocator.con1(offset).searchWithin(unit);
|
| + if (classElement.isEnum) {
|
| + return classNameNode.getAncestor((node) => node is EnumDeclaration);
|
| + } else {
|
| + return classNameNode.getAncestor(
|
| + (node) => node is ClassDeclaration || node is ClassTypeAlias);
|
| + }
|
| +}
|
| +
|
| +/**
|
| + * Returns a parsed [CompilationUnit] for the given [unitElement].
|
| + *
|
| + * The resulting AST structure may or may not be resolved.
|
| + * If it is not resolved, then at least the given [unitElement] will be set.
|
| + */
|
| +CompilationUnit getParsedUnit(CompilationUnitElement unitElement) {
|
| + AnalysisContext context = unitElement.context;
|
| + Source source = unitElement.source;
|
| + CompilationUnit unit = context.parseCompilationUnit(source);
|
| + if (unit.element == null) {
|
| + unit.element = unitElement;
|
| + }
|
| + return unit;
|
| +}
|
| +
|
| +/**
|
| * Returns a [PropertyAccessorElement] if the given [SimpleIdentifier] is a
|
| * reference to a property, or `null` in the other case.
|
| */
|
|
|