Skip to content

Commit f9481e1

Browse files
srawlinsCommit Queue
authored andcommitted
DAS: Remove unnecessary getNodeAtOffset helper
There are only two callers, and they each have enough information at hand to carry out this simple computation themselves. In particular, getElementAtOffset does not need to reach out to the driver twice, to get *Result objects for the same file. Change-Id: Ia5fd1e3045bad4d9aa64a7011a3a5c90db0c305d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423170 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent b40c308 commit f9481e1

File tree

2 files changed

+10
-26
lines changed

2 files changed

+10
-26
lines changed

pkg/analysis_server/lib/src/analysis_server.dart

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -646,28 +646,23 @@ abstract class AnalysisServer {
646646
/// [offset] of the given [file], or with `null` if there is no node at the
647647
/// [offset] or the node does not have an element.
648648
Future<Element2?> getElementAtOffset(String file, int offset) async {
649-
if (!priorityFiles.contains(file)) {
650-
var driver = getAnalysisDriver(file);
651-
if (driver == null) {
652-
return null;
653-
}
654-
655-
var unitElementResult = await driver.getUnitElement(file);
656-
if (unitElementResult is! UnitElementResult) {
657-
return null;
658-
}
649+
var unitResult = await getResolvedUnit(file);
650+
if (unitResult == null) {
651+
return null;
652+
}
659653

654+
if (!priorityFiles.contains(file)) {
660655
var fragment = findFragmentByNameOffset(
661-
unitElementResult.fragment,
656+
unitResult.libraryFragment,
662657
offset,
663658
);
664659
if (fragment != null) {
665660
return fragment.element;
666661
}
667662
}
668663

669-
var node = await getNodeAtOffset(file, offset);
670-
return getElementOfNode(node);
664+
var unit = unitResult.unit;
665+
return getElementOfNode(unit.nodeCovering(offset: offset));
671666
}
672667

673668
/// Returns the element associated with the [node].
@@ -733,18 +728,6 @@ abstract class AnalysisServer {
733728
}
734729
}
735730

736-
/// Return a [Future] that completes with the resolved [AstNode] at the
737-
/// given [offset] of the given [file], or with `null` if there is no node as
738-
/// the [offset].
739-
Future<AstNode?> getNodeAtOffset(String file, int offset) async {
740-
var result = await getResolvedUnit(file);
741-
var unit = result?.unit;
742-
if (unit != null) {
743-
return unit.nodeCovering(offset: offset);
744-
}
745-
return null;
746-
}
747-
748731
/// Return the unresolved unit for the file with the given [path].
749732
///
750733
/// Callers should handle [InconsistentAnalysisException] exceptions that may

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import 'package:analyzer/dart/element/element2.dart';
1717
import 'package:analyzer/dart/element/type.dart';
1818
import 'package:analyzer/source/line_info.dart';
1919
import 'package:analyzer/src/dart/element/extensions.dart';
20+
import 'package:analyzer/utilities/extensions/ast.dart';
2021

2122
typedef StaticOptions =
2223
Either3<bool, InlineValueOptions, InlineValueRegistrationOptions>;
@@ -75,7 +76,7 @@ class InlineValueHandler
7576
return stoppedOffset.mapResult((stoppedOffset) async {
7677
// Find the function that is executing. We will only show values for
7778
// this single function expression.
78-
var node = await server.getNodeAtOffset(filePath, stoppedOffset);
79+
var node = unitResult.unit.nodeCovering(offset: stoppedOffset);
7980
var function = node?.thisOrAncestorMatching(
8081
(node) => node is FunctionExpression || node is MethodDeclaration,
8182
);

0 commit comments

Comments
 (0)