Asynchronously request completion documentation if not present

This commit is contained in:
Julia 2023-09-28 14:16:30 -04:00
parent fcaf48eb49
commit fe62423344
2 changed files with 61 additions and 67 deletions

View file

@ -48,9 +48,9 @@ use gpui::{
impl_actions, impl_actions,
keymap_matcher::KeymapContext, keymap_matcher::KeymapContext,
platform::{CursorStyle, MouseButton}, platform::{CursorStyle, MouseButton},
serde_json, AnyElement, AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem, Element, serde_json, AnyElement, AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem,
Entity, ModelHandle, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, CursorRegion, Element, Entity, ModelHandle, MouseRegion, Subscription, Task, View, ViewContext,
WindowContext, ViewHandle, WeakViewHandle, WindowContext,
}; };
use highlight_matching_bracket::refresh_matching_bracket_highlights; use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState}; use hover_popover::{hide_hover, HoverState};
@ -119,6 +119,46 @@ pub const DOCUMENT_HIGHLIGHTS_DEBOUNCE_TIMEOUT: Duration = Duration::from_millis
pub const FORMAT_TIMEOUT: Duration = Duration::from_secs(2); pub const FORMAT_TIMEOUT: Duration = Duration::from_secs(2);
pub fn render_rendered_markdown(
md: &language::RenderedMarkdown,
style: &EditorStyle,
cx: &mut ViewContext<Editor>,
) -> Text {
enum RenderedRenderedMarkdown {}
let md = md.clone();
let code_span_background_color = style.document_highlight_read_background;
let view_id = cx.view_id();
let mut region_id = 0;
Text::new(md.text, style.text.clone())
.with_highlights(md.highlights)
.with_custom_runs(md.region_ranges, move |ix, bounds, scene, _| {
region_id += 1;
let region = md.regions[ix].clone();
if let Some(url) = region.link_url {
scene.push_cursor_region(CursorRegion {
bounds,
style: CursorStyle::PointingHand,
});
scene.push_mouse_region(
MouseRegion::new::<RenderedRenderedMarkdown>(view_id, region_id, bounds)
.on_click::<Editor, _>(MouseButton::Left, move |_, _, cx| {
cx.platform().open_url(&url)
}),
);
}
if region.code {
scene.push_quad(gpui::Quad {
bounds,
background: Some(code_span_background_color),
border: Default::default(),
corner_radii: (2.0).into(),
});
}
})
.with_soft_wrap(true)
}
#[derive(Clone, Deserialize, PartialEq, Default)] #[derive(Clone, Deserialize, PartialEq, Default)]
pub struct SelectNext { pub struct SelectNext {
#[serde(default)] #[serde(default)]
@ -938,11 +978,8 @@ impl CompletionsMenu {
project: Option<&ModelHandle<Project>>, project: Option<&ModelHandle<Project>>,
cx: &mut ViewContext<Editor>, cx: &mut ViewContext<Editor>,
) { ) {
println!("attempt_resolve_selected_completion"); let index = self.matches[self.selected_item].candidate_id;
let index = self.matches[dbg!(self.selected_item)].candidate_id;
dbg!(index);
let Some(project) = project else { let Some(project) = project else {
println!("no project");
return; return;
}; };
@ -950,7 +987,6 @@ impl CompletionsMenu {
let completions_guard = completions.read(); let completions_guard = completions.read();
let completion = &completions_guard[index]; let completion = &completions_guard[index];
if completion.lsp_completion.documentation.is_some() { if completion.lsp_completion.documentation.is_some() {
println!("has existing documentation");
return; return;
} }
@ -959,7 +995,6 @@ impl CompletionsMenu {
drop(completions_guard); drop(completions_guard);
let Some(server) = project.read(cx).language_server_for_id(server_id) else { let Some(server) = project.read(cx).language_server_for_id(server_id) else {
println!("no server");
return; return;
}; };
@ -969,26 +1004,21 @@ impl CompletionsMenu {
.as_ref() .as_ref()
.and_then(|options| options.resolve_provider) .and_then(|options| options.resolve_provider)
.unwrap_or(false); .unwrap_or(false);
if !dbg!(can_resolve) { if !can_resolve {
return; return;
} }
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
println!("in spawn");
let request = server.request::<lsp::request::ResolveCompletionItem>(completion); let request = server.request::<lsp::request::ResolveCompletionItem>(completion);
let Some(completion_item) = request.await.log_err() else { let Some(completion_item) = request.await.log_err() else {
println!("errored");
return; return;
}; };
if completion_item.documentation.is_some() { if completion_item.documentation.is_some() {
println!("got new documentation");
let mut completions = completions.write(); let mut completions = completions.write();
completions[index].lsp_completion.documentation = completion_item.documentation; completions[index].lsp_completion.documentation = completion_item.documentation;
println!("notifying"); drop(completions);
_ = this.update(&mut cx, |_, cx| cx.notify()); _ = this.update(&mut cx, |_, cx| cx.notify());
} else {
println!("did not get anything");
} }
}) })
.detach(); .detach();
@ -1169,12 +1199,16 @@ impl CompletionsMenu {
Some( Some(
Flex::column() Flex::column()
.scrollable::<CompletionDocsMarkdown>(0, None, cx) .scrollable::<CompletionDocsMarkdown>(0, None, cx)
// .with_child(language::markdown::render_markdown( .with_child(render_rendered_markdown(
// &content.value, &language::markdown::render_markdown(
// &registry, &content.value,
// &language, &registry,
// &style, &language,
// )) &style.theme,
),
&style,
cx,
))
.constrained() .constrained()
.with_width(alongside_docs_width) .with_width(alongside_docs_width)
.contained() .contained()

View file

@ -3,57 +3,17 @@ use std::sync::Arc;
use crate::{Language, LanguageRegistry}; use crate::{Language, LanguageRegistry};
use futures::FutureExt; use futures::FutureExt;
use gpui::{ use gpui::fonts::{HighlightStyle, Underline, Weight};
elements::Text,
fonts::{HighlightStyle, Underline, Weight},
platform::{CursorStyle, MouseButton},
CursorRegion, MouseRegion, ViewContext,
};
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag}; use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RenderedMarkdown { pub struct RenderedMarkdown {
text: String, pub text: String,
highlights: Vec<(Range<usize>, HighlightStyle)>, pub highlights: Vec<(Range<usize>, HighlightStyle)>,
region_ranges: Vec<Range<usize>>, pub region_ranges: Vec<Range<usize>>,
regions: Vec<RenderedRegion>, pub regions: Vec<RenderedRegion>,
} }
// impl RenderedMarkdown {
// pub fn render(&self, style: &theme::Editor, cx: &mut ViewContext<Editor>) -> Text {
// let code_span_background_color = style.document_highlight_read_background;
// let view_id = cx.view_id();
// let mut region_id = 0;
// Text::new(text, style.text.clone())
// .with_highlights(highlights)
// .with_custom_runs(region_ranges, move |ix, bounds, scene, _| {
// region_id += 1;
// let region = regions[ix].clone();
// if let Some(url) = region.link_url {
// scene.push_cursor_region(CursorRegion {
// bounds,
// style: CursorStyle::PointingHand,
// });
// scene.push_mouse_region(
// MouseRegion::new::<Editor>(view_id, region_id, bounds)
// .on_click::<Editor, _>(MouseButton::Left, move |_, _, cx| {
// cx.platform().open_url(&url)
// }),
// );
// }
// if region.code {
// scene.push_quad(gpui::Quad {
// bounds,
// background: Some(code_span_background_color),
// border: Default::default(),
// corner_radii: (2.0).into(),
// });
// }
// })
// .with_soft_wrap(true)
// }
// }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RenderedRegion { pub struct RenderedRegion {
pub code: bool, pub code: bool,