blob: c76d2c722f99db74bfdc674ed565d3f3732fbf50 [file] [log] [blame]
// Copyright (c) 2022, 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/analysis/session.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/utilities/extensions/library_element.dart';
extension AnalysisSessionExtension on AnalysisSession {
/// Return the resolved library for the library containing the file with the
/// given [path].
Future<ResolvedLibraryResult?> getResolvedContainingLibrary(
String path,
) async {
var unitElement = await getUnitElement(path);
if (unitElement is! UnitElementResult) {
return null;
}
var libraryPath = unitElement.element.library.source.fullName;
var result = await getResolvedLibrary(libraryPath);
return result is ResolvedLibraryResult ? result : null;
}
/// Locates the [Element] that [location] represents.
///
/// Local elements such as variables inside functions cannot be found using
/// this method.
///
/// Returns `null` if the element cannot be found.
Future<Element?> locateElement(ElementLocation location) async {
final components = location.components;
if (location.components.isEmpty) {
return null;
}
// The first component is the library which we'll use to start the search.
final libraryUri = components.first;
final result = await getLibraryByUri(libraryUri);
return result is LibraryElementResult
? result.element.locateElement(location)
: null;
}
}