Skip to content

Commit befa614

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Optimizes library resolution for bulk_fix_processor
Bug: #60472 Change-Id: Ia26b47f94e0851ae5da6e52fde3f9a2c4a3a0d20 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/420600 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent a4171e7 commit befa614

File tree

7 files changed

+88
-22
lines changed

7 files changed

+88
-22
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -514,20 +514,13 @@ class BulkFixProcessor {
514514
continue;
515515
}
516516

517-
var resolvedLibrary = await context.currentSession.getResolvedLibrary(
518-
path,
519-
);
517+
var resolvedLibrary = await context.currentSession
518+
.getResolvedLibraryContaining(path);
520519

521520
if (isCancelled) {
522521
break;
523522
}
524-
if (resolvedLibrary is NotLibraryButPartResult) {
525-
var resolvedUnit = await context.currentSession.getResolvedUnit(path);
526-
if (resolvedUnit is ResolvedUnitResult) {
527-
resolvedLibrary = await context.currentSession
528-
.getResolvedLibraryByElement2(resolvedUnit.libraryElement2);
529-
}
530-
}
523+
531524
if (resolvedLibrary is ResolvedLibraryResult) {
532525
await _fixErrorsInLibraryAt(
533526
resolvedLibrary,

pkg/analyzer/api.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ package:analyzer/dart/analysis/results.dart:
237237
UnitElementResult (class extends Object implements SomeUnitElementResult, FileResult):
238238
new (constructor: UnitElementResult Function())
239239
fragment (getter: LibraryFragment, experimental)
240-
UnspecifiedInvalidResult (class extends Object implements InvalidResult, SomeLibraryElementResult, SomeParsedLibraryResult):
240+
UnspecifiedInvalidResult (class extends Object implements InvalidResult, SomeErrorsResult, SomeFileResult, SomeLibraryElementResult, SomeParsedLibraryResult, SomeParsedUnitResult, SomeResolvedLibraryResult, SomeResolvedUnitResult, SomeUnitElementResult):
241241
new (constructor: UnspecifiedInvalidResult Function())
242242
UriOfExternalLibraryResult (class extends Object implements InvalidResult, SomeParsedLibraryResult, SomeResolvedLibraryResult):
243243
new (constructor: UriOfExternalLibraryResult Function())
@@ -256,6 +256,7 @@ package:analyzer/dart/analysis/session.dart:
256256
getParsedUnit (method: SomeParsedUnitResult Function(String))
257257
getResolvedLibrary (method: Future<SomeResolvedLibraryResult> Function(String))
258258
getResolvedLibraryByElement2 (method: Future<SomeResolvedLibraryResult> Function(LibraryElement2), experimental)
259+
getResolvedLibraryContaining (method: Future<SomeResolvedLibraryResult> Function(String))
259260
getResolvedUnit (method: Future<SomeResolvedUnitResult> Function(String))
260261
getUnitElement (method: Future<SomeUnitElementResult> Function(String))
261262
InconsistentAnalysisException (class extends AnalysisException):

pkg/analyzer/lib/dart/analysis/results.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,14 @@ abstract class UnitElementResult implements SomeUnitElementResult, FileResult {
393393
class UnspecifiedInvalidResult
394394
implements
395395
InvalidResult,
396+
SomeErrorsResult,
397+
SomeFileResult,
396398
SomeLibraryElementResult,
397-
SomeParsedLibraryResult {}
399+
SomeParsedLibraryResult,
400+
SomeParsedUnitResult,
401+
SomeResolvedLibraryResult,
402+
SomeResolvedUnitResult,
403+
SomeUnitElementResult {}
398404

399405
/// The type of [InvalidResult] returned when the given URI corresponds to
400406
/// a library that is served from an external summary bundle.

pkg/analyzer/lib/dart/analysis/session.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ abstract class AnalysisSession {
7272
Future<SomeResolvedLibraryResult> getResolvedLibraryByElement2(
7373
LibraryElement2 element);
7474

75+
/// Return a future that will complete with information about the results of
76+
/// resolving all of the files in the library containing the given absolute,
77+
/// normalized [path].
78+
///
79+
/// Similar to [getResolvedLibrary] but [path] can also be a part file of a
80+
/// library.
81+
Future<SomeResolvedLibraryResult> getResolvedLibraryContaining(String path);
82+
7583
/// Return a future that will complete with information about the results of
7684
/// resolving the file with the given absolute, normalized [path].
7785
Future<SomeResolvedUnitResult> getResolvedUnit(String path);

pkg/analyzer/lib/src/dart/analysis/session.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ class AnalysisSessionImpl implements AnalysisSession {
128128
return await _driver.getResolvedLibraryByUri(element.uri);
129129
}
130130

131+
@override
132+
Future<SomeResolvedLibraryResult> getResolvedLibraryContaining(
133+
String path,
134+
) async {
135+
checkConsistency();
136+
var libraryFragmentResult = await getUnitElement(path);
137+
return switch (libraryFragmentResult) {
138+
UnitElementResult(:var fragment) => await getResolvedLibraryByElement2(
139+
fragment.element,
140+
),
141+
SomeResolvedLibraryResult result => result,
142+
_ => UnspecifiedInvalidResult(),
143+
};
144+
}
145+
131146
@override
132147
Future<SomeResolvedUnitResult> getResolvedUnit(String path) async {
133148
checkConsistency();

pkg/analyzer/lib/src/dart/analysis/session_helper.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,7 @@ class AnalysisSessionHelper {
116116
Future<ResolvedLibraryResult?> _getResolvedLibrary(String path) async {
117117
var result = _resolvedLibraries[path];
118118
if (result == null) {
119-
var unit = await session.getUnitElement(path);
120-
if (unit is! UnitElementResult) {
121-
return null;
122-
}
123-
var some =
124-
await session.getResolvedLibraryByElement2(unit.fragment.element);
119+
var some = await session.getResolvedLibraryContaining(path);
125120
if (some is ResolvedLibraryResult) {
126121
result = _resolvedLibraries[path] = some;
127122
}

pkg/analyzer/test/src/dart/analysis/session_test.dart

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ class B {}
329329
var parsedLibrary = session.getParsedLibraryValid(testFile);
330330

331331
var element = libraryResult.element2.getClass2('A')!;
332-
var declaration = parsedLibrary.getFragmentDeclaration(element.firstFragment)!;
332+
var declaration =
333+
parsedLibrary.getFragmentDeclaration(element.firstFragment)!;
333334
var node = declaration.node as ClassDeclaration;
334335
expect(node.name.lexeme, 'A');
335336
expect(node.offset, 0);
@@ -582,14 +583,16 @@ class B2 extends X {}
582583
expect(bUnitResult.unit.declarations, hasLength(2));
583584
expect(bUnitResult.errors, isNotEmpty);
584585

585-
var aDeclaration = resolvedLibrary.getFragmentDeclaration(aClass.firstFragment)!;
586+
var aDeclaration =
587+
resolvedLibrary.getFragmentDeclaration(aClass.firstFragment)!;
586588
var aNode = aDeclaration.node as ClassDeclaration;
587589
expect(aNode.name.lexeme, 'A');
588590
expect(aNode.offset, 16);
589591
expect(aNode.length, 16);
590592
expect(aNode.declaredFragment!.name2, 'A');
591593

592-
var bDeclaration = resolvedLibrary.getFragmentDeclaration(bClass.firstFragment)!;
594+
var bDeclaration =
595+
resolvedLibrary.getFragmentDeclaration(bClass.firstFragment)!;
593596
var bNode = bDeclaration.node as ClassDeclaration;
594597
expect(bNode.name.lexeme, 'B');
595598
expect(bNode.offset, 19);
@@ -708,6 +711,50 @@ part 'c.dart';
708711
expect(result, isA<NotElementOfThisSessionResult>());
709712
}
710713

714+
test_getResolvedLibraryContaining_library() async {
715+
var a = newFile('$testPackageLibPath/a.dart', '');
716+
var currentSession = contextFor(a).currentSession;
717+
var filePath = a.toUri().toFilePath();
718+
var result = await currentSession.getResolvedLibraryContaining(filePath);
719+
var units = (result as ResolvedLibraryResult).units;
720+
var paths = units.map((unit) => unit.path);
721+
expect(paths, unorderedEquals([a.path]));
722+
}
723+
724+
test_getResolvedLibraryContaining_part() async {
725+
var lib = newFile('$testPackageLibPath/lib.dart', r'''
726+
part 'part.dart';
727+
''');
728+
var part = newFile('$testPackageLibPath/part.dart', r'''
729+
part of 'lib.dart';
730+
''');
731+
var currentSession = contextFor(part).currentSession;
732+
var filePath = part.toUri().toFilePath();
733+
var result = await currentSession.getResolvedLibraryContaining(filePath);
734+
var units = (result as ResolvedLibraryResult).units;
735+
var paths = units.map((unit) => unit.path);
736+
expect(paths, unorderedEquals([lib.path, part.path]));
737+
}
738+
739+
test_getResolvedLibraryContaining_part_part() async {
740+
var lib = newFile('$testPackageLibPath/lib.dart', r'''
741+
part 'part.dart';
742+
''');
743+
var part = newFile('$testPackageLibPath/part.dart', r'''
744+
part of 'lib.dart';
745+
part 'part_part.dart';
746+
''');
747+
var partPart = newFile('$testPackageLibPath/part_part.dart', r'''
748+
part of 'part.dart';
749+
''');
750+
var currentSession = contextFor(partPart).currentSession;
751+
var filePath = partPart.toUri().toFilePath();
752+
var result = await currentSession.getResolvedLibraryContaining(filePath);
753+
var units = (result as ResolvedLibraryResult).units;
754+
var paths = units.map((unit) => unit.path);
755+
expect(paths, unorderedEquals([lib.path, part.path, partPart.path]));
756+
}
757+
711758
test_getResolvedUnit() async {
712759
var test = newFile(testFile.path, r'''
713760
class A {}
@@ -907,7 +954,8 @@ unitElementResult
907954
var element = result.fragment as CompilationUnitElementImpl;
908955
sink.writelnWithIndent('reference: ${element.reference}');
909956

910-
var library = (element as LibraryFragment).element as LibraryElementImpl;
957+
var library =
958+
(element as LibraryFragment).element as LibraryElementImpl;
911959
sink.writelnWithIndent('library: ${library.reference}');
912960

913961
var classListStr = element.classes2.map((e) => e.name2).join(', ');

0 commit comments

Comments
 (0)