Skip to content

Commit f7d5061

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Create CompletionTarget with AstNode entry point.
When we resolve a file partially, there is no fully resolved CompilationUnit. So, anything we create for completion should be based on element models and a resolved, or unresolved AstNode. In a following CL I will make it possible to create instances of DartCompletionRequest based on such element models and node. Change-Id: Ief7e376588bb63f4d898e33299a9fd2014c88843 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/219748 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 0afb365 commit f7d5061

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

pkg/analyzer_plugin/lib/src/utilities/completion/completion_target.dart

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,8 @@ class CompletionTarget {
119119
ParameterElement? _parameterElement;
120120

121121
/// Compute the appropriate [CompletionTarget] for the given [offset] within
122-
/// the [compilationUnit].
123-
///
124-
/// Optionally, start the search from within [entryPoint] instead of using
125-
/// the [compilationUnit], which is useful for analyzing ASTs that have no
126-
/// [compilationUnit] such as dart expressions within angular templates.
127-
factory CompletionTarget.forOffset(
128-
CompilationUnit compilationUnit, int offset,
129-
{AstNode? entryPoint}) {
122+
/// the [entryPoint].
123+
factory CompletionTarget.forOffset(AstNode entryPoint, int offset) {
130124
// The precise algorithm is as follows. We perform a depth-first search of
131125
// all edges in the parse tree (both those that point to AST nodes and
132126
// those that point to tokens), visiting parents before children. The
@@ -141,7 +135,6 @@ class CompletionTarget {
141135
// prune the search to the point where no recursion is necessary; at each
142136
// step in the process we know exactly which child node we need to proceed
143137
// to.
144-
entryPoint ??= compilationUnit;
145138
var containingNode = entryPoint;
146139
outerLoop:
147140
while (true) {
@@ -197,7 +190,7 @@ class CompletionTarget {
197190
offset, docComment, commentToken, false);
198191
} else {
199192
return CompletionTarget._(
200-
offset, compilationUnit, commentToken, true);
193+
offset, entryPoint, commentToken, true);
201194
}
202195
}
203196
return CompletionTarget._(offset, containingNode, entity, false);
@@ -223,10 +216,12 @@ class CompletionTarget {
223216
assert(identical(containingNode, entryPoint));
224217

225218
// Check for comments on the EOF token (trailing comments in a file).
226-
var commentToken =
227-
_getContainingCommentToken(compilationUnit.endToken, offset);
228-
if (commentToken != null) {
229-
return CompletionTarget._(offset, compilationUnit, commentToken, true);
219+
if (entryPoint is CompilationUnit) {
220+
var commentToken =
221+
_getContainingCommentToken(entryPoint.endToken, offset);
222+
if (commentToken != null) {
223+
return CompletionTarget._(offset, entryPoint, commentToken, true);
224+
}
230225
}
231226

232227
// Since no completion target was found, we set the completion target

pkg/analyzer_plugin/lib/utilities/completion/inherited_reference_contributor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ class InheritedReferenceContributor
5959
CompletionTarget? target,
6060
OpType? optype,
6161
}) async {
62-
target ??= CompletionTarget.forOffset(request.result.unit, request.offset,
63-
entryPoint: entryPoint);
62+
entryPoint ??= request.result.unit;
63+
target ??= CompletionTarget.forOffset(entryPoint, request.offset);
6464
optype ??= OpType.forCompletion(target, request.offset);
6565
if (!optype.includeIdentifiers) {
6666
return;

pkg/analyzer_plugin/lib/utilities/completion/type_member_contributor.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TypeMemberContributor implements CompletionContributor {
2929
var containingLibrary = request.result.libraryElement;
3030

3131
// Recompute the target since resolution may have changed it
32-
var expression = _computeDotTarget(request, null);
32+
var expression = _computeDotTarget(request.result.unit, request.offset);
3333
if (expression == null || expression.isSynthetic) {
3434
return;
3535
}
@@ -42,18 +42,16 @@ class TypeMemberContributor implements CompletionContributor {
4242
var containingLibrary = request.result.libraryElement;
4343

4444
// Recompute the target since resolution may have changed it
45-
var expression = _computeDotTarget(request, entryPoint);
45+
var expression = _computeDotTarget(entryPoint, request.offset);
4646
if (expression == null || expression.isSynthetic) {
4747
return;
4848
}
4949
_computeSuggestions(request, collector, containingLibrary, expression);
5050
}
5151

5252
/// Update the completion [target] and [dotTarget] based on the given [unit].
53-
Expression? _computeDotTarget(
54-
DartCompletionRequest request, AstNode? entryPoint) {
55-
var target = CompletionTarget.forOffset(request.result.unit, request.offset,
56-
entryPoint: entryPoint);
53+
Expression? _computeDotTarget(AstNode entryPoint, int offset) {
54+
var target = CompletionTarget.forOffset(entryPoint, offset);
5755
return target.dotTarget;
5856
}
5957

0 commit comments

Comments
 (0)