Skip to content

Commit 05f2c44

Browse files
DanTupCommit Queue
authored andcommitted
[analysis_server] Switch selections in tests to markup instead of hard-coded numbers
See #60234 Change-Id: I39c9382acecd16d50d4af098fe5f19d49e024ef6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/453041 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 08dfbab commit 05f2c44

17 files changed

+280
-507
lines changed

pkg/analysis_server/test/services/completion/dart/completion_printer.dart

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,45 @@ class CompletionResponsePrinter {
160160
_indent = indent;
161161
}
162162

163+
/// Writes the completion text, marked with a `[!range!]` or `^position`
164+
/// indicating the selection.
163165
void _writeCompletion(CompletionSuggestion suggestion) {
164166
var completion = suggestion.completion;
167+
168+
var annotatedCompletion = completion;
169+
170+
// Show the selection in the completion text using `[!range!]` or
171+
// `^position` markers.
172+
if (configuration.withSelection) {
173+
annotatedCompletion = switch ((
174+
suggestion.selectionOffset,
175+
suggestion.selectionLength,
176+
)) {
177+
// We don't annotate positions at the end of the string
178+
(int offset, 0) when offset == suggestion.completion.length =>
179+
completion,
180+
// Position
181+
(int offset, 0) => [
182+
completion.substring(0, offset),
183+
'^',
184+
completion.substring(offset),
185+
].join(),
186+
// Range
187+
(int offset, int length) => [
188+
completion.substring(0, offset),
189+
'[!',
190+
completion.substring(offset, offset + length),
191+
'!]',
192+
completion.substring(offset + length),
193+
].join(),
194+
};
195+
}
196+
165197
if (RegExp(r'^\s').hasMatch(completion) ||
166198
RegExp(r'\s$').hasMatch(completion)) {
167-
_writelnWithIndent('|$completion|');
199+
_writelnWithIndent('|$annotatedCompletion|');
168200
} else {
169-
_writelnWithIndent(completion);
201+
_writelnWithIndent(annotatedCompletion);
170202
}
171203
}
172204

@@ -319,18 +351,6 @@ class CompletionResponsePrinter {
319351
}
320352
}
321353

322-
void _writeSelection(CompletionSuggestion suggestion) {
323-
if (configuration.withSelection) {
324-
var offset = suggestion.selectionOffset;
325-
var length = suggestion.selectionLength;
326-
if (length != 0) {
327-
_writelnWithIndent('selection: $offset $length');
328-
} else if (offset != suggestion.completion.length) {
329-
_writelnWithIndent('selection: $offset');
330-
}
331-
}
332-
}
333-
334354
void _writeSuggestion(CompletionSuggestion suggestion) {
335355
_writeCompletion(suggestion);
336356
_withIndent(() {
@@ -347,7 +367,6 @@ class CompletionResponsePrinter {
347367
_writeParameterNames(suggestion);
348368
_writeRelevance(suggestion);
349369
_writeReturnType(suggestion);
350-
_writeSelection(suggestion);
351370
});
352371
}
353372

pkg/analysis_server/test/services/completion/dart/completion_test.dart

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,10 @@ class Fclass extends Bclass with Eclass {}
323323
replacement
324324
right: 5
325325
suggestions
326-
import '';
326+
import '^';
327327
kind: keyword
328-
selection: 8
329-
export '';
328+
export '^';
330329
kind: keyword
331-
selection: 8
332330
abstract
333331
kind: keyword
334332
base
@@ -357,12 +355,10 @@ suggestions
357355
kind: keyword
358356
mixin
359357
kind: keyword
360-
part '';
358+
part '^';
361359
kind: keyword
362-
selection: 6
363-
part of '';
360+
part of '^';
364361
kind: keyword
365-
selection: 9
366362
sealed
367363
kind: keyword
368364
typedef
@@ -4113,35 +4109,30 @@ replacement
41134109
suggestions
41144110
@override
41154111
// TODO: implement hashCode
4116-
int get hashCode => super.hashCode;
4112+
int get hashCode => [!super.hashCode!];
41174113
kind: override
4118-
selection: 62 14
41194114
@override
41204115
// TODO: implement runtimeType
4121-
Type get runtimeType => super.runtimeType;
4116+
Type get runtimeType => [!super.runtimeType!];
41224117
kind: override
4123-
selection: 69 17
41244118
@override
41254119
String toString() {
41264120
// TODO: implement toString
4127-
return super.toString();
4121+
[!return super.toString();!]
41284122
}
41294123
kind: override
4130-
selection: 68 24
41314124
@override
41324125
bool operator ==(Object other) {
41334126
// TODO: implement ==
4134-
return super == other;
4127+
[!return super == other;!]
41354128
}
41364129
kind: override
4137-
selection: 75 22
41384130
@override
41394131
noSuchMethod(Invocation invocation) {
41404132
// TODO: implement noSuchMethod
4141-
return super.noSuchMethod(invocation);
4133+
[!return super.noSuchMethod(invocation);!]
41424134
}
41434135
kind: override
4144-
selection: 90 38
41454136
A
41464137
kind: class
41474138
final
@@ -4583,35 +4574,30 @@ class x {^}
45834574
suggestions
45844575
@override
45854576
// TODO: implement hashCode
4586-
int get hashCode => super.hashCode;
4577+
int get hashCode => [!super.hashCode!];
45874578
kind: override
4588-
selection: 62 14
45894579
@override
45904580
// TODO: implement runtimeType
4591-
Type get runtimeType => super.runtimeType;
4581+
Type get runtimeType => [!super.runtimeType!];
45924582
kind: override
4593-
selection: 69 17
45944583
@override
45954584
String toString() {
45964585
// TODO: implement toString
4597-
return super.toString();
4586+
[!return super.toString();!]
45984587
}
45994588
kind: override
4600-
selection: 68 24
46014589
@override
46024590
bool operator ==(Object other) {
46034591
// TODO: implement ==
4604-
return super == other;
4592+
[!return super == other;!]
46054593
}
46064594
kind: override
4607-
selection: 75 22
46084595
@override
46094596
noSuchMethod(Invocation invocation) {
46104597
// TODO: implement noSuchMethod
4611-
return super.noSuchMethod(invocation);
4598+
[!return super.noSuchMethod(invocation);!]
46124599
}
46134600
kind: override
4614-
selection: 90 38
46154601
num
46164602
kind: class
46174603
final
@@ -9053,9 +9039,8 @@ import^
90539039
replacement
90549040
left: 6
90559041
suggestions
9056-
import '';
9042+
import '^';
90579043
kind: keyword
9058-
selection: 8
90599044
''');
90609045
}
90619046

@@ -9106,10 +9091,9 @@ suggestions
91069091
@override
91079092
String toString() {
91089093
// TODO: implement toString
9109-
return super.toString();
9094+
[!return super.toString();!]
91109095
}
91119096
kind: override
9112-
selection: 68 24
91139097
String
91149098
kind: class
91159099
abstract
@@ -9678,35 +9662,30 @@ class Collection{}class List extends Collection{}class Foo { ^ }
96789662
suggestions
96799663
@override
96809664
// TODO: implement hashCode
9681-
int get hashCode => super.hashCode;
9665+
int get hashCode => [!super.hashCode!];
96829666
kind: override
9683-
selection: 62 14
96849667
@override
96859668
// TODO: implement runtimeType
9686-
Type get runtimeType => super.runtimeType;
9669+
Type get runtimeType => [!super.runtimeType!];
96879670
kind: override
9688-
selection: 69 17
96899671
@override
96909672
String toString() {
96919673
// TODO: implement toString
9692-
return super.toString();
9674+
[!return super.toString();!]
96939675
}
96949676
kind: override
9695-
selection: 68 24
96969677
@override
96979678
bool operator ==(Object other) {
96989679
// TODO: implement ==
9699-
return super == other;
9680+
[!return super == other;!]
97009681
}
97019682
kind: override
9702-
selection: 75 22
97039683
@override
97049684
noSuchMethod(Invocation invocation) {
97059685
// TODO: implement noSuchMethod
9706-
return super.noSuchMethod(invocation);
9686+
[!return super.noSuchMethod(invocation);!]
97079687
}
97089688
kind: override
9709-
selection: 90 38
97109689
Collection
97119690
kind: class
97129691
List
@@ -9747,35 +9726,30 @@ class Collection{}class List extends Collection{}class Foo {^}
97479726
suggestions
97489727
@override
97499728
// TODO: implement hashCode
9750-
int get hashCode => super.hashCode;
9729+
int get hashCode => [!super.hashCode!];
97519730
kind: override
9752-
selection: 62 14
97539731
@override
97549732
// TODO: implement runtimeType
9755-
Type get runtimeType => super.runtimeType;
9733+
Type get runtimeType => [!super.runtimeType!];
97569734
kind: override
9757-
selection: 69 17
97589735
@override
97599736
String toString() {
97609737
// TODO: implement toString
9761-
return super.toString();
9738+
[!return super.toString();!]
97629739
}
97639740
kind: override
9764-
selection: 68 24
97659741
@override
97669742
bool operator ==(Object other) {
97679743
// TODO: implement ==
9768-
return super == other;
9744+
[!return super == other;!]
97699745
}
97709746
kind: override
9771-
selection: 75 22
97729747
@override
97739748
noSuchMethod(Invocation invocation) {
97749749
// TODO: implement noSuchMethod
9775-
return super.noSuchMethod(invocation);
9750+
[!return super.noSuchMethod(invocation);!]
97769751
}
97779752
kind: override
9778-
selection: 90 38
97799753
Collection
97809754
kind: class
97819755
List
@@ -9840,16 +9814,14 @@ replacement
98409814
suggestions
98419815
@override
98429816
// TODO: implement hashCode
9843-
int get hashCode => super.hashCode;
9817+
int get hashCode => [!super.hashCode!];
98449818
kind: override
9845-
selection: 62 14
98469819
@override
98479820
noSuchMethod(Invocation invocation) {
98489821
// TODO: implement noSuchMethod
9849-
return super.noSuchMethod(invocation);
9822+
[!return super.noSuchMethod(invocation);!]
98509823
}
98519824
kind: override
9852-
selection: 90 38
98539825
Collection
98549826
kind: class
98559827
static

0 commit comments

Comments
 (0)