Skip to content

Commit db2fad7

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Update setState + closure completions to use the correct line endings
+ update completion tests to always use normalized code Change-Id: If6e2f838f23c8cdbb0590a7e408d51897d8b4a46 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443560 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent dc97aa7 commit db2fad7

File tree

10 files changed

+443
-366
lines changed

10 files changed

+443
-366
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ final class ClosureSuggestion extends CandidateSuggestion with SuggestionData {
8181
/// The identation to be used for a multi-line completion.
8282
final String indent;
8383

84+
/// The EOL marker to be used for a multi-line completion.
85+
final String endOfLine;
86+
8487
/// Initialize a newly created candidate suggestion to suggest a closure that
8588
/// conforms to the given [functionType].
8689
///
@@ -92,6 +95,7 @@ final class ClosureSuggestion extends CandidateSuggestion with SuggestionData {
9295
required super.matcherScore,
9396
required this.includeTypes,
9497
required this.indent,
98+
required this.endOfLine,
9599
this.useBlockStatement = true,
96100
});
97101

@@ -125,10 +129,11 @@ final class ClosureSuggestion extends CandidateSuggestion with SuggestionData {
125129
int selectionOffset;
126130
if (useBlockStatement) {
127131
displayText = '$parametersDisplayString {}';
128-
stringBuffer.writeln(' {');
132+
stringBuffer.write(' {');
133+
stringBuffer.write(endOfLine);
129134
stringBuffer.write('$indent ');
130135
selectionOffset = stringBuffer.length;
131-
stringBuffer.writeln();
136+
stringBuffer.write(endOfLine);
132137
stringBuffer.write('$indent}');
133138
} else {
134139
displayText = '$parametersDisplayString =>';
@@ -981,11 +986,15 @@ final class SetStateMethodSuggestion extends TypedExecutableSuggestion
981986
/// The identation to be used for a multi-line completion.
982987
final String indent;
983988

989+
/// The EOL marker to be used for a multi-line completion.
990+
final String endOfLine;
991+
984992
/// Initialize a newly created candidate suggestion to suggest the [element].
985993
SetStateMethodSuggestion({
986994
required this.element,
987995
required this.referencingInterface,
988996
required this.indent,
997+
required this.endOfLine,
989998
required super.importData,
990999
required super.matcherScore,
9911000
required super.replacementRange,
@@ -1011,10 +1020,11 @@ final class SetStateMethodSuggestion extends TypedExecutableSuggestion
10111020
// Build the completion and the selection offset.
10121021
var buffer = StringBuffer();
10131022

1014-
buffer.writeln('setState(() {');
1023+
buffer.write('setState(() {');
1024+
buffer.write(endOfLine);
10151025
buffer.write('$indent ');
10161026
var selectionOffset = buffer.length;
1017-
buffer.writeln();
1027+
buffer.write(endOfLine);
10181028
buffer.write('$indent});');
10191029
var completion = buffer.toString();
10201030
_data = _Data(completion, selectionOffset, displayText: 'setState(() {});');

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
57
import 'package:analysis_server/src/services/completion/dart/completion_manager.dart';
68
import 'package:analysis_server/src/services/completion/dart/utilities.dart';
79
import 'package:analysis_server_plugin/src/utilities/selection.dart';
@@ -11,6 +13,7 @@ import 'package:analyzer/dart/ast/ast.dart';
1113
import 'package:analyzer/dart/element/element.dart';
1214
import 'package:analyzer/dart/element/type.dart';
1315
import 'package:analyzer/src/utilities/completion_matcher.dart';
16+
import 'package:analyzer_plugin/src/utilities/extensions/string_extension.dart';
1417

1518
/// The information used to compute the suggestions for a completion request.
1619
class CompletionState {
@@ -47,6 +50,9 @@ class CompletionState {
4750
return selection.coveringNode.thisOrAncestorOfType<ClassMember>();
4851
}
4952

53+
/// The EOL marker for the completion text.
54+
String get endOfLine => request.content.endOfLine ?? Platform.lineTerminator;
55+
5056
/// Indicates if types should be specified whenever possible.
5157
bool get includeTypes => codeStyleOptions.specifyTypes;
5258

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,7 @@ class DeclarationHelper {
20582058
referencingInterface: referencingInterface,
20592059
matcherScore: matcherScore,
20602060
indent: state.indent,
2061+
endOfLine: state.endOfLine,
20612062
addTypeAnnotation: addTypeAnnotation,
20622063
keyword: keyword,
20632064
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
33003300
matcherScore: 0.0,
33013301
includeTypes: includeTypes,
33023302
indent: state.indent,
3303+
endOfLine: state.endOfLine,
33033304
),
33043305
);
33053306
collector.addSuggestion(
@@ -3310,6 +3311,7 @@ class InScopeCompletionPass extends SimpleAstVisitor<void> {
33103311
useBlockStatement: false,
33113312
includeTypes: includeTypes,
33123313
indent: state.indent,
3314+
endOfLine: state.endOfLine,
33133315
),
33143316
);
33153317
}

0 commit comments

Comments
 (0)