Show single-line docs in autocomplete and apply completion on mousedown

This commit is contained in:
Antonio Scandurra 2023-11-24 13:22:25 +01:00
parent 54357d6553
commit 19bfed165b
2 changed files with 148 additions and 199 deletions

View file

@ -1281,95 +1281,40 @@ impl CompletionsMenu {
styled_runs_for_code_label(&completion.label, &style.syntax), styled_runs_for_code_label(&completion.label, &style.syntax),
&mat.positions, &mat.positions,
); );
let completion_label = StyledText::new(completion.label.text.clone())
.with_runs(completion_runs);
let documentation_label =
if let Some(Documentation::SingleLine(text)) = documentation {
Some(SharedString::from(text.clone()))
} else {
None
};
// todo!("documentation")
// MouseEventHandler::new::<CompletionTag, _>(mat.candidate_id, cx, |state, _| {
// let completion_label = HighlightedLabel::new(
// completion.label.text.clone(),
// combine_syntax_and_fuzzy_match_highlights(
// &completion.label.text,
// style.text.color.into(),
// styled_runs_for_code_label(&completion.label, &style.syntax),
// &mat.positions,
// ),
// );
// Text::new(completion.label.text.clone(), style.text.clone())
// .with_soft_wrap(false)
// .with_highlights();
// if let Some(Documentation::SingleLine(text)) = documentation {
// h_stack()
// .child(completion_label)
// .with_children((|| {
// let text_style = TextStyle {
// color: style.autocomplete.inline_docs_color,
// font_size: style.text.font_size
// * style.autocomplete.inline_docs_size_percent,
// ..style.text.clone()
// };
// let label = Text::new(text.clone(), text_style)
// .aligned()
// .constrained()
// .dynamically(move |constraint, _, _| gpui::SizeConstraint {
// min: constraint.min,
// max: vec2f(constraint.max.x(), constraint.min.y()),
// });
// if Some(item_ix) == widest_completion_ix {
// Some(
// label
// .contained()
// .with_style(style.autocomplete.inline_docs_container)
// .into_any(),
// )
// } else {
// Some(label.flex_float().into_any())
// }
// })())
// .into_any()
// } else {
// completion_label.into_any()
// }
// .contained()
// .with_style(item_style)
// .constrained()
// .dynamically(move |constraint, _, _| {
// if Some(item_ix) == widest_completion_ix {
// constraint
// } else {
// gpui::SizeConstraint {
// min: constraint.min,
// max: constraint.min,
// }
// }
// })
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_down(MouseButton::Left, move |_, this, cx| {
// this.confirm_completion(
// &ConfirmCompletion {
// item_ix: Some(item_ix),
// },
// cx,
// )
// .map(|task| task.detach());
// })
// .constrained()
//
div() div()
.id(mat.candidate_id) .id(mat.candidate_id)
.min_w(px(300.))
.max_w(px(700.))
.whitespace_nowrap() .whitespace_nowrap()
.overflow_hidden() .overflow_hidden()
.bg(gpui::green()) .bg(gpui::green())
.hover(|style| style.bg(gpui::blue())) .hover(|style| style.bg(gpui::blue()))
.when(item_ix == selected_item, |div| div.bg(gpui::red())) .when(item_ix == selected_item, |div| div.bg(gpui::red()))
.child( .on_mouse_down(
StyledText::new(completion.label.text.clone()) MouseButton::Left,
.with_runs(completion_runs), cx.listener(move |editor, event, cx| {
cx.stop_propagation();
editor
.confirm_completion(
&ConfirmCompletion {
item_ix: Some(item_ix),
},
cx,
)
.map(|task| task.detach_and_log_err(cx));
}),
) )
.min_w(px(300.)) .child(completion_label)
.max_w(px(700.)) .children(documentation_label)
}) })
.collect() .collect()
}, },
@ -3698,135 +3643,135 @@ impl Editor {
self.completion_tasks.push((id, task)); self.completion_tasks.push((id, task));
} }
// pub fn confirm_completion( pub fn confirm_completion(
// &mut self, &mut self,
// action: &ConfirmCompletion, action: &ConfirmCompletion,
// cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
// ) -> Option<Task<Result<()>>> { ) -> Option<Task<Result<()>>> {
// use language::ToOffset as _; use language::ToOffset as _;
// let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? { let completions_menu = if let ContextMenu::Completions(menu) = self.hide_context_menu(cx)? {
// menu menu
// } else { } else {
// return None; return None;
// }; };
// let mat = completions_menu let mat = completions_menu
// .matches .matches
// .get(action.item_ix.unwrap_or(completions_menu.selected_item))?; .get(action.item_ix.unwrap_or(completions_menu.selected_item))?;
// let buffer_handle = completions_menu.buffer; let buffer_handle = completions_menu.buffer;
// let completions = completions_menu.completions.read(); let completions = completions_menu.completions.read();
// let completion = completions.get(mat.candidate_id)?; let completion = completions.get(mat.candidate_id)?;
// let snippet; let snippet;
// let text; let text;
// if completion.is_snippet() { if completion.is_snippet() {
// snippet = Some(Snippet::parse(&completion.new_text).log_err()?); snippet = Some(Snippet::parse(&completion.new_text).log_err()?);
// text = snippet.as_ref().unwrap().text.clone(); text = snippet.as_ref().unwrap().text.clone();
// } else { } else {
// snippet = None; snippet = None;
// text = completion.new_text.clone(); text = completion.new_text.clone();
// }; };
// let selections = self.selections.all::<usize>(cx); let selections = self.selections.all::<usize>(cx);
// let buffer = buffer_handle.read(cx); let buffer = buffer_handle.read(cx);
// let old_range = completion.old_range.to_offset(buffer); let old_range = completion.old_range.to_offset(buffer);
// let old_text = buffer.text_for_range(old_range.clone()).collect::<String>(); let old_text = buffer.text_for_range(old_range.clone()).collect::<String>();
// let newest_selection = self.selections.newest_anchor(); let newest_selection = self.selections.newest_anchor();
// if newest_selection.start.buffer_id != Some(buffer_handle.read(cx).remote_id()) { if newest_selection.start.buffer_id != Some(buffer_handle.read(cx).remote_id()) {
// return None; return None;
// } }
// let lookbehind = newest_selection let lookbehind = newest_selection
// .start .start
// .text_anchor .text_anchor
// .to_offset(buffer) .to_offset(buffer)
// .saturating_sub(old_range.start); .saturating_sub(old_range.start);
// let lookahead = old_range let lookahead = old_range
// .end .end
// .saturating_sub(newest_selection.end.text_anchor.to_offset(buffer)); .saturating_sub(newest_selection.end.text_anchor.to_offset(buffer));
// let mut common_prefix_len = old_text let mut common_prefix_len = old_text
// .bytes() .bytes()
// .zip(text.bytes()) .zip(text.bytes())
// .take_while(|(a, b)| a == b) .take_while(|(a, b)| a == b)
// .count(); .count();
// let snapshot = self.buffer.read(cx).snapshot(cx); let snapshot = self.buffer.read(cx).snapshot(cx);
// let mut range_to_replace: Option<Range<isize>> = None; let mut range_to_replace: Option<Range<isize>> = None;
// let mut ranges = Vec::new(); let mut ranges = Vec::new();
// for selection in &selections { for selection in &selections {
// if snapshot.contains_str_at(selection.start.saturating_sub(lookbehind), &old_text) { if snapshot.contains_str_at(selection.start.saturating_sub(lookbehind), &old_text) {
// let start = selection.start.saturating_sub(lookbehind); let start = selection.start.saturating_sub(lookbehind);
// let end = selection.end + lookahead; let end = selection.end + lookahead;
// if selection.id == newest_selection.id { if selection.id == newest_selection.id {
// range_to_replace = Some( range_to_replace = Some(
// ((start + common_prefix_len) as isize - selection.start as isize) ((start + common_prefix_len) as isize - selection.start as isize)
// ..(end as isize - selection.start as isize), ..(end as isize - selection.start as isize),
// ); );
// } }
// ranges.push(start + common_prefix_len..end); ranges.push(start + common_prefix_len..end);
// } else { } else {
// common_prefix_len = 0; common_prefix_len = 0;
// ranges.clear(); ranges.clear();
// ranges.extend(selections.iter().map(|s| { ranges.extend(selections.iter().map(|s| {
// if s.id == newest_selection.id { if s.id == newest_selection.id {
// range_to_replace = Some( range_to_replace = Some(
// old_range.start.to_offset_utf16(&snapshot).0 as isize old_range.start.to_offset_utf16(&snapshot).0 as isize
// - selection.start as isize - selection.start as isize
// ..old_range.end.to_offset_utf16(&snapshot).0 as isize ..old_range.end.to_offset_utf16(&snapshot).0 as isize
// - selection.start as isize, - selection.start as isize,
// ); );
// old_range.clone() old_range.clone()
// } else { } else {
// s.start..s.end s.start..s.end
// } }
// })); }));
// break; break;
// } }
// } }
// let text = &text[common_prefix_len..]; let text = &text[common_prefix_len..];
// cx.emit(Event::InputHandled { cx.emit(EditorEvent::InputHandled {
// utf16_range_to_replace: range_to_replace, utf16_range_to_replace: range_to_replace,
// text: text.into(), text: text.into(),
// }); });
// self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
// if let Some(mut snippet) = snippet { if let Some(mut snippet) = snippet {
// snippet.text = text.to_string(); snippet.text = text.to_string();
// for tabstop in snippet.tabstops.iter_mut().flatten() { for tabstop in snippet.tabstops.iter_mut().flatten() {
// tabstop.start -= common_prefix_len as isize; tabstop.start -= common_prefix_len as isize;
// tabstop.end -= common_prefix_len as isize; tabstop.end -= common_prefix_len as isize;
// } }
// this.insert_snippet(&ranges, snippet, cx).log_err(); this.insert_snippet(&ranges, snippet, cx).log_err();
// } else { } else {
// this.buffer.update(cx, |buffer, cx| { this.buffer.update(cx, |buffer, cx| {
// buffer.edit( buffer.edit(
// ranges.iter().map(|range| (range.clone(), text)), ranges.iter().map(|range| (range.clone(), text)),
// this.autoindent_mode.clone(), this.autoindent_mode.clone(),
// cx, cx,
// ); );
// }); });
// } }
// this.refresh_copilot_suggestions(true, cx); this.refresh_copilot_suggestions(true, cx);
// }); });
// let project = self.project.clone()?; let project = self.project.clone()?;
// let apply_edits = project.update(cx, |project, cx| { let apply_edits = project.update(cx, |project, cx| {
// project.apply_additional_edits_for_completion( project.apply_additional_edits_for_completion(
// buffer_handle, buffer_handle,
// completion.clone(), completion.clone(),
// true, true,
// cx, cx,
// ) )
// }); });
// Some(cx.foreground().spawn(async move { Some(cx.foreground_executor().spawn(async move {
// apply_edits.await?; apply_edits.await?;
// Ok(()) Ok(())
// })) }))
// } }
pub fn toggle_code_actions(&mut self, action: &ToggleCodeActions, cx: &mut ViewContext<Self>) { pub fn toggle_code_actions(&mut self, action: &ToggleCodeActions, cx: &mut ViewContext<Self>) {
let mut context_menu = self.context_menu.write(); let mut context_menu = self.context_menu.write();

View file

@ -268,7 +268,11 @@ impl EditorElement {
}); });
register_action(view, cx, Editor::restart_language_server); register_action(view, cx, Editor::restart_language_server);
register_action(view, cx, Editor::show_character_palette); register_action(view, cx, Editor::show_character_palette);
// on_action(cx, Editor::confirm_completion); todo!() register_action(view, cx, |editor, action, cx| {
editor
.confirm_completion(action, cx)
.map(|task| task.detach_and_log_err(cx));
});
register_action(view, cx, |editor, action, cx| { register_action(view, cx, |editor, action, cx| {
editor editor
.confirm_code_action(action, cx) .confirm_code_action(action, cx)