| Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| index 3d7e1a2f35ccb77a1b964bb3fd3173aa25518c9d..3c8f1a748ee7919649fe89e9d4a93e591ad06179 100644
|
| --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
|
| @@ -5,6 +5,7 @@
|
| library analysis_server.src.services.correction.fix_internal;
|
|
|
| import 'dart:collection';
|
| +import 'dart:core' hide Resource;
|
|
|
| import 'package:analysis_server/edit/fix/fix_core.dart';
|
| import 'package:analysis_server/edit/fix/fix_dart.dart';
|
| @@ -22,6 +23,7 @@ import 'package:analysis_server/src/services/correction/source_range.dart'
|
| import 'package:analysis_server/src/services/correction/strings.dart';
|
| import 'package:analysis_server/src/services/correction/util.dart';
|
| import 'package:analysis_server/src/services/search/hierarchy.dart';
|
| +import 'package:analyzer/file_system/file_system.dart';
|
| import 'package:analyzer/src/generated/ast.dart';
|
| import 'package:analyzer/src/generated/element.dart';
|
| import 'package:analyzer/src/generated/engine.dart';
|
| @@ -33,6 +35,7 @@ import 'package:analyzer/src/generated/sdk.dart';
|
| import 'package:analyzer/src/generated/source.dart';
|
| import 'package:analyzer/src/generated/utilities_dart.dart';
|
| import 'package:path/path.dart';
|
| +import 'package:path/path.dart' as ppp;
|
|
|
| /**
|
| * A predicate is a one-argument function that returns a boolean value.
|
| @@ -44,8 +47,9 @@ typedef bool ElementPredicate(Element argument);
|
| */
|
| class DefaultFixContributor extends DartFixContributor {
|
| @override
|
| - List<Fix> internalComputeFixes(CompilationUnit unit, AnalysisError error) {
|
| - FixProcessor processor = new FixProcessor(unit, error);
|
| + List<Fix> internalComputeFixes(ResourceProvider resourceProvider,
|
| + CompilationUnit unit, AnalysisError error) {
|
| + FixProcessor processor = new FixProcessor(resourceProvider, unit, error);
|
| return processor.compute();
|
| }
|
| }
|
| @@ -56,6 +60,7 @@ class DefaultFixContributor extends DartFixContributor {
|
| class FixProcessor {
|
| static const int MAX_LEVENSHTEIN_DISTANCE = 3;
|
|
|
| + final ResourceProvider resourceProvider;
|
| final CompilationUnit unit;
|
| final AnalysisError error;
|
| AnalysisContext context;
|
| @@ -83,7 +88,7 @@ class FixProcessor {
|
| AstNode node;
|
| AstNode coveredNode;
|
|
|
| - FixProcessor(this.unit, this.error) {
|
| + FixProcessor(this.resourceProvider, this.unit, this.error) {
|
| unitElement = unit.element;
|
| context = unitElement.context;
|
| unitSource = unitElement.source;
|
| @@ -1054,8 +1059,7 @@ class FixProcessor {
|
| if (source != null) {
|
| String file = source.fullName;
|
| if (isAbsolute(file)) {
|
| - String libName = removeEnd(source.shortName, '.dart');
|
| - libName = libName.replaceAll('_', '.');
|
| + String libName = _computeLibraryName(file);
|
| SourceEdit edit = new SourceEdit(0, 0, 'library $libName;$eol$eol');
|
| doSourceChange_addSourceEdit(change, context, source, edit);
|
| _addFix(DartFixKind.CREATE_FILE, [source.shortName]);
|
| @@ -2292,6 +2296,51 @@ class FixProcessor {
|
| }
|
|
|
| /**
|
| + * Computes the name of the library at the given [path].
|
| + * See https://www.dartlang.org/articles/style-guide/#names for conventions.
|
| + */
|
| + String _computeLibraryName(String path) {
|
| + Context pathContext = resourceProvider.pathContext;
|
| + String packageFolder = _computePackageFolder(path);
|
| + if (packageFolder == null) {
|
| + return pathContext.basenameWithoutExtension(path);
|
| + }
|
| + String packageName = pathContext.basename(packageFolder);
|
| + String relPath = pathContext.relative(path, from: packageFolder);
|
| + List<String> relPathParts = pathContext.split(relPath);
|
| + if (relPathParts.isNotEmpty) {
|
| + if (relPathParts[0].toLowerCase() == 'lib') {
|
| + relPathParts.removeAt(0);
|
| + }
|
| + {
|
| + String nameWithoutExt = pathContext.withoutExtension(relPathParts.last);
|
| + relPathParts[relPathParts.length - 1] = nameWithoutExt;
|
| + }
|
| + }
|
| + return packageName + '.' + relPathParts.join('.');
|
| + }
|
| +
|
| + /**
|
| + * Returns the path of the folder which contains the given [path].
|
| + */
|
| + String _computePackageFolder(String path) {
|
| + Context pathContext = resourceProvider.pathContext;
|
| + String pubspecFolder = dirname(path);
|
| + while (true) {
|
| + if (resourceProvider
|
| + .getResource(pathContext.join(pubspecFolder, 'pubspec.yaml'))
|
| + .exists) {
|
| + return pubspecFolder;
|
| + }
|
| + String pubspecFolderNew = pathContext.dirname(pubspecFolder);
|
| + if (pubspecFolderNew == pubspecFolder) {
|
| + return null;
|
| + }
|
| + pubspecFolder = pubspecFolderNew;
|
| + }
|
| + }
|
| +
|
| + /**
|
| * @return the string to display as the name of the given constructor in a proposal name.
|
| */
|
| String _getConstructorProposalName(ConstructorElement constructor) {
|
|
|