assistant2: Rework @mentions (#26983)

https://github.com/user-attachments/assets/167f753f-2775-4d31-bfef-55565e61e4bc

Release Notes:

- N/A
This commit is contained in:
Bennet Bo Fenner
2025-03-24 19:32:52 +01:00
committed by GitHub
parent 4a5f89aded
commit 699369995b
18 changed files with 1637 additions and 485 deletions
+13 -4
View File
@@ -1,6 +1,6 @@
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
div, px, uniform_list, AnyElement, BackgroundExecutor, Div, Entity, Focusable, FontWeight,
div, px, uniform_list, AnyElement, BackgroundExecutor, Entity, Focusable, FontWeight,
ListSizingBehavior, ScrollStrategy, SharedString, Size, StrikethroughStyle, StyledText,
UniformListScrollHandle,
};
@@ -236,6 +236,7 @@ impl CompletionsMenu {
runs: Default::default(),
filter_range: Default::default(),
},
icon_path: None,
documentation: None,
confirm: None,
source: CompletionSource::Custom,
@@ -539,9 +540,17 @@ impl CompletionsMenu {
} else {
None
};
let color_swatch = completion
let start_slot = completion
.color()
.map(|color| div().size_4().bg(color).rounded_xs());
.map(|color| div().size_4().bg(color).rounded_xs().into_any_element())
.or_else(|| {
completion.icon_path.as_ref().map(|path| {
Icon::from_path(path)
.size(IconSize::Small)
.into_any_element()
})
});
div().min_w(px(280.)).max_w(px(540.)).child(
ListItem::new(mat.candidate_id)
@@ -559,7 +568,7 @@ impl CompletionsMenu {
task.detach_and_log_err(cx)
}
}))
.start_slot::<Div>(color_swatch)
.start_slot::<AnyElement>(start_slot)
.child(h_flex().overflow_hidden().child(completion_label))
.end_slot::<Label>(documentation_label),
)
+41 -2
View File
@@ -531,6 +531,18 @@ impl EditPredictionPreview {
}
}
pub struct ContextMenuOptions {
pub min_entries_visible: usize,
pub max_entries_visible: usize,
pub placement: Option<ContextMenuPlacement>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContextMenuPlacement {
Above,
Below,
}
#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Debug, Default)]
struct EditorActionId(usize);
@@ -677,6 +689,7 @@ pub struct Editor {
active_indent_guides_state: ActiveIndentGuidesState,
nav_history: Option<ItemNavHistory>,
context_menu: RefCell<Option<CodeContextMenu>>,
context_menu_options: Option<ContextMenuOptions>,
mouse_context_menu: Option<MouseContextMenu>,
completion_tasks: Vec<(CompletionId, Task<Option<()>>)>,
signature_help_state: SignatureHelpState,
@@ -1441,6 +1454,7 @@ impl Editor {
active_indent_guides_state: ActiveIndentGuidesState::default(),
nav_history: None,
context_menu: RefCell::new(None),
context_menu_options: None,
mouse_context_menu: None,
completion_tasks: Default::default(),
signature_help_state: SignatureHelpState::default(),
@@ -4251,8 +4265,14 @@ impl Editor {
let (mut words, provided_completions) = match provider {
Some(provider) => {
let completions =
provider.completions(&buffer, buffer_position, completion_context, window, cx);
let completions = provider.completions(
position.excerpt_id,
&buffer,
buffer_position,
completion_context,
window,
cx,
);
let words = match completion_settings.words {
WordsCompletionMode::Disabled => Task::ready(HashMap::default()),
@@ -4310,6 +4330,7 @@ impl Editor {
old_range: old_range.clone(),
new_text: word.clone(),
label: CodeLabel::plain(word, None),
icon_path: None,
documentation: None,
source: CompletionSource::BufferWord {
word_range,
@@ -4384,6 +4405,17 @@ impl Editor {
self.completion_tasks.push((id, task));
}
#[cfg(feature = "test-support")]
pub fn current_completions(&self) -> Option<Vec<project::Completion>> {
let menu = self.context_menu.borrow();
if let CodeContextMenu::Completions(menu) = menu.as_ref()? {
let completions = menu.completions.borrow();
Some(completions.to_vec())
} else {
None
}
}
pub fn confirm_completion(
&mut self,
action: &ConfirmCompletion,
@@ -6435,6 +6467,10 @@ impl Editor {
.map(|menu| menu.origin())
}
pub fn set_context_menu_options(&mut self, options: ContextMenuOptions) {
self.context_menu_options = Some(options);
}
const EDIT_PREDICTION_POPOVER_PADDING_X: Pixels = Pixels(24.);
const EDIT_PREDICTION_POPOVER_PADDING_Y: Pixels = Pixels(2.);
@@ -17857,6 +17893,7 @@ pub trait SemanticsProvider {
pub trait CompletionProvider {
fn completions(
&self,
excerpt_id: ExcerptId,
buffer: &Entity<Buffer>,
buffer_position: text::Anchor,
trigger: CompletionContext,
@@ -18090,6 +18127,7 @@ fn snippet_completions(
runs: Vec::new(),
filter_range: 0..matching_prefix.len(),
},
icon_path: None,
documentation: snippet
.description
.clone()
@@ -18106,6 +18144,7 @@ fn snippet_completions(
impl CompletionProvider for Entity<Project> {
fn completions(
&self,
_excerpt_id: ExcerptId,
buffer: &Entity<Buffer>,
buffer_position: text::Anchor,
options: CompletionContext,
+65 -27
View File
@@ -16,15 +16,15 @@ use crate::{
items::BufferSearchHighlights,
mouse_context_menu::{self, MenuPosition, MouseContextMenu},
scroll::{axis_pair, scroll_amount::ScrollAmount, AxisPair},
BlockId, ChunkReplacement, CursorShape, CustomBlockId, DisplayDiffHunk, DisplayPoint,
DisplayRow, DocumentHighlightRead, DocumentHighlightWrite, EditDisplayMode, Editor, EditorMode,
EditorSettings, EditorSnapshot, EditorStyle, FocusedBlock, GoToHunk, GoToPreviousHunk,
GutterDimensions, HalfPageDown, HalfPageUp, HandleInput, HoveredCursor, InlayHintRefreshReason,
InlineCompletion, JumpData, LineDown, LineHighlight, LineUp, OpenExcerpts, PageDown, PageUp,
Point, RowExt, RowRangeExt, SelectPhase, SelectedTextHighlight, Selection, SoftWrap,
StickyHeaderExcerpt, ToPoint, ToggleFold, COLUMNAR_SELECTION_MODIFIERS, CURSORS_VISIBLE_FOR,
FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN, MIN_LINE_NUMBER_DIGITS,
MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
BlockId, ChunkReplacement, ContextMenuPlacement, CursorShape, CustomBlockId, DisplayDiffHunk,
DisplayPoint, DisplayRow, DocumentHighlightRead, DocumentHighlightWrite, EditDisplayMode,
Editor, EditorMode, EditorSettings, EditorSnapshot, EditorStyle, FocusedBlock, GoToHunk,
GoToPreviousHunk, GutterDimensions, HalfPageDown, HalfPageUp, HandleInput, HoveredCursor,
InlayHintRefreshReason, InlineCompletion, JumpData, LineDown, LineHighlight, LineUp,
OpenExcerpts, PageDown, PageUp, Point, RowExt, RowRangeExt, SelectPhase, SelectedTextHighlight,
Selection, SoftWrap, StickyHeaderExcerpt, ToPoint, ToggleFold, COLUMNAR_SELECTION_MODIFIERS,
CURSORS_VISIBLE_FOR, FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN,
MIN_LINE_NUMBER_DIGITS, MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
};
use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind};
use client::ParticipantIndex;
@@ -3338,6 +3338,7 @@ impl EditorElement {
let height_below_menu = Pixels::ZERO;
let mut edit_prediction_popover_visible = false;
let mut context_menu_visible = false;
let context_menu_placement;
{
let editor = self.editor.read(cx);
@@ -3351,11 +3352,22 @@ impl EditorElement {
if editor.context_menu_visible() {
if let Some(crate::ContextMenuOrigin::Cursor) = editor.context_menu_origin() {
min_menu_height += line_height * 3. + POPOVER_Y_PADDING;
max_menu_height += line_height * 12. + POPOVER_Y_PADDING;
let (min_height_in_lines, max_height_in_lines) = editor
.context_menu_options
.as_ref()
.map_or((3, 12), |options| {
(options.min_entries_visible, options.max_entries_visible)
});
min_menu_height += line_height * min_height_in_lines as f32 + POPOVER_Y_PADDING;
max_menu_height += line_height * max_height_in_lines as f32 + POPOVER_Y_PADDING;
context_menu_visible = true;
}
}
context_menu_placement = editor
.context_menu_options
.as_ref()
.and_then(|options| options.placement.clone());
}
let visible = edit_prediction_popover_visible || context_menu_visible;
@@ -3390,6 +3402,7 @@ impl EditorElement {
line_height,
min_height,
max_height,
context_menu_placement,
text_hitbox,
viewport_bounds,
window,
@@ -3532,8 +3545,16 @@ impl EditorElement {
x: -gutter_overshoot,
y: gutter_row.next_row().as_f32() * line_height - scroll_pixel_position.y,
};
let min_height = line_height * 3. + POPOVER_Y_PADDING;
let max_height = line_height * 12. + POPOVER_Y_PADDING;
let (min_height_in_lines, max_height_in_lines) = editor
.context_menu_options
.as_ref()
.map_or((3, 12), |options| {
(options.min_entries_visible, options.max_entries_visible)
});
let min_height = line_height * min_height_in_lines as f32 + POPOVER_Y_PADDING;
let max_height = line_height * max_height_in_lines as f32 + POPOVER_Y_PADDING;
let viewport_bounds =
Bounds::new(Default::default(), window.viewport_size()).extend(Edges {
right: -Self::SCROLLBAR_WIDTH - MENU_GAP,
@@ -3544,6 +3565,10 @@ impl EditorElement {
line_height,
min_height,
max_height,
editor
.context_menu_options
.as_ref()
.and_then(|options| options.placement.clone()),
text_hitbox,
viewport_bounds,
window,
@@ -3564,6 +3589,7 @@ impl EditorElement {
line_height: Pixels,
min_height: Pixels,
max_height: Pixels,
placement: Option<ContextMenuPlacement>,
text_hitbox: &Hitbox,
viewport_bounds: Bounds<Pixels>,
window: &mut Window,
@@ -3588,7 +3614,11 @@ impl EditorElement {
let available_above = bottom_y_when_flipped - text_hitbox.top();
let available_below = text_hitbox.bottom() - target_position.y;
let y_overflows_below = max_height > available_below;
let mut y_flipped = y_overflows_below && available_above > available_below;
let mut y_flipped = match placement {
Some(ContextMenuPlacement::Above) => true,
Some(ContextMenuPlacement::Below) => false,
None => y_overflows_below && available_above > available_below,
};
let mut height = cmp::min(
max_height,
if y_flipped {
@@ -3602,19 +3632,27 @@ impl EditorElement {
if height < min_height {
let available_above = bottom_y_when_flipped;
let available_below = viewport_bounds.bottom() - target_position.y;
if available_below > min_height {
y_flipped = false;
height = min_height;
} else if available_above > min_height {
y_flipped = true;
height = min_height;
} else if available_above > available_below {
y_flipped = true;
height = available_above;
} else {
y_flipped = false;
height = available_below;
}
let (y_flipped_override, height_override) = match placement {
Some(ContextMenuPlacement::Above) => {
(true, cmp::min(available_above, min_height))
}
Some(ContextMenuPlacement::Below) => {
(false, cmp::min(available_below, min_height))
}
None => {
if available_below > min_height {
(false, min_height)
} else if available_above > min_height {
(true, min_height)
} else if available_above > available_below {
(true, available_above)
} else {
(false, available_below)
}
}
};
y_flipped = y_flipped_override;
height = height_override;
}
let max_width_for_stable_x = viewport_bounds.right() - target_position.x;