6896a21216
* Combine DartFixContext and DartFixContextImpl into one class; separation seems unnecessary. * Change constructor (which was on DartFixContextImpl) to use all named parameters. * Change `resolveResult` field to `resolvedResult`, since the type is called `ResolvedUnitResult`. * Start doc comments with third person verbs [1]. [1]: https://dart.dev/effective-dart/documentation#prefer-starting-function-or-method-comments-with-third-person-verbs Work towards https://github.com/dart-lang/sdk/issues/53402 Change-Id: Idb3c6776f899d5bc9b634c1d9c4b998de2603d04 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/358382 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
85 lines
3.2 KiB
Dart
85 lines
3.2 KiB
Dart
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:analyzer/dart/analysis/results.dart';
|
|
import 'package:analyzer/dart/element/element.dart';
|
|
import 'package:analyzer/error/error.dart';
|
|
import 'package:analyzer/instrumentation/service.dart';
|
|
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
|
|
import 'package:analyzer/src/dart/analysis/file_state_filter.dart';
|
|
import 'package:analyzer/src/services/top_level_declarations.dart';
|
|
import 'package:analyzer_plugin/utilities/change_builder/change_workspace.dart';
|
|
import 'package:server_plugin/edit/fix/fix_context.dart';
|
|
|
|
/// An object used to provide context information for Dart fix contributors.
|
|
///
|
|
/// Clients may not extend, implement or mix-in this class.
|
|
class DartFixContext implements FixContext {
|
|
/// Whether fixes were triggered automatically (for example by a save
|
|
/// operation).
|
|
///
|
|
/// Some fixes may be excluded when running automatically. For example
|
|
/// removing unused imports or parameters is less acceptable while the code is
|
|
/// incomplete and being worked on than when manually executing fixes ready
|
|
/// for committing.
|
|
final bool autoTriggered;
|
|
|
|
/// The instrumentation service used to report errors that prevent a fix from
|
|
/// being composed.
|
|
final InstrumentationService instrumentationService;
|
|
|
|
/// The resolution result in which the fix operates.
|
|
final ResolvedUnitResult resolvedResult;
|
|
|
|
/// The workspace in which the fix contributor operates.
|
|
final ChangeWorkspace workspace;
|
|
|
|
@override
|
|
final AnalysisError error;
|
|
|
|
DartFixContext({
|
|
required this.instrumentationService,
|
|
required this.workspace,
|
|
required this.resolvedResult,
|
|
required this.error,
|
|
this.autoTriggered = false,
|
|
});
|
|
|
|
/// Returns the mapping from each library (that is available to this context)
|
|
/// to a top-level declaration that is exported (not necessary declared) by
|
|
/// this library, and has the requested base name.
|
|
///
|
|
/// For getters and setters the corresponding top-level variable is returned.
|
|
Future<Map<LibraryElement, Element>> getTopLevelDeclarations(
|
|
String name) async {
|
|
return TopLevelDeclarations(resolvedResult).withName(name);
|
|
}
|
|
|
|
/// Returns libraries with extensions that declare non-static public
|
|
/// extension members with the [memberName].
|
|
Stream<LibraryElement> librariesWithExtensions(String memberName) async* {
|
|
var analysisContext = resolvedResult.session.analysisContext;
|
|
var analysisDriver = (analysisContext as DriverBasedAnalysisContext).driver;
|
|
await analysisDriver.discoverAvailableFiles();
|
|
|
|
var fsState = analysisDriver.fsState;
|
|
var filter = FileStateFilter(
|
|
fsState.getFileForPath(resolvedResult.path),
|
|
);
|
|
|
|
for (var file in fsState.knownFiles.toList()) {
|
|
if (!filter.shouldInclude(file)) {
|
|
continue;
|
|
}
|
|
|
|
var elementResult = await analysisDriver.getLibraryByUri(file.uriStr);
|
|
if (elementResult is! LibraryElementResult) {
|
|
continue;
|
|
}
|
|
|
|
yield elementResult.element;
|
|
}
|
|
}
|
|
}
|