Implement selecting prev and next in outline view

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-01-13 16:44:06 +01:00
parent d74658fdb5
commit aee3bb98f2
3 changed files with 67 additions and 21 deletions

1
Cargo.lock generated
View file

@ -3130,6 +3130,7 @@ dependencies = [
"fuzzy", "fuzzy",
"gpui", "gpui",
"language", "language",
"ordered-float",
"postage", "postage",
"text", "text",
"workspace", "workspace",

View file

@ -13,4 +13,5 @@ gpui = { path = "../gpui" }
language = { path = "../language" } language = { path = "../language" }
text = { path = "../text" } text = { path = "../text" }
workspace = { path = "../workspace" } workspace = { path = "../workspace" }
ordered-float = "2.1.1"
postage = { version = "0.4", features = ["futures-traits"] } postage = { version = "0.4", features = ["futures-traits"] }

View file

@ -1,10 +1,18 @@
use editor::{Editor, EditorSettings}; use editor::{Editor, EditorSettings};
use fuzzy::StringMatch; use fuzzy::StringMatch;
use gpui::{ use gpui::{
action, elements::*, keymap::Binding, Axis, Entity, MutableAppContext, RenderContext, View, action,
ViewContext, ViewHandle, WeakViewHandle, elements::*,
keymap::{
self,
menu::{SelectNext, SelectPrev},
Binding,
},
AppContext, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
WeakViewHandle,
}; };
use language::Outline; use language::Outline;
use ordered_float::OrderedFloat;
use postage::watch; use postage::watch;
use std::{cmp, sync::Arc}; use std::{cmp, sync::Arc};
use workspace::{Settings, Workspace}; use workspace::{Settings, Workspace};
@ -20,11 +28,14 @@ pub fn init(cx: &mut MutableAppContext) {
]); ]);
cx.add_action(OutlineView::toggle); cx.add_action(OutlineView::toggle);
cx.add_action(OutlineView::confirm); cx.add_action(OutlineView::confirm);
cx.add_action(OutlineView::select_prev);
cx.add_action(OutlineView::select_next);
} }
struct OutlineView { struct OutlineView {
handle: WeakViewHandle<Self>, handle: WeakViewHandle<Self>,
outline: Outline, outline: Outline,
selected_match_index: usize,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
query_editor: ViewHandle<Editor>, query_editor: ViewHandle<Editor>,
list_state: UniformListState, list_state: UniformListState,
@ -40,6 +51,12 @@ impl View for OutlineView {
"OutlineView" "OutlineView"
} }
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
let mut cx = Self::default_keymap_context();
cx.set.insert("menu".into());
cx
}
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox { fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
let settings = self.settings.borrow(); let settings = self.settings.borrow();
@ -98,6 +115,7 @@ impl OutlineView {
let mut this = Self { let mut this = Self {
handle: cx.weak_handle(), handle: cx.weak_handle(),
matches: Default::default(), matches: Default::default(),
selected_match_index: 0,
outline, outline,
query_editor, query_editor,
list_state: Default::default(), list_state: Default::default(),
@ -122,7 +140,23 @@ impl OutlineView {
} }
} }
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {} fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
if self.selected_match_index > 0 {
self.selected_match_index -= 1;
self.list_state.scroll_to(self.selected_match_index);
cx.notify();
}
}
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
if self.selected_match_index + 1 < self.matches.len() {
self.selected_match_index += 1;
self.list_state.scroll_to(self.selected_match_index);
cx.notify();
}
}
fn confirm(&mut self, _: &Confirm, _: &mut ViewContext<Self>) {}
fn on_query_editor_event( fn on_query_editor_event(
&mut self, &mut self,
@ -151,9 +185,19 @@ impl OutlineView {
string: Default::default(), string: Default::default(),
}) })
.collect(); .collect();
self.selected_match_index = 0;
} else { } else {
self.matches = self.outline.search(&query, cx); self.matches = self.outline.search(&query, cx);
self.selected_match_index = self
.matches
.iter()
.enumerate()
.max_by_key(|(_, m)| OrderedFloat(m.score))
.map(|(ix, _)| ix)
.unwrap_or(0);
} }
self.list_state.scroll_to(self.selected_match_index);
cx.notify(); cx.notify();
} }
@ -172,8 +216,7 @@ impl OutlineView {
} }
let handle = self.handle.clone(); let handle = self.handle.clone();
let list = let list = UniformList::new(
UniformList::new(
self.list_state.clone(), self.list_state.clone(),
self.matches.len(), self.matches.len(),
move |mut range, items, cx| { move |mut range, items, cx| {
@ -182,9 +225,12 @@ impl OutlineView {
let view = view.read(cx); let view = view.read(cx);
let start = range.start; let start = range.start;
range.end = cmp::min(range.end, view.matches.len()); range.end = cmp::min(range.end, view.matches.len());
items.extend(view.matches[range].iter().enumerate().map( items.extend(
move |(i, outline_match)| view.render_match(outline_match, start + i), view.matches[range]
)); .iter()
.enumerate()
.map(move |(ix, m)| view.render_match(m, start + ix)),
);
}, },
); );
@ -194,10 +240,8 @@ impl OutlineView {
} }
fn render_match(&self, string_match: &StringMatch, index: usize) -> ElementBox { fn render_match(&self, string_match: &StringMatch, index: usize) -> ElementBox {
// TODO: maintain selected index.
let selected_index = 0;
let settings = self.settings.borrow(); let settings = self.settings.borrow();
let style = if index == selected_index { let style = if index == self.selected_match_index {
&settings.theme.selector.active_item &settings.theme.selector.active_item
} else { } else {
&settings.theme.selector.item &settings.theme.selector.item