Implement navigation via outline modal

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Max Brunsfeld 2022-01-13 11:48:44 -08:00
parent 373fe6fadf
commit f2cef0b795
2 changed files with 114 additions and 27 deletions

View file

@ -26,7 +26,7 @@ pub struct GoToLine {
line_editor: ViewHandle<Editor>, line_editor: ViewHandle<Editor>,
active_editor: ViewHandle<Editor>, active_editor: ViewHandle<Editor>,
restore_state: Option<RestoreState>, restore_state: Option<RestoreState>,
line_selection: Option<Selection<usize>>, line_selection_id: Option<usize>,
cursor_point: Point, cursor_point: Point,
max_point: Point, max_point: Point,
} }
@ -84,7 +84,7 @@ impl GoToLine {
line_editor, line_editor,
active_editor, active_editor,
restore_state, restore_state,
line_selection: None, line_selection_id: None,
cursor_point, cursor_point,
max_point, max_point,
} }
@ -139,14 +139,18 @@ impl GoToLine {
column.map(|column| column.saturating_sub(1)).unwrap_or(0), column.map(|column| column.saturating_sub(1)).unwrap_or(0),
) )
}) { }) {
self.line_selection = self.active_editor.update(cx, |active_editor, cx| { self.line_selection_id = self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot; let snapshot = active_editor.snapshot(cx).display_snapshot;
let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left); let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
let display_point = point.to_display_point(&snapshot); let display_point = point.to_display_point(&snapshot);
let row = display_point.row(); let row = display_point.row();
active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx); active_editor.select_ranges([point..point], Some(Autoscroll::Center), cx);
active_editor.set_highlighted_rows(Some(row..row + 1)); active_editor.set_highlighted_rows(Some(row..row + 1));
Some(active_editor.newest_selection(&snapshot.buffer_snapshot)) Some(
active_editor
.newest_selection::<usize>(&snapshot.buffer_snapshot)
.id,
)
}); });
cx.notify(); cx.notify();
} }
@ -160,14 +164,14 @@ impl Entity for GoToLine {
type Event = Event; type Event = Event;
fn release(&mut self, cx: &mut MutableAppContext) { fn release(&mut self, cx: &mut MutableAppContext) {
let line_selection = self.line_selection.take(); let line_selection_id = self.line_selection_id.take();
let restore_state = self.restore_state.take(); let restore_state = self.restore_state.take();
self.active_editor.update(cx, |editor, cx| { self.active_editor.update(cx, |editor, cx| {
editor.set_highlighted_rows(None); editor.set_highlighted_rows(None);
if let Some((line_selection, restore_state)) = line_selection.zip(restore_state) { if let Some((line_selection_id, restore_state)) = line_selection_id.zip(restore_state) {
let newest_selection = let newest_selection =
editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx)); editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx));
if line_selection.id == newest_selection.id { if line_selection_id == newest_selection.id {
editor.set_scroll_position(restore_state.scroll_position, cx); editor.set_scroll_position(restore_state.scroll_position, cx);
editor.update_selections(restore_state.selections, None, cx); editor.update_selections(restore_state.selections, None, cx);
} }

View file

@ -1,8 +1,12 @@
use editor::{Anchor, AnchorRangeExt, Editor, EditorSettings}; use editor::{
display_map::ToDisplayPoint, Anchor, AnchorRangeExt, Autoscroll, Editor, EditorSettings,
ToPoint,
};
use fuzzy::StringMatch; use fuzzy::StringMatch;
use gpui::{ use gpui::{
action, action,
elements::*, elements::*,
geometry::vector::Vector2F,
keymap::{ keymap::{
self, self,
menu::{SelectNext, SelectPrev}, menu::{SelectNext, SelectPrev},
@ -11,7 +15,7 @@ use gpui::{
AppContext, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle, AppContext, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
WeakViewHandle, WeakViewHandle,
}; };
use language::Outline; use language::{Outline, Selection};
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use postage::watch; use postage::watch;
use std::{ use std::{
@ -37,17 +41,32 @@ pub fn init(cx: &mut MutableAppContext) {
struct OutlineView { struct OutlineView {
handle: WeakViewHandle<Self>, handle: WeakViewHandle<Self>,
editor: ViewHandle<Editor>, active_editor: ViewHandle<Editor>,
outline: Outline<Anchor>, outline: Outline<Anchor>,
selected_match_index: usize, selected_match_index: usize,
restore_state: Option<RestoreState>,
symbol_selection_id: Option<usize>,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
query_editor: ViewHandle<Editor>, query_editor: ViewHandle<Editor>,
list_state: UniformListState, list_state: UniformListState,
settings: watch::Receiver<Settings>, settings: watch::Receiver<Settings>,
} }
struct RestoreState {
scroll_position: Vector2F,
selections: Vec<Selection<usize>>,
}
pub enum Event {
Dismissed,
}
impl Entity for OutlineView { impl Entity for OutlineView {
type Event = (); type Event = Event;
fn release(&mut self, cx: &mut MutableAppContext) {
self.restore_active_editor(cx);
}
} }
impl View for OutlineView { impl View for OutlineView {
@ -79,8 +98,8 @@ impl View for OutlineView {
.with_style(settings.theme.selector.container) .with_style(settings.theme.selector.container)
.boxed(), .boxed(),
) )
.with_max_width(500.0) .with_max_width(800.0)
.with_max_height(420.0) .with_max_height(1200.0)
.boxed(), .boxed(),
) )
.top() .top()
@ -117,11 +136,21 @@ impl OutlineView {
}); });
cx.subscribe(&query_editor, Self::on_query_editor_event) cx.subscribe(&query_editor, Self::on_query_editor_event)
.detach(); .detach();
let restore_state = editor.update(cx, |editor, cx| {
Some(RestoreState {
scroll_position: editor.scroll_position(cx),
selections: editor.local_selections::<usize>(cx),
})
});
let mut this = Self { let mut this = Self {
handle: cx.weak_handle(), handle: cx.weak_handle(),
editor, active_editor: editor,
matches: Default::default(), matches: Default::default(),
selected_match_index: 0, selected_match_index: 0,
restore_state,
symbol_selection_id: None,
outline, outline,
query_editor, query_editor,
list_state: Default::default(), list_state: Default::default(),
@ -141,28 +170,79 @@ impl OutlineView {
let buffer = editor.read(cx).buffer().read(cx).read(cx).outline(); let buffer = editor.read(cx).buffer().read(cx).read(cx).outline();
if let Some(outline) = buffer { if let Some(outline) = buffer {
workspace.toggle_modal(cx, |cx, workspace| { workspace.toggle_modal(cx, |cx, workspace| {
cx.add_view(|cx| OutlineView::new(outline, editor, workspace.settings(), cx)) let view =
cx.add_view(|cx| OutlineView::new(outline, editor, workspace.settings(), cx));
cx.subscribe(&view, Self::on_event).detach();
view
}) })
} }
} }
fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) { fn select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
if self.selected_match_index > 0 { if self.selected_match_index > 0 {
self.selected_match_index -= 1; self.select(self.selected_match_index - 1, true, cx);
self.list_state.scroll_to(self.selected_match_index);
cx.notify();
} }
} }
fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) { fn select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
if self.selected_match_index + 1 < self.matches.len() { if self.selected_match_index + 1 < self.matches.len() {
self.selected_match_index += 1; self.select(self.selected_match_index + 1, true, cx);
self.list_state.scroll_to(self.selected_match_index); }
}
fn select(&mut self, index: usize, navigate: bool, cx: &mut ViewContext<Self>) {
self.selected_match_index = index;
self.list_state.scroll_to(self.selected_match_index);
if navigate {
let selected_match = &self.matches[self.selected_match_index];
let outline_item = &self.outline.items[selected_match.candidate_index];
self.symbol_selection_id = self.active_editor.update(cx, |active_editor, cx| {
let snapshot = active_editor.snapshot(cx).display_snapshot;
let buffer_snapshot = &snapshot.buffer_snapshot;
let start = outline_item.range.start.to_point(&buffer_snapshot);
let end = outline_item.range.end.to_point(&buffer_snapshot);
let display_rows = start.to_display_point(&snapshot).row()
..end.to_display_point(&snapshot).row() + 1;
active_editor.select_ranges([start..start], Some(Autoscroll::Center), cx);
active_editor.set_highlighted_rows(Some(display_rows));
Some(active_editor.newest_selection::<usize>(&buffer_snapshot).id)
});
cx.notify(); cx.notify();
} }
} }
fn confirm(&mut self, _: &Confirm, _: &mut ViewContext<Self>) {} fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
self.restore_state.take();
cx.emit(Event::Dismissed);
}
fn restore_active_editor(&mut self, cx: &mut MutableAppContext) {
let symbol_selection_id = self.symbol_selection_id.take();
self.active_editor.update(cx, |editor, cx| {
editor.set_highlighted_rows(None);
if let Some((symbol_selection_id, restore_state)) =
symbol_selection_id.zip(self.restore_state.as_ref())
{
let newest_selection =
editor.newest_selection::<usize>(&editor.buffer().read(cx).read(cx));
if symbol_selection_id == newest_selection.id {
editor.set_scroll_position(restore_state.scroll_position, cx);
editor.update_selections(restore_state.selections.clone(), None, cx);
}
}
})
}
fn on_event(
workspace: &mut Workspace,
_: ViewHandle<Self>,
event: &Event,
cx: &mut ViewContext<Workspace>,
) {
match event {
Event::Dismissed => workspace.dismiss_modal(cx),
}
}
fn on_query_editor_event( fn on_query_editor_event(
&mut self, &mut self,
@ -177,8 +257,11 @@ impl OutlineView {
} }
fn update_matches(&mut self, cx: &mut ViewContext<Self>) { fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
let selected_index;
let navigate_to_selected_index;
let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx)); 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.matches = self self.matches = self
.outline .outline
.items .items
@ -192,10 +275,10 @@ impl OutlineView {
}) })
.collect(); .collect();
let editor = self.editor.read(cx); let editor = self.active_editor.read(cx);
let buffer = editor.buffer().read(cx).read(cx); let buffer = editor.buffer().read(cx).read(cx);
let cursor_offset = editor.newest_selection::<usize>(&buffer).head(); let cursor_offset = editor.newest_selection::<usize>(&buffer).head();
self.selected_match_index = self selected_index = self
.outline .outline
.items .items
.iter() .iter()
@ -216,19 +299,19 @@ impl OutlineView {
.max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance))) .max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
.unwrap() .unwrap()
.0; .0;
navigate_to_selected_index = false;
} else { } else {
self.matches = self.outline.search(&query, cx); self.matches = self.outline.search(&query, cx);
self.selected_match_index = self selected_index = self
.matches .matches
.iter() .iter()
.enumerate() .enumerate()
.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 = true;
} }
self.select(selected_index, navigate_to_selected_index, cx);
self.list_state.scroll_to(self.selected_match_index);
cx.notify();
} }
fn render_matches(&self) -> ElementBox { fn render_matches(&self) -> ElementBox {