Skip to content

Commit 6dac184

Browse files
FMorschelCommit Queue
authored andcommitted
Adds library result
[email protected] Fixes #57092 Change-Id: I125c53e138c97e57fc22d69b2362351ddcbb6b4b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395420 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent 6e54ea6 commit 6dac184

File tree

22 files changed

+255
-183
lines changed

22 files changed

+255
-183
lines changed

pkg/analysis_server/lib/plugin/edit/assist/assist_dart.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,22 @@ abstract class DartAssistContext {
1616
/// fix from being composed.
1717
InstrumentationService get instrumentationService;
1818

19+
/// The resolved library result in which assist operates.
20+
ResolvedLibraryResult get libraryResult;
21+
1922
/// A mapping of [ProducerGenerator]s to the set of lint names with which they
2023
/// are associated (can fix).
2124
Map<ProducerGenerator, Set<LintCode>> get producerGeneratorsForLintRules;
2225

23-
/// The resolution result in which assist operates.
24-
ResolvedUnitResult get resolveResult;
25-
2626
/// The length of the selection.
2727
int get selectionLength;
2828

2929
/// The start of the selection.
3030
int get selectionOffset;
3131

32+
/// The unit result in which assist operates.
33+
ResolvedUnitResult get unitResult;
34+
3235
/// The workspace in which the fix contributor operates.
3336
ChangeWorkspace get workspace;
3437
}

pkg/analysis_server/lib/src/cider/assists.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class CiderAssistsComputer {
3535
int length,
3636
) async {
3737
var result = <Assist>[];
38-
var resolvedUnit = await _fileResolver.resolve(path: path);
38+
var resolvedLibrary = await _fileResolver.resolveLibrary2(path: path);
39+
var resolvedUnit = resolvedLibrary.unitWithPath(path)!;
40+
3941
var lineInfo = resolvedUnit.lineInfo;
4042
var offset = lineInfo.getOffsetOfLine(lineNumber) + colNumber;
4143

@@ -45,6 +47,7 @@ class CiderAssistsComputer {
4547
var context = DartAssistContextImpl(
4648
InstrumentationService.NULL_SERVICE,
4749
workspace,
50+
resolvedLibrary,
4851
resolvedUnit,
4952
_producerGeneratorsForLintRules,
5053
offset,

pkg/analysis_server/lib/src/cider/fixes.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class CiderFixesComputer {
3939
/// Compute quick fixes for errors on the line with the [offset].
4040
Future<List<CiderErrorFixes>> compute(String path, int lineNumber) async {
4141
var result = <CiderErrorFixes>[];
42-
var resolvedUnit = await _fileResolver.resolve(path: path);
42+
var resolvedLibrary = await _fileResolver.resolveLibrary2(path: path);
43+
var resolvedUnit = resolvedLibrary.unitWithPath(path)!;
4344

4445
var lineInfo = resolvedUnit.lineInfo;
4546

@@ -51,7 +52,8 @@ class CiderFixesComputer {
5152
var context = _CiderDartFixContextImpl(
5253
_fileResolver,
5354
workspace: workspace,
54-
resolvedResult: resolvedUnit,
55+
libraryResult: resolvedLibrary,
56+
unitResult: resolvedUnit,
5557
error: error,
5658
);
5759

@@ -75,7 +77,8 @@ class _CiderDartFixContextImpl extends DartFixContext {
7577
_CiderDartFixContextImpl(
7678
this._fileResolver, {
7779
required super.workspace,
78-
required super.resolvedResult,
80+
required super.libraryResult,
81+
required super.unitResult,
7982
required super.error,
8083
}) : super(instrumentationService: InstrumentationService.NULL_SERVICE);
8184

pkg/analysis_server/lib/src/g3/fixes.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ class LintFixTester {
6666
var analysisContext = collection.contextFor(path);
6767
var analysisSession = analysisContext.currentSession;
6868

69-
var unitResult = await analysisSession.getResolvedUnit(path);
70-
unitResult as ResolvedUnitResult;
69+
var resolvedLibrary = await analysisSession.getResolvedLibrary(path);
70+
resolvedLibrary as ResolvedLibraryResult;
71+
72+
var unitResult = resolvedLibrary.unitWithPath(path)!;
7173

7274
AnalysisError error;
7375
var errors = unitResult.errors;
@@ -95,7 +97,8 @@ class LintFixTester {
9597
var context = DartFixContext(
9698
instrumentationService: InstrumentationService.NULL_SERVICE,
9799
workspace: workspace,
98-
resolvedResult: unitResult,
100+
libraryResult: resolvedLibrary,
101+
unitResult: unitResult,
99102
error: error,
100103
);
101104

pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ class EditGetAssistsHandler extends LegacyHandler
100100
) async {
101101
var changes = <SourceChange>[];
102102

103-
var result = await server.getResolvedUnit(file);
103+
var libraryResult = await server.getResolvedLibrary(file);
104104
server.requestStatistics?.addItemTimeNow(request, 'resolvedUnit');
105105

106-
if (result != null) {
106+
if (libraryResult != null) {
107+
var unitResult = libraryResult.unitWithPath(file)!;
107108
var context = DartAssistContextImpl(
108109
server.instrumentationService,
109110
DartChangeWorkspace(await server.currentSessions),
110-
result,
111+
libraryResult,
112+
unitResult,
111113
server.producerGeneratorsForLintRules,
112114
offset,
113115
length,
@@ -128,7 +130,7 @@ offset: $offset
128130
length: $length
129131
''';
130132
throw CaughtExceptionWithFiles(exception, stackTrace, {
131-
file: result.content,
133+
file: unitResult.content,
132134
'parameters': parametersFile,
133135
});
134136
}

pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,21 @@ class EditGetFixesHandler extends LegacyHandler
187187
int offset,
188188
) async {
189189
var errorFixesList = <AnalysisErrorFixes>[];
190-
var result = await server.getResolvedUnit(file);
190+
var libraryResult = await server.getResolvedLibrary(file);
191191
server.requestStatistics?.addItemTimeNow(request, 'resolvedUnit');
192-
if (result != null) {
193-
var lineInfo = result.lineInfo;
192+
if (libraryResult != null) {
193+
var unitResult = libraryResult.unitWithPath(file)!;
194+
var lineInfo = unitResult.lineInfo;
194195
var requestLine = lineInfo.getLocation(offset).lineNumber;
195-
for (var error in result.errors) {
196+
for (var error in unitResult.errors) {
196197
var errorLine = lineInfo.getLocation(error.offset).lineNumber;
197198
if (errorLine == requestLine) {
198199
var workspace = DartChangeWorkspace(await server.currentSessions);
199200
var context = DartFixContext(
200201
instrumentationService: server.instrumentationService,
201202
workspace: workspace,
202-
resolvedResult: result,
203+
libraryResult: libraryResult,
204+
unitResult: unitResult,
203205
error: error,
204206
);
205207

@@ -215,14 +217,14 @@ error: $error
215217
error.errorCode: ${error.errorCode}
216218
''';
217219
throw CaughtExceptionWithFiles(exception, stackTrace, {
218-
file: result.content,
220+
file: unitResult.content,
219221
'parameters': parametersFile,
220222
});
221223
}
222224

223225
if (fixes.isNotEmpty) {
224226
fixes.sort(Fix.compareFixes);
225-
var serverError = newAnalysisError_fromEngine(result, error);
227+
var serverError = newAnalysisError_fromEngine(unitResult, error);
226228
var errorFixes = AnalysisErrorFixes(serverError);
227229
errorFixesList.add(errorFixes);
228230
for (var fix in fixes) {

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import 'package:analyzer/src/utilities/extensions/element.dart';
2828
/// Produces [CodeAction]s from Dart source commands, fixes, assists and
2929
/// refactors from the server.
3030
class DartCodeActionsProducer extends AbstractCodeActionsProducer {
31-
ResolvedLibraryResult library;
32-
ResolvedUnitResult unit;
31+
ResolvedLibraryResult libraryResult;
32+
ResolvedUnitResult unitResult;
3333
Range range;
3434
final OptionalVersionedTextDocumentIdentifier docIdentifier;
3535
final CodeActionTriggerKind? triggerKind;
@@ -39,8 +39,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
3939
super.file,
4040
super.lineInfo,
4141
this.docIdentifier,
42-
this.library,
43-
this.unit, {
42+
this.libraryResult,
43+
this.unitResult, {
4444
required this.range,
4545
required super.offset,
4646
required super.length,
@@ -126,7 +126,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
126126
var context = DartAssistContextImpl(
127127
server.instrumentationService,
128128
workspace,
129-
unit,
129+
libraryResult,
130+
unitResult,
130131
server.producerGeneratorsForLintRules,
131132
offset,
132133
length,
@@ -137,8 +138,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
137138
return assists.map((assist) {
138139
var action = createAssistAction(
139140
assist.change,
140-
unit.path,
141-
unit.lineInfo,
141+
unitResult.path,
142+
unitResult.lineInfo,
142143
);
143144
return (action: action, priority: assist.kind.priority);
144145
}).toList();
@@ -157,12 +158,12 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
157158
return [];
158159
}
159160

160-
var lineInfo = unit.lineInfo;
161+
var lineInfo = unitResult.lineInfo;
161162
var codeActions = <CodeActionWithPriority>[];
162163

163164
try {
164165
var workspace = DartChangeWorkspace(await server.currentSessions);
165-
for (var error in unit.errors) {
166+
for (var error in unitResult.errors) {
166167
// Return fixes for any part of the line where a diagnostic is.
167168
// If a diagnostic spans multiple lines, the fix will be included for
168169
// all of those lines.
@@ -177,14 +178,15 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
177178
var context = DartFixContext(
178179
instrumentationService: server.instrumentationService,
179180
workspace: workspace,
180-
resolvedResult: unit,
181+
libraryResult: libraryResult,
182+
unitResult: unitResult,
181183
error: error,
182184
);
183185
var fixes = await computeFixes(context);
184186
if (fixes.isNotEmpty) {
185187
var diagnostic = toDiagnostic(
186188
server.uriConverter,
187-
unit,
189+
unitResult,
188190
error,
189191
supportedTags: supportedDiagnosticTags,
190192
clientSupportsCodeDescription: supportsCodeDescription,
@@ -227,8 +229,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
227229
var context = RefactoringContext(
228230
server: server,
229231
startSessions: await server.currentSessions,
230-
resolvedLibraryResult: library,
231-
resolvedUnitResult: unit,
232+
resolvedLibraryResult: libraryResult,
233+
resolvedUnitResult: unitResult,
232234
clientCapabilities: capabilities,
233235
selectionOffset: offset,
234236
selectionLength: length,
@@ -244,7 +246,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
244246
// Extract Method
245247
if (ExtractMethodRefactoring(
246248
server.searchEngine,
247-
unit,
249+
unitResult,
248250
offset,
249251
length,
250252
).isAvailable()) {
@@ -258,7 +260,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
258260
}
259261

260262
// Extract Local Variable
261-
if (ExtractLocalRefactoring(unit, offset, length).isAvailable()) {
263+
if (ExtractLocalRefactoring(unitResult, offset, length).isAvailable()) {
262264
refactorActions.add(
263265
createRefactor(
264266
CodeActionKind.RefactorExtract,
@@ -271,7 +273,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
271273
// Extract Widget
272274
if (ExtractWidgetRefactoring(
273275
server.searchEngine,
274-
unit,
276+
unitResult,
275277
offset,
276278
length,
277279
).isAvailable()) {
@@ -290,7 +292,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
290292
// Inline Local Variable
291293
if (InlineLocalRefactoring(
292294
server.searchEngine,
293-
unit,
295+
unitResult,
294296
offset,
295297
).isAvailable()) {
296298
refactorActions.add(
@@ -305,7 +307,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
305307
// Inline Method
306308
if (InlineMethodRefactoring(
307309
server.searchEngine,
308-
unit,
310+
unitResult,
309311
offset,
310312
).isAvailable()) {
311313
refactorActions.add(
@@ -320,14 +322,14 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
320322

321323
// Converts/Rewrites
322324
if (shouldIncludeKind(CodeActionKind.RefactorRewrite)) {
323-
var node = NodeLocator(offset).searchWithin(unit.unit);
325+
var node = NodeLocator(offset).searchWithin(unitResult.unit);
324326
var element = server.getElementOfNode(node).asElement2;
325327

326328
// Getter to Method
327329
if (element is GetterElement &&
328330
ConvertGetterToMethodRefactoring(
329331
server.refactoringWorkspace,
330-
unit.session,
332+
unitResult.session,
331333
element,
332334
).isAvailable()) {
333335
refactorActions.add(
@@ -343,7 +345,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
343345
if (element is ExecutableElement2 &&
344346
ConvertMethodToGetterRefactoring(
345347
server.refactoringWorkspace,
346-
unit.session,
348+
unitResult.session,
347349
element,
348350
).isAvailable()) {
349351
refactorActions.add(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ class DartAssistContextImpl implements DartAssistContext {
1919
final ChangeWorkspace workspace;
2020

2121
@override
22-
final ResolvedUnitResult resolveResult;
22+
final ResolvedLibraryResult libraryResult;
23+
24+
@override
25+
final ResolvedUnitResult unitResult;
2326

2427
@override
2528
final Map<ProducerGenerator, Set<LintCode>> producerGeneratorsForLintRules;
@@ -33,7 +36,8 @@ class DartAssistContextImpl implements DartAssistContext {
3336
DartAssistContextImpl(
3437
this.instrumentationService,
3538
this.workspace,
36-
this.resolveResult,
39+
this.libraryResult,
40+
this.unitResult,
3741
this.producerGeneratorsForLintRules,
3842
this.selectionOffset,
3943
this.selectionLength,

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class AssistProcessor {
178178
AssistProcessor(this._assistContext);
179179

180180
Future<List<Assist>> compute() async {
181-
if (isMacroGenerated(_assistContext.resolveResult.file.path)) {
181+
if (isMacroGenerated(_assistContext.unitResult.file.path)) {
182182
return _assists;
183183
}
184184
await _addFromProducers();
@@ -201,9 +201,10 @@ class AssistProcessor {
201201

202202
Future<void> _addFromProducers() async {
203203
var context = CorrectionProducerContext.createResolved(
204+
libraryResult: _assistContext.libraryResult,
205+
unitResult: _assistContext.unitResult,
204206
selectionOffset: _assistContext.selectionOffset,
205207
selectionLength: _assistContext.selectionLength,
206-
resolvedResult: _assistContext.resolveResult,
207208
);
208209

209210
Future<void> compute(CorrectionProducer producer) async {
@@ -258,15 +259,15 @@ class AssistProcessor {
258259
var selectionEnd =
259260
_assistContext.selectionOffset + _assistContext.selectionLength;
260261
var locator = NodeLocator(_assistContext.selectionOffset, selectionEnd);
261-
var node = locator.searchWithin(_assistContext.resolveResult.unit);
262+
var node = locator.searchWithin(_assistContext.unitResult.unit);
262263
if (node == null) {
263264
return false;
264265
}
265266

266267
var fileOffset = node.offset;
267-
for (var error in _assistContext.resolveResult.errors) {
268+
for (var error in _assistContext.unitResult.errors) {
268269
var errorSource = error.source;
269-
if (_assistContext.resolveResult.path == errorSource.fullName) {
270+
if (_assistContext.unitResult.path == errorSource.fullName) {
270271
if (fileOffset >= error.offset &&
271272
fileOffset <= error.offset + error.length) {
272273
if (errorCodes.contains(error.errorCode)) {

0 commit comments

Comments
 (0)