
Closes #32756 - Uses `filter_text` from LSP source to filter items in completion list. This fixes noisy lists like on typing `await` in Rust, it would suggest `await.or`, `await.and`, etc., which are bad suggestions. Fallbacks to label. - Add `penalize_length` flag to fuzzy matcher, which was the default behavior across. Now, this flag is set to `false` just for code completion fuzzy matching. This fixes the case where if the query is `unreac` and the completion items are `unreachable` and `unreachable!()`, the item with a shorter length would have a larger score than the other one, which is not right in the case of auto-complete context. Now these two items will have the same fuzzy score, and LSP `sort_text` will take over in finalizing its ranking. - Updated test to be more utility based rather than example based. This will help to iterate/verify logic faster on what's going on. Before/After: await: <img width="600" alt="before-await" src="https://github.com/user-attachments/assets/384138dd-a90d-4942-a430-6ae15df37268" /> <img width="600" alt="after-await" src="https://github.com/user-attachments/assets/d05a10fa-bae5-49bd-9fe7-9933ff215f29" /> iter: <img width="600" alt="before-iter" src="https://github.com/user-attachments/assets/6e57ffe9-007d-4b17-9cc2-d48fc0176c8e" /> <img width="600" alt="after-iter" src="https://github.com/user-attachments/assets/a8577a9f-dcc8-4fd6-9ba0-b7590584ec31" /> opt: <img width="600" alt="opt-before" src="https://github.com/user-attachments/assets/d45b6c52-c9ee-4bf3-8552-d5e3fdbecbff" /> <img width="600" alt="opt-after" src="https://github.com/user-attachments/assets/daac11a8-9699-48f8-b441-19fe9803848d" /> Release Notes: - Improved code completion filtering to provide fewer and more accurate suggestions.
331 lines
10 KiB
Rust
331 lines
10 KiB
Rust
use dap::{DapRegistry, DebugRequest};
|
|
use fuzzy::{StringMatch, StringMatchCandidate};
|
|
use gpui::{AppContext, DismissEvent, Entity, EventEmitter, Focusable, Render};
|
|
use gpui::{Subscription, WeakEntity};
|
|
use picker::{Picker, PickerDelegate};
|
|
use task::ZedDebugConfig;
|
|
use util::debug_panic;
|
|
|
|
use std::sync::Arc;
|
|
use sysinfo::System;
|
|
use ui::{Context, Tooltip, prelude::*};
|
|
use ui::{ListItem, ListItemSpacing};
|
|
use workspace::{ModalView, Workspace};
|
|
|
|
use crate::debugger_panel::DebugPanel;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(super) struct Candidate {
|
|
pub(super) pid: u32,
|
|
pub(super) name: SharedString,
|
|
pub(super) command: Vec<String>,
|
|
}
|
|
|
|
pub(crate) struct AttachModalDelegate {
|
|
selected_index: usize,
|
|
matches: Vec<StringMatch>,
|
|
placeholder_text: Arc<str>,
|
|
pub(crate) definition: ZedDebugConfig,
|
|
workspace: WeakEntity<Workspace>,
|
|
candidates: Arc<[Candidate]>,
|
|
}
|
|
|
|
impl AttachModalDelegate {
|
|
fn new(
|
|
workspace: WeakEntity<Workspace>,
|
|
definition: ZedDebugConfig,
|
|
candidates: Arc<[Candidate]>,
|
|
) -> Self {
|
|
Self {
|
|
workspace,
|
|
definition,
|
|
candidates,
|
|
selected_index: 0,
|
|
matches: Vec::default(),
|
|
placeholder_text: Arc::from("Select the process you want to attach the debugger to"),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct AttachModal {
|
|
_subscription: Subscription,
|
|
pub(crate) picker: Entity<Picker<AttachModalDelegate>>,
|
|
}
|
|
|
|
impl AttachModal {
|
|
pub fn new(
|
|
definition: ZedDebugConfig,
|
|
workspace: WeakEntity<Workspace>,
|
|
modal: bool,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) -> Self {
|
|
let mut processes: Box<[_]> = System::new_all()
|
|
.processes()
|
|
.values()
|
|
.map(|process| {
|
|
let name = process.name().to_string_lossy().into_owned();
|
|
Candidate {
|
|
name: name.into(),
|
|
pid: process.pid().as_u32(),
|
|
command: process
|
|
.cmd()
|
|
.iter()
|
|
.map(|s| s.to_string_lossy().to_string())
|
|
.collect::<Vec<_>>(),
|
|
}
|
|
})
|
|
.collect();
|
|
processes.sort_by_key(|k| k.name.clone());
|
|
let processes = processes.into_iter().collect();
|
|
Self::with_processes(workspace, definition, processes, modal, window, cx)
|
|
}
|
|
|
|
pub(super) fn with_processes(
|
|
workspace: WeakEntity<Workspace>,
|
|
definition: ZedDebugConfig,
|
|
processes: Arc<[Candidate]>,
|
|
modal: bool,
|
|
window: &mut Window,
|
|
cx: &mut Context<Self>,
|
|
) -> Self {
|
|
let picker = cx.new(|cx| {
|
|
Picker::uniform_list(
|
|
AttachModalDelegate::new(workspace, definition, processes),
|
|
window,
|
|
cx,
|
|
)
|
|
.modal(modal)
|
|
});
|
|
Self {
|
|
_subscription: cx.subscribe(&picker, |_, _, _, cx| {
|
|
cx.emit(DismissEvent);
|
|
}),
|
|
picker,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Render for AttachModal {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
|
|
v_flex()
|
|
.key_context("AttachModal")
|
|
.track_focus(&self.focus_handle(cx))
|
|
.w(rems(34.))
|
|
.child(self.picker.clone())
|
|
}
|
|
}
|
|
|
|
impl EventEmitter<DismissEvent> for AttachModal {}
|
|
|
|
impl Focusable for AttachModal {
|
|
fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
|
|
self.picker.read(cx).focus_handle(cx)
|
|
}
|
|
}
|
|
|
|
impl ModalView for AttachModal {}
|
|
|
|
impl PickerDelegate for AttachModalDelegate {
|
|
type ListItem = ListItem;
|
|
|
|
fn match_count(&self) -> usize {
|
|
self.matches.len()
|
|
}
|
|
|
|
fn selected_index(&self) -> usize {
|
|
self.selected_index
|
|
}
|
|
|
|
fn set_selected_index(
|
|
&mut self,
|
|
ix: usize,
|
|
_window: &mut Window,
|
|
_: &mut Context<Picker<Self>>,
|
|
) {
|
|
self.selected_index = ix;
|
|
}
|
|
|
|
fn placeholder_text(&self, _window: &mut Window, _cx: &mut App) -> std::sync::Arc<str> {
|
|
self.placeholder_text.clone()
|
|
}
|
|
|
|
fn update_matches(
|
|
&mut self,
|
|
query: String,
|
|
_window: &mut Window,
|
|
cx: &mut Context<Picker<Self>>,
|
|
) -> gpui::Task<()> {
|
|
cx.spawn(async move |this, cx| {
|
|
let Some(processes) = this
|
|
.read_with(cx, |this, _| this.delegate.candidates.clone())
|
|
.ok()
|
|
else {
|
|
return;
|
|
};
|
|
|
|
let matches = fuzzy::match_strings(
|
|
&processes
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(id, candidate)| {
|
|
StringMatchCandidate::new(
|
|
id,
|
|
format!(
|
|
"{} {} {}",
|
|
candidate.command.join(" "),
|
|
candidate.pid,
|
|
candidate.name
|
|
)
|
|
.as_str(),
|
|
)
|
|
})
|
|
.collect::<Vec<_>>(),
|
|
&query,
|
|
true,
|
|
true,
|
|
100,
|
|
&Default::default(),
|
|
cx.background_executor().clone(),
|
|
)
|
|
.await;
|
|
|
|
this.update(cx, |this, _| {
|
|
let delegate = &mut this.delegate;
|
|
|
|
delegate.matches = matches;
|
|
|
|
if delegate.matches.is_empty() {
|
|
delegate.selected_index = 0;
|
|
} else {
|
|
delegate.selected_index =
|
|
delegate.selected_index.min(delegate.matches.len() - 1);
|
|
}
|
|
})
|
|
.ok();
|
|
})
|
|
}
|
|
|
|
fn confirm(&mut self, _: bool, window: &mut Window, cx: &mut Context<Picker<Self>>) {
|
|
let candidate = self
|
|
.matches
|
|
.get(self.selected_index())
|
|
.and_then(|current_match| {
|
|
let ix = current_match.candidate_id;
|
|
self.candidates.get(ix)
|
|
});
|
|
|
|
let Some(candidate) = candidate else {
|
|
return cx.emit(DismissEvent);
|
|
};
|
|
|
|
match &mut self.definition.request {
|
|
DebugRequest::Attach(config) => {
|
|
config.process_id = Some(candidate.pid);
|
|
}
|
|
DebugRequest::Launch(_) => {
|
|
debug_panic!("Debugger attach modal used on launch debug config");
|
|
return;
|
|
}
|
|
}
|
|
|
|
let Some(adapter) = cx.read_global::<DapRegistry, _>(|registry, _| {
|
|
registry.adapter(&self.definition.adapter)
|
|
}) else {
|
|
return;
|
|
};
|
|
|
|
let workspace = self.workspace.clone();
|
|
let definition = self.definition.clone();
|
|
cx.spawn_in(window, async move |this, cx| {
|
|
let Ok(scenario) = adapter.config_from_zed_format(definition).await else {
|
|
return;
|
|
};
|
|
|
|
let panel = workspace
|
|
.update(cx, |workspace, cx| workspace.panel::<DebugPanel>(cx))
|
|
.ok()
|
|
.flatten();
|
|
if let Some(panel) = panel {
|
|
panel
|
|
.update_in(cx, |panel, window, cx| {
|
|
panel.start_session(scenario, Default::default(), None, None, window, cx);
|
|
})
|
|
.ok();
|
|
}
|
|
this.update(cx, |_, cx| {
|
|
cx.emit(DismissEvent);
|
|
})
|
|
.ok();
|
|
})
|
|
.detach();
|
|
}
|
|
|
|
fn dismissed(&mut self, _window: &mut Window, cx: &mut Context<Picker<Self>>) {
|
|
self.selected_index = 0;
|
|
|
|
cx.emit(DismissEvent);
|
|
}
|
|
|
|
fn render_match(
|
|
&self,
|
|
ix: usize,
|
|
selected: bool,
|
|
_window: &mut Window,
|
|
_: &mut Context<Picker<Self>>,
|
|
) -> Option<Self::ListItem> {
|
|
let hit = &self.matches[ix];
|
|
let candidate = self.candidates.get(hit.candidate_id)?;
|
|
|
|
Some(
|
|
ListItem::new(SharedString::from(format!("process-entry-{ix}")))
|
|
.inset(true)
|
|
.spacing(ListItemSpacing::Sparse)
|
|
.toggle_state(selected)
|
|
.child(
|
|
v_flex()
|
|
.items_start()
|
|
.child(Label::new(format!("{} {}", candidate.name, candidate.pid)))
|
|
.child(
|
|
div()
|
|
.id(SharedString::from(format!("process-entry-{ix}-command")))
|
|
.tooltip(Tooltip::text(
|
|
candidate
|
|
.command
|
|
.clone()
|
|
.into_iter()
|
|
.collect::<Vec<_>>()
|
|
.join(" "),
|
|
))
|
|
.child(
|
|
Label::new(format!(
|
|
"{} {}",
|
|
candidate.name,
|
|
candidate
|
|
.command
|
|
.clone()
|
|
.into_iter()
|
|
.skip(1)
|
|
.collect::<Vec<_>>()
|
|
.join(" ")
|
|
))
|
|
.size(LabelSize::Small)
|
|
.color(Color::Muted),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
pub(crate) fn _process_names(modal: &AttachModal, cx: &mut Context<AttachModal>) -> Vec<String> {
|
|
modal.picker.read_with(cx, |picker, _| {
|
|
picker
|
|
.delegate
|
|
.matches
|
|
.iter()
|
|
.map(|hit| hit.string.clone())
|
|
.collect::<Vec<_>>()
|
|
})
|
|
}
|