2022-01-13 17:43:49 +00:00
|
|
|
use editor::{Anchor, AnchorRangeExt, Editor, EditorSettings};
|
2022-01-13 14:10:50 +00:00
|
|
|
use fuzzy::StringMatch;
|
2022-01-13 02:17:19 +00:00
|
|
|
use gpui::{
|
2022-01-13 15:44:06 +00:00
|
|
|
action,
|
|
|
|
elements::*,
|
|
|
|
keymap::{
|
|
|
|
self,
|
|
|
|
menu::{SelectNext, SelectPrev},
|
|
|
|
Binding,
|
|
|
|
},
|
|
|
|
AppContext, Axis, Entity, MutableAppContext, RenderContext, View, ViewContext, ViewHandle,
|
|
|
|
WeakViewHandle,
|
2022-01-13 02:17:19 +00:00
|
|
|
};
|
2022-01-13 14:10:50 +00:00
|
|
|
use language::Outline;
|
2022-01-13 15:44:06 +00:00
|
|
|
use ordered_float::OrderedFloat;
|
2022-01-13 02:17:19 +00:00
|
|
|
use postage::watch;
|
2022-01-13 17:43:49 +00:00
|
|
|
use std::{
|
|
|
|
cmp::{self, Reverse},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2022-01-13 02:17:19 +00:00
|
|
|
use workspace::{Settings, Workspace};
|
|
|
|
|
|
|
|
action!(Toggle);
|
|
|
|
action!(Confirm);
|
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.add_bindings([
|
|
|
|
Binding::new("cmd-shift-O", Toggle, Some("Editor")),
|
2022-01-13 11:01:11 +00:00
|
|
|
Binding::new("escape", Toggle, Some("OutlineView")),
|
|
|
|
Binding::new("enter", Confirm, Some("OutlineView")),
|
2022-01-13 02:17:19 +00:00
|
|
|
]);
|
|
|
|
cx.add_action(OutlineView::toggle);
|
|
|
|
cx.add_action(OutlineView::confirm);
|
2022-01-13 15:44:06 +00:00
|
|
|
cx.add_action(OutlineView::select_prev);
|
|
|
|
cx.add_action(OutlineView::select_next);
|
2022-01-13 02:17:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 11:01:11 +00:00
|
|
|
struct OutlineView {
|
|
|
|
handle: WeakViewHandle<Self>,
|
2022-01-13 17:43:49 +00:00
|
|
|
editor: ViewHandle<Editor>,
|
2022-01-13 17:24:00 +00:00
|
|
|
outline: Outline<Anchor>,
|
2022-01-13 15:44:06 +00:00
|
|
|
selected_match_index: usize,
|
2022-01-13 14:10:50 +00:00
|
|
|
matches: Vec<StringMatch>,
|
2022-01-13 11:01:11 +00:00
|
|
|
query_editor: ViewHandle<Editor>,
|
|
|
|
list_state: UniformListState,
|
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
}
|
2022-01-13 02:17:19 +00:00
|
|
|
|
|
|
|
impl Entity for OutlineView {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for OutlineView {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"OutlineView"
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:44:06 +00:00
|
|
|
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
|
|
|
|
let mut cx = Self::default_keymap_context();
|
|
|
|
cx.set.insert("menu".into());
|
|
|
|
cx
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:10:50 +00:00
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
2022-01-13 11:01:11 +00:00
|
|
|
let settings = self.settings.borrow();
|
|
|
|
|
|
|
|
Align::new(
|
|
|
|
ConstrainedBox::new(
|
|
|
|
Container::new(
|
|
|
|
Flex::new(Axis::Vertical)
|
|
|
|
.with_child(
|
|
|
|
Container::new(ChildView::new(self.query_editor.id()).boxed())
|
|
|
|
.with_style(settings.theme.selector.input_editor.container)
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_child(Flexible::new(1.0, false, self.render_matches()).boxed())
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_style(settings.theme.selector.container)
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_max_width(500.0)
|
|
|
|
.with_max_height(420.0)
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.top()
|
|
|
|
.named("outline view")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
cx.focus(&self.query_editor);
|
2022-01-13 02:17:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OutlineView {
|
2022-01-13 11:01:11 +00:00
|
|
|
fn new(
|
2022-01-13 17:24:00 +00:00
|
|
|
outline: Outline<Anchor>,
|
2022-01-13 17:43:49 +00:00
|
|
|
editor: ViewHandle<Editor>,
|
2022-01-13 11:01:11 +00:00
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Self {
|
|
|
|
let query_editor = cx.add_view(|cx| {
|
|
|
|
Editor::single_line(
|
|
|
|
{
|
|
|
|
let settings = settings.clone();
|
|
|
|
Arc::new(move |_| {
|
|
|
|
let settings = settings.borrow();
|
|
|
|
EditorSettings {
|
|
|
|
style: settings.theme.selector.input_editor.as_editor(),
|
|
|
|
tab_size: settings.tab_size,
|
|
|
|
soft_wrap: editor::SoftWrap::None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
cx.subscribe(&query_editor, Self::on_query_editor_event)
|
|
|
|
.detach();
|
2022-01-13 14:10:50 +00:00
|
|
|
let mut this = Self {
|
2022-01-13 11:01:11 +00:00
|
|
|
handle: cx.weak_handle(),
|
2022-01-13 17:43:49 +00:00
|
|
|
editor,
|
2022-01-13 14:10:50 +00:00
|
|
|
matches: Default::default(),
|
2022-01-13 15:44:06 +00:00
|
|
|
selected_match_index: 0,
|
2022-01-13 11:01:11 +00:00
|
|
|
outline,
|
|
|
|
query_editor,
|
|
|
|
list_state: Default::default(),
|
|
|
|
settings,
|
2022-01-13 14:10:50 +00:00
|
|
|
};
|
|
|
|
this.update_matches(cx);
|
|
|
|
this
|
2022-01-13 11:01:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 02:17:19 +00:00
|
|
|
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
|
|
|
|
let editor = workspace
|
|
|
|
.active_item(cx)
|
|
|
|
.unwrap()
|
|
|
|
.to_any()
|
|
|
|
.downcast::<Editor>()
|
|
|
|
.unwrap();
|
2022-01-13 11:01:11 +00:00
|
|
|
let buffer = editor.read(cx).buffer().read(cx).read(cx).outline();
|
|
|
|
if let Some(outline) = buffer {
|
|
|
|
workspace.toggle_modal(cx, |cx, workspace| {
|
2022-01-13 17:43:49 +00:00
|
|
|
cx.add_view(|cx| OutlineView::new(outline, editor, workspace.settings(), cx))
|
2022-01-13 11:01:11 +00:00
|
|
|
})
|
|
|
|
}
|
2022-01-13 02:17:19 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 15:44:06 +00:00
|
|
|
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>) {}
|
2022-01-13 11:01:11 +00:00
|
|
|
|
|
|
|
fn on_query_editor_event(
|
|
|
|
&mut self,
|
|
|
|
_: ViewHandle<Editor>,
|
|
|
|
event: &editor::Event,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
match event {
|
2022-01-13 14:10:50 +00:00
|
|
|
editor::Event::Edited => self.update_matches(cx),
|
2022-01-13 11:01:11 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:10:50 +00:00
|
|
|
fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
|
|
|
|
if query.is_empty() {
|
|
|
|
self.matches = self
|
|
|
|
.outline
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, _)| StringMatch {
|
|
|
|
candidate_index: index,
|
|
|
|
score: Default::default(),
|
|
|
|
positions: Default::default(),
|
|
|
|
string: Default::default(),
|
|
|
|
})
|
|
|
|
.collect();
|
2022-01-13 17:43:49 +00:00
|
|
|
|
|
|
|
let editor = self.editor.read(cx);
|
|
|
|
let buffer = editor.buffer().read(cx).read(cx);
|
|
|
|
let cursor_offset = editor.newest_selection::<usize>(&buffer).head();
|
|
|
|
self.selected_match_index = self
|
|
|
|
.outline
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(ix, item)| {
|
|
|
|
let range = item.range.to_offset(&buffer);
|
|
|
|
let distance_to_closest_endpoint = cmp::min(
|
|
|
|
(range.start as isize - cursor_offset as isize).abs() as usize,
|
|
|
|
(range.end as isize - cursor_offset as isize).abs() as usize,
|
|
|
|
);
|
|
|
|
let depth = if range.contains(&cursor_offset) {
|
|
|
|
Some(item.depth)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
(ix, depth, distance_to_closest_endpoint)
|
|
|
|
})
|
|
|
|
.max_by_key(|(_, depth, distance)| (*depth, Reverse(*distance)))
|
|
|
|
.unwrap()
|
|
|
|
.0;
|
2022-01-13 14:10:50 +00:00
|
|
|
} else {
|
|
|
|
self.matches = self.outline.search(&query, cx);
|
2022-01-13 15:44:06 +00:00
|
|
|
self.selected_match_index = self
|
|
|
|
.matches
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.max_by_key(|(_, m)| OrderedFloat(m.score))
|
|
|
|
.map(|(ix, _)| ix)
|
|
|
|
.unwrap_or(0);
|
2022-01-13 14:10:50 +00:00
|
|
|
}
|
2022-01-13 15:44:06 +00:00
|
|
|
|
|
|
|
self.list_state.scroll_to(self.selected_match_index);
|
2022-01-13 14:10:50 +00:00
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
2022-01-13 11:01:11 +00:00
|
|
|
fn render_matches(&self) -> ElementBox {
|
|
|
|
if self.matches.is_empty() {
|
|
|
|
let settings = self.settings.borrow();
|
|
|
|
return Container::new(
|
|
|
|
Label::new(
|
|
|
|
"No matches".into(),
|
|
|
|
settings.theme.selector.empty.label.clone(),
|
|
|
|
)
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_style(settings.theme.selector.empty.container)
|
|
|
|
.named("empty matches");
|
|
|
|
}
|
|
|
|
|
|
|
|
let handle = self.handle.clone();
|
2022-01-13 15:44:06 +00:00
|
|
|
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)),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
2022-01-13 11:01:11 +00:00
|
|
|
|
|
|
|
Container::new(list.boxed())
|
|
|
|
.with_margin_top(6.0)
|
|
|
|
.named("matches")
|
|
|
|
}
|
|
|
|
|
2022-01-13 14:10:50 +00:00
|
|
|
fn render_match(&self, string_match: &StringMatch, index: usize) -> ElementBox {
|
2022-01-13 11:01:11 +00:00
|
|
|
let settings = self.settings.borrow();
|
2022-01-13 15:44:06 +00:00
|
|
|
let style = if index == self.selected_match_index {
|
2022-01-13 11:01:11 +00:00
|
|
|
&settings.theme.selector.active_item
|
|
|
|
} else {
|
|
|
|
&settings.theme.selector.item
|
|
|
|
};
|
2022-01-13 14:10:50 +00:00
|
|
|
let outline_match = &self.outline.items[string_match.candidate_index];
|
2022-01-13 11:01:11 +00:00
|
|
|
|
|
|
|
Label::new(outline_match.text.clone(), style.label.clone())
|
2022-01-13 14:10:50 +00:00
|
|
|
.with_highlights(string_match.positions.clone())
|
2022-01-13 11:01:11 +00:00
|
|
|
.contained()
|
|
|
|
.with_padding_left(20. * outline_match.depth as f32)
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
|
|
|
.boxed()
|
|
|
|
}
|
2022-01-13 02:17:19 +00:00
|
|
|
}
|