[analysis_server] Include types in closure completions when always_specify_types is enabled

Fixes https://github.com/Dart-Code/Dart-Code/issues/5149

Change-Id: Ic212e13d9fe90b4c728f92ecdf8fd5ca519047ed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372426
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2024-06-21 20:43:37 +00:00 committed by Commit Queue
parent 793194e2b9
commit a42ae0c906
3 changed files with 44 additions and 8 deletions

View file

@ -351,13 +351,16 @@ class SuggestionBuilder {
/// If [includeTrailingComma] is `true` then the completion text will include
/// a trailing comma, such as when the closure is part of an argument list.
void suggestClosure(FunctionType type, {bool includeTrailingComma = false}) {
var includeTypes =
request.fileState.analysisOptions.codeStyleOptions.specifyTypes;
var indent = getRequestLineIndent(request);
var parametersString = buildClosureParameters(type);
// Build a version of the parameter string without keywords for the
// completion label because `required` is less useful and may push the
// end of the completion (`=>` vs `() {}`) off the end.
var parametersDisplayString =
buildClosureParameters(type, includeKeywords: false);
var parametersString = buildClosureParameters(type,
includeTypes: includeTypes, includeKeywords: true);
// Build a short version of the parameter string without keywords or types
// for the completion label because they're less useful there and may push
// the end of the completion (`=>` vs `() {}`) off the end.
var parametersDisplayString = buildClosureParameters(type,
includeKeywords: false, includeTypes: false);
var blockBuffer = StringBuffer(parametersString);
blockBuffer.writeln(' {');

View file

@ -31,8 +31,11 @@ Comparator<CompletionSuggestionBuilder> completionComparator = (a, b) {
return b.relevance.compareTo(a.relevance);
};
String buildClosureParameters(FunctionType type,
{bool includeKeywords = true}) {
String buildClosureParameters(
FunctionType type, {
required bool includeTypes,
required bool includeKeywords,
}) {
var buffer = StringBuffer();
buffer.write('(');
@ -52,6 +55,10 @@ String buildClosureParameters(FunctionType type,
hasOptionalPositional = true;
buffer.write('[');
}
if (includeTypes) {
buffer.write(parameter.type);
buffer.write(' ');
}
var name = parameter.name;
if (name.isEmpty) {
name = 'p$i';

View file

@ -1,5 +1,8 @@
import 'package:analysis_server/src/services/linter/lint_names.dart';
import 'package:linter/src/rules.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../../../analysis_server_base.dart';
import '../../../../client/completion_driver_test.dart';
void main() {
@ -116,6 +119,29 @@ ${' ' * 4}
''');
}
Future<void> test_lint_alwaysSpecifyTypes() async {
registerLintRules();
writeTestPackageAnalysisOptionsFile(
AnalysisOptionsFileConfig(lints: [LintNames.always_specify_types]),
);
await computeSuggestions('''
void Function(List<int> a, Object? b, [dynamic c]) v = ^;
''');
assertResponse('''
suggestions
|(List<int> a, Object? b, [dynamic c]) => |
kind: invocation
displayText: (a, b, [c]) =>
(List<int> a, Object? b, [dynamic c]) {
${' ' * 2}
}
kind: invocation
displayText: (a, b, [c]) {}
selection: 42
''');
}
Future<void> test_parameters_optionalNamed() async {
await computeSuggestions('''
void f({void Function(int a, {int b, int c}) closure}) {}