Skip to content

Commit

Permalink
Adds library result
Browse files Browse the repository at this point in the history
[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]>
  • Loading branch information
FMorschel authored and Commit Queue committed Nov 19, 2024
1 parent 6e54ea6 commit 6dac184
Show file tree
Hide file tree
Showing 22 changed files with 255 additions and 183 deletions.
9 changes: 6 additions & 3 deletions pkg/analysis_server/lib/plugin/edit/assist/assist_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ abstract class DartAssistContext {
/// fix from being composed.
InstrumentationService get instrumentationService;

/// The resolved library result in which assist operates.
ResolvedLibraryResult get libraryResult;

/// A mapping of [ProducerGenerator]s to the set of lint names with which they
/// are associated (can fix).
Map<ProducerGenerator, Set<LintCode>> get producerGeneratorsForLintRules;

/// The resolution result in which assist operates.
ResolvedUnitResult get resolveResult;

/// The length of the selection.
int get selectionLength;

/// The start of the selection.
int get selectionOffset;

/// The unit result in which assist operates.
ResolvedUnitResult get unitResult;

/// The workspace in which the fix contributor operates.
ChangeWorkspace get workspace;
}
5 changes: 4 additions & 1 deletion pkg/analysis_server/lib/src/cider/assists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class CiderAssistsComputer {
int length,
) async {
var result = <Assist>[];
var resolvedUnit = await _fileResolver.resolve(path: path);
var resolvedLibrary = await _fileResolver.resolveLibrary2(path: path);
var resolvedUnit = resolvedLibrary.unitWithPath(path)!;

var lineInfo = resolvedUnit.lineInfo;
var offset = lineInfo.getOffsetOfLine(lineNumber) + colNumber;

Expand All @@ -45,6 +47,7 @@ class CiderAssistsComputer {
var context = DartAssistContextImpl(
InstrumentationService.NULL_SERVICE,
workspace,
resolvedLibrary,
resolvedUnit,
_producerGeneratorsForLintRules,
offset,
Expand Down
9 changes: 6 additions & 3 deletions pkg/analysis_server/lib/src/cider/fixes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class CiderFixesComputer {
/// Compute quick fixes for errors on the line with the [offset].
Future<List<CiderErrorFixes>> compute(String path, int lineNumber) async {
var result = <CiderErrorFixes>[];
var resolvedUnit = await _fileResolver.resolve(path: path);
var resolvedLibrary = await _fileResolver.resolveLibrary2(path: path);
var resolvedUnit = resolvedLibrary.unitWithPath(path)!;

var lineInfo = resolvedUnit.lineInfo;

Expand All @@ -51,7 +52,8 @@ class CiderFixesComputer {
var context = _CiderDartFixContextImpl(
_fileResolver,
workspace: workspace,
resolvedResult: resolvedUnit,
libraryResult: resolvedLibrary,
unitResult: resolvedUnit,
error: error,
);

Expand All @@ -75,7 +77,8 @@ class _CiderDartFixContextImpl extends DartFixContext {
_CiderDartFixContextImpl(
this._fileResolver, {
required super.workspace,
required super.resolvedResult,
required super.libraryResult,
required super.unitResult,
required super.error,
}) : super(instrumentationService: InstrumentationService.NULL_SERVICE);

Expand Down
9 changes: 6 additions & 3 deletions pkg/analysis_server/lib/src/g3/fixes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class LintFixTester {
var analysisContext = collection.contextFor(path);
var analysisSession = analysisContext.currentSession;

var unitResult = await analysisSession.getResolvedUnit(path);
unitResult as ResolvedUnitResult;
var resolvedLibrary = await analysisSession.getResolvedLibrary(path);
resolvedLibrary as ResolvedLibraryResult;

var unitResult = resolvedLibrary.unitWithPath(path)!;

AnalysisError error;
var errors = unitResult.errors;
Expand Down Expand Up @@ -95,7 +97,8 @@ class LintFixTester {
var context = DartFixContext(
instrumentationService: InstrumentationService.NULL_SERVICE,
workspace: workspace,
resolvedResult: unitResult,
libraryResult: resolvedLibrary,
unitResult: unitResult,
error: error,
);

Expand Down
10 changes: 6 additions & 4 deletions pkg/analysis_server/lib/src/handler/legacy/edit_get_assists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ class EditGetAssistsHandler extends LegacyHandler
) async {
var changes = <SourceChange>[];

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

if (result != null) {
if (libraryResult != null) {
var unitResult = libraryResult.unitWithPath(file)!;
var context = DartAssistContextImpl(
server.instrumentationService,
DartChangeWorkspace(await server.currentSessions),
result,
libraryResult,
unitResult,
server.producerGeneratorsForLintRules,
offset,
length,
Expand All @@ -128,7 +130,7 @@ offset: $offset
length: $length
''';
throw CaughtExceptionWithFiles(exception, stackTrace, {
file: result.content,
file: unitResult.content,
'parameters': parametersFile,
});
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,21 @@ class EditGetFixesHandler extends LegacyHandler
int offset,
) async {
var errorFixesList = <AnalysisErrorFixes>[];
var result = await server.getResolvedUnit(file);
var libraryResult = await server.getResolvedLibrary(file);
server.requestStatistics?.addItemTimeNow(request, 'resolvedUnit');
if (result != null) {
var lineInfo = result.lineInfo;
if (libraryResult != null) {
var unitResult = libraryResult.unitWithPath(file)!;
var lineInfo = unitResult.lineInfo;
var requestLine = lineInfo.getLocation(offset).lineNumber;
for (var error in result.errors) {
for (var error in unitResult.errors) {
var errorLine = lineInfo.getLocation(error.offset).lineNumber;
if (errorLine == requestLine) {
var workspace = DartChangeWorkspace(await server.currentSessions);
var context = DartFixContext(
instrumentationService: server.instrumentationService,
workspace: workspace,
resolvedResult: result,
libraryResult: libraryResult,
unitResult: unitResult,
error: error,
);

Expand All @@ -215,14 +217,14 @@ error: $error
error.errorCode: ${error.errorCode}
''';
throw CaughtExceptionWithFiles(exception, stackTrace, {
file: result.content,
file: unitResult.content,
'parameters': parametersFile,
});
}

if (fixes.isNotEmpty) {
fixes.sort(Fix.compareFixes);
var serverError = newAnalysisError_fromEngine(result, error);
var serverError = newAnalysisError_fromEngine(unitResult, error);
var errorFixes = AnalysisErrorFixes(serverError);
errorFixesList.add(errorFixes);
for (var fix in fixes) {
Expand Down
44 changes: 23 additions & 21 deletions pkg/analysis_server/lib/src/lsp/handlers/code_actions/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import 'package:analyzer/src/utilities/extensions/element.dart';
/// Produces [CodeAction]s from Dart source commands, fixes, assists and
/// refactors from the server.
class DartCodeActionsProducer extends AbstractCodeActionsProducer {
ResolvedLibraryResult library;
ResolvedUnitResult unit;
ResolvedLibraryResult libraryResult;
ResolvedUnitResult unitResult;
Range range;
final OptionalVersionedTextDocumentIdentifier docIdentifier;
final CodeActionTriggerKind? triggerKind;
Expand All @@ -39,8 +39,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
super.file,
super.lineInfo,
this.docIdentifier,
this.library,
this.unit, {
this.libraryResult,
this.unitResult, {
required this.range,
required super.offset,
required super.length,
Expand Down Expand Up @@ -126,7 +126,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
var context = DartAssistContextImpl(
server.instrumentationService,
workspace,
unit,
libraryResult,
unitResult,
server.producerGeneratorsForLintRules,
offset,
length,
Expand All @@ -137,8 +138,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
return assists.map((assist) {
var action = createAssistAction(
assist.change,
unit.path,
unit.lineInfo,
unitResult.path,
unitResult.lineInfo,
);
return (action: action, priority: assist.kind.priority);
}).toList();
Expand All @@ -157,12 +158,12 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
return [];
}

var lineInfo = unit.lineInfo;
var lineInfo = unitResult.lineInfo;
var codeActions = <CodeActionWithPriority>[];

try {
var workspace = DartChangeWorkspace(await server.currentSessions);
for (var error in unit.errors) {
for (var error in unitResult.errors) {
// Return fixes for any part of the line where a diagnostic is.
// If a diagnostic spans multiple lines, the fix will be included for
// all of those lines.
Expand All @@ -177,14 +178,15 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
var context = DartFixContext(
instrumentationService: server.instrumentationService,
workspace: workspace,
resolvedResult: unit,
libraryResult: libraryResult,
unitResult: unitResult,
error: error,
);
var fixes = await computeFixes(context);
if (fixes.isNotEmpty) {
var diagnostic = toDiagnostic(
server.uriConverter,
unit,
unitResult,
error,
supportedTags: supportedDiagnosticTags,
clientSupportsCodeDescription: supportsCodeDescription,
Expand Down Expand Up @@ -227,8 +229,8 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
var context = RefactoringContext(
server: server,
startSessions: await server.currentSessions,
resolvedLibraryResult: library,
resolvedUnitResult: unit,
resolvedLibraryResult: libraryResult,
resolvedUnitResult: unitResult,
clientCapabilities: capabilities,
selectionOffset: offset,
selectionLength: length,
Expand All @@ -244,7 +246,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
// Extract Method
if (ExtractMethodRefactoring(
server.searchEngine,
unit,
unitResult,
offset,
length,
).isAvailable()) {
Expand All @@ -258,7 +260,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
}

// Extract Local Variable
if (ExtractLocalRefactoring(unit, offset, length).isAvailable()) {
if (ExtractLocalRefactoring(unitResult, offset, length).isAvailable()) {
refactorActions.add(
createRefactor(
CodeActionKind.RefactorExtract,
Expand All @@ -271,7 +273,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
// Extract Widget
if (ExtractWidgetRefactoring(
server.searchEngine,
unit,
unitResult,
offset,
length,
).isAvailable()) {
Expand All @@ -290,7 +292,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
// Inline Local Variable
if (InlineLocalRefactoring(
server.searchEngine,
unit,
unitResult,
offset,
).isAvailable()) {
refactorActions.add(
Expand All @@ -305,7 +307,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
// Inline Method
if (InlineMethodRefactoring(
server.searchEngine,
unit,
unitResult,
offset,
).isAvailable()) {
refactorActions.add(
Expand All @@ -320,14 +322,14 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {

// Converts/Rewrites
if (shouldIncludeKind(CodeActionKind.RefactorRewrite)) {
var node = NodeLocator(offset).searchWithin(unit.unit);
var node = NodeLocator(offset).searchWithin(unitResult.unit);
var element = server.getElementOfNode(node).asElement2;

// Getter to Method
if (element is GetterElement &&
ConvertGetterToMethodRefactoring(
server.refactoringWorkspace,
unit.session,
unitResult.session,
element,
).isAvailable()) {
refactorActions.add(
Expand All @@ -343,7 +345,7 @@ class DartCodeActionsProducer extends AbstractCodeActionsProducer {
if (element is ExecutableElement2 &&
ConvertMethodToGetterRefactoring(
server.refactoringWorkspace,
unit.session,
unitResult.session,
element,
).isAvailable()) {
refactorActions.add(
Expand Down
8 changes: 6 additions & 2 deletions pkg/analysis_server/lib/src/services/correction/assist.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class DartAssistContextImpl implements DartAssistContext {
final ChangeWorkspace workspace;

@override
final ResolvedUnitResult resolveResult;
final ResolvedLibraryResult libraryResult;

@override
final ResolvedUnitResult unitResult;

@override
final Map<ProducerGenerator, Set<LintCode>> producerGeneratorsForLintRules;
Expand All @@ -33,7 +36,8 @@ class DartAssistContextImpl implements DartAssistContext {
DartAssistContextImpl(
this.instrumentationService,
this.workspace,
this.resolveResult,
this.libraryResult,
this.unitResult,
this.producerGeneratorsForLintRules,
this.selectionOffset,
this.selectionLength,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class AssistProcessor {
AssistProcessor(this._assistContext);

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

Future<void> _addFromProducers() async {
var context = CorrectionProducerContext.createResolved(
libraryResult: _assistContext.libraryResult,
unitResult: _assistContext.unitResult,
selectionOffset: _assistContext.selectionOffset,
selectionLength: _assistContext.selectionLength,
resolvedResult: _assistContext.resolveResult,
);

Future<void> compute(CorrectionProducer producer) async {
Expand Down Expand Up @@ -258,15 +259,15 @@ class AssistProcessor {
var selectionEnd =
_assistContext.selectionOffset + _assistContext.selectionLength;
var locator = NodeLocator(_assistContext.selectionOffset, selectionEnd);
var node = locator.searchWithin(_assistContext.resolveResult.unit);
var node = locator.searchWithin(_assistContext.unitResult.unit);
if (node == null) {
return false;
}

var fileOffset = node.offset;
for (var error in _assistContext.resolveResult.errors) {
for (var error in _assistContext.unitResult.errors) {
var errorSource = error.source;
if (_assistContext.resolveResult.path == errorSource.fullName) {
if (_assistContext.unitResult.path == errorSource.fullName) {
if (fileOffset >= error.offset &&
fileOffset <= error.offset + error.length) {
if (errorCodes.contains(error.errorCode)) {
Expand Down
Loading

0 comments on commit 6dac184

Please sign in to comment.