Skip to content

Commit a26d653

Browse files
FMorschelCommit Queue
authored and
Commit Queue
committed
[DAS] Fixes keywords suggestion for patterns
Bug: #59854 Change-Id: I4e3b3ca51e08ea63e9516d6b1fca88f75895ade6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417143 Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 060e4e2 commit a26d653

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

pkg/analysis_server/lib/src/services/completion/dart/declaration_helper.dart

-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ class DeclarationHelper {
302302
);
303303
} else if (type is RecordType) {
304304
_addFieldsOfRecordType(type: type, excludedFields: excludedGetters);
305-
_addMembersOfDartCoreObject();
306305
}
307306
}
308307

pkg/analysis_server/lib/src/services/completion/dart/in_scope_completion_pass.dart

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import 'package:analysis_server/src/utilities/extensions/ast.dart';
1818
import 'package:analysis_server/src/utilities/extensions/completion_request.dart';
1919
import 'package:analysis_server/src/utilities/extensions/object.dart';
2020
import 'package:analyzer/dart/analysis/features.dart';
21-
import 'package:analyzer/dart/ast/ast.dart';
2221
import 'package:analyzer/dart/ast/syntactic_entity.dart';
2322
import 'package:analyzer/dart/ast/token.dart';
2423
import 'package:analyzer/dart/ast/visitor.dart';
@@ -2211,7 +2210,10 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
22112210
}
22122211
} else if (name.name == null) {
22132212
collector.completionLocation = 'PatternField_pattern';
2214-
_forVariablePattern();
2213+
var parent = node.thisOrAncestorOfType<PatternVariableDeclarationImpl>();
2214+
if (parent == null) {
2215+
_forVariablePattern();
2216+
}
22152217
_forPatternFieldName(name);
22162218
} else {
22172219
collector.completionLocation = 'PatternField_pattern';
@@ -3627,15 +3629,24 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
36273629
// Might be a start of a ObjectPattern.
36283630
// Might be a ConstantPattern.
36293631
if (coveringNode case SimpleIdentifier identifier) {
3630-
if (!identifier.isSynthetic) {
3631-
if (identifier.parent is ConstantPattern) {
3632-
keywordHelper.addPatternKeywords();
3633-
// TODO(scheglov): Actually we need constructors, but only const.
3634-
// And not-yet imported contributors does not work well yet.
3632+
if (identifier.isSynthetic) {
3633+
if (coveringNode.parent?.parent case PatternField _) {
3634+
keywordHelper.addVariablePatternKeywords();
36353635
collector.preferConstants = true;
3636-
declarationHelper(mustBeNonVoid: true).addLexicalDeclarations(node);
3636+
declarationHelper(
3637+
mustBeConstant: mustBeConst,
3638+
mustBeStatic: node.inStaticContext,
3639+
objectPatternAllowed: true,
3640+
).addLexicalDeclarations(node);
36373641
return;
36383642
}
3643+
} else if (identifier.parent is ConstantPattern) {
3644+
keywordHelper.addPatternKeywords();
3645+
// TODO(scheglov): Actually we need constructors, but only const.
3646+
// And not-yet imported contributors does not work well yet.
3647+
collector.preferConstants = true;
3648+
declarationHelper(mustBeNonVoid: true).addLexicalDeclarations(node);
3649+
return;
36393650
}
36403651
}
36413652

pkg/analysis_server/test/services/completion/dart/location/object_pattern_test.dart

-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ suggestions
5858
kind: field
5959
f11
6060
kind: field
61-
final
62-
kind: keyword
6361
g01
6462
kind: getter
6563
g11
6664
kind: getter
67-
var
68-
kind: keyword
6965
''');
7066
}
7167

pkg/analysis_server/test/services/completion/dart/location/record_pattern_test.dart

+27-13
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,12 @@ void f(({int f01, int f02, int g01}) x0) {
4141
(f01: ^) = x0;
4242
}
4343
''');
44-
// TODO(scheglov): This is wrong.
4544
assertResponse(r'''
4645
suggestions
4746
v01
4847
kind: localVariable
4948
x0
5049
kind: parameter
51-
false
52-
kind: keyword
53-
null
54-
kind: keyword
55-
true
56-
kind: keyword
57-
const
58-
kind: keyword
5950
final
6051
kind: keyword
6152
var
@@ -109,12 +100,8 @@ suggestions
109100
kind: identifier
110101
f02
111102
kind: identifier
112-
final
113-
kind: keyword
114103
g01
115104
kind: identifier
116-
var
117-
kind: keyword
118105
''');
119106
}
120107

@@ -277,6 +264,33 @@ suggestions
277264
kind: identifier
278265
f02
279266
kind: identifier
267+
''');
268+
}
269+
270+
Future<void> test_noObjectGetters() async {
271+
allowedIdentifiers = {'hashCode'};
272+
await computeSuggestions('''
273+
void f(({int f01, int f02}) record) {
274+
var (:^) = record;
275+
}
276+
''');
277+
assertResponse(r'''
278+
suggestions
279+
f01
280+
kind: identifier
281+
f02
282+
kind: identifier
283+
''');
284+
}
285+
286+
Future<void> test_noVarKeyword_afterVar() async {
287+
await computeSuggestions('''
288+
void f((int, int) record) {
289+
var (:^) = record;
290+
}
291+
''');
292+
assertResponse(r'''
293+
suggestions
280294
''');
281295
}
282296
}

0 commit comments

Comments
 (0)