Skip to content

Commit

Permalink
Version 3.3.0-89.0.dev
Browse files Browse the repository at this point in the history
Merge 248afd5 into dev
  • Loading branch information
Dart CI committed Nov 1, 2023
2 parents a11b7ca + 248afd5 commit 15101f5
Show file tree
Hide file tree
Showing 111 changed files with 1,210 additions and 1,191 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ vars = {
"vector_math_rev": "294896dedc6da2a736f47c3c6a19643df934641c",
"watcher_rev": "6ad58dcbbf328fbecf18d6ad2357c67be300b489",
"web_socket_channel_rev": "f3ac1bf2bd3c93eb6d5d78646ff7de31797f4cf6",
"webdev_rev": "50534a1299d1fdcac065ac337abcad26886ff029",
"webdev_rev": "5ad79c240b000a50057612d6af4573f6e649f65c", # https://github.com/dart-lang/webdev/issues/2276
"webdriver_rev": "eaf9c582e6e72c3551d3a875b2d522cd1ad06593",
"webkit_inspection_protocol_rev": "82f0c1c46dfdba5edf7c5fa84456233121dd69e1",
"yaml_rev": "9f0d64934c07bc27438074616455618b7103582d",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ final class LabelSuggestion extends CandidateSuggestion {
/// The information about a candidate suggestion based on a local function.
final class LocalFunctionSuggestion extends CandidateSuggestion {
/// The element on which the suggestion is based.
final ExecutableElement element;
final FunctionElement element;

/// Initialize a newly created candidate suggestion to suggest the [element].
LocalFunctionSuggestion(this.element);
Expand Down Expand Up @@ -160,8 +160,7 @@ extension SuggestionBuilderExtension on SuggestionBuilder {
case LabelSuggestion():
suggestLabel(suggestion.label);
case LocalFunctionSuggestion():
// TODO(brianwilkerson) Add support for suggesting a local function.
break;
suggestTopLevelFunction(suggestion.element);
case LocalVariableSuggestion():
// TODO(brianwilkerson) Enhance `suggestLocalVariable` to allow the
// distance to be passed in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import 'package:analysis_server/src/services/completion/dart/super_formal_contri
import 'package:analysis_server/src/services/completion/dart/type_member_contributor.dart';
import 'package:analysis_server/src/services/completion/dart/uri_contributor.dart';
import 'package:analysis_server/src/services/completion/dart/variable_name_contributor.dart';
import 'package:analysis_server/src/services/completion/dart/visibility_tracker.dart';
import 'package:analysis_server/src/utilities/selection.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/analysis/results.dart';
Expand Down Expand Up @@ -145,6 +146,7 @@ class DartCompletionManager {
// Compute the list of contributors that will be run.
var builder =
SuggestionBuilder(request, useFilter: useFilter, listener: listener);
var localReferenceContributor = LocalReferenceContributor(request, builder);
var contributors = <DartCompletionContributor>[
ArgListContributor(request, builder),
ClosureContributor(request, builder),
Expand All @@ -155,7 +157,7 @@ class DartCompletionManager {
LibraryMemberContributor(request, builder),
LibraryPrefixContributor(request, builder),
LocalLibraryContributor(request, builder),
LocalReferenceContributor(request, builder),
localReferenceContributor,
NamedConstructorContributor(request, builder),
if (enableOverrideContributor) OverrideContributor(request, builder),
RecordLiteralContributor(request, builder),
Expand Down Expand Up @@ -185,12 +187,16 @@ class DartCompletionManager {
}

try {
VisibilityTracker? visibilityTracker;
await performance.runAsync(
'InScopeCompletionPass',
(performance) async {
_runFirstPass(request, builder);
visibilityTracker = _runFirstPass(request, builder);
},
);
if (visibilityTracker != null) {
localReferenceContributor.visibilityTracker = visibilityTracker!;
}
for (var contributor in contributors) {
await performance.runAsync(
'${contributor.runtimeType}',
Expand Down Expand Up @@ -298,7 +304,10 @@ class DartCompletionManager {
}

// Run the first pass of the code completion algorithm.
void _runFirstPass(DartCompletionRequest request, SuggestionBuilder builder) {
VisibilityTracker? _runFirstPass(
DartCompletionRequest request, SuggestionBuilder builder) {
// TODO(brianwilkerson) Stop returning the visibility tracker when the
// `LocalReferenceContributor` has been deleted.
var collector = SuggestionCollector();
var selection = request.unit.select(offset: request.offset, length: 0);
if (selection == null) {
Expand All @@ -308,6 +317,7 @@ class DartCompletionManager {
var pass = InScopeCompletionPass(state: state, collector: collector);
pass.computeSuggestions();
builder.suggestFromCandidates(collector.suggestions);
return pass.visibilityTracker;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:analysis_server/src/services/completion/dart/candidate_suggestion.dart';
import 'package:analysis_server/src/services/completion/dart/suggestion_collector.dart';
import 'package:analysis_server/src/services/completion/dart/visibility_tracker.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
Expand All @@ -23,7 +24,7 @@ class DeclarationHelper {

/// The visibility tracker used to prevent suggesting elements that have been
/// shadowed by local declarations.
final _VisibilityTracker _visibilityTracker = _VisibilityTracker();
final VisibilityTracker visibilityTracker = VisibilityTracker();

/// A flag indicating whether suggestions should be limited to only include
/// valid constants.
Expand Down Expand Up @@ -73,6 +74,9 @@ class DeclarationHelper {
case FieldDeclaration():
return currentNode;
case ForElement(forLoopParts: var parts):
if (parts != previousNode) {
_visitForLoopParts(parts);
}
case ForStatement(forLoopParts: var parts):
if (parts != previousNode) {
_visitForLoopParts(parts);
Expand Down Expand Up @@ -117,7 +121,7 @@ class DeclarationHelper {

/// Add a suggestion for the local function represented by the [element].
void _suggestFunction(ExecutableElement element) {
if (_visibilityTracker.isVisible(element)) {
if (element is FunctionElement && visibilityTracker.isVisible(element)) {
var suggestion = LocalFunctionSuggestion(element);
collector.addSuggestion(suggestion);
}
Expand All @@ -128,7 +132,7 @@ class DeclarationHelper {
if (mustBeConstant && !element.isConst) {
return;
}
if (_visibilityTracker.isVisible(element) && !_isUnused(element.name)) {
if (visibilityTracker.isVisible(element) && !_isUnused(element.name)) {
var suggestion = FormalParameterSuggestion(element);
collector.addSuggestion(suggestion);
}
Expand All @@ -139,7 +143,7 @@ class DeclarationHelper {
if (mustBeConstant && !element.isConst) {
return;
}
if (_visibilityTracker.isVisible(element)) {
if (visibilityTracker.isVisible(element)) {
var suggestion = LocalVariableSuggestion(element, _variableDistance++);
collector.addSuggestion(suggestion);
}
Expand All @@ -162,6 +166,8 @@ class DeclarationHelper {
switch (member) {
case ConstructorDeclaration():
_visitParameterList(member.parameters);
case FunctionDeclaration():
_visitParameterList(member.functionExpression.parameters);
case FunctionExpression():
_visitParameterList(member.parameters);
case MethodDeclaration():
Expand Down Expand Up @@ -363,19 +369,3 @@ class DeclarationHelper {
}
}
}

/// This class tracks the set of names already added in the completion list in
/// order to prevent suggesting elements that have been shadowed by local
/// declarations.
class _VisibilityTracker {
/// The set of known previously declared names in this contributor.
final Set<String> declaredNames = {};

/// Before completions are added by this contributor, we verify with this
/// method if the element has already been added, this prevents suggesting
/// [Element]s that are shadowed.
bool isVisible(Element? element) {
var name = element?.name;
return name != null && declaredNames.add(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analysis_server/src/services/completion/dart/declaration_helper.
import 'package:analysis_server/src/services/completion/dart/keyword_helper.dart';
import 'package:analysis_server/src/services/completion/dart/label_helper.dart';
import 'package:analysis_server/src/services/completion/dart/suggestion_collector.dart';
import 'package:analysis_server/src/services/completion/dart/visibility_tracker.dart';
import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
Expand All @@ -32,17 +33,16 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
/// The suggestion collector to which suggestions will be added.
final SuggestionCollector collector;

/// The helper used to suggest declarations that are in scope.
late final DeclarationHelper declarationHelper =
DeclarationHelper(collector: collector, offset: offset);

/// The helper used to suggest keywords.
late final KeywordHelper keywordHelper = KeywordHelper(
collector: collector, featureSet: featureSet, offset: offset);

/// The helper used to suggest labels.
late final LabelHelper labelHelper = LabelHelper(collector: collector);

/// The helper used to suggest declarations that are in scope.
DeclarationHelper? _declarationHelper;

/// Initialize a newly created completion visitor that can use the [state] to
/// add candidate suggestions to the [collector].
InScopeCompletionPass({required this.state, required this.collector});
Expand All @@ -54,6 +54,10 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
/// Return the offset at which completion was requested.
int get offset => state.selection.offset;

/// Returns the visibility tracker used by this pass.
VisibilityTracker? get visibilityTracker =>
_declarationHelper?.visibilityTracker;

/// Return the node that should be used as the context in which completion is
/// occurring.
///
Expand Down Expand Up @@ -95,6 +99,10 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
_completionNode.accept(this);
}

/// Return the helper used to suggest declarations that are in scope.
DeclarationHelper declarationHelper() => _declarationHelper =
DeclarationHelper(collector: collector, offset: offset);

@override
void visitAdjacentStrings(AdjacentStrings node) {
_visitParentIfAtOrBeforeNode(node);
Expand Down Expand Up @@ -295,7 +303,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {

@override
void visitCommentReference(CommentReference node) {
declarationHelper.addLexicalDeclarations(node);
declarationHelper().addLexicalDeclarations(node);
}

@override
Expand Down Expand Up @@ -829,7 +837,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {

@override
void visitInterpolationExpression(InterpolationExpression node) {
declarationHelper.addLexicalDeclarations(node);
declarationHelper().addLexicalDeclarations(node);
}

@override
Expand Down Expand Up @@ -1512,7 +1520,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
TypedLiteral literal, NodeList<CollectionElement> elements) {
var preceedingElement = elements.elementBefore(offset);
keywordHelper.addCollectionElementKeywords(literal, elements);
declarationHelper.addLexicalDeclarations(preceedingElement ?? literal);
declarationHelper().addLexicalDeclarations(preceedingElement ?? literal);
}

/// Add the suggestions that are appropriate when the selection is at the
Expand All @@ -1539,7 +1547,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
var inConstantContext = node is Expression && node.inConstantContext;
keywordHelper.addConstantExpressionKeywords(
inConstantContext: inConstantContext);
declarationHelper.addLexicalDeclarations(node, mustBeConstant: true);
declarationHelper().addLexicalDeclarations(node, mustBeConstant: true);
}

/// Add the suggestions that are appropriate when the selection is at the
Expand All @@ -1561,7 +1569,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
void _forExpression(AstNode? node) {
keywordHelper.addExpressionKeywords(node);
if (node != null) {
declarationHelper.addLexicalDeclarations(node);
declarationHelper().addLexicalDeclarations(node);
}
}

Expand Down Expand Up @@ -1652,7 +1660,8 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
/// beginning of a pattern.
void _forPattern(AstNode node, {bool mustBeConst = true}) {
keywordHelper.addPatternKeywords();
declarationHelper.addLexicalDeclarations(node, mustBeConstant: mustBeConst);
declarationHelper()
.addLexicalDeclarations(node, mustBeConstant: mustBeConst);
}

/// Add the suggestions that are appropriate when the selection is at the
Expand Down Expand Up @@ -1804,7 +1813,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
}
} else if (!node.inKeyword.isSynthetic) {
keywordHelper.addKeyword(Keyword.AWAIT);
declarationHelper.addLexicalDeclarations(node);
declarationHelper().addLexicalDeclarations(node);
}
}

Expand Down
Loading

0 comments on commit 15101f5

Please sign in to comment.