Improve slash commands (#16195)
This PR: - Makes slash commands easier to compose by adding a concept, `CompletionIntent`. When using `tab` on a completion in the assistant panel, that completion item will be expanded but the associated command will not be run. Using `enter` will still either run the completion item or continue command composition as before. - Fixes a bug where running `/diagnostics` on a project with no diagnostics will delete the entire command, rather than rendering an empty header. - Improves the autocomplete rendering for files, showing when directories are selected and re-arranging the results to have the file name or trailing directory show first. <img width="642" alt="Screenshot 2024-08-13 at 8 12 43 PM" src="https://github.com/user-attachments/assets/97c96cd2-741f-4f15-ad03-7cf78129a71c"> Release Notes: - N/A
This commit is contained in:
@@ -64,6 +64,12 @@ pub struct ConfirmCompletion {
|
||||
pub item_ix: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Deserialize, Default)]
|
||||
pub struct ComposeCompletion {
|
||||
#[serde(default)]
|
||||
pub item_ix: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Deserialize, Default)]
|
||||
pub struct ConfirmCodeAction {
|
||||
#[serde(default)]
|
||||
@@ -140,6 +146,7 @@ impl_actions!(
|
||||
[
|
||||
ConfirmCodeAction,
|
||||
ConfirmCompletion,
|
||||
ComposeCompletion,
|
||||
ExpandExcerpts,
|
||||
ExpandExcerptsUp,
|
||||
ExpandExcerptsDown,
|
||||
|
||||
@@ -114,7 +114,7 @@ use ordered_float::OrderedFloat;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use project::project_settings::{GitGutterSetting, ProjectSettings};
|
||||
use project::{
|
||||
CodeAction, Completion, FormatTrigger, Item, Location, Project, ProjectPath,
|
||||
CodeAction, Completion, CompletionIntent, FormatTrigger, Item, Location, Project, ProjectPath,
|
||||
ProjectTransaction, TaskSourceKind, WorktreeId,
|
||||
};
|
||||
use rand::prelude::*;
|
||||
@@ -4213,6 +4213,23 @@ impl Editor {
|
||||
action: &ConfirmCompletion,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Option<Task<Result<()>>> {
|
||||
self.do_completion(action.item_ix, CompletionIntent::Complete, cx)
|
||||
}
|
||||
|
||||
pub fn compose_completion(
|
||||
&mut self,
|
||||
action: &ComposeCompletion,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Option<Task<Result<()>>> {
|
||||
self.do_completion(action.item_ix, CompletionIntent::Compose, cx)
|
||||
}
|
||||
|
||||
fn do_completion(
|
||||
&mut self,
|
||||
item_ix: Option<usize>,
|
||||
intent: CompletionIntent,
|
||||
cx: &mut ViewContext<Editor>,
|
||||
) -> Option<Task<std::result::Result<(), anyhow::Error>>> {
|
||||
use language::ToOffset as _;
|
||||
|
||||
let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? {
|
||||
@@ -4223,7 +4240,7 @@ impl Editor {
|
||||
|
||||
let mat = completions_menu
|
||||
.matches
|
||||
.get(action.item_ix.unwrap_or(completions_menu.selected_item))?;
|
||||
.get(item_ix.unwrap_or(completions_menu.selected_item))?;
|
||||
let buffer_handle = completions_menu.buffer;
|
||||
let completions = completions_menu.completions.read();
|
||||
let completion = completions.get(mat.candidate_id)?;
|
||||
@@ -4358,7 +4375,7 @@ impl Editor {
|
||||
});
|
||||
|
||||
if let Some(confirm) = completion.confirm.as_ref() {
|
||||
(confirm)(cx);
|
||||
(confirm)(intent, cx);
|
||||
}
|
||||
|
||||
if completion.show_new_completions_on_confirm {
|
||||
|
||||
@@ -372,6 +372,13 @@ impl EditorElement {
|
||||
cx.propagate();
|
||||
}
|
||||
});
|
||||
register_action(view, cx, |editor, action, cx| {
|
||||
if let Some(task) = editor.compose_completion(action, cx) {
|
||||
task.detach_and_log_err(cx);
|
||||
} else {
|
||||
cx.propagate();
|
||||
}
|
||||
});
|
||||
register_action(view, cx, |editor, action, cx| {
|
||||
if let Some(task) = editor.confirm_code_action(action, cx) {
|
||||
task.detach_and_log_err(cx);
|
||||
|
||||
Reference in New Issue
Block a user