Merge branch 'main' into unborked-git-zed2-diagnostics-view

This commit is contained in:
Julia 2023-11-15 17:38:59 -05:00
commit a464a7da2a
33 changed files with 2523 additions and 2395 deletions

2
Cargo.lock generated
View file

@ -11316,7 +11316,7 @@ dependencies = [
[[package]] [[package]]
name = "zed" name = "zed"
version = "0.113.0" version = "0.114.0"
dependencies = [ dependencies = [
"activity_indicator", "activity_indicator",
"ai", "ai",

View file

@ -354,129 +354,116 @@ impl std::fmt::Debug for Command {
} }
} }
// #[cfg(test)] #[cfg(test)]
// mod tests { mod tests {
// use std::sync::Arc; use std::sync::Arc;
// use super::*; use super::*;
// use editor::Editor; use editor::Editor;
// use gpui::{executor::Deterministic, TestAppContext}; use gpui::TestAppContext;
// use project::Project; use project::Project;
// use workspace::{AppState, Workspace}; use workspace::{AppState, Workspace};
// #[test] #[test]
// fn test_humanize_action_name() { fn test_humanize_action_name() {
// assert_eq!( assert_eq!(
// humanize_action_name("editor::GoToDefinition"), humanize_action_name("editor::GoToDefinition"),
// "editor: go to definition" "editor: go to definition"
// ); );
// assert_eq!( assert_eq!(
// humanize_action_name("editor::Backspace"), humanize_action_name("editor::Backspace"),
// "editor: backspace" "editor: backspace"
// ); );
// assert_eq!( assert_eq!(
// humanize_action_name("go_to_line::Deploy"), humanize_action_name("go_to_line::Deploy"),
// "go to line: deploy" "go to line: deploy"
// ); );
// } }
// #[gpui::test] #[gpui::test]
// async fn test_command_palette(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) { async fn test_command_palette(cx: &mut TestAppContext) {
// let app_state = init_test(cx); let app_state = init_test(cx);
// let project = Project::test(app_state.fs.clone(), [], cx).await; let project = Project::test(app_state.fs.clone(), [], cx).await;
// let window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx)); let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
// let workspace = window.root(cx);
// let editor = window.add_view(cx, |cx| {
// let mut editor = Editor::single_line(None, cx);
// editor.set_text("abc", cx);
// editor
// });
// workspace.update(cx, |workspace, cx| { let editor = cx.build_view(|cx| {
// cx.focus(&editor); let mut editor = Editor::single_line(cx);
// workspace.add_item(Box::new(editor.clone()), cx) editor.set_text("abc", cx);
// }); editor
});
// workspace.update(cx, |workspace, cx| { workspace.update(cx, |workspace, cx| {
// toggle_command_palette(workspace, &Toggle, cx); workspace.add_item(Box::new(editor.clone()), cx);
// }); editor.update(cx, |editor, cx| editor.focus(cx))
});
// let palette = workspace.read_with(cx, |workspace, _| { cx.simulate_keystrokes("cmd-shift-p");
// workspace.modal::<CommandPalette>().unwrap()
// });
// palette let palette = workspace.update(cx, |workspace, cx| {
// .update(cx, |palette, cx| { workspace
// // Fill up palette's command list by running an empty query; .active_modal::<CommandPalette>(cx)
// // we only need it to subsequently assert that the palette is initially .unwrap()
// // sorted by command's name. .read(cx)
// palette.delegate_mut().update_matches("".to_string(), cx) .picker
// }) .clone()
// .await; });
// palette.update(cx, |palette, _| { palette.update(cx, |palette, _| {
// let is_sorted = assert!(palette.delegate.commands.len() > 5);
// |actions: &[Command]| actions.windows(2).all(|pair| pair[0].name <= pair[1].name); let is_sorted =
// assert!(is_sorted(&palette.delegate().actions)); |actions: &[Command]| actions.windows(2).all(|pair| pair[0].name <= pair[1].name);
// }); assert!(is_sorted(&palette.delegate.commands));
});
// palette cx.simulate_input("bcksp");
// .update(cx, |palette, cx| {
// palette
// .delegate_mut()
// .update_matches("bcksp".to_string(), cx)
// })
// .await;
// palette.update(cx, |palette, cx| { palette.update(cx, |palette, _| {
// assert_eq!(palette.delegate().matches[0].string, "editor: backspace"); assert_eq!(palette.delegate.matches[0].string, "editor: backspace");
// palette.confirm(&Default::default(), cx); });
// });
// deterministic.run_until_parked();
// editor.read_with(cx, |editor, cx| {
// assert_eq!(editor.text(cx), "ab");
// });
// // Add namespace filter, and redeploy the palette cx.simulate_keystrokes("enter");
// cx.update(|cx| {
// cx.update_default_global::<CommandPaletteFilter, _, _>(|filter, _| {
// filter.filtered_namespaces.insert("editor");
// })
// });
// workspace.update(cx, |workspace, cx| { workspace.update(cx, |workspace, cx| {
// toggle_command_palette(workspace, &Toggle, cx); assert!(workspace.active_modal::<CommandPalette>(cx).is_none());
// }); assert_eq!(editor.read(cx).text(cx), "ab")
});
// // Assert editor command not present // Add namespace filter, and redeploy the palette
// let palette = workspace.read_with(cx, |workspace, _| { cx.update(|cx| {
// workspace.modal::<CommandPalette>().unwrap() cx.set_global(CommandPaletteFilter::default());
// }); cx.update_global::<CommandPaletteFilter, _>(|filter, _| {
filter.filtered_namespaces.insert("editor");
})
});
// palette cx.simulate_keystrokes("cmd-shift-p");
// .update(cx, |palette, cx| { cx.simulate_input("bcksp");
// palette
// .delegate_mut()
// .update_matches("bcksp".to_string(), cx)
// })
// .await;
// palette.update(cx, |palette, _| { let palette = workspace.update(cx, |workspace, cx| {
// assert!(palette.delegate().matches.is_empty()) workspace
// }); .active_modal::<CommandPalette>(cx)
// } .unwrap()
.read(cx)
.picker
.clone()
});
palette.update(cx, |palette, _| {
assert!(palette.delegate.matches.is_empty())
});
}
// fn init_test(cx: &mut TestAppContext) -> Arc<AppState> { fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
// cx.update(|cx| { cx.update(|cx| {
// let app_state = AppState::test(cx); let app_state = AppState::test(cx);
// theme::init(cx); theme::init(cx);
// language::init(cx); language::init(cx);
// editor::init(cx); editor::init(cx);
// workspace::init(app_state.clone(), cx); workspace::init(app_state.clone(), cx);
// init(cx); init(cx);
// Project::init_settings(cx); Project::init_settings(cx);
// app_state settings::load_default_keymap(cx);
// }) app_state
// } })
// } }
}

View file

@ -31,7 +31,7 @@ pub use block_map::{
BlockDisposition, BlockId, BlockProperties, BlockStyle, RenderBlock, TransformBlock, BlockDisposition, BlockId, BlockProperties, BlockStyle, RenderBlock, TransformBlock,
}; };
pub use self::fold_map::FoldPoint; pub use self::fold_map::{Fold, FoldPoint};
pub use self::inlay_map::{Inlay, InlayOffset, InlayPoint}; pub use self::inlay_map::{Inlay, InlayOffset, InlayPoint};
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
@ -124,7 +124,7 @@ impl DisplayMap {
self.fold( self.fold(
other other
.folds_in_range(0..other.buffer_snapshot.len()) .folds_in_range(0..other.buffer_snapshot.len())
.map(|fold| fold.to_offset(&other.buffer_snapshot)), .map(|fold| fold.range.to_offset(&other.buffer_snapshot)),
cx, cx,
); );
} }
@ -578,12 +578,7 @@ impl DisplaySnapshot {
line.push_str(chunk.chunk); line.push_str(chunk.chunk);
let text_style = if let Some(style) = chunk.style { let text_style = if let Some(style) = chunk.style {
editor_style Cow::Owned(editor_style.text.clone().highlight(style))
.text
.clone()
.highlight(style)
.map(Cow::Owned)
.unwrap_or_else(|_| Cow::Borrowed(&editor_style.text))
} else { } else {
Cow::Borrowed(&editor_style.text) Cow::Borrowed(&editor_style.text)
}; };
@ -728,7 +723,7 @@ impl DisplaySnapshot {
DisplayPoint(point) DisplayPoint(point)
} }
pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Range<Anchor>> pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>
where where
T: ToOffset, T: ToOffset,
{ {

View file

@ -2,7 +2,7 @@ use super::{
wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot}, wrap_map::{self, WrapEdit, WrapPoint, WrapSnapshot},
Highlights, Highlights,
}; };
use crate::{Anchor, Editor, ExcerptId, ExcerptRange, ToPoint as _}; use crate::{Anchor, Editor, EditorStyle, ExcerptId, ExcerptRange, ToPoint as _};
use collections::{Bound, HashMap, HashSet}; use collections::{Bound, HashMap, HashSet};
use gpui::{AnyElement, Pixels, ViewContext}; use gpui::{AnyElement, Pixels, ViewContext};
use language::{BufferSnapshot, Chunk, Patch, Point}; use language::{BufferSnapshot, Chunk, Patch, Point};
@ -88,6 +88,7 @@ pub struct BlockContext<'a, 'b> {
pub em_width: Pixels, pub em_width: Pixels,
pub line_height: Pixels, pub line_height: Pixels,
pub block_id: usize, pub block_id: usize,
pub editor_style: &'b EditorStyle,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]

View file

@ -3,15 +3,16 @@ use super::{
Highlights, Highlights,
}; };
use crate::{Anchor, AnchorRangeExt, MultiBufferSnapshot, ToOffset}; use crate::{Anchor, AnchorRangeExt, MultiBufferSnapshot, ToOffset};
use gpui::{HighlightStyle, Hsla}; use gpui::{ElementId, HighlightStyle, Hsla};
use language::{Chunk, Edit, Point, TextSummary}; use language::{Chunk, Edit, Point, TextSummary};
use std::{ use std::{
any::TypeId, any::TypeId,
cmp::{self, Ordering}, cmp::{self, Ordering},
iter, iter,
ops::{Add, AddAssign, Range, Sub}, ops::{Add, AddAssign, Deref, DerefMut, Range, Sub},
}; };
use sum_tree::{Bias, Cursor, FilterCursor, SumTree}; use sum_tree::{Bias, Cursor, FilterCursor, SumTree};
use util::post_inc;
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)] #[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct FoldPoint(pub Point); pub struct FoldPoint(pub Point);
@ -90,12 +91,16 @@ impl<'a> FoldMapWriter<'a> {
} }
// For now, ignore any ranges that span an excerpt boundary. // For now, ignore any ranges that span an excerpt boundary.
let fold = Fold(buffer.anchor_after(range.start)..buffer.anchor_before(range.end)); let fold_range =
if fold.0.start.excerpt_id != fold.0.end.excerpt_id { FoldRange(buffer.anchor_after(range.start)..buffer.anchor_before(range.end));
if fold_range.0.start.excerpt_id != fold_range.0.end.excerpt_id {
continue; continue;
} }
folds.push(fold); folds.push(Fold {
id: FoldId(post_inc(&mut self.0.next_fold_id.0)),
range: fold_range,
});
let inlay_range = let inlay_range =
snapshot.to_inlay_offset(range.start)..snapshot.to_inlay_offset(range.end); snapshot.to_inlay_offset(range.start)..snapshot.to_inlay_offset(range.end);
@ -106,13 +111,13 @@ impl<'a> FoldMapWriter<'a> {
} }
let buffer = &snapshot.buffer; let buffer = &snapshot.buffer;
folds.sort_unstable_by(|a, b| sum_tree::SeekTarget::cmp(a, b, buffer)); folds.sort_unstable_by(|a, b| sum_tree::SeekTarget::cmp(&a.range, &b.range, buffer));
self.0.snapshot.folds = { self.0.snapshot.folds = {
let mut new_tree = SumTree::new(); let mut new_tree = SumTree::new();
let mut cursor = self.0.snapshot.folds.cursor::<Fold>(); let mut cursor = self.0.snapshot.folds.cursor::<FoldRange>();
for fold in folds { for fold in folds {
new_tree.append(cursor.slice(&fold, Bias::Right, buffer), buffer); new_tree.append(cursor.slice(&fold.range, Bias::Right, buffer), buffer);
new_tree.push(fold, buffer); new_tree.push(fold, buffer);
} }
new_tree.append(cursor.suffix(buffer), buffer); new_tree.append(cursor.suffix(buffer), buffer);
@ -138,7 +143,8 @@ impl<'a> FoldMapWriter<'a> {
let mut folds_cursor = let mut folds_cursor =
intersecting_folds(&snapshot, &self.0.snapshot.folds, range, inclusive); intersecting_folds(&snapshot, &self.0.snapshot.folds, range, inclusive);
while let Some(fold) = folds_cursor.item() { while let Some(fold) = folds_cursor.item() {
let offset_range = fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer); let offset_range =
fold.range.start.to_offset(buffer)..fold.range.end.to_offset(buffer);
if offset_range.end > offset_range.start { if offset_range.end > offset_range.start {
let inlay_range = snapshot.to_inlay_offset(offset_range.start) let inlay_range = snapshot.to_inlay_offset(offset_range.start)
..snapshot.to_inlay_offset(offset_range.end); ..snapshot.to_inlay_offset(offset_range.end);
@ -175,6 +181,7 @@ impl<'a> FoldMapWriter<'a> {
pub struct FoldMap { pub struct FoldMap {
snapshot: FoldSnapshot, snapshot: FoldSnapshot,
ellipses_color: Option<Hsla>, ellipses_color: Option<Hsla>,
next_fold_id: FoldId,
} }
impl FoldMap { impl FoldMap {
@ -197,6 +204,7 @@ impl FoldMap {
ellipses_color: None, ellipses_color: None,
}, },
ellipses_color: None, ellipses_color: None,
next_fold_id: FoldId::default(),
}; };
let snapshot = this.snapshot.clone(); let snapshot = this.snapshot.clone();
(this, snapshot) (this, snapshot)
@ -242,8 +250,8 @@ impl FoldMap {
while let Some(fold) = folds.next() { while let Some(fold) = folds.next() {
if let Some(next_fold) = folds.peek() { if let Some(next_fold) = folds.peek() {
let comparison = fold let comparison = fold
.0 .range
.cmp(&next_fold.0, &self.snapshot.inlay_snapshot.buffer); .cmp(&next_fold.range, &self.snapshot.inlay_snapshot.buffer);
assert!(comparison.is_le()); assert!(comparison.is_le());
} }
} }
@ -304,9 +312,9 @@ impl FoldMap {
let anchor = inlay_snapshot let anchor = inlay_snapshot
.buffer .buffer
.anchor_before(inlay_snapshot.to_buffer_offset(edit.new.start)); .anchor_before(inlay_snapshot.to_buffer_offset(edit.new.start));
let mut folds_cursor = self.snapshot.folds.cursor::<Fold>(); let mut folds_cursor = self.snapshot.folds.cursor::<FoldRange>();
folds_cursor.seek( folds_cursor.seek(
&Fold(anchor..Anchor::max()), &FoldRange(anchor..Anchor::max()),
Bias::Left, Bias::Left,
&inlay_snapshot.buffer, &inlay_snapshot.buffer,
); );
@ -315,8 +323,8 @@ impl FoldMap {
let inlay_snapshot = &inlay_snapshot; let inlay_snapshot = &inlay_snapshot;
move || { move || {
let item = folds_cursor.item().map(|f| { let item = folds_cursor.item().map(|f| {
let buffer_start = f.0.start.to_offset(&inlay_snapshot.buffer); let buffer_start = f.range.start.to_offset(&inlay_snapshot.buffer);
let buffer_end = f.0.end.to_offset(&inlay_snapshot.buffer); let buffer_end = f.range.end.to_offset(&inlay_snapshot.buffer);
inlay_snapshot.to_inlay_offset(buffer_start) inlay_snapshot.to_inlay_offset(buffer_start)
..inlay_snapshot.to_inlay_offset(buffer_end) ..inlay_snapshot.to_inlay_offset(buffer_end)
}); });
@ -596,13 +604,13 @@ impl FoldSnapshot {
self.transforms.summary().output.longest_row self.transforms.summary().output.longest_row
} }
pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Range<Anchor>> pub fn folds_in_range<T>(&self, range: Range<T>) -> impl Iterator<Item = &Fold>
where where
T: ToOffset, T: ToOffset,
{ {
let mut folds = intersecting_folds(&self.inlay_snapshot, &self.folds, range, false); let mut folds = intersecting_folds(&self.inlay_snapshot, &self.folds, range, false);
iter::from_fn(move || { iter::from_fn(move || {
let item = folds.item().map(|f| &f.0); let item = folds.item();
folds.next(&self.inlay_snapshot.buffer); folds.next(&self.inlay_snapshot.buffer);
item item
}) })
@ -830,10 +838,39 @@ impl sum_tree::Summary for TransformSummary {
} }
} }
#[derive(Clone, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
struct Fold(Range<Anchor>); pub struct FoldId(usize);
impl Default for Fold { impl Into<ElementId> for FoldId {
fn into(self) -> ElementId {
ElementId::Integer(self.0)
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Fold {
pub id: FoldId,
pub range: FoldRange,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FoldRange(Range<Anchor>);
impl Deref for FoldRange {
type Target = Range<Anchor>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for FoldRange {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Default for FoldRange {
fn default() -> Self { fn default() -> Self {
Self(Anchor::min()..Anchor::max()) Self(Anchor::min()..Anchor::max())
} }
@ -844,17 +881,17 @@ impl sum_tree::Item for Fold {
fn summary(&self) -> Self::Summary { fn summary(&self) -> Self::Summary {
FoldSummary { FoldSummary {
start: self.0.start.clone(), start: self.range.start.clone(),
end: self.0.end.clone(), end: self.range.end.clone(),
min_start: self.0.start.clone(), min_start: self.range.start.clone(),
max_end: self.0.end.clone(), max_end: self.range.end.clone(),
count: 1, count: 1,
} }
} }
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct FoldSummary { pub struct FoldSummary {
start: Anchor, start: Anchor,
end: Anchor, end: Anchor,
min_start: Anchor, min_start: Anchor,
@ -900,14 +937,14 @@ impl sum_tree::Summary for FoldSummary {
} }
} }
impl<'a> sum_tree::Dimension<'a, FoldSummary> for Fold { impl<'a> sum_tree::Dimension<'a, FoldSummary> for FoldRange {
fn add_summary(&mut self, summary: &'a FoldSummary, _: &MultiBufferSnapshot) { fn add_summary(&mut self, summary: &'a FoldSummary, _: &MultiBufferSnapshot) {
self.0.start = summary.start.clone(); self.0.start = summary.start.clone();
self.0.end = summary.end.clone(); self.0.end = summary.end.clone();
} }
} }
impl<'a> sum_tree::SeekTarget<'a, FoldSummary, Fold> for Fold { impl<'a> sum_tree::SeekTarget<'a, FoldSummary, FoldRange> for FoldRange {
fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering { fn cmp(&self, other: &Self, buffer: &MultiBufferSnapshot) -> Ordering {
self.0.cmp(&other.0, buffer) self.0.cmp(&other.0, buffer)
} }
@ -1321,7 +1358,10 @@ mod tests {
let (snapshot, _) = map.read(inlay_snapshot.clone(), vec![]); let (snapshot, _) = map.read(inlay_snapshot.clone(), vec![]);
let fold_ranges = snapshot let fold_ranges = snapshot
.folds_in_range(Point::new(1, 0)..Point::new(1, 3)) .folds_in_range(Point::new(1, 0)..Point::new(1, 3))
.map(|fold| fold.start.to_point(&buffer_snapshot)..fold.end.to_point(&buffer_snapshot)) .map(|fold| {
fold.range.start.to_point(&buffer_snapshot)
..fold.range.end.to_point(&buffer_snapshot)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!( assert_eq!(
fold_ranges, fold_ranges,
@ -1553,10 +1593,9 @@ mod tests {
.filter(|fold| { .filter(|fold| {
let start = buffer_snapshot.anchor_before(start); let start = buffer_snapshot.anchor_before(start);
let end = buffer_snapshot.anchor_after(end); let end = buffer_snapshot.anchor_after(end);
start.cmp(&fold.0.end, &buffer_snapshot) == Ordering::Less start.cmp(&fold.range.end, &buffer_snapshot) == Ordering::Less
&& end.cmp(&fold.0.start, &buffer_snapshot) == Ordering::Greater && end.cmp(&fold.range.start, &buffer_snapshot) == Ordering::Greater
}) })
.map(|fold| fold.0)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!( assert_eq!(
@ -1639,10 +1678,10 @@ mod tests {
let buffer = &inlay_snapshot.buffer; let buffer = &inlay_snapshot.buffer;
let mut folds = self.snapshot.folds.items(buffer); let mut folds = self.snapshot.folds.items(buffer);
// Ensure sorting doesn't change how folds get merged and displayed. // Ensure sorting doesn't change how folds get merged and displayed.
folds.sort_by(|a, b| a.0.cmp(&b.0, buffer)); folds.sort_by(|a, b| a.range.cmp(&b.range, buffer));
let mut fold_ranges = folds let mut fold_ranges = folds
.iter() .iter()
.map(|fold| fold.0.start.to_offset(buffer)..fold.0.end.to_offset(buffer)) .map(|fold| fold.range.start.to_offset(buffer)..fold.range.end.to_offset(buffer))
.peekable(); .peekable();
let mut merged_ranges = Vec::new(); let mut merged_ranges = Vec::new();

View file

@ -100,7 +100,9 @@ use theme::{
use ui::{v_stack, HighlightedLabel, IconButton, StyledExt, TextTooltip}; use ui::{v_stack, HighlightedLabel, IconButton, StyledExt, TextTooltip};
use util::{post_inc, RangeExt, ResultExt, TryFutureExt}; use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
use workspace::{ use workspace::{
item::ItemEvent, searchable::SearchEvent, ItemNavHistory, SplitDirection, ViewId, Workspace, item::{ItemEvent, ItemHandle},
searchable::SearchEvent,
ItemNavHistory, SplitDirection, ViewId, Workspace,
}; };
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
@ -1878,10 +1880,8 @@ impl Editor {
); );
let focus_handle = cx.focus_handle(); let focus_handle = cx.focus_handle();
cx.on_focus_in(&focus_handle, Self::handle_focus_in) cx.on_focus(&focus_handle, Self::handle_focus).detach();
.detach(); cx.on_blur(&focus_handle, Self::handle_blur).detach();
cx.on_focus_out(&focus_handle, Self::handle_focus_out)
.detach();
let mut this = Self { let mut this = Self {
handle: cx.view().downgrade(), handle: cx.view().downgrade(),
@ -4372,69 +4372,42 @@ impl Editor {
} }
} }
// pub fn render_fold_indicators( pub fn render_fold_indicators(
// &self, &self,
// fold_data: Vec<Option<(FoldStatus, u32, bool)>>, fold_data: Vec<Option<(FoldStatus, u32, bool)>>,
// style: &EditorStyle, style: &EditorStyle,
// gutter_hovered: bool, gutter_hovered: bool,
// line_height: f32, line_height: Pixels,
// gutter_margin: f32, gutter_margin: Pixels,
// cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
// ) -> Vec<Option<AnyElement<Self>>> { ) -> Vec<Option<AnyElement<Self>>> {
// enum FoldIndicators {} fold_data
.iter()
// let style = style.folds.clone(); .enumerate()
.map(|(ix, fold_data)| {
// fold_data fold_data
// .iter() .map(|(fold_status, buffer_row, active)| {
// .enumerate() (active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| {
// .map(|(ix, fold_data)| { let icon = match fold_status {
// fold_data FoldStatus::Folded => ui::Icon::ChevronRight,
// .map(|(fold_status, buffer_row, active)| { FoldStatus::Foldable => ui::Icon::ChevronDown,
// (active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| { };
// MouseEventHandler::new::<FoldIndicators, _>( IconButton::new(ix as usize, icon)
// ix as usize, .on_click(move |editor: &mut Editor, cx| match fold_status {
// cx, FoldStatus::Folded => {
// |mouse_state, _| { editor.unfold_at(&UnfoldAt { buffer_row }, cx);
// Svg::new(match fold_status { }
// FoldStatus::Folded => style.folded_icon.clone(), FoldStatus::Foldable => {
// FoldStatus::Foldable => style.foldable_icon.clone(), editor.fold_at(&FoldAt { buffer_row }, cx);
// }) }
// .with_color( })
// style .render()
// .indicator })
// .in_state(fold_status == FoldStatus::Folded) })
// .style_for(mouse_state) .flatten()
// .color, })
// ) .collect()
// .constrained() }
// .with_width(gutter_margin * style.icon_margin_scale)
// .aligned()
// .constrained()
// .with_height(line_height)
// .with_width(gutter_margin)
// .aligned()
// },
// )
// .with_cursor_style(CursorStyle::PointingHand)
// .with_padding(Padding::uniform(3.))
// .on_click(MouseButton::Left, {
// move |_, editor, cx| match fold_status {
// FoldStatus::Folded => {
// editor.unfold_at(&UnfoldAt { buffer_row }, cx);
// }
// FoldStatus::Foldable => {
// editor.fold_at(&FoldAt { buffer_row }, cx);
// }
// }
// })
// .into_any()
// })
// })
// .flatten()
// })
// .collect()
// }
pub fn context_menu_visible(&self) -> bool { pub fn context_menu_visible(&self) -> bool {
self.context_menu self.context_menu
@ -5330,8 +5303,8 @@ impl Editor {
buffer.anchor_before(range_to_move.start) buffer.anchor_before(range_to_move.start)
..buffer.anchor_after(range_to_move.end), ..buffer.anchor_after(range_to_move.end),
) { ) {
let mut start = fold.start.to_point(&buffer); let mut start = fold.range.start.to_point(&buffer);
let mut end = fold.end.to_point(&buffer); let mut end = fold.range.end.to_point(&buffer);
start.row -= row_delta; start.row -= row_delta;
end.row -= row_delta; end.row -= row_delta;
refold_ranges.push(start..end); refold_ranges.push(start..end);
@ -5421,8 +5394,8 @@ impl Editor {
buffer.anchor_before(range_to_move.start) buffer.anchor_before(range_to_move.start)
..buffer.anchor_after(range_to_move.end), ..buffer.anchor_after(range_to_move.end),
) { ) {
let mut start = fold.start.to_point(&buffer); let mut start = fold.range.start.to_point(&buffer);
let mut end = fold.end.to_point(&buffer); let mut end = fold.range.end.to_point(&buffer);
start.row += row_delta; start.row += row_delta;
end.row += row_delta; end.row += row_delta;
refold_ranges.push(start..end); refold_ranges.push(start..end);
@ -7690,183 +7663,203 @@ impl Editor {
} }
} }
// pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> { pub fn rename(&mut self, _: &Rename, cx: &mut ViewContext<Self>) -> Option<Task<Result<()>>> {
// use language::ToOffset as _; use language::ToOffset as _;
// let project = self.project.clone()?; let project = self.project.clone()?;
// let selection = self.selections.newest_anchor().clone(); let selection = self.selections.newest_anchor().clone();
// let (cursor_buffer, cursor_buffer_position) = self let (cursor_buffer, cursor_buffer_position) = self
// .buffer .buffer
// .read(cx) .read(cx)
// .text_anchor_for_position(selection.head(), cx)?; .text_anchor_for_position(selection.head(), cx)?;
// let (tail_buffer, _) = self let (tail_buffer, _) = self
// .buffer .buffer
// .read(cx) .read(cx)
// .text_anchor_for_position(selection.tail(), cx)?; .text_anchor_for_position(selection.tail(), cx)?;
// if tail_buffer != cursor_buffer { if tail_buffer != cursor_buffer {
// return None; return None;
// } }
// let snapshot = cursor_buffer.read(cx).snapshot(); let snapshot = cursor_buffer.read(cx).snapshot();
// let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot); let cursor_buffer_offset = cursor_buffer_position.to_offset(&snapshot);
// let prepare_rename = project.update(cx, |project, cx| { let prepare_rename = project.update(cx, |project, cx| {
// project.prepare_rename(cursor_buffer, cursor_buffer_offset, cx) project.prepare_rename(cursor_buffer, cursor_buffer_offset, cx)
// }); });
// Some(cx.spawn(|this, mut cx| async move { Some(cx.spawn(|this, mut cx| async move {
// let rename_range = if let Some(range) = prepare_rename.await? { let rename_range = if let Some(range) = prepare_rename.await? {
// Some(range) Some(range)
// } else { } else {
// this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
// let buffer = this.buffer.read(cx).snapshot(cx); let buffer = this.buffer.read(cx).snapshot(cx);
// let mut buffer_highlights = this let mut buffer_highlights = this
// .document_highlights_for_position(selection.head(), &buffer) .document_highlights_for_position(selection.head(), &buffer)
// .filter(|highlight| { .filter(|highlight| {
// highlight.start.excerpt_id == selection.head().excerpt_id highlight.start.excerpt_id == selection.head().excerpt_id
// && highlight.end.excerpt_id == selection.head().excerpt_id && highlight.end.excerpt_id == selection.head().excerpt_id
// }); });
// buffer_highlights buffer_highlights
// .next() .next()
// .map(|highlight| highlight.start.text_anchor..highlight.end.text_anchor) .map(|highlight| highlight.start.text_anchor..highlight.end.text_anchor)
// })? })?
// }; };
// if let Some(rename_range) = rename_range { if let Some(rename_range) = rename_range {
// let rename_buffer_range = rename_range.to_offset(&snapshot); let rename_buffer_range = rename_range.to_offset(&snapshot);
// let cursor_offset_in_rename_range = let cursor_offset_in_rename_range =
// cursor_buffer_offset.saturating_sub(rename_buffer_range.start); cursor_buffer_offset.saturating_sub(rename_buffer_range.start);
// this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
// this.take_rename(false, cx); this.take_rename(false, cx);
// let buffer = this.buffer.read(cx).read(cx); let buffer = this.buffer.read(cx).read(cx);
// let cursor_offset = selection.head().to_offset(&buffer); let cursor_offset = selection.head().to_offset(&buffer);
// let rename_start = cursor_offset.saturating_sub(cursor_offset_in_rename_range); let rename_start = cursor_offset.saturating_sub(cursor_offset_in_rename_range);
// let rename_end = rename_start + rename_buffer_range.len(); let rename_end = rename_start + rename_buffer_range.len();
// let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end); let range = buffer.anchor_before(rename_start)..buffer.anchor_after(rename_end);
// let mut old_highlight_id = None; let mut old_highlight_id = None;
// let old_name: Arc<str> = buffer let old_name: Arc<str> = buffer
// .chunks(rename_start..rename_end, true) .chunks(rename_start..rename_end, true)
// .map(|chunk| { .map(|chunk| {
// if old_highlight_id.is_none() { if old_highlight_id.is_none() {
// old_highlight_id = chunk.syntax_highlight_id; old_highlight_id = chunk.syntax_highlight_id;
// } }
// chunk.text chunk.text
// }) })
// .collect::<String>() .collect::<String>()
// .into(); .into();
// drop(buffer); drop(buffer);
// // Position the selection in the rename editor so that it matches the current selection. // Position the selection in the rename editor so that it matches the current selection.
// this.show_local_selections = false; this.show_local_selections = false;
// let rename_editor = cx.build_view(|cx| { let rename_editor = cx.build_view(|cx| {
// let mut editor = Editor::single_line(cx); let mut editor = Editor::single_line(cx);
// if let Some(old_highlight_id) = old_highlight_id { editor.buffer.update(cx, |buffer, cx| {
// editor.override_text_style = buffer.edit([(0..0, old_name.clone())], None, cx)
// Some(Box::new(move |style| old_highlight_id.style(&style.syntax))); });
// } editor.select_all(&SelectAll, cx);
// editor.buffer.update(cx, |buffer, cx| { editor
// buffer.edit([(0..0, old_name.clone())], None, cx) });
// });
// editor.select_all(&SelectAll, cx);
// editor
// });
// let ranges = this let ranges = this
// .clear_background_highlights::<DocumentHighlightWrite>(cx) .clear_background_highlights::<DocumentHighlightWrite>(cx)
// .into_iter() .into_iter()
// .flat_map(|(_, ranges)| ranges.into_iter()) .flat_map(|(_, ranges)| ranges.into_iter())
// .chain( .chain(
// this.clear_background_highlights::<DocumentHighlightRead>(cx) this.clear_background_highlights::<DocumentHighlightRead>(cx)
// .into_iter() .into_iter()
// .flat_map(|(_, ranges)| ranges.into_iter()), .flat_map(|(_, ranges)| ranges.into_iter()),
// ) )
// .collect(); .collect();
// this.highlight_text::<Rename>( this.highlight_text::<Rename>(
// ranges, ranges,
// HighlightStyle { HighlightStyle {
// fade_out: Some(style.rename_fade), fade_out: Some(0.6),
// ..Default::default() ..Default::default()
// }, },
// cx, cx,
// ); );
// cx.focus(&rename_editor); let rename_focus_handle = rename_editor.focus_handle(cx);
// let block_id = this.insert_blocks( cx.focus(&rename_focus_handle);
// [BlockProperties { let block_id = this.insert_blocks(
// style: BlockStyle::Flex, [BlockProperties {
// position: range.start.clone(), style: BlockStyle::Flex,
// height: 1, position: range.start.clone(),
// render: Arc::new({ height: 1,
// let editor = rename_editor.clone(); render: Arc::new({
// move |cx: &mut BlockContext| { let rename_editor = rename_editor.clone();
// ChildView::new(&editor, cx) move |cx: &mut BlockContext| {
// .contained() let mut text_style = cx.editor_style.text.clone();
// .with_padding_left(cx.anchor_x) if let Some(highlight_style) = old_highlight_id
// .into_any() .and_then(|h| h.style(&cx.editor_style.syntax))
// } {
// }), text_style = text_style.highlight(highlight_style);
// disposition: BlockDisposition::Below, }
// }], div()
// Some(Autoscroll::fit()), .pl(cx.anchor_x)
// cx, .child(rename_editor.render_with(EditorElement::new(
// )[0]; &rename_editor,
// this.pending_rename = Some(RenameState { EditorStyle {
// range, background: cx.theme().system().transparent,
// old_name, local_player: cx.editor_style.local_player,
// editor: rename_editor, text: text_style,
// block_id, scrollbar_width: cx.editor_style.scrollbar_width,
// }); syntax: cx.editor_style.syntax.clone(),
// })?; diagnostic_style:
// } cx.editor_style.diagnostic_style.clone(),
},
)))
.render()
}
}),
disposition: BlockDisposition::Below,
}],
Some(Autoscroll::fit()),
cx,
)[0];
this.pending_rename = Some(RenameState {
range,
old_name,
editor: rename_editor,
block_id,
});
})?;
}
// Ok(()) Ok(())
// })) }))
// } }
// pub fn confirm_rename( pub fn confirm_rename(
// workspace: &mut Workspace, &mut self,
// _: &ConfirmRename, _: &ConfirmRename,
// cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
// let editor = workspace.active_item(cx)?.act_as::<Editor>(cx)?; let rename = self.take_rename(false, cx)?;
let workspace = self.workspace()?;
let (start_buffer, start) = self
.buffer
.read(cx)
.text_anchor_for_position(rename.range.start.clone(), cx)?;
let (end_buffer, end) = self
.buffer
.read(cx)
.text_anchor_for_position(rename.range.end.clone(), cx)?;
if start_buffer != end_buffer {
return None;
}
// let (buffer, range, old_name, new_name) = editor.update(cx, |editor, cx| { let buffer = start_buffer;
// let rename = editor.take_rename(false, cx)?; let range = start..end;
// let buffer = editor.buffer.read(cx); let old_name = rename.old_name;
// let (start_buffer, start) = let new_name = rename.editor.read(cx).text(cx);
// buffer.text_anchor_for_position(rename.range.start.clone(), cx)?;
// let (end_buffer, end) =
// buffer.text_anchor_for_position(rename.range.end.clone(), cx)?;
// if start_buffer == end_buffer {
// let new_name = rename.editor.read(cx).text(cx);
// Some((start_buffer, start..end, rename.old_name, new_name))
// } else {
// None
// }
// })?;
// let rename = workspace.project().clone().update(cx, |project, cx| { let rename = workspace
// project.perform_rename(buffer.clone(), range.start, new_name.clone(), true, cx) .read(cx)
// }); .project()
.clone()
.update(cx, |project, cx| {
project.perform_rename(buffer.clone(), range.start, new_name.clone(), true, cx)
});
let workspace = workspace.downgrade();
// let editor = editor.downgrade(); Some(cx.spawn(|editor, mut cx| async move {
// Some(cx.spawn(|workspace, mut cx| async move { let project_transaction = rename.await?;
// let project_transaction = rename.await?; Self::open_project_transaction(
// Self::open_project_transaction( &editor,
// &editor, workspace,
// workspace, project_transaction,
// project_transaction, format!("Rename: {}{}", old_name, new_name),
// format!("Rename: {} → {}", old_name, new_name), cx.clone(),
// cx.clone(), )
// ) .await?;
// .await?;
// editor.update(&mut cx, |editor, cx| { editor.update(&mut cx, |editor, cx| {
// editor.refresh_document_highlights(cx); editor.refresh_document_highlights(cx);
// })?; })?;
// Ok(()) Ok(())
// })) }))
// } }
fn take_rename( fn take_rename(
&mut self, &mut self,
@ -7874,6 +7867,10 @@ impl Editor {
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<RenameState> { ) -> Option<RenameState> {
let rename = self.pending_rename.take()?; let rename = self.pending_rename.take()?;
if rename.editor.focus_handle(cx).is_focused(cx) {
cx.focus(&self.focus_handle);
}
self.remove_blocks( self.remove_blocks(
[rename.block_id].into_iter().collect(), [rename.block_id].into_iter().collect(),
Some(Autoscroll::fit()), Some(Autoscroll::fit()),
@ -9172,17 +9169,13 @@ impl Editor {
self.focus_handle.is_focused(cx) self.focus_handle.is_focused(cx)
} }
fn handle_focus_in(&mut self, cx: &mut ViewContext<Self>) { fn handle_focus(&mut self, cx: &mut ViewContext<Self>) {
if self.focus_handle.is_focused(cx) { cx.emit(EditorEvent::Focused);
// todo!()
// let focused_event = EditorFocused(cx.handle());
// cx.emit_global(focused_event);
cx.emit(EditorEvent::Focused);
}
if let Some(rename) = self.pending_rename.as_ref() { if let Some(rename) = self.pending_rename.as_ref() {
let rename_editor_focus_handle = rename.editor.read(cx).focus_handle.clone(); let rename_editor_focus_handle = rename.editor.read(cx).focus_handle.clone();
cx.focus(&rename_editor_focus_handle); cx.focus(&rename_editor_focus_handle);
} else if self.focus_handle.is_focused(cx) { } else {
self.blink_manager.update(cx, BlinkManager::enable); self.blink_manager.update(cx, BlinkManager::enable);
self.buffer.update(cx, |buffer, cx| { self.buffer.update(cx, |buffer, cx| {
buffer.finalize_last_transaction(cx); buffer.finalize_last_transaction(cx);
@ -9198,7 +9191,7 @@ impl Editor {
} }
} }
fn handle_focus_out(&mut self, cx: &mut ViewContext<Self>) { fn handle_blur(&mut self, cx: &mut ViewContext<Self>) {
// todo!() // todo!()
// let blurred_event = EditorBlurred(cx.handle()); // let blurred_event = EditorBlurred(cx.handle());
// cx.emit_global(blurred_event); // cx.emit_global(blurred_event);

View file

@ -3851,12 +3851,12 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx) Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx)
}); });
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (view, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (view, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
view.condition::<crate::EditorEvent>(&cx, |view, cx| !view.buffer.read(cx).is_parsing(cx)) view.condition::<crate::EditorEvent>(&cx, |view, cx| !view.buffer.read(cx).is_parsing(cx))
.await; .await;
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.change_selections(None, cx, |s| { view.change_selections(None, cx, |s| {
s.select_display_ranges([ s.select_display_ranges([
DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25), DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25),
@ -3867,7 +3867,7 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx); view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| { view.selections.display_ranges(cx) }), view.update(cx, |view, cx| { view.selections.display_ranges(cx) }),
&[ &[
DisplayPoint::new(0, 23)..DisplayPoint::new(0, 27), DisplayPoint::new(0, 23)..DisplayPoint::new(0, 27),
DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7), DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7),
@ -3875,50 +3875,50 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
] ]
); );
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx); view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28), DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28),
DisplayPoint::new(4, 1)..DisplayPoint::new(2, 0), DisplayPoint::new(4, 1)..DisplayPoint::new(2, 0),
] ]
); );
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx); view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)] &[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)]
); );
// Trying to expand the selected syntax node one more time has no effect. // Trying to expand the selected syntax node one more time has no effect.
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx); view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)] &[DisplayPoint::new(5, 0)..DisplayPoint::new(0, 0)]
); );
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx); view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28), DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28),
DisplayPoint::new(4, 1)..DisplayPoint::new(2, 0), DisplayPoint::new(4, 1)..DisplayPoint::new(2, 0),
] ]
); );
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx); view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 23)..DisplayPoint::new(0, 27), DisplayPoint::new(0, 23)..DisplayPoint::new(0, 27),
DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7), DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7),
@ -3926,11 +3926,11 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
] ]
); );
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx); view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25), DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25),
DisplayPoint::new(2, 24)..DisplayPoint::new(2, 12), DisplayPoint::new(2, 24)..DisplayPoint::new(2, 12),
@ -3939,11 +3939,11 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
); );
// Trying to shrink the selected syntax node one more time has no effect. // Trying to shrink the selected syntax node one more time has no effect.
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx); view.select_smaller_syntax_node(&SelectSmallerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25), DisplayPoint::new(0, 25)..DisplayPoint::new(0, 25),
DisplayPoint::new(2, 24)..DisplayPoint::new(2, 12), DisplayPoint::new(2, 24)..DisplayPoint::new(2, 12),
@ -3953,7 +3953,7 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
// Ensure that we keep expanding the selection if the larger selection starts or ends within // Ensure that we keep expanding the selection if the larger selection starts or ends within
// a fold. // a fold.
view.update(&mut cx, |view, cx| { view.update(cx, |view, cx| {
view.fold_ranges( view.fold_ranges(
vec![ vec![
Point::new(0, 21)..Point::new(0, 24), Point::new(0, 21)..Point::new(0, 24),
@ -3965,7 +3965,7 @@ async fn test_select_larger_smaller_syntax_node(cx: &mut gpui::TestAppContext) {
view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx); view.select_larger_syntax_node(&SelectLargerSyntaxNode, cx);
}); });
assert_eq!( assert_eq!(
view.update(&mut cx, |view, cx| view.selections.display_ranges(cx)), view.update(cx, |view, cx| view.selections.display_ranges(cx)),
&[ &[
DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28), DisplayPoint::new(0, 16)..DisplayPoint::new(0, 28),
DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7), DisplayPoint::new(2, 35)..DisplayPoint::new(2, 7),
@ -4017,8 +4017,7 @@ async fn test_autoindent_selections(cx: &mut gpui::TestAppContext) {
Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx) Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx)
}); });
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor editor
.condition::<crate::EditorEvent>(cx, |editor, cx| !editor.buffer.read(cx).is_parsing(cx)) .condition::<crate::EditorEvent>(cx, |editor, cx| !editor.buffer.read(cx).is_parsing(cx))
.await; .await;
@ -4583,8 +4582,7 @@ async fn test_surround_with_pair(cx: &mut gpui::TestAppContext) {
Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx) Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx)
}); });
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (view, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (view, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
view.condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx)) view.condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx))
.await; .await;
@ -4734,8 +4732,7 @@ async fn test_delete_autoclose_pair(cx: &mut gpui::TestAppContext) {
Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx) Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx)
}); });
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor editor
.condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx)) .condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx))
.await; .await;
@ -4957,8 +4954,7 @@ async fn test_document_format_during_save(cx: &mut gpui::TestAppContext) {
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx)); editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx))); assert!(cx.read(|cx| editor.is_dirty(cx)));
@ -5077,8 +5073,7 @@ async fn test_range_format_during_save(cx: &mut gpui::TestAppContext) {
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx)); editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx))); assert!(cx.read(|cx| editor.is_dirty(cx)));
@ -5205,8 +5200,7 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) {
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx)); editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
let format = editor let format = editor
@ -5993,8 +5987,7 @@ fn test_editing_disjoint_excerpts(cx: &mut TestAppContext) {
multibuffer multibuffer
}); });
let (view, mut cx) = cx.add_window_view(|cx| build_editor(multibuffer, cx)); let (view, cx) = cx.add_window_view(|cx| build_editor(multibuffer, cx));
let cx = &mut cx;
view.update(cx, |view, cx| { view.update(cx, |view, cx| {
assert_eq!(view.text(cx), "aaaa\nbbbb"); assert_eq!(view.text(cx), "aaaa\nbbbb");
view.change_selections(None, cx, |s| { view.change_selections(None, cx, |s| {
@ -6064,8 +6057,7 @@ fn test_editing_overlapping_excerpts(cx: &mut TestAppContext) {
multibuffer multibuffer
}); });
let (view, mut cx) = cx.add_window_view(|cx| build_editor(multibuffer, cx)); let (view, cx) = cx.add_window_view(|cx| build_editor(multibuffer, cx));
let cx = &mut cx;
view.update(cx, |view, cx| { view.update(cx, |view, cx| {
let (expected_text, selection_ranges) = marked_text_ranges( let (expected_text, selection_ranges) = marked_text_ranges(
indoc! {" indoc! {"
@ -6302,8 +6294,7 @@ async fn test_extra_newline_insertion(cx: &mut gpui::TestAppContext) {
Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx) Buffer::new(0, cx.entity_id().as_u64(), text).with_language(language, cx)
}); });
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (view, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (view, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
view.condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx)) view.condition::<crate::EditorEvent>(cx, |view, cx| !view.buffer.read(cx).is_parsing(cx))
.await; .await;
@ -8112,8 +8103,7 @@ async fn test_document_format_with_prettier(cx: &mut gpui::TestAppContext) {
let buffer_text = "one\ntwo\nthree\n"; let buffer_text = "one\ntwo\nthree\n";
let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx)); let buffer = cx.build_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, mut cx) = cx.add_window_view(|cx| build_editor(buffer, cx)); let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
let cx = &mut cx;
editor.update(cx, |editor, cx| editor.set_text(buffer_text, cx)); editor.update(cx, |editor, cx| editor.set_text(buffer_text, cx));
editor editor

File diff suppressed because it is too large Load diff

View file

@ -60,8 +60,8 @@ pub fn diff_hunk_to_display(hunk: DiffHunk<u32>, snapshot: &DisplaySnapshot) ->
let folds_end = Point::new(hunk.buffer_range.end + 2, 0); let folds_end = Point::new(hunk.buffer_range.end + 2, 0);
let folds_range = folds_start..folds_end; let folds_range = folds_start..folds_end;
let containing_fold = snapshot.folds_in_range(folds_range).find(|fold_range| { let containing_fold = snapshot.folds_in_range(folds_range).find(|fold| {
let fold_point_range = fold_range.to_point(&snapshot.buffer_snapshot); let fold_point_range = fold.range.to_point(&snapshot.buffer_snapshot);
let fold_point_range = fold_point_range.start..=fold_point_range.end; let fold_point_range = fold_point_range.start..=fold_point_range.end;
let folded_start = fold_point_range.contains(&hunk_start_point); let folded_start = fold_point_range.contains(&hunk_start_point);
@ -72,7 +72,7 @@ pub fn diff_hunk_to_display(hunk: DiffHunk<u32>, snapshot: &DisplaySnapshot) ->
}); });
if let Some(fold) = containing_fold { if let Some(fold) = containing_fold {
let row = fold.start.to_display_point(snapshot).row(); let row = fold.range.start.to_display_point(snapshot).row();
DisplayDiffHunk::Folded { display_row: row } DisplayDiffHunk::Folded { display_row: row }
} else { } else {
let start = hunk_start_point.to_display_point(snapshot).row(); let start = hunk_start_point.to_display_point(snapshot).row();

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,41 @@
# Contexts
GPUI makes extensive use of *context parameters*, typically named `cx` and positioned at the end of the parameter list, unless they're before a final function parameter. A context reference provides access to application state and services.
There are multiple kinds of contexts, and contexts implement the `Deref` trait so that a function taking `&mut AppContext` could be passed a `&mut WindowContext` or `&mut ViewContext` instead.
```
AppContext
/ \
ModelContext WindowContext
/
ViewContext
```
- The `AppContext` forms the root of the hierarchy
- `ModelContext` and `WindowContext` both dereference to `AppContext`
- `ViewContext` dereferences to `WindowContext`
## `AppContext`
Provides access to the global application state. All other kinds of contexts ultimately deref to an `AppContext`. You can update a `Model<T>` by passing an `AppContext`, but you can't update a view. For that you need a `WindowContext`...
## `WindowContext`
Provides access to the state of an application window, and also derefs to an `AppContext`, so you can pass a window context reference to any method taking an app context. Obtain this context by calling `WindowHandle::update`.
## `ModelContext<T>`
Available when you create or update a `Model<T>`. It derefs to an `AppContext`, but also contains methods specific to the particular model, such as the ability to notify change observers or emit events.
## `ViewContext<V>`
Available when you create or update a `View<V>`. It derefs to a `WindowContext`, but also contains methods specific to the particular view, such as the ability to notify change observers or emit events.
## `AsyncAppContext` and `AsyncWindowContext`
Whereas the above contexts are always passed to your code as references, you can call `to_async` on the reference to create an async context, which has a static lifetime and can be held across `await` points in async code. When you interact with `Model`s or `View`s with an async context, the calls become fallible, because the context may outlive the window or even the app itself.
## `TestAppContext` and `TestVisualContext`
These are similar to the async contexts above, but they panic if you attempt to access a non-existent app or window, and they also contain other features specific to tests.

View file

@ -54,6 +54,9 @@ pub trait Action: std::fmt::Debug + 'static {
where where
Self: Sized; Self: Sized;
fn build(value: Option<serde_json::Value>) -> Result<Box<dyn Action>> fn build(value: Option<serde_json::Value>) -> Result<Box<dyn Action>>
where
Self: Sized;
fn is_registered() -> bool
where where
Self: Sized; Self: Sized;
@ -88,6 +91,14 @@ where
Ok(Box::new(action)) Ok(Box::new(action))
} }
fn is_registered() -> bool {
ACTION_REGISTRY
.read()
.names_by_type_id
.get(&TypeId::of::<A>())
.is_some()
}
fn partial_eq(&self, action: &dyn Action) -> bool { fn partial_eq(&self, action: &dyn Action) -> bool {
action action
.as_any() .as_any()

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
div, Action, AnyView, AnyWindowHandle, AppCell, AppContext, AsyncAppContext, div, Action, AnyView, AnyWindowHandle, AppCell, AppContext, AsyncAppContext,
BackgroundExecutor, Context, Div, EventEmitter, ForegroundExecutor, InputEvent, KeyDownEvent, BackgroundExecutor, Context, Div, EventEmitter, ForegroundExecutor, InputEvent, KeyDownEvent,
Keystroke, Model, ModelContext, Render, Result, Task, TestDispatcher, TestPlatform, View, Keystroke, Model, ModelContext, Render, Result, Task, TestDispatcher, TestPlatform, TestWindow,
ViewContext, VisualContext, WindowContext, WindowHandle, WindowOptions, View, ViewContext, VisualContext, WindowContext, WindowHandle, WindowOptions,
}; };
use anyhow::{anyhow, bail}; use anyhow::{anyhow, bail};
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
@ -140,7 +140,7 @@ impl TestAppContext {
.any_handle .any_handle
} }
pub fn add_window_view<F, V>(&mut self, build_window: F) -> (View<V>, VisualTestContext) pub fn add_window_view<F, V>(&mut self, build_window: F) -> (View<V>, &mut VisualTestContext)
where where
F: FnOnce(&mut ViewContext<V>) -> V, F: FnOnce(&mut ViewContext<V>) -> V,
V: Render, V: Render,
@ -149,7 +149,9 @@ impl TestAppContext {
let window = cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window)); let window = cx.open_window(WindowOptions::default(), |cx| cx.build_view(build_window));
drop(cx); drop(cx);
let view = window.root_view(self).unwrap(); let view = window.root_view(self).unwrap();
(view, VisualTestContext::from_window(*window.deref(), self)) let cx = Box::new(VisualTestContext::from_window(*window.deref(), self));
// it might be nice to try and cleanup these at the end of each test.
(view, Box::leak(cx))
} }
pub fn simulate_new_path_selection( pub fn simulate_new_path_selection(
@ -220,7 +222,35 @@ impl TestAppContext {
{ {
window window
.update(self, |_, cx| cx.dispatch_action(action.boxed_clone())) .update(self, |_, cx| cx.dispatch_action(action.boxed_clone()))
.unwrap() .unwrap();
self.background_executor.run_until_parked()
}
/// simulate_keystrokes takes a space-separated list of keys to type.
/// cx.simulate_keystrokes("cmd-shift-p b k s p enter")
/// will run backspace on the current editor through the command palette.
pub fn simulate_keystrokes(&mut self, window: AnyWindowHandle, keystrokes: &str) {
for keystroke in keystrokes
.split(" ")
.map(Keystroke::parse)
.map(Result::unwrap)
{
self.dispatch_keystroke(window, keystroke.into(), false);
}
self.background_executor.run_until_parked()
}
/// simulate_input takes a string of text to type.
/// cx.simulate_input("abc")
/// will type abc into your current editor.
pub fn simulate_input(&mut self, window: AnyWindowHandle, input: &str) {
for keystroke in input.split("").map(Keystroke::parse).map(Result::unwrap) {
self.dispatch_keystroke(window, keystroke.into(), false);
}
self.background_executor.run_until_parked()
} }
pub fn dispatch_keystroke( pub fn dispatch_keystroke(
@ -229,15 +259,41 @@ impl TestAppContext {
keystroke: Keystroke, keystroke: Keystroke,
is_held: bool, is_held: bool,
) { ) {
let keystroke2 = keystroke.clone();
let handled = window let handled = window
.update(self, |_, cx| { .update(self, |_, cx| {
cx.dispatch_event(InputEvent::KeyDown(KeyDownEvent { keystroke, is_held })) cx.dispatch_event(InputEvent::KeyDown(KeyDownEvent { keystroke, is_held }))
}) })
.is_ok_and(|handled| handled); .is_ok_and(|handled| handled);
if handled {
if !handled { return;
// todo!() simluate input here
} }
let input_handler = self.update_test_window(window, |window| window.input_handler.clone());
let Some(input_handler) = input_handler else {
panic!(
"dispatch_keystroke {:?} failed to dispatch action or input",
&keystroke2
);
};
let text = keystroke2.ime_key.unwrap_or(keystroke2.key);
input_handler.lock().replace_text_in_range(None, &text);
}
pub fn update_test_window<R>(
&mut self,
window: AnyWindowHandle,
f: impl FnOnce(&mut TestWindow) -> R,
) -> R {
window
.update(self, |_, cx| {
f(cx.window
.platform_window
.as_any_mut()
.downcast_mut::<TestWindow>()
.unwrap())
})
.unwrap()
} }
pub fn notifications<T: 'static>(&mut self, entity: &Model<T>) -> impl Stream<Item = ()> { pub fn notifications<T: 'static>(&mut self, entity: &Model<T>) -> impl Stream<Item = ()> {
@ -401,12 +457,24 @@ impl<'a> VisualTestContext<'a> {
Self { cx, window } Self { cx, window }
} }
pub fn run_until_parked(&self) {
self.cx.background_executor.run_until_parked();
}
pub fn dispatch_action<A>(&mut self, action: A) pub fn dispatch_action<A>(&mut self, action: A)
where where
A: Action, A: Action,
{ {
self.cx.dispatch_action(self.window, action) self.cx.dispatch_action(self.window, action)
} }
pub fn simulate_keystrokes(&mut self, keystrokes: &str) {
self.cx.simulate_keystrokes(self.window, keystrokes)
}
pub fn simulate_input(&mut self, input: &str) {
self.cx.simulate_input(self.window, input)
}
} }
impl<'a> Context for VisualTestContext<'a> { impl<'a> Context for VisualTestContext<'a> {

View file

@ -3,7 +3,7 @@ use crate::{
}; };
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec; pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem}; use std::{any::Any, fmt::Debug, mem};
pub trait Element<V: 'static> { pub trait Element<V: 'static> {
type ElementState: 'static; type ElementState: 'static;
@ -33,6 +33,42 @@ pub trait Element<V: 'static> {
element_state: &mut Self::ElementState, element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
); );
fn draw<T, R>(
self,
origin: Point<Pixels>,
available_space: Size<T>,
view_state: &mut V,
cx: &mut ViewContext<V>,
f: impl FnOnce(&Self::ElementState, &mut ViewContext<V>) -> R,
) -> R
where
Self: Sized,
T: Clone + Default + Debug + Into<AvailableSpace>,
{
let mut element = RenderedElement {
element: self,
phase: ElementRenderPhase::Start,
};
element.draw(origin, available_space.map(Into::into), view_state, cx);
if let ElementRenderPhase::Painted { frame_state } = &element.phase {
if let Some(frame_state) = frame_state.as_ref() {
f(&frame_state, cx)
} else {
let element_id = element
.element
.element_id()
.expect("we either have some frame_state or some element_id");
cx.with_element_state(element_id, |element_state, cx| {
let element_state = element_state.unwrap();
let result = f(&element_state, cx);
(result, element_state)
})
}
} else {
unreachable!()
}
}
} }
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)] #[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
@ -100,7 +136,9 @@ enum ElementRenderPhase<V> {
available_space: Size<AvailableSpace>, available_space: Size<AvailableSpace>,
frame_state: Option<V>, frame_state: Option<V>,
}, },
Painted, Painted {
frame_state: Option<V>,
},
} }
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered. /// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
@ -162,7 +200,7 @@ where
ElementRenderPhase::Start => panic!("must call initialize before layout"), ElementRenderPhase::Start => panic!("must call initialize before layout"),
ElementRenderPhase::LayoutRequested { .. } ElementRenderPhase::LayoutRequested { .. }
| ElementRenderPhase::LayoutComputed { .. } | ElementRenderPhase::LayoutComputed { .. }
| ElementRenderPhase::Painted => { | ElementRenderPhase::Painted { .. } => {
panic!("element rendered twice") panic!("element rendered twice")
} }
}; };
@ -197,7 +235,7 @@ where
self.element self.element
.paint(bounds, view_state, frame_state.as_mut().unwrap(), cx); .paint(bounds, view_state, frame_state.as_mut().unwrap(), cx);
} }
ElementRenderPhase::Painted ElementRenderPhase::Painted { frame_state }
} }
_ => panic!("must call layout before paint"), _ => panic!("must call layout before paint"),

View file

@ -6,15 +6,15 @@ use crate::{
SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility, SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility,
}; };
use collections::HashMap; use collections::HashMap;
use parking_lot::Mutex;
use refineable::Refineable; use refineable::Refineable;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
cell::RefCell,
fmt::Debug, fmt::Debug,
marker::PhantomData, marker::PhantomData,
mem, mem,
sync::Arc, rc::Rc,
time::Duration, time::Duration,
}; };
use taffy::style::Overflow; use taffy::style::Overflow;
@ -229,6 +229,20 @@ pub trait InteractiveComponent<V: 'static>: Sized + Element<V> {
mut self, mut self,
listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static, listener: impl Fn(&mut V, &A, &mut ViewContext<V>) + 'static,
) -> Self { ) -> Self {
// NOTE: this debug assert has the side-effect of working around
// a bug where a crate consisting only of action definitions does
// not register the actions in debug builds:
//
// https://github.com/rust-lang/rust/issues/47384
// https://github.com/mmastrac/rust-ctor/issues/280
//
// if we are relying on this side-effect still, removing the debug_assert!
// likely breaks the command_palette tests.
debug_assert!(
A::is_registered(),
"{:?} is not registered as an action",
A::qualified_name()
);
self.interactivity().action_listeners.push(( self.interactivity().action_listeners.push((
TypeId::of::<A>(), TypeId::of::<A>(),
Box::new(move |view, action, phase, cx| { Box::new(move |view, action, phase, cx| {
@ -406,7 +420,7 @@ pub trait StatefulInteractiveComponent<V: 'static, E: Element<V>>: InteractiveCo
self.interactivity().tooltip_builder.is_none(), self.interactivity().tooltip_builder.is_none(),
"calling tooltip more than once on the same element is not supported" "calling tooltip more than once on the same element is not supported"
); );
self.interactivity().tooltip_builder = Some(Arc::new(move |view_state, cx| { self.interactivity().tooltip_builder = Some(Rc::new(move |view_state, cx| {
build_tooltip(view_state, cx).into() build_tooltip(view_state, cx).into()
})); }));
@ -555,7 +569,7 @@ type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
pub type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>; pub type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>;
pub type TooltipBuilder<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>; pub type TooltipBuilder<V> = Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>;
pub type KeyDownListener<V> = pub type KeyDownListener<V> =
Box<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static>; Box<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static>;
@ -597,7 +611,7 @@ impl<V: 'static> ParentComponent<V> for Div<V> {
} }
impl<V: 'static> Element<V> for Div<V> { impl<V: 'static> Element<V> for Div<V> {
type ElementState = NodeState; type ElementState = DivState;
fn element_id(&self) -> Option<ElementId> { fn element_id(&self) -> Option<ElementId> {
self.interactivity.element_id.clone() self.interactivity.element_id.clone()
@ -617,7 +631,7 @@ impl<V: 'static> Element<V> for Div<V> {
child.initialize(view_state, cx); child.initialize(view_state, cx);
} }
NodeState { DivState {
interactive_state, interactive_state,
child_layout_ids: SmallVec::new(), child_layout_ids: SmallVec::new(),
} }
@ -706,11 +720,17 @@ impl<V: 'static> Component<V> for Div<V> {
} }
} }
pub struct NodeState { pub struct DivState {
child_layout_ids: SmallVec<[LayoutId; 4]>, child_layout_ids: SmallVec<[LayoutId; 4]>,
interactive_state: InteractiveElementState, interactive_state: InteractiveElementState,
} }
impl DivState {
pub fn is_active(&self) -> bool {
self.interactive_state.pending_mouse_down.borrow().is_some()
}
}
pub struct Interactivity<V> { pub struct Interactivity<V> {
pub element_id: Option<ElementId>, pub element_id: Option<ElementId>,
pub key_context: KeyContext, pub key_context: KeyContext,
@ -876,7 +896,7 @@ where
if !click_listeners.is_empty() || drag_listener.is_some() { if !click_listeners.is_empty() || drag_listener.is_some() {
let pending_mouse_down = element_state.pending_mouse_down.clone(); let pending_mouse_down = element_state.pending_mouse_down.clone();
let mouse_down = pending_mouse_down.lock().clone(); let mouse_down = pending_mouse_down.borrow().clone();
if let Some(mouse_down) = mouse_down { if let Some(mouse_down) = mouse_down {
if let Some(drag_listener) = drag_listener { if let Some(drag_listener) = drag_listener {
let active_state = element_state.clicked_state.clone(); let active_state = element_state.clicked_state.clone();
@ -890,7 +910,7 @@ where
&& bounds.contains_point(&event.position) && bounds.contains_point(&event.position)
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD && (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
{ {
*active_state.lock() = ElementClickedState::default(); *active_state.borrow_mut() = ElementClickedState::default();
let cursor_offset = event.position - bounds.origin; let cursor_offset = event.position - bounds.origin;
let drag = drag_listener(view_state, cursor_offset, cx); let drag = drag_listener(view_state, cursor_offset, cx);
cx.active_drag = Some(drag); cx.active_drag = Some(drag);
@ -910,12 +930,14 @@ where
listener(view_state, &mouse_click, cx); listener(view_state, &mouse_click, cx);
} }
} }
*pending_mouse_down.lock() = None; *pending_mouse_down.borrow_mut() = None;
cx.notify();
}); });
} else { } else {
cx.on_mouse_event(move |_state, event: &MouseDownEvent, phase, _cx| { cx.on_mouse_event(move |_view_state, event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
*pending_mouse_down.lock() = Some(event.clone()); *pending_mouse_down.borrow_mut() = Some(event.clone());
cx.notify();
} }
}); });
} }
@ -930,8 +952,8 @@ where
return; return;
} }
let is_hovered = let is_hovered =
bounds.contains_point(&event.position) && has_mouse_down.lock().is_none(); bounds.contains_point(&event.position) && has_mouse_down.borrow().is_none();
let mut was_hovered = was_hovered.lock(); let mut was_hovered = was_hovered.borrow_mut();
if is_hovered != was_hovered.clone() { if is_hovered != was_hovered.clone() {
*was_hovered = is_hovered; *was_hovered = is_hovered;
@ -952,13 +974,13 @@ where
} }
let is_hovered = let is_hovered =
bounds.contains_point(&event.position) && pending_mouse_down.lock().is_none(); bounds.contains_point(&event.position) && pending_mouse_down.borrow().is_none();
if !is_hovered { if !is_hovered {
active_tooltip.lock().take(); active_tooltip.borrow_mut().take();
return; return;
} }
if active_tooltip.lock().is_none() { if active_tooltip.borrow().is_none() {
let task = cx.spawn({ let task = cx.spawn({
let active_tooltip = active_tooltip.clone(); let active_tooltip = active_tooltip.clone();
let tooltip_builder = tooltip_builder.clone(); let tooltip_builder = tooltip_builder.clone();
@ -966,7 +988,7 @@ where
move |view, mut cx| async move { move |view, mut cx| async move {
cx.background_executor().timer(TOOLTIP_DELAY).await; cx.background_executor().timer(TOOLTIP_DELAY).await;
view.update(&mut cx, move |view_state, cx| { view.update(&mut cx, move |view_state, cx| {
active_tooltip.lock().replace(ActiveTooltip { active_tooltip.borrow_mut().replace(ActiveTooltip {
waiting: None, waiting: None,
tooltip: Some(AnyTooltip { tooltip: Some(AnyTooltip {
view: tooltip_builder(view_state, cx), view: tooltip_builder(view_state, cx),
@ -978,14 +1000,14 @@ where
.ok(); .ok();
} }
}); });
active_tooltip.lock().replace(ActiveTooltip { active_tooltip.borrow_mut().replace(ActiveTooltip {
waiting: Some(task), waiting: Some(task),
tooltip: None, tooltip: None,
}); });
} }
}); });
if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() { if let Some(active_tooltip) = element_state.active_tooltip.borrow().as_ref() {
if active_tooltip.tooltip.is_some() { if active_tooltip.tooltip.is_some() {
cx.active_tooltip = active_tooltip.tooltip.clone() cx.active_tooltip = active_tooltip.tooltip.clone()
} }
@ -993,10 +1015,10 @@ where
} }
let active_state = element_state.clicked_state.clone(); let active_state = element_state.clicked_state.clone();
if !active_state.lock().is_clicked() { if !active_state.borrow().is_clicked() {
cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| { cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Capture { if phase == DispatchPhase::Capture {
*active_state.lock() = ElementClickedState::default(); *active_state.borrow_mut() = ElementClickedState::default();
cx.notify(); cx.notify();
} }
}); });
@ -1011,7 +1033,7 @@ where
.map_or(false, |bounds| bounds.contains_point(&down.position)); .map_or(false, |bounds| bounds.contains_point(&down.position));
let element = bounds.contains_point(&down.position); let element = bounds.contains_point(&down.position);
if group || element { if group || element {
*active_state.lock() = ElementClickedState { group, element }; *active_state.borrow_mut() = ElementClickedState { group, element };
cx.notify(); cx.notify();
} }
} }
@ -1022,14 +1044,14 @@ where
if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll { if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
let scroll_offset = element_state let scroll_offset = element_state
.scroll_offset .scroll_offset
.get_or_insert_with(Arc::default) .get_or_insert_with(Rc::default)
.clone(); .clone();
let line_height = cx.line_height(); let line_height = cx.line_height();
let scroll_max = (content_size - bounds.size).max(&Size::default()); let scroll_max = (content_size - bounds.size).max(&Size::default());
cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| { cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
let mut scroll_offset = scroll_offset.lock(); let mut scroll_offset = scroll_offset.borrow_mut();
let old_scroll_offset = *scroll_offset; let old_scroll_offset = *scroll_offset;
let delta = event.delta.pixel_delta(line_height); let delta = event.delta.pixel_delta(line_height);
@ -1058,7 +1080,7 @@ where
let scroll_offset = element_state let scroll_offset = element_state
.scroll_offset .scroll_offset
.as_ref() .as_ref()
.map(|scroll_offset| *scroll_offset.lock()); .map(|scroll_offset| *scroll_offset.borrow());
cx.with_key_dispatch( cx.with_key_dispatch(
self.key_context.clone(), self.key_context.clone(),
@ -1157,7 +1179,7 @@ where
} }
} }
let clicked_state = element_state.clicked_state.lock(); let clicked_state = element_state.clicked_state.borrow();
if clicked_state.group { if clicked_state.group {
if let Some(group) = self.group_active_style.as_ref() { if let Some(group) = self.group_active_style.as_ref() {
style.refine(&group.style) style.refine(&group.style)
@ -1211,11 +1233,11 @@ impl<V: 'static> Default for Interactivity<V> {
#[derive(Default)] #[derive(Default)]
pub struct InteractiveElementState { pub struct InteractiveElementState {
pub focus_handle: Option<FocusHandle>, pub focus_handle: Option<FocusHandle>,
pub clicked_state: Arc<Mutex<ElementClickedState>>, pub clicked_state: Rc<RefCell<ElementClickedState>>,
pub hover_state: Arc<Mutex<bool>>, pub hover_state: Rc<RefCell<bool>>,
pub pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>, pub pending_mouse_down: Rc<RefCell<Option<MouseDownEvent>>>,
pub scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>, pub scroll_offset: Option<Rc<RefCell<Point<Pixels>>>>,
pub active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>, pub active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
} }
pub struct ActiveTooltip { pub struct ActiveTooltip {

View file

@ -3,9 +3,8 @@ use crate::{
ElementId, InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels, ElementId, InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels,
Point, Size, StyleRefinement, Styled, ViewContext, Point, Size, StyleRefinement, Styled, ViewContext,
}; };
use parking_lot::Mutex;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{cmp, mem, ops::Range, sync::Arc}; use std::{cell::RefCell, cmp, mem, ops::Range, rc::Rc};
use taffy::style::Overflow; use taffy::style::Overflow;
/// uniform_list provides lazy rendering for a set of items that are of uniform height. /// uniform_list provides lazy rendering for a set of items that are of uniform height.
@ -61,23 +60,23 @@ pub struct UniformList<V: 'static> {
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct UniformListScrollHandle(Arc<Mutex<Option<ScrollHandleState>>>); pub struct UniformListScrollHandle(Rc<RefCell<Option<ScrollHandleState>>>);
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct ScrollHandleState { struct ScrollHandleState {
item_height: Pixels, item_height: Pixels,
list_height: Pixels, list_height: Pixels,
scroll_offset: Arc<Mutex<Point<Pixels>>>, scroll_offset: Rc<RefCell<Point<Pixels>>>,
} }
impl UniformListScrollHandle { impl UniformListScrollHandle {
pub fn new() -> Self { pub fn new() -> Self {
Self(Arc::new(Mutex::new(None))) Self(Rc::new(RefCell::new(None)))
} }
pub fn scroll_to_item(&self, ix: usize) { pub fn scroll_to_item(&self, ix: usize) {
if let Some(state) = &*self.0.lock() { if let Some(state) = &*self.0.borrow() {
let mut scroll_offset = state.scroll_offset.lock(); let mut scroll_offset = state.scroll_offset.borrow_mut();
let item_top = state.item_height * ix; let item_top = state.item_height * ix;
let item_bottom = item_top + state.item_height; let item_bottom = item_top + state.item_height;
let scroll_top = -scroll_offset.y; let scroll_top = -scroll_offset.y;
@ -196,7 +195,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
let shared_scroll_offset = element_state let shared_scroll_offset = element_state
.interactive .interactive
.scroll_offset .scroll_offset
.get_or_insert_with(Arc::default) .get_or_insert_with(Rc::default)
.clone(); .clone();
interactivity.paint( interactivity.paint(
@ -222,7 +221,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
.measure_item(view_state, Some(padded_bounds.size.width), cx) .measure_item(view_state, Some(padded_bounds.size.width), cx)
.height; .height;
if let Some(scroll_handle) = self.scroll_handle.clone() { if let Some(scroll_handle) = self.scroll_handle.clone() {
scroll_handle.0.lock().replace(ScrollHandleState { scroll_handle.0.borrow_mut().replace(ScrollHandleState {
item_height, item_height,
list_height: padded_bounds.size.height, list_height: padded_bounds.size.height,
scroll_offset: shared_scroll_offset, scroll_offset: shared_scroll_offset,

View file

@ -337,7 +337,6 @@ mod test {
.update(cx, |test_view, cx| cx.focus(&test_view.focus_handle)) .update(cx, |test_view, cx| cx.focus(&test_view.focus_handle))
.unwrap(); .unwrap();
cx.dispatch_keystroke(*window, Keystroke::parse("space").unwrap(), false);
cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap(), false); cx.dispatch_keystroke(*window, Keystroke::parse("ctrl-g").unwrap(), false);
window window

View file

@ -60,7 +60,7 @@ impl DispatchTree {
self.keystroke_matchers.clear(); self.keystroke_matchers.clear();
} }
pub fn push_node(&mut self, context: KeyContext, old_dispatcher: &mut Self) { pub fn push_node(&mut self, context: KeyContext) {
let parent = self.node_stack.last().copied(); let parent = self.node_stack.last().copied();
let node_id = DispatchNodeId(self.nodes.len()); let node_id = DispatchNodeId(self.nodes.len());
self.nodes.push(DispatchNode { self.nodes.push(DispatchNode {
@ -71,12 +71,6 @@ impl DispatchTree {
if !context.is_empty() { if !context.is_empty() {
self.active_node().context = context.clone(); self.active_node().context = context.clone();
self.context_stack.push(context); self.context_stack.push(context);
if let Some((context_stack, matcher)) = old_dispatcher
.keystroke_matchers
.remove_entry(self.context_stack.as_slice())
{
self.keystroke_matchers.insert(context_stack, matcher);
}
} }
} }
@ -87,6 +81,33 @@ impl DispatchTree {
} }
} }
pub fn clear_keystroke_matchers(&mut self) {
self.keystroke_matchers.clear();
}
/// Preserve keystroke matchers from previous frames to support multi-stroke
/// bindings across multiple frames.
pub fn preserve_keystroke_matchers(&mut self, old_tree: &mut Self, focus_id: Option<FocusId>) {
if let Some(node_id) = focus_id.and_then(|focus_id| self.focusable_node_id(focus_id)) {
let dispatch_path = self.dispatch_path(node_id);
self.context_stack.clear();
for node_id in dispatch_path {
let node = self.node(node_id);
if !node.context.is_empty() {
self.context_stack.push(node.context.clone());
}
if let Some((context_stack, matcher)) = old_tree
.keystroke_matchers
.remove_entry(self.context_stack.as_slice())
{
self.keystroke_matchers.insert(context_stack, matcher);
}
}
}
}
pub fn on_key_event(&mut self, listener: KeyListener) { pub fn on_key_event(&mut self, listener: KeyListener) {
self.active_node().key_listeners.push(listener); self.active_node().key_listeners.push(listener);
} }

View file

@ -184,7 +184,11 @@ pub trait PlatformTextSystem: Send + Sync {
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>; fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> Result<Size<f32>>;
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>; fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>; fn glyph_raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>>;
fn rasterize_glyph(&self, params: &RenderGlyphParams) -> Result<(Size<DevicePixels>, Vec<u8>)>; fn rasterize_glyph(
&self,
params: &RenderGlyphParams,
raster_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)>;
fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout; fn layout_line(&self, text: &str, font_size: Pixels, runs: &[FontRun]) -> LineLayout;
fn wrap_line( fn wrap_line(
&self, &self,

View file

@ -116,7 +116,9 @@ impl PlatformTextSystem for MacTextSystem {
}, },
)?; )?;
Ok(candidates[ix]) let font_id = candidates[ix];
lock.font_selections.insert(font.clone(), font_id);
Ok(font_id)
} }
} }
@ -145,8 +147,9 @@ impl PlatformTextSystem for MacTextSystem {
fn rasterize_glyph( fn rasterize_glyph(
&self, &self,
glyph_id: &RenderGlyphParams, glyph_id: &RenderGlyphParams,
raster_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)> { ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
self.0.read().rasterize_glyph(glyph_id) self.0.read().rasterize_glyph(glyph_id, raster_bounds)
} }
fn layout_line(&self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout { fn layout_line(&self, text: &str, font_size: Pixels, font_runs: &[FontRun]) -> LineLayout {
@ -247,8 +250,11 @@ impl MacTextSystemState {
.into()) .into())
} }
fn rasterize_glyph(&self, params: &RenderGlyphParams) -> Result<(Size<DevicePixels>, Vec<u8>)> { fn rasterize_glyph(
let glyph_bounds = self.raster_bounds(params)?; &self,
params: &RenderGlyphParams,
glyph_bounds: Bounds<DevicePixels>,
) -> Result<(Size<DevicePixels>, Vec<u8>)> {
if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 { if glyph_bounds.size.width.0 == 0 || glyph_bounds.size.height.0 == 0 {
Err(anyhow!("glyph bounds are empty")) Err(anyhow!("glyph bounds are empty"))
} else { } else {
@ -260,6 +266,7 @@ impl MacTextSystemState {
if params.subpixel_variant.y > 0 { if params.subpixel_variant.y > 0 {
bitmap_size.height += DevicePixels(1); bitmap_size.height += DevicePixels(1);
} }
let bitmap_size = bitmap_size;
let mut bytes; let mut bytes;
let cx; let cx;

View file

@ -22,7 +22,7 @@ pub struct TestWindow {
bounds: WindowBounds, bounds: WindowBounds,
current_scene: Mutex<Option<Scene>>, current_scene: Mutex<Option<Scene>>,
display: Rc<dyn PlatformDisplay>, display: Rc<dyn PlatformDisplay>,
input_handler: Option<Box<dyn PlatformInputHandler>>, pub(crate) input_handler: Option<Arc<Mutex<Box<dyn PlatformInputHandler>>>>,
handlers: Mutex<Handlers>, handlers: Mutex<Handlers>,
platform: Weak<TestPlatform>, platform: Weak<TestPlatform>,
sprite_atlas: Arc<dyn PlatformAtlas>, sprite_atlas: Arc<dyn PlatformAtlas>,
@ -80,11 +80,11 @@ impl PlatformWindow for TestWindow {
} }
fn as_any_mut(&mut self) -> &mut dyn std::any::Any { fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
todo!() self
} }
fn set_input_handler(&mut self, input_handler: Box<dyn crate::PlatformInputHandler>) { fn set_input_handler(&mut self, input_handler: Box<dyn crate::PlatformInputHandler>) {
self.input_handler = Some(input_handler); self.input_handler = Some(Arc::new(Mutex::new(input_handler)));
} }
fn prompt( fn prompt(

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask, black, phi, point, rems, AbsoluteLength, BorrowAppContext, BorrowWindow, Bounds, ContentMask,
Corners, CornersRefinement, CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font, Corners, CornersRefinement, CursorStyle, DefiniteLength, Edges, EdgesRefinement, Font,
FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Result, FontFeatures, FontStyle, FontWeight, Hsla, Length, Pixels, Point, PointRefinement, Rgba,
Rgba, SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext, SharedString, Size, SizeRefinement, Styled, TextRun, ViewContext,
}; };
use refineable::{Cascade, Refineable}; use refineable::{Cascade, Refineable};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -157,7 +157,7 @@ impl Default for TextStyle {
} }
impl TextStyle { impl TextStyle {
pub fn highlight(mut self, style: HighlightStyle) -> Result<Self> { pub fn highlight(mut self, style: HighlightStyle) -> Self {
if let Some(weight) = style.font_weight { if let Some(weight) = style.font_weight {
self.font_weight = weight; self.font_weight = weight;
} }
@ -177,7 +177,7 @@ impl TextStyle {
self.underline = Some(underline); self.underline = Some(underline);
} }
Ok(self) self
} }
pub fn font(&self) -> Font { pub fn font(&self) -> Font {

View file

@ -39,6 +39,7 @@ pub struct TextSystem {
platform_text_system: Arc<dyn PlatformTextSystem>, platform_text_system: Arc<dyn PlatformTextSystem>,
font_ids_by_font: RwLock<HashMap<Font, FontId>>, font_ids_by_font: RwLock<HashMap<Font, FontId>>,
font_metrics: RwLock<HashMap<FontId, FontMetrics>>, font_metrics: RwLock<HashMap<FontId, FontMetrics>>,
raster_bounds: RwLock<HashMap<RenderGlyphParams, Bounds<DevicePixels>>>,
wrapper_pool: Mutex<HashMap<FontIdWithSize, Vec<LineWrapper>>>, wrapper_pool: Mutex<HashMap<FontIdWithSize, Vec<LineWrapper>>>,
font_runs_pool: Mutex<Vec<Vec<FontRun>>>, font_runs_pool: Mutex<Vec<Vec<FontRun>>>,
} }
@ -48,10 +49,11 @@ impl TextSystem {
TextSystem { TextSystem {
line_layout_cache: Arc::new(LineLayoutCache::new(platform_text_system.clone())), line_layout_cache: Arc::new(LineLayoutCache::new(platform_text_system.clone())),
platform_text_system, platform_text_system,
font_metrics: RwLock::new(HashMap::default()), font_metrics: RwLock::default(),
font_ids_by_font: RwLock::new(HashMap::default()), raster_bounds: RwLock::default(),
wrapper_pool: Mutex::new(HashMap::default()), font_ids_by_font: RwLock::default(),
font_runs_pool: Default::default(), wrapper_pool: Mutex::default(),
font_runs_pool: Mutex::default(),
} }
} }
@ -252,14 +254,24 @@ impl TextSystem {
} }
pub fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> { pub fn raster_bounds(&self, params: &RenderGlyphParams) -> Result<Bounds<DevicePixels>> {
self.platform_text_system.glyph_raster_bounds(params) let raster_bounds = self.raster_bounds.upgradable_read();
if let Some(bounds) = raster_bounds.get(params) {
Ok(bounds.clone())
} else {
let mut raster_bounds = RwLockUpgradableReadGuard::upgrade(raster_bounds);
let bounds = self.platform_text_system.glyph_raster_bounds(params)?;
raster_bounds.insert(params.clone(), bounds);
Ok(bounds)
}
} }
pub fn rasterize_glyph( pub fn rasterize_glyph(
&self, &self,
glyph_id: &RenderGlyphParams, params: &RenderGlyphParams,
) -> Result<(Size<DevicePixels>, Vec<u8>)> { ) -> Result<(Size<DevicePixels>, Vec<u8>)> {
self.platform_text_system.rasterize_glyph(glyph_id) let raster_bounds = self.raster_bounds(params)?;
self.platform_text_system
.rasterize_glyph(params, raster_bounds)
} }
} }

View file

@ -63,6 +63,16 @@ impl<V: 'static> View<V> {
pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V { pub fn read<'a>(&self, cx: &'a AppContext) -> &'a V {
self.model.read(cx) self.model.read(cx)
} }
pub fn render_with<C>(&self, component: C) -> RenderViewWith<C, V>
where
C: 'static + Component<V>,
{
RenderViewWith {
view: self.clone(),
component: Some(component),
}
}
} }
impl<V> Clone for View<V> { impl<V> Clone for View<V> {
@ -281,6 +291,67 @@ impl<V: Render> From<WeakView<V>> for AnyWeakView {
// } // }
// } // }
pub struct RenderViewWith<C, V> {
view: View<V>,
component: Option<C>,
}
impl<C, ParentViewState, ViewState> Component<ParentViewState> for RenderViewWith<C, ViewState>
where
C: 'static + Component<ViewState>,
ParentViewState: 'static,
ViewState: 'static,
{
fn render(self) -> AnyElement<ParentViewState> {
AnyElement::new(self)
}
}
impl<C, ParentViewState, ViewState> Element<ParentViewState> for RenderViewWith<C, ViewState>
where
C: 'static + Component<ViewState>,
ParentViewState: 'static,
ViewState: 'static,
{
type ElementState = AnyElement<ViewState>;
fn element_id(&self) -> Option<ElementId> {
Some(self.view.entity_id().into())
}
fn initialize(
&mut self,
_: &mut ParentViewState,
_: Option<Self::ElementState>,
cx: &mut ViewContext<ParentViewState>,
) -> Self::ElementState {
self.view.update(cx, |view, cx| {
let mut element = self.component.take().unwrap().render();
element.initialize(view, cx);
element
})
}
fn layout(
&mut self,
_: &mut ParentViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentViewState>,
) -> LayoutId {
self.view.update(cx, |view, cx| element.layout(view, cx))
}
fn paint(
&mut self,
_: Bounds<Pixels>,
_: &mut ParentViewState,
element: &mut Self::ElementState,
cx: &mut ViewContext<ParentViewState>,
) {
self.view.update(cx, |view, cx| element.paint(view, cx))
}
}
mod any_view { mod any_view {
use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext}; use crate::{AnyElement, AnyView, BorrowWindow, LayoutId, Render, WindowContext};
use std::any::Any; use std::any::Any;

View file

@ -189,7 +189,7 @@ impl Drop for FocusHandle {
pub struct Window { pub struct Window {
pub(crate) handle: AnyWindowHandle, pub(crate) handle: AnyWindowHandle,
pub(crate) removed: bool, pub(crate) removed: bool,
platform_window: Box<dyn PlatformWindow>, pub(crate) platform_window: Box<dyn PlatformWindow>,
display_id: DisplayId, display_id: DisplayId,
sprite_atlas: Arc<dyn PlatformAtlas>, sprite_atlas: Arc<dyn PlatformAtlas>,
rem_size: Pixels, rem_size: Pixels,
@ -216,7 +216,7 @@ pub struct Window {
// #[derive(Default)] // #[derive(Default)]
pub(crate) struct Frame { pub(crate) struct Frame {
element_states: HashMap<GlobalElementId, AnyBox>, pub(crate) element_states: HashMap<GlobalElementId, AnyBox>,
mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyMouseListener)>>, mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyMouseListener)>>,
pub(crate) dispatch_tree: DispatchTree, pub(crate) dispatch_tree: DispatchTree,
pub(crate) focus_listeners: Vec<AnyFocusListener>, pub(crate) focus_listeners: Vec<AnyFocusListener>,
@ -393,6 +393,10 @@ impl<'a> WindowContext<'a> {
/// Move focus to the element associated with the given `FocusHandle`. /// Move focus to the element associated with the given `FocusHandle`.
pub fn focus(&mut self, handle: &FocusHandle) { pub fn focus(&mut self, handle: &FocusHandle) {
if self.window.focus == Some(handle.id) {
return;
}
let focus_id = handle.id; let focus_id = handle.id;
if self.window.last_blur.is_none() { if self.window.last_blur.is_none() {
@ -400,6 +404,10 @@ impl<'a> WindowContext<'a> {
} }
self.window.focus = Some(focus_id); self.window.focus = Some(focus_id);
self.window
.current_frame
.dispatch_tree
.clear_keystroke_matchers();
self.app.push_effect(Effect::FocusChanged { self.app.push_effect(Effect::FocusChanged {
window_handle: self.window.handle, window_handle: self.window.handle,
focused: Some(focus_id), focused: Some(focus_id),
@ -1091,6 +1099,14 @@ impl<'a> WindowContext<'a> {
}); });
} }
self.window
.current_frame
.dispatch_tree
.preserve_keystroke_matchers(
&mut self.window.previous_frame.dispatch_tree,
self.window.focus,
);
self.window.root_view = Some(root_view); self.window.root_view = Some(root_view);
let scene = self.window.current_frame.scene_builder.build(); let scene = self.window.current_frame.scene_builder.build();
@ -2093,7 +2109,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
window window
.current_frame .current_frame
.dispatch_tree .dispatch_tree
.push_node(context.clone(), &mut window.previous_frame.dispatch_tree); .push_node(context.clone());
if let Some(focus_handle) = focus_handle.as_ref() { if let Some(focus_handle) = focus_handle.as_ref() {
window window
.current_frame .current_frame
@ -2471,7 +2487,7 @@ impl From<SmallVec<[u32; 16]>> for StackingOrder {
#[derive(Clone, Debug, Eq, PartialEq, Hash)] #[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum ElementId { pub enum ElementId {
View(EntityId), View(EntityId),
Number(usize), Integer(usize),
Name(SharedString), Name(SharedString),
FocusHandle(FocusId), FocusHandle(FocusId),
} }
@ -2496,13 +2512,13 @@ impl From<EntityId> for ElementId {
impl From<usize> for ElementId { impl From<usize> for ElementId {
fn from(id: usize) -> Self { fn from(id: usize) -> Self {
ElementId::Number(id) ElementId::Integer(id)
} }
} }
impl From<i32> for ElementId { impl From<i32> for ElementId {
fn from(id: i32) -> Self { fn from(id: i32) -> Self {
Self::Number(id as usize) Self::Integer(id as usize)
} }
} }

View file

@ -1709,6 +1709,7 @@ impl Project {
self.open_remote_buffer_internal(&project_path.path, &worktree, cx) self.open_remote_buffer_internal(&project_path.path, &worktree, cx)
}; };
let project_path = project_path.clone();
cx.spawn(move |this, mut cx| async move { cx.spawn(move |this, mut cx| async move {
let load_result = load_buffer.await; let load_result = load_buffer.await;
*tx.borrow_mut() = Some(this.update(&mut cx, |this, _| { *tx.borrow_mut() = Some(this.update(&mut cx, |this, _| {
@ -1726,7 +1727,7 @@ impl Project {
cx.foreground().spawn(async move { cx.foreground().spawn(async move {
wait_for_loading_buffer(loading_watch) wait_for_loading_buffer(loading_watch)
.await .await
.map_err(|error| anyhow!("{}", error)) .map_err(|error| anyhow!("{project_path:?} opening failure: {error:#}"))
}) })
} }

View file

@ -3694,7 +3694,7 @@ impl BackgroundScanner {
} }
Err(err) => { Err(err) => {
// TODO - create a special 'error' entry in the entries tree to mark this // TODO - create a special 'error' entry in the entries tree to mark this
log::error!("error reading file on event {:?}", err); log::error!("error reading file {abs_path:?} on event: {err:#}");
} }
} }
} }

View file

@ -1741,6 +1741,7 @@ impl Project {
self.open_remote_buffer_internal(&project_path.path, &worktree, cx) self.open_remote_buffer_internal(&project_path.path, &worktree, cx)
}; };
let project_path = project_path.clone();
cx.spawn(move |this, mut cx| async move { cx.spawn(move |this, mut cx| async move {
let load_result = load_buffer.await; let load_result = load_buffer.await;
*tx.borrow_mut() = Some(this.update(&mut cx, |this, _| { *tx.borrow_mut() = Some(this.update(&mut cx, |this, _| {
@ -1759,7 +1760,7 @@ impl Project {
cx.background_executor().spawn(async move { cx.background_executor().spawn(async move {
wait_for_loading_buffer(loading_watch) wait_for_loading_buffer(loading_watch)
.await .await
.map_err(|error| anyhow!("{}", error)) .map_err(|error| anyhow!("{project_path:?} opening failure: {error:#}"))
}) })
} }

View file

@ -3684,7 +3684,7 @@ impl BackgroundScanner {
} }
Err(err) => { Err(err) => {
// TODO - create a special 'error' entry in the entries tree to mark this // TODO - create a special 'error' entry in the entries tree to mark this
log::error!("error reading file on event {:?}", err); log::error!("error reading file {abs_path:?} on event: {err:#}");
} }
} }
} }

View file

@ -72,7 +72,7 @@ impl ModalLayer {
cx.notify(); cx.notify();
} }
pub fn current_modal<V>(&self) -> Option<View<V>> pub fn active_modal<V>(&self) -> Option<View<V>>
where where
V: 'static, V: 'static,
{ {

View file

@ -8,8 +8,8 @@ use anyhow::Result;
use collections::{HashMap, HashSet, VecDeque}; use collections::{HashMap, HashSet, VecDeque};
use gpui::{ use gpui::{
actions, prelude::*, register_action, AppContext, AsyncWindowContext, Component, Div, EntityId, actions, prelude::*, register_action, AppContext, AsyncWindowContext, Component, Div, EntityId,
EventEmitter, FocusHandle, Model, PromptLevel, Render, Task, View, ViewContext, VisualContext, EventEmitter, FocusHandle, Focusable, Model, PromptLevel, Render, Task, View, ViewContext,
WeakView, WindowContext, VisualContext, WeakView, WindowContext,
}; };
use parking_lot::Mutex; use parking_lot::Mutex;
use project2::{Project, ProjectEntryId, ProjectPath}; use project2::{Project, ProjectEntryId, ProjectPath};
@ -1017,7 +1017,11 @@ impl Pane {
.unwrap_or_else(|| item_index.min(self.items.len()).saturating_sub(1)); .unwrap_or_else(|| item_index.min(self.items.len()).saturating_sub(1));
let should_activate = activate_pane || self.has_focus(cx); let should_activate = activate_pane || self.has_focus(cx);
self.activate_item(index_to_activate, should_activate, should_activate, cx); if self.items.len() == 1 && should_activate {
self.focus_handle.focus(cx);
} else {
self.activate_item(index_to_activate, should_activate, should_activate, cx);
}
} }
let item = self.items.remove(item_index); let item = self.items.remove(item_index);
@ -1913,11 +1917,12 @@ impl Pane {
// } // }
impl Render for Pane { impl Render for Pane {
type Element = Div<Self>; type Element = Focusable<Self, Div<Self>>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
v_stack() v_stack()
.key_context("Pane") .key_context("Pane")
.track_focus(&self.focus_handle)
.size_full() .size_full()
.on_action(|pane: &mut Self, action, cx| { .on_action(|pane: &mut Self, action, cx| {
pane.close_active_item(action, cx) pane.close_active_item(action, cx)

View file

@ -38,9 +38,9 @@ use futures::{
use gpui::{ use gpui::{
actions, div, point, prelude::*, rems, size, Action, AnyModel, AnyView, AnyWeakView, actions, div, point, prelude::*, rems, size, Action, AnyModel, AnyView, AnyWeakView,
AppContext, AsyncAppContext, AsyncWindowContext, Bounds, Component, Div, Entity, EntityId, AppContext, AsyncAppContext, AsyncWindowContext, Bounds, Component, Div, Entity, EntityId,
EventEmitter, FocusHandle, GlobalPixels, KeyContext, Model, ModelContext, ParentComponent, EventEmitter, GlobalPixels, KeyContext, Model, ModelContext, ParentComponent, Point, Render,
Point, Render, Size, Styled, Subscription, Task, View, ViewContext, WeakView, WindowBounds, Size, Styled, Subscription, Task, View, ViewContext, WeakView, WindowBounds, WindowContext,
WindowContext, WindowHandle, WindowOptions, WindowHandle, WindowOptions,
}; };
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem}; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
use itertools::Itertools; use itertools::Itertools;
@ -433,7 +433,6 @@ pub enum Event {
pub struct Workspace { pub struct Workspace {
weak_self: WeakView<Self>, weak_self: WeakView<Self>,
focus_handle: FocusHandle,
workspace_actions: Vec<Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>>, workspace_actions: Vec<Box<dyn Fn(Div<Workspace>) -> Div<Workspace>>>,
zoomed: Option<AnyWeakView>, zoomed: Option<AnyWeakView>,
zoomed_position: Option<DockPosition>, zoomed_position: Option<DockPosition>,
@ -651,7 +650,6 @@ impl Workspace {
cx.defer(|this, cx| this.update_window_title(cx)); cx.defer(|this, cx| this.update_window_title(cx));
Workspace { Workspace {
weak_self: weak_handle.clone(), weak_self: weak_handle.clone(),
focus_handle: cx.focus_handle(),
zoomed: None, zoomed: None,
zoomed_position: None, zoomed_position: None,
center: PaneGroup::new(center_pane.clone()), center: PaneGroup::new(center_pane.clone()),
@ -1450,6 +1448,11 @@ impl Workspace {
self.active_pane().read(cx).active_item() self.active_pane().read(cx).active_item()
} }
pub fn active_item_as<I: 'static>(&self, cx: &AppContext) -> Option<View<I>> {
let item = self.active_item(cx)?;
item.to_any().downcast::<I>().ok()
}
fn active_project_path(&self, cx: &ViewContext<Self>) -> Option<ProjectPath> { fn active_project_path(&self, cx: &ViewContext<Self>) -> Option<ProjectPath> {
self.active_item(cx).and_then(|item| item.project_path(cx)) self.active_item(cx).and_then(|item| item.project_path(cx))
} }
@ -1570,7 +1573,7 @@ impl Workspace {
} }
if focus_center { if focus_center {
cx.focus(&self.focus_handle); self.active_pane.update(cx, |pane, cx| pane.focus(cx))
} }
cx.notify(); cx.notify();
@ -1704,7 +1707,7 @@ impl Workspace {
} }
if focus_center { if focus_center {
cx.focus(&self.focus_handle); self.active_pane.update(cx, |pane, cx| pane.focus(cx))
} }
if self.zoomed_position != dock_to_reveal { if self.zoomed_position != dock_to_reveal {
@ -3475,8 +3478,8 @@ impl Workspace {
div div
} }
pub fn current_modal<V: Modal + 'static>(&mut self, cx: &ViewContext<Self>) -> Option<View<V>> { pub fn active_modal<V: Modal + 'static>(&mut self, cx: &ViewContext<Self>) -> Option<View<V>> {
self.modal_layer.read(cx).current_modal() self.modal_layer.read(cx).active_modal()
} }
pub fn toggle_modal<V: Modal, B>(&mut self, cx: &mut ViewContext<Self>, build: B) pub fn toggle_modal<V: Modal, B>(&mut self, cx: &mut ViewContext<Self>, build: B)

View file

@ -3,7 +3,7 @@ authors = ["Nathan Sobo <nathansobo@gmail.com>"]
description = "The fast, collaborative code editor." description = "The fast, collaborative code editor."
edition = "2021" edition = "2021"
name = "zed" name = "zed"
version = "0.113.0" version = "0.114.0"
publish = false publish = false
[lib] [lib]