Use Picker in Outline view

This commit is contained in:
Max Brunsfeld 2022-04-15 14:28:01 -07:00
parent c56e2ead23
commit 3bbc021a7e
7 changed files with 131 additions and 237 deletions

1
Cargo.lock generated
View file

@ -3374,6 +3374,7 @@ dependencies = [
"gpui", "gpui",
"language", "language",
"ordered-float", "ordered-float",
"picker",
"postage", "postage",
"settings", "settings",
"smol", "smol",

View file

@ -250,22 +250,10 @@
"\n" "\n"
] ]
}, },
"OutlineView": {
"escape": "outline::Toggle"
},
"ProjectSymbolsView": {
"escape": "project_symbols::Toggle"
},
"ThemeSelector": {
"escape": "theme_selector::Toggle"
},
"GoToLine": { "GoToLine": {
"escape": "go_to_line::Toggle", "escape": "go_to_line::Toggle",
"enter": "go_to_line::Confirm" "enter": "go_to_line::Confirm"
}, },
"FileFinder": {
"escape": "file_finder::Toggle"
},
"ChatPanel": { "ChatPanel": {
"enter": "chat_panel::Send" "enter": "chat_panel::Send"
}, },

View file

@ -113,14 +113,11 @@ impl FileFinder {
} }
pub fn new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self { pub fn new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
cx.observe(&project, Self::project_updated).detach();
let handle = cx.weak_handle(); let handle = cx.weak_handle();
let picker = cx.add_view(|cx| Picker::new(handle, cx)); cx.observe(&project, Self::project_updated).detach();
Self { Self {
project, project,
picker, picker: cx.add_view(|cx| Picker::new(handle, cx)),
search_count: 0, search_count: 0,
latest_search_id: 0, latest_search_id: 0,
latest_search_did_cancel: false, latest_search_did_cancel: false,

View file

@ -12,6 +12,7 @@ editor = { path = "../editor" }
fuzzy = { path = "../fuzzy" } fuzzy = { path = "../fuzzy" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
language = { path = "../language" } language = { path = "../language" }
picker = { path = "../picker" }
settings = { path = "../settings" } settings = { path = "../settings" }
text = { path = "../text" } text = { path = "../text" }
workspace = { path = "../workspace" } workspace = { path = "../workspace" }

View file

@ -4,38 +4,31 @@ use editor::{
}; };
use fuzzy::StringMatch; use fuzzy::StringMatch;
use gpui::{ use gpui::{
actions, elements::*, geometry::vector::Vector2F, keymap, AppContext, Axis, Entity, actions, elements::*, geometry::vector::Vector2F, AppContext, Entity, MutableAppContext,
MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle, RenderContext, Task, View, ViewContext, ViewHandle,
}; };
use language::Outline; use language::Outline;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
use std::cmp::{self, Reverse}; use std::cmp::{self, Reverse};
use workspace::{ use workspace::Workspace;
menu::{Confirm, SelectFirst, SelectLast, SelectNext, SelectPrev},
Workspace,
};
actions!(outline, [Toggle]); actions!(outline, [Toggle]);
pub fn init(cx: &mut MutableAppContext) { pub fn init(cx: &mut MutableAppContext) {
cx.add_action(OutlineView::toggle); cx.add_action(OutlineView::toggle);
cx.add_action(OutlineView::confirm); Picker::<OutlineView>::init(cx);
cx.add_action(OutlineView::select_prev);
cx.add_action(OutlineView::select_next);
cx.add_action(OutlineView::select_first);
cx.add_action(OutlineView::select_last);
} }
struct OutlineView { struct OutlineView {
handle: WeakViewHandle<Self>, picker: ViewHandle<Picker<Self>>,
active_editor: ViewHandle<Editor>, active_editor: ViewHandle<Editor>,
outline: Outline<Anchor>, outline: Outline<Anchor>,
selected_match_index: usize, selected_match_index: usize,
prev_scroll_position: Option<Vector2F>, prev_scroll_position: Option<Vector2F>,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
query_editor: ViewHandle<Editor>, last_query: String,
list_state: UniformListState,
} }
pub enum Event { pub enum Event {
@ -55,38 +48,12 @@ impl View for OutlineView {
"OutlineView" "OutlineView"
} }
fn keymap_context(&self, _: &AppContext) -> keymap::Context { fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
let mut cx = Self::default_keymap_context(); ChildView::new(self.picker.clone()).boxed()
cx.set.insert("menu".into());
cx
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
let settings = cx.global::<Settings>();
Flex::new(Axis::Vertical)
.with_child(
Container::new(ChildView::new(&self.query_editor).boxed())
.with_style(settings.theme.selector.input_editor.container)
.boxed(),
)
.with_child(
FlexItem::new(self.render_matches(cx))
.flex(1.0, false)
.boxed(),
)
.contained()
.with_style(settings.theme.selector.container)
.constrained()
.with_max_width(800.0)
.with_max_height(1200.0)
.aligned()
.top()
.named("outline view")
} }
fn on_focus(&mut self, cx: &mut ViewContext<Self>) { fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
cx.focus(&self.query_editor); cx.focus(&self.picker);
} }
} }
@ -96,24 +63,16 @@ impl OutlineView {
editor: ViewHandle<Editor>, editor: ViewHandle<Editor>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let query_editor = cx.add_view(|cx| { let handle = cx.weak_handle();
Editor::single_line(Some(|theme| theme.selector.input_editor.clone()), cx) Self {
}); picker: cx.add_view(|cx| Picker::new(handle, cx).with_max_size(800., 1200.)),
cx.subscribe(&query_editor, Self::on_query_editor_event) last_query: Default::default(),
.detach();
let mut this = Self {
handle: cx.weak_handle(),
matches: Default::default(), matches: Default::default(),
selected_match_index: 0, selected_match_index: 0,
prev_scroll_position: Some(editor.update(cx, |editor, cx| editor.scroll_position(cx))), prev_scroll_position: Some(editor.update(cx, |editor, cx| editor.scroll_position(cx))),
active_editor: editor, active_editor: editor,
outline, outline,
query_editor, }
list_state: Default::default(),
};
this.update_matches(cx);
this
} }
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) { fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
@ -137,34 +96,18 @@ impl OutlineView {
} }
} }
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) { fn restore_active_editor(&mut self, cx: &mut MutableAppContext) {
if self.selected_match_index > 0 { self.active_editor.update(cx, |editor, cx| {
self.select(self.selected_match_index - 1, true, false, cx); editor.highlight_rows(None);
} if let Some(scroll_position) = self.prev_scroll_position {
editor.set_scroll_position(scroll_position, cx);
}
})
} }
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) { fn set_selected_index(&mut self, ix: usize, navigate: bool, cx: &mut ViewContext<Self>) {
if self.selected_match_index + 1 < self.matches.len() { self.selected_match_index = ix;
self.select(self.selected_match_index + 1, true, false, cx); if navigate && !self.matches.is_empty() {
}
}
fn select_first(&mut self, _: &SelectFirst, cx: &mut ViewContext<Self>) {
self.select(0, true, false, cx);
}
fn select_last(&mut self, _: &SelectLast, cx: &mut ViewContext<Self>) {
self.select(self.matches.len().saturating_sub(1), true, false, cx);
}
fn select(&mut self, index: usize, navigate: bool, center: bool, cx: &mut ViewContext<Self>) {
self.selected_match_index = index;
self.list_state.scroll_to(if center {
ScrollTarget::Center(index)
} else {
ScrollTarget::Show(index)
});
if navigate {
let selected_match = &self.matches[self.selected_match_index]; let selected_match = &self.matches[self.selected_match_index];
let outline_item = &self.outline.items[selected_match.candidate_id]; let outline_item = &self.outline.items[selected_match.candidate_id];
self.active_editor.update(cx, |active_editor, cx| { self.active_editor.update(cx, |active_editor, cx| {
@ -181,27 +124,6 @@ impl OutlineView {
cx.notify(); cx.notify();
} }
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.prev_scroll_position.take();
self.active_editor.update(cx, |active_editor, cx| {
if let Some(rows) = active_editor.highlighted_rows() {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let position = DisplayPoint::new(rows.start, 0).to_point(&snapshot);
active_editor.select_ranges([position..position], Some(Autoscroll::Center), cx);
}
});
cx.emit(Event::Dismissed);
}
fn restore_active_editor(&mut self, cx: &mut MutableAppContext) {
self.active_editor.update(cx, |editor, cx| {
editor.highlight_rows(None);
if let Some(scroll_position) = self.prev_scroll_position {
editor.set_scroll_position(scroll_position, cx);
}
})
}
fn on_event( fn on_event(
workspace: &mut Workspace, workspace: &mut Workspace,
_: ViewHandle<Self>, _: ViewHandle<Self>,
@ -212,24 +134,27 @@ impl OutlineView {
Event::Dismissed => workspace.dismiss_modal(cx), Event::Dismissed => workspace.dismiss_modal(cx),
} }
} }
}
fn on_query_editor_event( impl PickerDelegate for OutlineView {
&mut self, fn match_count(&self) -> usize {
_: ViewHandle<Editor>, self.matches.len()
event: &editor::Event,
cx: &mut ViewContext<Self>,
) {
match event {
editor::Event::Blurred => cx.emit(Event::Dismissed),
editor::Event::BufferEdited { .. } => self.update_matches(cx),
_ => {}
}
} }
fn update_matches(&mut self, cx: &mut ViewContext<Self>) { fn selected_index(&self) -> usize {
self.selected_match_index
}
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>) {
self.set_selected_index(ix, true, cx);
}
fn center_selection_after_match_updates(&self) -> bool {
true
}
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> Task<()> {
let selected_index; let selected_index;
let navigate_to_selected_index;
let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
if query.is_empty() { if query.is_empty() {
self.restore_active_editor(cx); self.restore_active_editor(cx);
self.matches = self self.matches = self
@ -271,7 +196,6 @@ impl OutlineView {
.max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance))) .max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
.map(|(ix, _, _)| ix) .map(|(ix, _, _)| ix)
.unwrap_or(0); .unwrap_or(0);
navigate_to_selected_index = false;
} else { } else {
self.matches = smol::block_on(self.outline.search(&query, cx.background().clone())); self.matches = smol::block_on(self.outline.search(&query, cx.background().clone()));
selected_index = self selected_index = self
@ -281,57 +205,33 @@ impl OutlineView {
.max_by_key(|(_, m)| OrderedFloat(m.score)) .max_by_key(|(_, m)| OrderedFloat(m.score))
.map(|(ix, _)| ix) .map(|(ix, _)| ix)
.unwrap_or(0); .unwrap_or(0);
navigate_to_selected_index = !self.matches.is_empty();
} }
self.select(selected_index, navigate_to_selected_index, true, cx); self.last_query = query;
self.set_selected_index(selected_index, false, cx);
Task::ready(())
} }
fn render_matches(&self, cx: &AppContext) -> ElementBox { fn confirm(&mut self, cx: &mut ViewContext<Self>) {
if self.matches.is_empty() { self.prev_scroll_position.take();
let settings = cx.global::<Settings>(); self.active_editor.update(cx, |active_editor, cx| {
return Container::new( if let Some(rows) = active_editor.highlighted_rows() {
Label::new( let snapshot = active_editor.snapshot(cx).display_snapshot;
"No matches".into(), let position = DisplayPoint::new(rows.start, 0).to_point(&snapshot);
settings.theme.selector.empty.label.clone(), active_editor.select_ranges([position..position], Some(Autoscroll::Center), cx);
) }
.boxed(), });
) cx.emit(Event::Dismissed);
.with_style(settings.theme.selector.empty.container)
.named("empty matches");
}
let handle = self.handle.clone();
let list = UniformList::new(
self.list_state.clone(),
self.matches.len(),
move |mut range, items, cx| {
let cx = cx.as_ref();
let view = handle.upgrade(cx).unwrap();
let view = view.read(cx);
let start = range.start;
range.end = cmp::min(range.end, view.matches.len());
items.extend(
view.matches[range]
.iter()
.enumerate()
.map(move |(ix, m)| view.render_match(m, start + ix, cx)),
);
},
);
Container::new(list.boxed())
.with_margin_top(6.0)
.named("matches")
} }
fn render_match( fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
&self, self.restore_active_editor(cx);
string_match: &StringMatch, cx.emit(Event::Dismissed);
index: usize, }
cx: &AppContext,
) -> ElementBox { fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox {
let settings = cx.global::<Settings>(); let settings = cx.global::<Settings>();
let style = if index == self.selected_match_index { let string_match = &self.matches[ix];
let style = if selected {
&settings.theme.selector.active_item &settings.theme.selector.active_item
} else { } else {
&settings.theme.selector.item &settings.theme.selector.item

View file

@ -1,9 +1,10 @@
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
elements::{ elements::{
ChildView, EventHandler, Flex, FlexItem, Label, ParentElement, ScrollTarget, UniformList, ChildView, EventHandler, Flex, Label, ParentElement, ScrollTarget, UniformList,
UniformListState, UniformListState,
}, },
geometry::vector::{vec2f, Vector2F},
keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task, keymap, AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, Task,
View, ViewContext, ViewHandle, WeakViewHandle, View, ViewContext, ViewHandle, WeakViewHandle,
}; };
@ -18,6 +19,7 @@ pub struct Picker<D: PickerDelegate> {
query_editor: ViewHandle<Editor>, query_editor: ViewHandle<Editor>,
list_state: UniformListState, list_state: UniformListState,
update_task: Option<Task<()>>, update_task: Option<Task<()>>,
max_size: Vector2F,
} }
pub trait PickerDelegate: View { pub trait PickerDelegate: View {
@ -28,6 +30,9 @@ pub trait PickerDelegate: View {
fn confirm(&mut self, cx: &mut ViewContext<Self>); fn confirm(&mut self, cx: &mut ViewContext<Self>);
fn dismiss(&mut self, cx: &mut ViewContext<Self>); fn dismiss(&mut self, cx: &mut ViewContext<Self>);
fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox; fn render_match(&self, ix: usize, selected: bool, cx: &AppContext) -> ElementBox;
fn center_selection_after_match_updates(&self) -> bool {
false
}
} }
impl<D: PickerDelegate> Entity for Picker<D> { impl<D: PickerDelegate> Entity for Picker<D> {
@ -41,6 +46,13 @@ impl<D: PickerDelegate> View for Picker<D> {
fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox { fn render(&mut self, cx: &mut RenderContext<Self>) -> gpui::ElementBox {
let settings = cx.global::<Settings>(); let settings = cx.global::<Settings>();
let delegate = self.delegate.clone();
let match_count = if let Some(delegate) = delegate.upgrade(cx.app) {
delegate.read(cx).match_count()
} else {
0
};
Flex::new(Axis::Vertical) Flex::new(Axis::Vertical)
.with_child( .with_child(
ChildView::new(&self.query_editor) ChildView::new(&self.query_editor)
@ -49,15 +61,44 @@ impl<D: PickerDelegate> View for Picker<D> {
.boxed(), .boxed(),
) )
.with_child( .with_child(
FlexItem::new(self.render_matches(cx)) if match_count == 0 {
.flex(1., false) Label::new(
.boxed(), "No matches".into(),
settings.theme.selector.empty.label.clone(),
)
.contained()
.with_style(settings.theme.selector.empty.container)
} else {
UniformList::new(
self.list_state.clone(),
match_count,
move |mut range, items, cx| {
let cx = cx.as_ref();
let delegate = delegate.upgrade(cx).unwrap();
let delegate = delegate.read(cx);
let selected_ix = delegate.selected_index();
range.end = cmp::min(range.end, delegate.match_count());
items.extend(range.map(move |ix| {
EventHandler::new(delegate.render_match(ix, ix == selected_ix, cx))
.on_mouse_down(move |cx| {
cx.dispatch_action(SelectIndex(ix));
true
})
.boxed()
}));
},
)
.contained()
.with_margin_top(6.0)
}
.flex(1., false)
.boxed(),
) )
.contained() .contained()
.with_style(settings.theme.selector.container) .with_style(settings.theme.selector.container)
.constrained() .constrained()
.with_max_width(500.0) .with_max_width(self.max_size.x())
.with_max_height(420.0) .with_max_height(self.max_size.y())
.aligned() .aligned()
.top() .top()
.named("picker") .named("picker")
@ -91,57 +132,20 @@ impl<D: PickerDelegate> Picker<D> {
}); });
cx.subscribe(&query_editor, Self::on_query_editor_event) cx.subscribe(&query_editor, Self::on_query_editor_event)
.detach(); .detach();
let mut this = Self { let this = Self {
query_editor, query_editor,
list_state: Default::default(), list_state: Default::default(),
update_task: None, update_task: None,
delegate, delegate,
max_size: vec2f(500., 420.),
}; };
this.update_matches(cx); cx.defer(|this, cx| this.update_matches(cx));
this this
} }
fn render_matches(&self, cx: &AppContext) -> ElementBox { pub fn with_max_size(mut self, width: f32, height: f32) -> Self {
let delegate = self.delegate.clone(); self.max_size = vec2f(width, height);
let match_count = if let Some(delegate) = delegate.upgrade(cx) { self
delegate.read(cx).match_count()
} else {
0
};
if match_count == 0 {
let settings = cx.global::<Settings>();
return Label::new(
"No matches".into(),
settings.theme.selector.empty.label.clone(),
)
.contained()
.with_style(settings.theme.selector.empty.container)
.named("empty matches");
}
UniformList::new(
self.list_state.clone(),
match_count,
move |mut range, items, cx| {
let cx = cx.as_ref();
let delegate = delegate.upgrade(cx).unwrap();
let delegate = delegate.read(cx);
let selected_ix = delegate.selected_index();
range.end = cmp::min(range.end, delegate.match_count());
items.extend(range.map(move |ix| {
EventHandler::new(delegate.render_match(ix, ix == selected_ix, cx))
.on_mouse_down(move |cx| {
cx.dispatch_action(SelectIndex(ix));
true
})
.boxed()
}));
},
)
.contained()
.with_margin_top(6.0)
.named("matches")
} }
fn on_query_editor_event( fn on_query_editor_event(
@ -172,8 +176,14 @@ impl<D: PickerDelegate> Picker<D> {
update.await; update.await;
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
if let Some(delegate) = this.delegate.upgrade(cx) { if let Some(delegate) = this.delegate.upgrade(cx) {
let index = delegate.read(cx).selected_index(); let delegate = delegate.read(cx);
this.list_state.scroll_to(ScrollTarget::Show(index)); let index = delegate.selected_index();
let target = if delegate.center_selection_after_match_updates() {
ScrollTarget::Center(index)
} else {
ScrollTarget::Show(index)
};
this.list_state.scroll_to(target);
cx.notify(); cx.notify();
this.update_task.take(); this.update_task.take();
} }

View file

@ -57,18 +57,15 @@ impl View for ProjectSymbolsView {
impl ProjectSymbolsView { impl ProjectSymbolsView {
fn new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self { fn new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
let handle = cx.weak_handle(); let handle = cx.weak_handle();
let picker = cx.add_view(|cx| Picker::new(handle, cx)); Self {
let mut this = Self {
picker,
project, project,
picker: cx.add_view(|cx| Picker::new(handle, cx)),
selected_match_index: 0, selected_match_index: 0,
symbols: Default::default(), symbols: Default::default(),
match_candidates: Default::default(), match_candidates: Default::default(),
matches: Default::default(), matches: Default::default(),
show_worktree_root_name: false, show_worktree_root_name: false,
}; }
this.update_matches(String::new(), cx).detach();
this
} }
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) { fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {