Skip to content

Commit 143e5ef

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Revert 'Import Library' quick fix changes.
This basically reverts https://dart-review.googlesource.com/c/sdk/+/103921 because internally IntelliJ does not provide module dependencies yet. Change-Id: I7717b2841bf3d6391b991875a594c6df9e246ff1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106482 Reviewed-by: Ari Aye <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 4adaa1f commit 143e5ef

17 files changed

Lines changed: 796 additions & 197 deletions

File tree

pkg/analysis_server/lib/plugin/edit/fix/fix_dart.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
6-
import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart';
76
import 'package:analyzer/dart/analysis/results.dart';
87
import 'package:analyzer_plugin/utilities/change_builder/change_workspace.dart';
98

@@ -22,10 +21,4 @@ abstract class DartFixContext implements FixContext {
2221
* The workspace in which the fix contributor operates.
2322
*/
2423
ChangeWorkspace get workspace;
25-
26-
/**
27-
* Return top-level declarations with the [name] in libraries that are
28-
* available to this context.
29-
*/
30-
List<TopLevelDeclaration> getTopLevelDeclarations(String name);
3124
}

pkg/analysis_server/lib/src/edit/edit_domain.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import 'package:analysis_server/src/services/correction/assist_internal.dart';
2323
import 'package:analysis_server/src/services/correction/change_workspace.dart';
2424
import 'package:analysis_server/src/services/correction/fix.dart';
2525
import 'package:analysis_server/src/services/correction/fix/analysis_options/fix_generator.dart';
26-
import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart';
2726
import 'package:analysis_server/src/services/correction/fix/manifest/fix_generator.dart';
2827
import 'package:analysis_server/src/services/correction/fix/pubspec/fix_generator.dart';
2928
import 'package:analysis_server/src/services/correction/fix_internal.dart';
@@ -635,16 +634,7 @@ class EditDomainHandler extends AbstractRequestHandler {
635634
int errorLine = lineInfo.getLocation(error.offset).lineNumber;
636635
if (errorLine == requestLine) {
637636
var workspace = DartChangeWorkspace(server.currentSessions);
638-
var context =
639-
new DartFixContextImpl(workspace, result, error, (name) {
640-
var tracker = server.declarationsTracker;
641-
var provider = TopLevelDeclarationsProvider(tracker);
642-
return provider.get(
643-
result.session.analysisContext,
644-
result.path,
645-
name,
646-
);
647-
});
637+
var context = new DartFixContextImpl(workspace, result, error);
648638
List<Fix> fixes =
649639
await new DartFixContributor().computeFixes(context);
650640
if (fixes.isNotEmpty) {

pkg/analysis_server/lib/src/edit/fix/fix_error_task.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ class FixErrorTask {
2828

2929
Future<void> fixError(ResolvedUnitResult result, AnalysisError error) async {
3030
final workspace = DartChangeWorkspace(listener.server.currentSessions);
31-
final dartContext = new DartFixContextImpl(
32-
workspace,
33-
result,
34-
error,
35-
(name) => [],
36-
);
31+
final dartContext = new DartFixContextImpl(workspace, result, error);
3732
final processor = new FixProcessor(dartContext);
3833
Fix fix = await processor.computeFix();
3934
final location = listener.locationFor(result, error.offset, error.length);

pkg/analysis_server/lib/src/lsp/handlers/handler_code_actions.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import 'package:analysis_server/src/services/correction/assist.dart';
1818
import 'package:analysis_server/src/services/correction/assist_internal.dart';
1919
import 'package:analysis_server/src/services/correction/change_workspace.dart';
2020
import 'package:analysis_server/src/services/correction/fix.dart';
21-
import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart';
2221
import 'package:analysis_server/src/services/correction/fix_internal.dart';
2322
import 'package:analysis_server/src/services/refactoring/refactoring.dart';
2423
import 'package:analyzer/dart/analysis/results.dart';
@@ -195,14 +194,7 @@ class CodeActionHandler extends MessageHandler<CodeActionParams,
195194
int errorLine = lineInfo.getLocation(error.offset).lineNumber - 1;
196195
if (errorLine >= range.start.line && errorLine <= range.end.line) {
197196
var workspace = DartChangeWorkspace(server.currentSessions);
198-
var context = new DartFixContextImpl(workspace, unit, error, (name) {
199-
var tracker = server.declarationsTracker;
200-
return TopLevelDeclarationsProvider(tracker).get(
201-
unit.session.analysisContext,
202-
unit.path,
203-
name,
204-
);
205-
});
197+
var context = new DartFixContextImpl(workspace, unit, error);
206198
final fixes = await fixContributor.computeFixes(context);
207199
if (fixes.isNotEmpty) {
208200
fixes.sort(Fix.SORT_BY_RELEVANCE);

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
6-
import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart';
76
import 'package:analysis_server/src/services/correction/fix_internal.dart';
87
import 'package:analyzer/dart/analysis/results.dart';
98
import 'package:analyzer/error/error.dart';
@@ -120,16 +119,7 @@ class DartFixContextImpl implements DartFixContext {
120119
@override
121120
final AnalysisError error;
122121

123-
final List<TopLevelDeclaration> Function(String name)
124-
getTopLevelDeclarationsFunction;
125-
126-
DartFixContextImpl(this.workspace, this.resolveResult, this.error,
127-
this.getTopLevelDeclarationsFunction);
128-
129-
@override
130-
List<TopLevelDeclaration> getTopLevelDeclarations(String name) {
131-
return getTopLevelDeclarationsFunction(name);
132-
}
122+
DartFixContextImpl(this.workspace, this.resolveResult, this.error);
133123
}
134124

135125
/// An enumeration of quick fix kinds found in a Dart file.

pkg/analysis_server/lib/src/services/correction/fix/dart/top_level_declarations.dart

Lines changed: 0 additions & 107 deletions
This file was deleted.

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
1010
import 'package:analysis_server/plugin/edit/fix/fix_dart.dart';
1111
import 'package:analysis_server/src/services/completion/dart/utilities.dart';
1212
import 'package:analysis_server/src/services/correction/fix.dart';
13-
import 'package:analysis_server/src/services/correction/fix/dart/top_level_declarations.dart';
1413
import 'package:analysis_server/src/services/correction/levenshtein.dart';
1514
import 'package:analysis_server/src/services/correction/namespace.dart';
1615
import 'package:analysis_server/src/services/correction/strings.dart';
@@ -27,6 +26,7 @@ import 'package:analyzer/dart/element/type.dart';
2726
import 'package:analyzer/error/error.dart';
2827
import 'package:analyzer/file_system/file_system.dart';
2928
import 'package:analyzer/src/dart/analysis/session_helper.dart';
29+
import 'package:analyzer/src/dart/analysis/top_level_declaration.dart';
3030
import 'package:analyzer/src/dart/ast/ast.dart';
3131
import 'package:analyzer/src/dart/ast/token.dart';
3232
import 'package:analyzer/src/dart/ast/utilities.dart';
@@ -105,11 +105,7 @@ class DartFixContributor implements FixContributor {
105105
// For each fix, put the fix into the HashMap.
106106
for (int i = 0; i < allAnalysisErrors.length; i++) {
107107
final FixContext fixContextI = new DartFixContextImpl(
108-
context.workspace,
109-
context.resolveResult,
110-
allAnalysisErrors[i],
111-
(name) => [],
112-
);
108+
context.workspace, context.resolveResult, allAnalysisErrors[i]);
113109
final FixProcessor processorI = new FixProcessor(fixContextI);
114110
final List<Fix> fixesListI = await processorI.compute();
115111
for (Fix f in fixesListI) {
@@ -2416,7 +2412,7 @@ class FixProcessor {
24162412
}
24172413
// may be there is an existing import,
24182414
// but it is with prefix and we don't use this prefix
2419-
var alreadyImportedWithPrefix = new Set<String>();
2415+
Set<Source> alreadyImportedWithPrefix = new Set<Source>();
24202416
for (ImportElement imp in unitLibraryElement.imports) {
24212417
// prepare element
24222418
LibraryElement libraryElement = imp.importedLibrary;
@@ -2458,7 +2454,7 @@ class FixProcessor {
24582454
libraryName = libraryElement.source.shortName;
24592455
}
24602456
// don't add this library again
2461-
alreadyImportedWithPrefix.add(libraryElement.source.fullName);
2457+
alreadyImportedWithPrefix.add(libraryElement.source);
24622458
// update library
24632459
String newShowCode = 'show ${showNames.join(', ')}';
24642460
int offset = showCombinator.offset;
@@ -2477,21 +2473,25 @@ class FixProcessor {
24772473
}
24782474
// Find new top-level declarations.
24792475
{
2480-
var declarations = await context.getTopLevelDeclarations(name);
2481-
for (var declaration in declarations) {
2476+
var declarations = await session.getTopLevelDeclarations(name);
2477+
for (TopLevelDeclarationInSource declaration in declarations) {
24822478
// Check the kind.
2483-
if (!kinds2.contains(declaration.kind)) {
2479+
if (!kinds2.contains(declaration.declaration.kind)) {
24842480
continue;
24852481
}
24862482
// Check the source.
2487-
if (alreadyImportedWithPrefix.contains(declaration.path)) {
2483+
Source librarySource = declaration.source;
2484+
if (alreadyImportedWithPrefix.contains(librarySource)) {
2485+
continue;
2486+
}
2487+
if (!_isSourceVisibleToLibrary(librarySource)) {
24882488
continue;
24892489
}
24902490
// Compute the fix kind.
24912491
FixKind fixKind;
2492-
if (declaration.uri.isScheme('dart')) {
2492+
if (librarySource.isInSystemLibrary) {
24932493
fixKind = DartFixKind.IMPORT_LIBRARY_SDK;
2494-
} else if (_isLibSrcPath(declaration.path)) {
2494+
} else if (_isLibSrcPath(librarySource.fullName)) {
24952495
// Bad: non-API.
24962496
fixKind = DartFixKind.IMPORT_LIBRARY_PROJECT3;
24972497
} else if (declaration.isExported) {
@@ -2503,8 +2503,8 @@ class FixProcessor {
25032503
}
25042504
// Add the fix.
25052505
var relativeURI =
2506-
_getRelativeURIFromLibrary(unitLibraryElement, declaration.path);
2507-
await _addFix_importLibrary(fixKind, declaration.uri, relativeURI);
2506+
_getRelativeURIFromLibrary(unitLibraryElement, librarySource);
2507+
await _addFix_importLibrary(fixKind, librarySource.uri, relativeURI);
25082508
}
25092509
}
25102510
}
@@ -4246,20 +4246,21 @@ class FixProcessor {
42464246
}
42474247

42484248
/**
4249-
* Return the relative uri from the passed [library] to the given [path].
4250-
* If the [path] is not in the LibraryElement, `null` is returned.
4249+
* Return the relative uri from the passed [library] to the passed
4250+
* [source]. If the [source] is not in the LibraryElement, `null` is returned.
42514251
*/
4252-
String _getRelativeURIFromLibrary(LibraryElement library, String path) {
4252+
String _getRelativeURIFromLibrary(LibraryElement library, Source source) {
42534253
var librarySource = library?.librarySource;
42544254
if (librarySource == null) {
42554255
return null;
42564256
}
42574257
var pathCtx = resourceProvider.pathContext;
42584258
var libraryDirectory = pathCtx.dirname(librarySource.fullName);
4259-
var sourceDirectory = pathCtx.dirname(path);
4260-
if (pathCtx.isWithin(libraryDirectory, path) ||
4259+
var sourceDirectory = pathCtx.dirname(source.fullName);
4260+
if (pathCtx.isWithin(libraryDirectory, source.fullName) ||
42614261
pathCtx.isWithin(sourceDirectory, libraryDirectory)) {
4262-
String relativeFile = pathCtx.relative(path, from: libraryDirectory);
4262+
String relativeFile =
4263+
pathCtx.relative(source.fullName, from: libraryDirectory);
42634264
return pathCtx.split(relativeFile).join('/');
42644265
}
42654266
return null;
@@ -4468,6 +4469,30 @@ class FixProcessor {
44684469
return false;
44694470
}
44704471

4472+
/**
4473+
* Return `true` if the [source] can be imported into current library.
4474+
*/
4475+
bool _isSourceVisibleToLibrary(Source source) {
4476+
String path = source.fullName;
4477+
4478+
var contextRoot = context.resolveResult.session.analysisContext.contextRoot;
4479+
if (contextRoot == null) {
4480+
return true;
4481+
}
4482+
4483+
// We don't want to use private libraries of other packages.
4484+
if (source.uri.isScheme('package') && _isLibSrcPath(path)) {
4485+
return contextRoot.root.contains(path);
4486+
}
4487+
4488+
// We cannot use relative URIs to reference files outside of our package.
4489+
if (source.uri.isScheme('file')) {
4490+
return contextRoot.root.contains(path);
4491+
}
4492+
4493+
return true;
4494+
}
4495+
44714496
bool _isToListMethodElement(MethodElement method) {
44724497
if (method.name != 'toList') {
44734498
return false;

pkg/analysis_server/test/analysis_abstract.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ class AbstractAnalysisTest with ResourceProviderMixin {
135135
handleSuccessfulRequest(request, handler: analysisHandler);
136136
}
137137

138-
void doAllDeclarationsTrackerWork() {
139-
while (server.declarationsTracker.hasWork) {
140-
server.declarationsTracker.doWork();
141-
}
142-
}
143-
144138
/**
145139
* Returns the offset of [search] in [testCode].
146140
* Fails if not found.

0 commit comments

Comments
 (0)