Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 890f98f

Browse files
committed
internal: Allow explicitly specifying end of fixture annotation
1 parent 828196b commit 890f98f

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

crates/test_utils/src/lib.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String {
212212
/// // ^^^ first line
213213
/// // | second line
214214
///
215+
/// Trailing whitespace is sometimes desired but usually stripped by the editor
216+
/// if at the end of a line, or incorrectly sized if followed by another
217+
/// annotation. In those cases the annotation can be explicitly ended with the
218+
/// `$` character.
219+
///
220+
/// // ^^^ trailing-ws-wanted $
221+
///
215222
/// Annotations point to the last line that actually was long enough for the
216223
/// range, not counting annotations themselves. So overlapping annotations are
217224
/// possible:
@@ -229,9 +236,10 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
229236
let mut prev_line_annotations: Vec<(TextSize, usize)> = Vec::new();
230237
for line in text.split_inclusive('\n') {
231238
let mut this_line_annotations = Vec::new();
232-
let line_length = if let Some(idx) = line.find("//") {
233-
let annotation_offset = TextSize::of(&line[..idx + "//".len()]);
234-
for annotation in extract_line_annotations(&line[idx + "//".len()..]) {
239+
let line_length = if let Some((prefix, suffix)) = line.split_once("//") {
240+
let ss_len = TextSize::of("//");
241+
let annotation_offset = TextSize::of(prefix) + ss_len;
242+
for annotation in extract_line_annotations(suffix.trim_end_matches('\n')) {
235243
match annotation {
236244
LineAnnotation::Annotation { mut range, content, file } => {
237245
range += annotation_offset;
@@ -257,7 +265,7 @@ pub fn extract_annotations(text: &str) -> Vec<(TextRange, String)> {
257265
}
258266
}
259267
}
260-
idx.try_into().unwrap()
268+
annotation_offset
261269
} else {
262270
TextSize::of(line)
263271
};
@@ -294,16 +302,29 @@ fn extract_line_annotations(mut line: &str) -> Vec<LineAnnotation> {
294302
len = 1;
295303
}
296304
let range = TextRange::at(offset, len.try_into().unwrap());
297-
let next = line[len..].find(marker).map_or(line.len(), |it| it + len);
298-
let mut content = &line[len..][..next - len];
305+
let line_no_caret = &line[len..];
306+
let end_marker = line_no_caret.find(|c| c == '$');
307+
let next = line_no_caret.find(marker).map_or(line.len(), |it| it + len);
308+
309+
let mut content = match end_marker {
310+
Some(end_marker)
311+
if end_marker < next
312+
&& line_no_caret[end_marker..]
313+
.strip_prefix(|c: char| c.is_whitespace() || c == '^')
314+
.is_some() =>
315+
{
316+
&line_no_caret[..end_marker]
317+
}
318+
_ => line_no_caret[..next - len].trim_end(),
319+
};
299320

300321
let mut file = false;
301322
if !continuation && content.starts_with("file") {
302323
file = true;
303324
content = &content["file".len()..];
304325
}
305326

306-
let content = content.trim().to_string();
327+
let content = content.trim_start().to_string();
307328

308329
let annotation = if continuation {
309330
LineAnnotation::Continuation { offset: range.end(), content }

0 commit comments

Comments
 (0)