Introduce an optional primary field to ExcerptRange

This commit is contained in:
Antonio Scandurra 2022-06-08 12:23:12 +02:00
parent ffb75b0f02
commit 15b13fe511
4 changed files with 66 additions and 8 deletions

View file

@ -351,6 +351,7 @@ impl ProjectDiagnosticsEditor {
buffer.clone(), buffer.clone(),
[ExcerptRange { [ExcerptRange {
context: excerpt_start..excerpt_end, context: excerpt_start..excerpt_end,
primary: Some(range.clone()),
}], }],
excerpts_cx, excerpts_cx,
) )

View file

@ -7721,6 +7721,7 @@ mod tests {
toml_buffer.clone(), toml_buffer.clone(),
[ExcerptRange { [ExcerptRange {
context: Point::new(0, 0)..Point::new(2, 0), context: Point::new(0, 0)..Point::new(2, 0),
primary: None,
}], }],
cx, cx,
); );
@ -7728,6 +7729,7 @@ mod tests {
rust_buffer.clone(), rust_buffer.clone(),
[ExcerptRange { [ExcerptRange {
context: Point::new(0, 0)..Point::new(1, 0), context: Point::new(0, 0)..Point::new(1, 0),
primary: None,
}], }],
cx, cx,
); );
@ -9602,9 +9604,11 @@ mod tests {
[ [
ExcerptRange { ExcerptRange {
context: Point::new(0, 0)..Point::new(0, 4), context: Point::new(0, 0)..Point::new(0, 4),
primary: None,
}, },
ExcerptRange { ExcerptRange {
context: Point::new(1, 0)..Point::new(1, 4), context: Point::new(1, 0)..Point::new(1, 4),
primary: None,
}, },
], ],
cx, cx,
@ -9645,7 +9649,7 @@ mod tests {
cccc)"}); cccc)"});
let excerpt_ranges = excerpt_ranges let excerpt_ranges = excerpt_ranges
.into_iter() .into_iter()
.map(|context| ExcerptRange { context }); .map(|context| ExcerptRange { context, primary: None });
let buffer = cx.add_model(|cx| Buffer::new(0, initial_text, cx)); let buffer = cx.add_model(|cx| Buffer::new(0, initial_text, cx));
let multibuffer = cx.add_model(|cx| { let multibuffer = cx.add_model(|cx| {
let mut multibuffer = MultiBuffer::new(0); let mut multibuffer = MultiBuffer::new(0);
@ -9701,9 +9705,11 @@ mod tests {
[ [
ExcerptRange { ExcerptRange {
context: Point::new(0, 0)..Point::new(1, 4), context: Point::new(0, 0)..Point::new(1, 4),
primary: None,
}, },
ExcerptRange { ExcerptRange {
context: Point::new(1, 0)..Point::new(2, 4), context: Point::new(1, 0)..Point::new(2, 4),
primary: None,
}, },
], ],
cx, cx,
@ -9789,9 +9795,11 @@ mod tests {
[ [
ExcerptRange { ExcerptRange {
context: Point::new(0, 0)..Point::new(1, 4), context: Point::new(0, 0)..Point::new(1, 4),
primary: None,
}, },
ExcerptRange { ExcerptRange {
context: Point::new(1, 0)..Point::new(2, 4), context: Point::new(1, 0)..Point::new(2, 4),
primary: None,
}, },
], ],
cx, cx,

View file

@ -496,9 +496,11 @@ mod tests {
[ [
ExcerptRange { ExcerptRange {
context: Point::new(0, 0)..Point::new(1, 4), context: Point::new(0, 0)..Point::new(1, 4),
primary: None,
}, },
ExcerptRange { ExcerptRange {
context: Point::new(2, 0)..Point::new(3, 2), context: Point::new(2, 0)..Point::new(3, 2),
primary: None,
}, },
], ],
cx, cx,

View file

@ -122,6 +122,7 @@ struct Excerpt {
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExcerptRange<T> { pub struct ExcerptRange<T> {
pub context: Range<T>, pub context: Range<T>,
pub primary: Option<Range<T>>,
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -224,6 +225,7 @@ impl MultiBuffer {
buffer, buffer,
[ExcerptRange { [ExcerptRange {
context: text::Anchor::MIN..text::Anchor::MAX, context: text::Anchor::MIN..text::Anchor::MAX,
primary: None,
}], }],
cx, cx,
); );
@ -722,6 +724,7 @@ impl MultiBuffer {
excerpt_ranges.push(ExcerptRange { excerpt_ranges.push(ExcerptRange {
context: excerpt_start..excerpt_end, context: excerpt_start..excerpt_end,
primary: Some(range),
}); });
range_counts.push(ranges_in_excerpt); range_counts.push(ranges_in_excerpt);
} }
@ -819,6 +822,10 @@ impl MultiBuffer {
let range = ExcerptRange { let range = ExcerptRange {
context: buffer_snapshot.anchor_before(&range.context.start) context: buffer_snapshot.anchor_before(&range.context.start)
..buffer_snapshot.anchor_after(&range.context.end), ..buffer_snapshot.anchor_after(&range.context.end),
primary: range.primary.map(|primary| {
buffer_snapshot.anchor_before(&primary.start)
..buffer_snapshot.anchor_after(&primary.end)
}),
}; };
let excerpt = Excerpt::new( let excerpt = Excerpt::new(
id.clone(), id.clone(),
@ -1418,6 +1425,7 @@ impl MultiBuffer {
let start_ix = buffer.clip_offset(rng.gen_range(0..=end_ix), Bias::Left); let start_ix = buffer.clip_offset(rng.gen_range(0..=end_ix), Bias::Left);
ExcerptRange { ExcerptRange {
context: start_ix..end_ix, context: start_ix..end_ix,
primary: None,
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -3142,6 +3150,7 @@ mod tests {
buffer_1.clone(), buffer_1.clone(),
[ExcerptRange { [ExcerptRange {
context: Point::new(1, 2)..Point::new(2, 5), context: Point::new(1, 2)..Point::new(2, 5),
primary: None,
}], }],
cx, cx,
); );
@ -3157,6 +3166,7 @@ mod tests {
buffer_1.clone(), buffer_1.clone(),
[ExcerptRange { [ExcerptRange {
context: Point::new(3, 3)..Point::new(4, 4), context: Point::new(3, 3)..Point::new(4, 4),
primary: None,
}], }],
cx, cx,
); );
@ -3164,6 +3174,7 @@ mod tests {
buffer_2.clone(), buffer_2.clone(),
[ExcerptRange { [ExcerptRange {
context: Point::new(3, 1)..Point::new(3, 3), context: Point::new(3, 1)..Point::new(3, 3),
primary: None,
}], }],
cx, cx,
); );
@ -3411,8 +3422,22 @@ mod tests {
let buffer_2 = cx.add_model(|cx| Buffer::new(0, "efghi", cx)); let buffer_2 = cx.add_model(|cx| Buffer::new(0, "efghi", cx));
let multibuffer = cx.add_model(|cx| { let multibuffer = cx.add_model(|cx| {
let mut multibuffer = MultiBuffer::new(0); let mut multibuffer = MultiBuffer::new(0);
multibuffer.push_excerpts(buffer_1.clone(), [ExcerptRange { context: 0..4 }], cx); multibuffer.push_excerpts(
multibuffer.push_excerpts(buffer_2.clone(), [ExcerptRange { context: 0..5 }], cx); buffer_1.clone(),
[ExcerptRange {
context: 0..4,
primary: None,
}],
cx,
);
multibuffer.push_excerpts(
buffer_2.clone(),
[ExcerptRange {
context: 0..5,
primary: None,
}],
cx,
);
multibuffer multibuffer
}); });
let old_snapshot = multibuffer.read(cx).snapshot(cx); let old_snapshot = multibuffer.read(cx).snapshot(cx);
@ -3462,7 +3487,14 @@ mod tests {
buffer_1.update(cx, |buffer, cx| buffer.edit([(4..4, "123")], cx)); buffer_1.update(cx, |buffer, cx| buffer.edit([(4..4, "123")], cx));
let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| { let excerpt_id_1 = multibuffer.update(cx, |multibuffer, cx| {
multibuffer multibuffer
.push_excerpts(buffer_1.clone(), [ExcerptRange { context: 0..7 }], cx) .push_excerpts(
buffer_1.clone(),
[ExcerptRange {
context: 0..7,
primary: None,
}],
cx,
)
.pop() .pop()
.unwrap() .unwrap()
}); });
@ -3477,9 +3509,18 @@ mod tests {
.push_excerpts( .push_excerpts(
buffer_2.clone(), buffer_2.clone(),
[ [
ExcerptRange { context: 0..4 }, ExcerptRange {
ExcerptRange { context: 6..10 }, context: 0..4,
ExcerptRange { context: 12..16 }, primary: None,
},
ExcerptRange {
context: 6..10,
primary: None,
},
ExcerptRange {
context: 12..16,
primary: None,
},
], ],
cx, cx,
) )
@ -3525,7 +3566,10 @@ mod tests {
.insert_excerpts_after( .insert_excerpts_after(
&excerpt_id_3, &excerpt_id_3,
buffer_2.clone(), buffer_2.clone(),
[ExcerptRange { context: 5..8 }], [ExcerptRange {
context: 5..8,
primary: None,
}],
cx, cx,
) )
.pop() .pop()
@ -3676,6 +3720,7 @@ mod tests {
buffer_handle.clone(), buffer_handle.clone(),
[ExcerptRange { [ExcerptRange {
context: start_ix..end_ix, context: start_ix..end_ix,
primary: None,
}], }],
cx, cx,
) )
@ -3989,6 +4034,7 @@ mod tests {
buffer_1.clone(), buffer_1.clone(),
[ExcerptRange { [ExcerptRange {
context: 0..buffer_1.read(cx).len(), context: 0..buffer_1.read(cx).len(),
primary: None,
}], }],
cx, cx,
); );
@ -3996,6 +4042,7 @@ mod tests {
buffer_2.clone(), buffer_2.clone(),
[ExcerptRange { [ExcerptRange {
context: 0..buffer_2.read(cx).len(), context: 0..buffer_2.read(cx).len(),
primary: None,
}], }],
cx, cx,
); );