assistant panel: automatically insert selections (#17589)

Addresses parts of feedback from
https://www.jacobcolling.com/friction-log/zed-friction-log

Release Notes:
- "Assistant::NewContext" now automatically does quote selection as well
- "Assistant::QuoteSelection" now handles multicursor selections,
inserting multiple excerpts.
This commit is contained in:
Piotr Osiewicz 2024-09-09 18:31:55 +02:00 committed by GitHub
parent dd257b8412
commit 12dde17608
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -939,9 +939,16 @@ impl AssistantPanel {
cx: &mut ViewContext<Workspace>, cx: &mut ViewContext<Workspace>,
) { ) {
if let Some(panel) = workspace.panel::<AssistantPanel>(cx) { if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
panel.update(cx, |panel, cx| { let did_create_context = panel
panel.new_context(cx); .update(cx, |panel, cx| {
}); panel.new_context(cx)?;
Some(())
})
.is_some();
if did_create_context {
ContextEditor::quote_selection(workspace, &Default::default(), cx);
}
} }
} }
@ -3186,16 +3193,17 @@ impl ContextEditor {
return; return;
}; };
let selection = editor.update(cx, |editor, cx| editor.selections.newest_adjusted(cx)); let mut creases = vec![];
let editor = editor.read(cx); editor.update(cx, |editor, cx| {
let selections = editor.selections.all_adjusted(cx);
let buffer = editor.buffer().read(cx).snapshot(cx); let buffer = editor.buffer().read(cx).snapshot(cx);
for selection in selections {
let range = editor::ToOffset::to_offset(&selection.start, &buffer) let range = editor::ToOffset::to_offset(&selection.start, &buffer)
..editor::ToOffset::to_offset(&selection.end, &buffer); ..editor::ToOffset::to_offset(&selection.end, &buffer);
let selected_text = buffer.text_for_range(range.clone()).collect::<String>(); let selected_text = buffer.text_for_range(range.clone()).collect::<String>();
if selected_text.is_empty() { if selected_text.is_empty() {
return; continue;
} }
let start_language = buffer.language_at(range.start); let start_language = buffer.language_at(range.start);
let end_language = buffer.language_at(range.end); let end_language = buffer.language_at(range.end);
let language_name = if start_language == end_language { let language_name = if start_language == end_language {
@ -3204,11 +3212,9 @@ impl ContextEditor {
None None
}; };
let language_name = language_name.as_deref().unwrap_or(""); let language_name = language_name.as_deref().unwrap_or("");
let filename = buffer let filename = buffer
.file_at(selection.start) .file_at(selection.start)
.map(|file| file.full_path(cx)); .map(|file| file.full_path(cx));
let text = if language_name == "markdown" { let text = if language_name == "markdown" {
selected_text selected_text
.lines() .lines()
@ -3223,8 +3229,9 @@ impl ContextEditor {
.symbols_containing(selection.end, None) .symbols_containing(selection.end, None)
.map(|(_, symbols)| symbols); .map(|(_, symbols)| symbols);
let outline_text = let outline_text = if let Some((start_symbols, end_symbols)) =
if let Some((start_symbols, end_symbols)) = start_symbols.zip(end_symbols) { start_symbols.zip(end_symbols)
{
Some( Some(
start_symbols start_symbols
.into_iter() .into_iter()
@ -3246,15 +3253,16 @@ impl ContextEditor {
Some(selection.start.row..selection.end.row), Some(selection.start.row..selection.end.row),
); );
if let Some((line_comment_prefix, outline_text)) = line_comment_prefix.zip(outline_text) if let Some((line_comment_prefix, outline_text)) =
line_comment_prefix.zip(outline_text)
{ {
let breadcrumb = format!("{line_comment_prefix}Excerpt from: {outline_text}\n"); let breadcrumb =
format!("{line_comment_prefix}Excerpt from: {outline_text}\n");
format!("{fence}{breadcrumb}{selected_text}\n```") format!("{fence}{breadcrumb}{selected_text}\n```")
} else { } else {
format!("{fence}{selected_text}\n```") format!("{fence}{selected_text}\n```")
} }
}; };
let crease_title = if let Some(path) = filename { let crease_title = if let Some(path) = filename {
let start_line = selection.start.row + 1; let start_line = selection.start.row + 1;
let end_line = selection.end.row + 1; let end_line = selection.end.row + 1;
@ -3266,7 +3274,12 @@ impl ContextEditor {
} else { } else {
"Quoted selection".to_string() "Quoted selection".to_string()
}; };
creases.push((text, crease_title));
}
});
if creases.is_empty() {
return;
}
// Activate the panel // Activate the panel
if !panel.focus_handle(cx).contains_focused(cx) { if !panel.focus_handle(cx).contains_focused(cx) {
workspace.toggle_panel_focus::<AssistantPanel>(cx); workspace.toggle_panel_focus::<AssistantPanel>(cx);
@ -3283,7 +3296,7 @@ impl ContextEditor {
context.update(cx, |context, cx| { context.update(cx, |context, cx| {
context.editor.update(cx, |editor, cx| { context.editor.update(cx, |editor, cx| {
editor.insert("\n", cx); editor.insert("\n", cx);
for (text, crease_title) in creases {
let point = editor.selections.newest::<Point>(cx).head(); let point = editor.selections.newest::<Point>(cx).head();
let start_row = MultiBufferRow(point.row); let start_row = MultiBufferRow(point.row);
@ -3316,6 +3329,7 @@ impl ContextEditor {
}, },
cx, cx,
); );
}
}) })
}); });
}; };