Remove unsafe code from App::test_async
I don't actually think it was correct to allow the future to borrow a mutable app reference. I went back to passing a wrapper around the refcell to async tests. They'll be a bit more annoying to write but also totally safe.
This commit is contained in:
@@ -125,7 +125,7 @@ impl BufferView {
|
||||
});
|
||||
ctx.observe(&display_map, Self::on_display_map_changed);
|
||||
|
||||
let buffer_ref = buffer.as_ref(ctx);
|
||||
let buffer_ref = buffer.read(ctx);
|
||||
Self {
|
||||
handle: ctx.handle().downgrade(),
|
||||
buffer,
|
||||
@@ -188,7 +188,7 @@ impl BufferView {
|
||||
return false;
|
||||
}
|
||||
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
let visible_lines = viewport_height / line_height;
|
||||
let first_cursor_top = self
|
||||
.selections
|
||||
@@ -238,7 +238,7 @@ impl BufferView {
|
||||
layouts: &[Arc<text_layout::Line>],
|
||||
app: &AppContext,
|
||||
) {
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
|
||||
let mut target_left = std::f32::INFINITY;
|
||||
let mut target_right = 0.0_f32;
|
||||
@@ -287,7 +287,7 @@ impl BufferView {
|
||||
ctx.emit(Event::Activate);
|
||||
}
|
||||
|
||||
let display_map = self.display_map.as_ref(ctx);
|
||||
let display_map = self.display_map.read(ctx);
|
||||
let cursor = display_map
|
||||
.anchor_before(position, Bias::Left, ctx.app())
|
||||
.unwrap();
|
||||
@@ -312,8 +312,8 @@ impl BufferView {
|
||||
scroll_position: Vector2F,
|
||||
ctx: &mut ViewContext<Self>,
|
||||
) {
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
let cursor = map.anchor_before(position, Bias::Left, ctx.app()).unwrap();
|
||||
if let Some(selection) = self.pending_selection.as_mut() {
|
||||
selection.set_head(buffer, cursor);
|
||||
@@ -347,8 +347,8 @@ impl BufferView {
|
||||
where
|
||||
T: IntoIterator<Item = &'a Range<DisplayPoint>>,
|
||||
{
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
let mut selections = Vec::new();
|
||||
for range in ranges {
|
||||
selections.push(Selection {
|
||||
@@ -366,7 +366,7 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn insert(&mut self, text: &String, ctx: &mut ViewContext<Self>) {
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let mut offset_ranges = SmallVec::<[Range<usize>; 32]>::new();
|
||||
for selection in &self.selections {
|
||||
let start = selection.start.to_offset(buffer).unwrap();
|
||||
@@ -381,7 +381,7 @@ impl BufferView {
|
||||
};
|
||||
});
|
||||
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let char_count = text.chars().count() as isize;
|
||||
let mut delta = 0_isize;
|
||||
self.selections = offset_ranges
|
||||
@@ -416,8 +416,8 @@ impl BufferView {
|
||||
}
|
||||
|
||||
pub fn backspace(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut self.selections {
|
||||
if selection.range(buffer).is_empty() {
|
||||
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
|
||||
@@ -439,7 +439,7 @@ impl BufferView {
|
||||
pub fn move_left(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
{
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut self.selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
@@ -462,8 +462,8 @@ impl BufferView {
|
||||
|
||||
pub fn select_left(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
{
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut self.selections {
|
||||
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
|
||||
let cursor = map
|
||||
@@ -483,7 +483,7 @@ impl BufferView {
|
||||
pub fn move_right(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
{
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut self.selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
@@ -506,9 +506,9 @@ impl BufferView {
|
||||
|
||||
pub fn select_right(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
{
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut self.selections {
|
||||
let head = selection.head().to_display_point(map, ctx.app()).unwrap();
|
||||
let cursor = map
|
||||
@@ -526,7 +526,7 @@ impl BufferView {
|
||||
ctx.propagate_action();
|
||||
} else {
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut self.selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
@@ -551,8 +551,8 @@ impl BufferView {
|
||||
ctx.propagate_action();
|
||||
} else {
|
||||
let app = ctx.app();
|
||||
let buffer = self.buffer.as_ref(app);
|
||||
let map = self.display_map.as_ref(app);
|
||||
let buffer = self.buffer.read(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut self.selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let (head, goal_column) =
|
||||
@@ -569,7 +569,7 @@ impl BufferView {
|
||||
ctx.propagate_action();
|
||||
} else {
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &mut self.selections {
|
||||
let start = selection.start.to_display_point(map, app).unwrap();
|
||||
let end = selection.end.to_display_point(map, app).unwrap();
|
||||
@@ -594,8 +594,8 @@ impl BufferView {
|
||||
ctx.propagate_action();
|
||||
} else {
|
||||
let app = ctx.app();
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let map = self.display_map.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let map = self.display_map.read(ctx);
|
||||
for selection in &mut self.selections {
|
||||
let head = selection.head().to_display_point(map, app).unwrap();
|
||||
let (head, goal_column) =
|
||||
@@ -615,7 +615,7 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn merge_selections(&mut self, ctx: &AppContext) {
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let mut i = 1;
|
||||
while i < self.selections.len() {
|
||||
if self.selections[i - 1]
|
||||
@@ -651,14 +651,14 @@ impl BufferView {
|
||||
self.selections
|
||||
.first()
|
||||
.unwrap()
|
||||
.display_range(self.display_map.as_ref(app), app)
|
||||
.display_range(self.display_map.read(app), app)
|
||||
}
|
||||
|
||||
pub fn last_selection(&self, app: &AppContext) -> Range<DisplayPoint> {
|
||||
self.selections
|
||||
.last()
|
||||
.unwrap()
|
||||
.display_range(self.display_map.as_ref(app), app)
|
||||
.display_range(self.display_map.read(app), app)
|
||||
}
|
||||
|
||||
pub fn selections_in_range<'a>(
|
||||
@@ -666,7 +666,7 @@ impl BufferView {
|
||||
range: Range<DisplayPoint>,
|
||||
app: &'a AppContext,
|
||||
) -> impl 'a + Iterator<Item = Range<DisplayPoint>> {
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
|
||||
let start = map.anchor_before(range.start, Bias::Left, app).unwrap();
|
||||
let start_index = self.selection_insertion_index(&start, app);
|
||||
@@ -686,7 +686,7 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn selection_insertion_index(&self, start: &Anchor, app: &AppContext) -> usize {
|
||||
let buffer = self.buffer.as_ref(app);
|
||||
let buffer = self.buffer.read(app);
|
||||
|
||||
match self
|
||||
.selections
|
||||
@@ -720,7 +720,7 @@ impl BufferView {
|
||||
let mut fold_ranges = Vec::new();
|
||||
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
for selection in &self.selections {
|
||||
let (start, end) = selection.display_range(map, app).sorted();
|
||||
let buffer_start_row = start.to_buffer_point(map, Bias::Left, app).unwrap().row;
|
||||
@@ -750,8 +750,8 @@ impl BufferView {
|
||||
use super::RangeExt;
|
||||
|
||||
let app = ctx.app();
|
||||
let map = self.display_map.as_ref(app);
|
||||
let buffer = self.buffer.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
let buffer = self.buffer.read(app);
|
||||
let ranges = self
|
||||
.selections
|
||||
.iter()
|
||||
@@ -796,7 +796,7 @@ impl BufferView {
|
||||
let mut is_blank = true;
|
||||
for c in self
|
||||
.display_map
|
||||
.as_ref(app)
|
||||
.read(app)
|
||||
.chars_at(DisplayPoint::new(display_row, 0), app)?
|
||||
{
|
||||
if c == ' ' {
|
||||
@@ -810,7 +810,7 @@ impl BufferView {
|
||||
}
|
||||
|
||||
fn foldable_range_for_line(&self, start_row: u32, app: &AppContext) -> Result<Range<Point>> {
|
||||
let map = self.display_map.as_ref(app);
|
||||
let map = self.display_map.read(app);
|
||||
let max_point = self.max_point(app);
|
||||
|
||||
let (start_indent, _) = self.line_indent(start_row, app)?;
|
||||
@@ -831,7 +831,7 @@ impl BufferView {
|
||||
|
||||
pub fn fold_selected_ranges(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
|
||||
self.display_map.update(ctx, |map, ctx| {
|
||||
let buffer = self.buffer.as_ref(ctx);
|
||||
let buffer = self.buffer.read(ctx);
|
||||
let ranges = self
|
||||
.selections
|
||||
.iter()
|
||||
@@ -842,23 +842,23 @@ impl BufferView {
|
||||
}
|
||||
|
||||
pub fn line(&self, display_row: u32, app: &AppContext) -> Result<String> {
|
||||
self.display_map.as_ref(app).line(display_row, app)
|
||||
self.display_map.read(app).line(display_row, app)
|
||||
}
|
||||
|
||||
pub fn line_len(&self, display_row: u32, app: &AppContext) -> Result<u32> {
|
||||
self.display_map.as_ref(app).line_len(display_row, app)
|
||||
self.display_map.read(app).line_len(display_row, app)
|
||||
}
|
||||
|
||||
pub fn rightmost_point(&self, app: &AppContext) -> DisplayPoint {
|
||||
self.display_map.as_ref(app).rightmost_point()
|
||||
self.display_map.read(app).rightmost_point()
|
||||
}
|
||||
|
||||
pub fn max_point(&self, app: &AppContext) -> DisplayPoint {
|
||||
self.display_map.as_ref(app).max_point(app)
|
||||
self.display_map.read(app).max_point(app)
|
||||
}
|
||||
|
||||
pub fn text(&self, app: &AppContext) -> String {
|
||||
self.display_map.as_ref(app).text(app)
|
||||
self.display_map.read(app).text(app)
|
||||
}
|
||||
|
||||
pub fn font_size(&self) -> f32 {
|
||||
@@ -902,7 +902,7 @@ impl BufferView {
|
||||
let font_size = settings.buffer_font_size;
|
||||
let font_id =
|
||||
font_cache.select_font(settings.buffer_font_family, &FontProperties::new())?;
|
||||
let digit_count = ((self.buffer.as_ref(app).max_point().row + 1) as f32)
|
||||
let digit_count = ((self.buffer.read(app).max_point().row + 1) as f32)
|
||||
.log10()
|
||||
.floor() as usize
|
||||
+ 1;
|
||||
@@ -923,7 +923,7 @@ impl BufferView {
|
||||
layout_cache: &TextLayoutCache,
|
||||
app: &AppContext,
|
||||
) -> Result<Vec<Arc<text_layout::Line>>> {
|
||||
let display_map = self.display_map.as_ref(app);
|
||||
let display_map = self.display_map.read(app);
|
||||
|
||||
let settings = smol::block_on(self.settings.read());
|
||||
let font_size = settings.buffer_font_size;
|
||||
@@ -959,7 +959,7 @@ impl BufferView {
|
||||
layout_cache: &TextLayoutCache,
|
||||
app: &AppContext,
|
||||
) -> Result<Vec<Arc<text_layout::Line>>> {
|
||||
let display_map = self.display_map.as_ref(app);
|
||||
let display_map = self.display_map.read(app);
|
||||
|
||||
rows.end = cmp::min(rows.end, display_map.max_point(app).row() + 1);
|
||||
if rows.start >= rows.end {
|
||||
@@ -1149,7 +1149,7 @@ impl workspace::ItemView for BufferView {
|
||||
}
|
||||
|
||||
fn title(&self, app: &AppContext) -> std::string::String {
|
||||
if let Some(path) = self.buffer.as_ref(app).path(app) {
|
||||
if let Some(path) = self.buffer.read(app).path(app) {
|
||||
path.file_name()
|
||||
.expect("buffer's path is always to a file")
|
||||
.to_string_lossy()
|
||||
@@ -1160,7 +1160,7 @@ impl workspace::ItemView for BufferView {
|
||||
}
|
||||
|
||||
fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)> {
|
||||
self.buffer.as_ref(app).entry_id()
|
||||
self.buffer.read(app).entry_id()
|
||||
}
|
||||
|
||||
fn clone_on_split(&self, ctx: &mut ViewContext<Self>) -> Option<Self>
|
||||
@@ -1177,7 +1177,7 @@ impl workspace::ItemView for BufferView {
|
||||
}
|
||||
|
||||
fn is_dirty(&self, ctx: &AppContext) -> bool {
|
||||
self.buffer.as_ref(ctx).is_dirty()
|
||||
self.buffer.read(ctx).is_dirty()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1255,7 +1255,7 @@ mod tests {
|
||||
view.begin_selection(DisplayPoint::new(2, 2), false, ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1271,7 +1271,7 @@ mod tests {
|
||||
view.update_selection(DisplayPoint::new(3, 3), Vector2F::zero(), ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1287,7 +1287,7 @@ mod tests {
|
||||
view.update_selection(DisplayPoint::new(1, 1), Vector2F::zero(), ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1304,7 +1304,7 @@ mod tests {
|
||||
view.update_selection(DisplayPoint::new(3, 3), Vector2F::zero(), ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1321,7 +1321,7 @@ mod tests {
|
||||
view.update_selection(DisplayPoint::new(0, 0), Vector2F::zero(), ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1340,7 +1340,7 @@ mod tests {
|
||||
view.end_selection(ctx);
|
||||
});
|
||||
|
||||
let view = buffer_view.as_ref(app);
|
||||
let view = buffer_view.read(app);
|
||||
let selections = view
|
||||
.selections_in_range(
|
||||
DisplayPoint::zero()..view.max_point(app.as_ref()),
|
||||
@@ -1367,7 +1367,7 @@ mod tests {
|
||||
app.add_window(|ctx| BufferView::for_buffer(buffer.clone(), settings, ctx));
|
||||
|
||||
let layouts = view
|
||||
.as_ref(app)
|
||||
.read(app)
|
||||
.layout_line_numbers(1000.0, &font_cache, &layout_cache, app.as_ref())
|
||||
.unwrap();
|
||||
assert_eq!(layouts.len(), 6);
|
||||
@@ -1460,7 +1460,7 @@ mod tests {
|
||||
);
|
||||
|
||||
view.unfold(&(), ctx);
|
||||
assert_eq!(view.text(ctx.app()), buffer.as_ref(ctx).text());
|
||||
assert_eq!(view.text(ctx.app()), buffer.read(ctx).text());
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1529,7 +1529,7 @@ mod tests {
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
buffer.as_ref(app).text(),
|
||||
buffer.read(app).text(),
|
||||
"oe two three\nfou five six\nseven ten\n"
|
||||
);
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user