2022-02-22 09:01:08 +00:00
|
|
|
use editor::{
|
2022-03-16 20:34:04 +00:00
|
|
|
combine_syntax_and_fuzzy_match_highlights, styled_runs_for_code_label, Autoscroll, Bias, Editor,
|
2022-02-22 09:01:08 +00:00
|
|
|
};
|
2022-02-22 09:54:25 +00:00
|
|
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
2022-02-22 07:42:12 +00:00
|
|
|
use gpui::{
|
2022-04-15 00:35:32 +00:00
|
|
|
actions, elements::*, AppContext, Entity, ModelHandle, MutableAppContext, RenderContext, Task,
|
|
|
|
View, ViewContext, ViewHandle,
|
2022-02-22 07:42:12 +00:00
|
|
|
};
|
2022-02-22 09:54:25 +00:00
|
|
|
use ordered_float::OrderedFloat;
|
2022-04-15 00:35:32 +00:00
|
|
|
use picker::{Picker, PickerDelegate};
|
2022-02-22 13:50:06 +00:00
|
|
|
use project::{Project, Symbol};
|
2022-04-06 00:10:17 +00:00
|
|
|
use settings::Settings;
|
2022-04-15 00:35:32 +00:00
|
|
|
use std::{borrow::Cow, cmp::Reverse};
|
2022-02-22 09:54:25 +00:00
|
|
|
use util::ResultExt;
|
2022-04-15 00:35:32 +00:00
|
|
|
use workspace::Workspace;
|
2022-02-22 07:42:12 +00:00
|
|
|
|
2022-04-07 23:20:49 +00:00
|
|
|
actions!(project_symbols, [Toggle]);
|
2022-02-22 07:42:12 +00:00
|
|
|
|
|
|
|
pub fn init(cx: &mut MutableAppContext) {
|
|
|
|
cx.add_action(ProjectSymbolsView::toggle);
|
2022-04-15 00:35:32 +00:00
|
|
|
Picker::<ProjectSymbolsView>::init(cx);
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ProjectSymbolsView {
|
2022-04-15 00:35:32 +00:00
|
|
|
picker: ViewHandle<Picker<Self>>,
|
2022-02-22 07:42:12 +00:00
|
|
|
project: ModelHandle<Project>,
|
|
|
|
selected_match_index: usize,
|
2022-02-22 13:50:06 +00:00
|
|
|
symbols: Vec<Symbol>,
|
2022-02-22 09:54:25 +00:00
|
|
|
match_candidates: Vec<StringMatchCandidate>,
|
2022-04-15 00:35:32 +00:00
|
|
|
show_worktree_root_name: bool,
|
2022-04-20 23:03:11 +00:00
|
|
|
pending_update: Task<()>,
|
2022-02-22 07:42:12 +00:00
|
|
|
matches: Vec<StringMatch>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
Dismissed,
|
2022-02-22 15:26:01 +00:00
|
|
|
Selected(Symbol),
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ProjectSymbolsView {
|
|
|
|
type Event = Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for ProjectSymbolsView {
|
|
|
|
fn ui_name() -> &'static str {
|
|
|
|
"ProjectSymbolsView"
|
|
|
|
}
|
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn render(&mut self, _: &mut RenderContext<Self>) -> ElementBox {
|
|
|
|
ChildView::new(self.picker.clone()).boxed()
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
2022-04-15 00:35:32 +00:00
|
|
|
cx.focus(&self.picker);
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProjectSymbolsView {
|
2022-03-11 22:59:05 +00:00
|
|
|
fn new(project: ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
|
2022-04-15 00:35:32 +00:00
|
|
|
let handle = cx.weak_handle();
|
2022-04-15 21:28:01 +00:00
|
|
|
Self {
|
2022-02-22 07:42:12 +00:00
|
|
|
project,
|
2022-04-15 21:28:01 +00:00
|
|
|
picker: cx.add_view(|cx| Picker::new(handle, cx)),
|
2022-02-22 07:42:12 +00:00
|
|
|
selected_match_index: 0,
|
|
|
|
symbols: Default::default(),
|
2022-02-22 09:54:25 +00:00
|
|
|
match_candidates: Default::default(),
|
2022-02-22 07:42:12 +00:00
|
|
|
matches: Default::default(),
|
2022-04-15 00:35:32 +00:00
|
|
|
show_worktree_root_name: false,
|
2022-04-20 23:03:11 +00:00
|
|
|
pending_update: Task::ready(()),
|
2022-04-15 21:28:01 +00:00
|
|
|
}
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
|
|
|
|
workspace.toggle_modal(cx, |cx, workspace| {
|
|
|
|
let project = workspace.project().clone();
|
2022-03-11 22:59:05 +00:00
|
|
|
let symbols = cx.add_view(|cx| Self::new(project, cx));
|
2022-02-22 07:42:12 +00:00
|
|
|
cx.subscribe(&symbols, Self::on_event).detach();
|
|
|
|
symbols
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn filter(&mut self, query: &str, cx: &mut ViewContext<Self>) {
|
2022-02-22 09:54:25 +00:00
|
|
|
let mut matches = if query.is_empty() {
|
|
|
|
self.match_candidates
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(candidate_id, candidate)| StringMatch {
|
|
|
|
candidate_id,
|
|
|
|
score: Default::default(),
|
|
|
|
positions: Default::default(),
|
|
|
|
string: candidate.string.clone(),
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
2022-04-20 22:07:05 +00:00
|
|
|
cx.background_executor().block(fuzzy::match_strings(
|
2022-02-22 09:54:25 +00:00
|
|
|
&self.match_candidates,
|
2022-04-15 00:35:32 +00:00
|
|
|
query,
|
2022-02-22 09:54:25 +00:00
|
|
|
false,
|
|
|
|
100,
|
|
|
|
&Default::default(),
|
|
|
|
cx.background().clone(),
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
matches.sort_unstable_by_key(|mat| {
|
|
|
|
let label = &self.symbols[mat.candidate_id].label;
|
|
|
|
(
|
|
|
|
Reverse(OrderedFloat(mat.score)),
|
|
|
|
&label.text[label.filter_range.clone()],
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
for mat in &mut matches {
|
|
|
|
let filter_start = self.symbols[mat.candidate_id].label.filter_range.start;
|
|
|
|
for position in &mut mat.positions {
|
|
|
|
*position += filter_start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.matches = matches;
|
2022-04-15 00:35:32 +00:00
|
|
|
self.set_selected_index(0, cx);
|
2022-02-22 09:54:25 +00:00
|
|
|
cx.notify();
|
2022-02-22 09:01:08 +00:00
|
|
|
}
|
2022-02-22 07:42:12 +00:00
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn on_event(
|
|
|
|
workspace: &mut Workspace,
|
|
|
|
_: ViewHandle<Self>,
|
|
|
|
event: &Event,
|
|
|
|
cx: &mut ViewContext<Workspace>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
Event::Dismissed => workspace.dismiss_modal(cx),
|
|
|
|
Event::Selected(symbol) => {
|
|
|
|
let buffer = workspace
|
|
|
|
.project()
|
|
|
|
.update(cx, |project, cx| project.open_buffer_for_symbol(symbol, cx));
|
|
|
|
|
|
|
|
let symbol = symbol.clone();
|
|
|
|
cx.spawn(|workspace, mut cx| async move {
|
|
|
|
let buffer = buffer.await?;
|
|
|
|
workspace.update(&mut cx, |workspace, cx| {
|
|
|
|
let position = buffer
|
|
|
|
.read(cx)
|
|
|
|
.clip_point_utf16(symbol.range.start, Bias::Left);
|
|
|
|
|
|
|
|
let editor = workspace.open_project_item::<Editor>(buffer, cx);
|
|
|
|
editor.update(cx, |editor, cx| {
|
|
|
|
editor.select_ranges(
|
|
|
|
[position..position],
|
|
|
|
Some(Autoscroll::Center),
|
|
|
|
cx,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Ok::<_, anyhow::Error>(())
|
|
|
|
})
|
|
|
|
.detach_and_log_err(cx);
|
|
|
|
workspace.dismiss_modal(cx);
|
|
|
|
}
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
2022-04-15 00:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-22 07:42:12 +00:00
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
impl PickerDelegate for ProjectSymbolsView {
|
|
|
|
fn confirm(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
if let Some(symbol) = self
|
|
|
|
.matches
|
|
|
|
.get(self.selected_match_index)
|
|
|
|
.map(|mat| self.symbols[mat.candidate_id].clone())
|
|
|
|
{
|
|
|
|
cx.emit(Event::Selected(symbol));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
cx.emit(Event::Dismissed);
|
|
|
|
}
|
2022-02-22 17:55:38 +00:00
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn match_count(&self) -> usize {
|
|
|
|
self.matches.len()
|
|
|
|
}
|
2022-02-22 07:42:12 +00:00
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn selected_index(&self) -> usize {
|
|
|
|
self.selected_match_index
|
2022-02-22 07:42:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 00:35:32 +00:00
|
|
|
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>) {
|
|
|
|
self.selected_match_index = ix;
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> Task<()> {
|
|
|
|
self.filter(&query, cx);
|
|
|
|
self.show_worktree_root_name = self.project.read(cx).visible_worktrees(cx).count() > 1;
|
|
|
|
let symbols = self
|
|
|
|
.project
|
|
|
|
.update(cx, |project, cx| project.symbols(&query, cx));
|
2022-04-20 23:03:11 +00:00
|
|
|
self.pending_update = cx.spawn_weak(|this, mut cx| async move {
|
2022-04-15 00:35:32 +00:00
|
|
|
let symbols = symbols.await.log_err();
|
|
|
|
if let Some(this) = this.upgrade(&cx) {
|
|
|
|
if let Some(symbols) = symbols {
|
|
|
|
this.update(&mut cx, |this, cx| {
|
|
|
|
this.match_candidates = symbols
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(id, symbol)| {
|
|
|
|
StringMatchCandidate::new(
|
|
|
|
id,
|
|
|
|
symbol.label.text[symbol.label.filter_range.clone()]
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
this.symbols = symbols;
|
|
|
|
this.filter(&query, cx);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 23:03:11 +00:00
|
|
|
});
|
|
|
|
Task::ready(())
|
2022-04-15 00:35:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:17:56 +00:00
|
|
|
fn render_match(
|
|
|
|
&self,
|
|
|
|
ix: usize,
|
|
|
|
mouse_state: &MouseState,
|
|
|
|
selected: bool,
|
|
|
|
cx: &AppContext,
|
|
|
|
) -> ElementBox {
|
2022-04-15 00:35:32 +00:00
|
|
|
let string_match = &self.matches[ix];
|
2022-03-17 13:33:01 +00:00
|
|
|
let settings = cx.global::<Settings>();
|
2022-04-28 22:17:56 +00:00
|
|
|
let style = &settings.theme.picker.item;
|
|
|
|
let current_style = style.style_for(mouse_state, selected);
|
2022-02-22 07:42:12 +00:00
|
|
|
let symbol = &self.symbols[string_match.candidate_id];
|
2022-03-15 09:39:43 +00:00
|
|
|
let syntax_runs = styled_runs_for_code_label(&symbol.label, &settings.theme.editor.syntax);
|
2022-02-22 07:42:12 +00:00
|
|
|
|
2022-02-22 17:55:38 +00:00
|
|
|
let mut path = symbol.path.to_string_lossy();
|
2022-04-15 00:35:32 +00:00
|
|
|
if self.show_worktree_root_name {
|
2022-02-22 17:55:38 +00:00
|
|
|
let project = self.project.read(cx);
|
|
|
|
if let Some(worktree) = project.worktree_for_id(symbol.worktree_id, cx) {
|
|
|
|
path = Cow::Owned(format!(
|
|
|
|
"{}{}{}",
|
|
|
|
worktree.read(cx).root_name(),
|
|
|
|
std::path::MAIN_SEPARATOR,
|
|
|
|
path.as_ref()
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:48:14 +00:00
|
|
|
Flex::column()
|
|
|
|
.with_child(
|
2022-04-28 22:17:56 +00:00
|
|
|
Text::new(symbol.label.text.clone(), current_style.label.text.clone())
|
2022-02-22 16:48:14 +00:00
|
|
|
.with_soft_wrap(false)
|
|
|
|
.with_highlights(combine_syntax_and_fuzzy_match_highlights(
|
|
|
|
&symbol.label.text,
|
2022-04-28 22:17:56 +00:00
|
|
|
current_style.label.text.clone().into(),
|
2022-02-22 16:48:14 +00:00
|
|
|
syntax_runs,
|
|
|
|
&string_match.positions,
|
|
|
|
))
|
|
|
|
.boxed(),
|
|
|
|
)
|
2022-02-22 20:56:43 +00:00
|
|
|
.with_child(
|
|
|
|
// Avoid styling the path differently when it is selected, since
|
|
|
|
// the symbol's syntax highlighting doesn't change when selected.
|
2022-04-28 22:17:56 +00:00
|
|
|
Label::new(path.to_string(), style.default.label.clone()).boxed(),
|
2022-02-22 20:56:43 +00:00
|
|
|
)
|
2022-02-22 07:42:12 +00:00
|
|
|
.contained()
|
2022-04-28 22:17:56 +00:00
|
|
|
.with_style(current_style.container)
|
2022-02-22 07:42:12 +00:00
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 22:07:05 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use futures::StreamExt;
|
|
|
|
use gpui::{serde_json::json, TestAppContext};
|
|
|
|
use language::{FakeLspAdapter, Language, LanguageConfig};
|
|
|
|
use project::FakeFs;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
async fn test_project_symbols(cx: &mut TestAppContext) {
|
|
|
|
cx.foreground().forbid_parking();
|
|
|
|
cx.update(|cx| cx.set_global(Settings::test(cx)));
|
|
|
|
|
|
|
|
let mut language = Language::new(
|
|
|
|
LanguageConfig {
|
|
|
|
name: "Rust".into(),
|
|
|
|
path_suffixes: vec!["rs".to_string()],
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let mut fake_servers = language.set_fake_lsp_adapter(FakeLspAdapter::default());
|
|
|
|
|
|
|
|
let fs = FakeFs::new(cx.background());
|
|
|
|
fs.insert_tree("/dir", json!({ "test.rs": "" })).await;
|
|
|
|
|
|
|
|
let project = Project::test(fs.clone(), cx);
|
|
|
|
project.update(cx, |project, _| {
|
|
|
|
project.languages().add(Arc::new(language));
|
|
|
|
});
|
|
|
|
|
|
|
|
let worktree_id = project
|
|
|
|
.update(cx, |project, cx| {
|
|
|
|
project.find_or_create_local_worktree("/dir", true, cx)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.0
|
|
|
|
.read_with(cx, |tree, _| tree.id());
|
|
|
|
|
|
|
|
let _buffer = project
|
|
|
|
.update(cx, |project, cx| {
|
|
|
|
project.open_buffer((worktree_id, "test.rs"), cx)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Set up fake langauge server to return fuzzy matches against
|
|
|
|
// a fixed set of symbol names.
|
|
|
|
let fake_symbol_names = ["one", "ton", "uno"];
|
|
|
|
let fake_server = fake_servers.next().await.unwrap();
|
|
|
|
fake_server.handle_request::<lsp::request::WorkspaceSymbol, _, _>(
|
|
|
|
move |params: lsp::WorkspaceSymbolParams, cx| {
|
|
|
|
let executor = cx.background();
|
|
|
|
async move {
|
|
|
|
let candidates = fake_symbol_names
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| StringMatchCandidate::new(0, name.into()))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let matches = fuzzy::match_strings(
|
|
|
|
&candidates,
|
|
|
|
¶ms.query,
|
|
|
|
true,
|
|
|
|
100,
|
|
|
|
&Default::default(),
|
|
|
|
executor.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
Ok(Some(
|
|
|
|
matches.into_iter().map(|mat| symbol(&mat.string)).collect(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create the project symbols view.
|
|
|
|
let (_, symbols_view) = cx.add_window(|cx| ProjectSymbolsView::new(project.clone(), cx));
|
|
|
|
let picker = symbols_view.read_with(cx, |symbols_view, _| symbols_view.picker.clone());
|
|
|
|
|
|
|
|
// Spawn multiples updates before the first update completes,
|
|
|
|
// such that in the end, there are no matches. Testing for regression:
|
|
|
|
// https://github.com/zed-industries/zed/issues/861
|
|
|
|
picker.update(cx, |p, cx| {
|
|
|
|
p.update_matches("o".to_string(), cx);
|
|
|
|
p.update_matches("on".to_string(), cx);
|
|
|
|
p.update_matches("onex".to_string(), cx);
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.foreground().run_until_parked();
|
|
|
|
symbols_view.read_with(cx, |symbols_view, _| {
|
|
|
|
assert_eq!(symbols_view.matches.len(), 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Spawn more updates such that in the end, there are matches.
|
|
|
|
picker.update(cx, |p, cx| {
|
|
|
|
p.update_matches("one".to_string(), cx);
|
|
|
|
p.update_matches("on".to_string(), cx);
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.foreground().run_until_parked();
|
|
|
|
symbols_view.read_with(cx, |symbols_view, _| {
|
|
|
|
assert_eq!(symbols_view.matches.len(), 2);
|
|
|
|
assert_eq!(symbols_view.matches[0].string, "one");
|
|
|
|
assert_eq!(symbols_view.matches[1].string, "ton");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Spawn more updates such that in the end, there are again no matches.
|
|
|
|
picker.update(cx, |p, cx| {
|
|
|
|
p.update_matches("o".to_string(), cx);
|
|
|
|
p.update_matches("".to_string(), cx);
|
|
|
|
});
|
|
|
|
|
|
|
|
cx.foreground().run_until_parked();
|
|
|
|
symbols_view.read_with(cx, |symbols_view, _| {
|
|
|
|
assert_eq!(symbols_view.matches.len(), 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn symbol(name: &str) -> lsp::SymbolInformation {
|
|
|
|
#[allow(deprecated)]
|
|
|
|
lsp::SymbolInformation {
|
|
|
|
name: name.to_string(),
|
|
|
|
kind: lsp::SymbolKind::FUNCTION,
|
|
|
|
tags: None,
|
|
|
|
deprecated: None,
|
|
|
|
container_name: None,
|
|
|
|
location: lsp::Location::new(
|
|
|
|
lsp::Url::from_file_path("/a/b").unwrap(),
|
|
|
|
lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 0)),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|