assistant: Add /rustdoc slash command (#12453)

This PR adds a `/rustdoc` slash command for retrieving and inserting
rustdoc docs into the Assistant.

Right now the command accepts the crate name as an argument and will
return the top-level docs from `docs.rs`.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-05-29 18:14:29 -04:00 committed by GitHub
parent dd328efaa7
commit 08881828ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 152 additions and 21 deletions

1
Cargo.lock generated
View File

@ -366,6 +366,7 @@ dependencies = [
"rand 0.8.5",
"regex",
"rope",
"rustdoc_to_markdown",
"schemars",
"search",
"semantic_index",

View File

@ -39,6 +39,7 @@ parking_lot.workspace = true
project.workspace = true
regex.workspace = true
rope.workspace = true
rustdoc_to_markdown.workspace = true
schemars.workspace = true
search.workspace = true
semantic_index.workspace = true

View File

@ -1,5 +1,5 @@
use crate::prompts::{generate_content_prompt, PromptLibrary, PromptManager};
use crate::slash_command::{search_command, tabs_command};
use crate::slash_command::{rustdoc_command, search_command, tabs_command};
use crate::{
assistant_settings::{AssistantDockPosition, AssistantSettings, ZedDotDevModel},
codegen::{self, Codegen, CodegenKind},
@ -210,6 +210,7 @@ impl AssistantPanel {
slash_command_registry.register_command(tabs_command::TabsSlashCommand);
slash_command_registry.register_command(project_command::ProjectSlashCommand);
slash_command_registry.register_command(search_command::SearchSlashCommand);
slash_command_registry.register_command(rustdoc_command::RustdocSlashCommand);
Self {
workspace: workspace_handle,

View File

@ -20,6 +20,7 @@ pub mod active_command;
pub mod file_command;
pub mod project_command;
pub mod prompt_command;
pub mod rustdoc_command;
pub mod search_command;
pub mod tabs_command;

View File

@ -0,0 +1,137 @@
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use anyhow::{anyhow, bail, Context, Result};
use assistant_slash_command::{SlashCommand, SlashCommandOutput, SlashCommandOutputSection};
use futures::AsyncReadExt;
use gpui::{AppContext, Task, WeakView};
use http::{AsyncBody, HttpClient, HttpClientWithUrl};
use language::LspAdapterDelegate;
use rustdoc_to_markdown::convert_rustdoc_to_markdown;
use ui::{prelude::*, ButtonLike, ElevationIndex};
use workspace::Workspace;
pub(crate) struct RustdocSlashCommand;
impl RustdocSlashCommand {
async fn build_message(
http_client: Arc<HttpClientWithUrl>,
crate_name: String,
) -> Result<String> {
let mut response = http_client
.get(
&format!("https://docs.rs/{crate_name}"),
AsyncBody::default(),
true,
)
.await?;
let mut body = Vec::new();
response
.body_mut()
.read_to_end(&mut body)
.await
.context("error reading docs.rs response body")?;
if response.status().is_client_error() {
let text = String::from_utf8_lossy(body.as_slice());
bail!(
"status error {}, response: {text:?}",
response.status().as_u16()
);
}
convert_rustdoc_to_markdown(&body[..])
}
}
impl SlashCommand for RustdocSlashCommand {
fn name(&self) -> String {
"rustdoc".into()
}
fn description(&self) -> String {
"insert the docs for a Rust crate".into()
}
fn tooltip_text(&self) -> String {
"insert rustdoc".into()
}
fn requires_argument(&self) -> bool {
true
}
fn complete_argument(
&self,
_query: String,
_cancel: Arc<AtomicBool>,
_workspace: WeakView<Workspace>,
_cx: &mut AppContext,
) -> Task<Result<Vec<String>>> {
Task::ready(Ok(Vec::new()))
}
fn run(
self: Arc<Self>,
argument: Option<&str>,
workspace: WeakView<Workspace>,
_delegate: Arc<dyn LspAdapterDelegate>,
cx: &mut WindowContext,
) -> Task<Result<SlashCommandOutput>> {
let Some(argument) = argument else {
return Task::ready(Err(anyhow!("missing crate name")));
};
let Some(workspace) = workspace.upgrade() else {
return Task::ready(Err(anyhow!("workspace was dropped")));
};
let http_client = workspace.read(cx).client().http_client();
let crate_name = argument.to_string();
let text = cx.background_executor().spawn({
let crate_name = crate_name.clone();
async move { Self::build_message(http_client, crate_name).await }
});
let crate_name = SharedString::from(crate_name);
cx.foreground_executor().spawn(async move {
let text = text.await?;
let range = 0..text.len();
Ok(SlashCommandOutput {
text,
sections: vec![SlashCommandOutputSection {
range,
render_placeholder: Arc::new(move |id, unfold, _cx| {
RustdocPlaceholder {
id,
unfold,
crate_name: crate_name.clone(),
}
.into_any_element()
}),
}],
})
})
}
}
#[derive(IntoElement)]
struct RustdocPlaceholder {
pub id: ElementId,
pub unfold: Arc<dyn Fn(&mut WindowContext)>,
pub crate_name: SharedString,
}
impl RenderOnce for RustdocPlaceholder {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
let unfold = self.unfold;
ButtonLike::new(self.id)
.style(ButtonStyle::Filled)
.layer(ElevationIndex::ElevatedSurface)
.child(Icon::new(IconName::FileRust))
.child(Label::new(format!("rustdoc: {}", self.crate_name)))
.on_click(move |_, cx| unfold(cx))
}
}

View File

@ -23,7 +23,7 @@ pub fn main() {
// ```
// let html = include_str!("/path/to/zed/target/doc/gpui/index.html");
// ```
let markdown = convert_rustdoc_to_markdown(html).unwrap();
let markdown = convert_rustdoc_to_markdown(html.as_bytes()).unwrap();
println!("{markdown}");
}

View File

@ -36,12 +36,6 @@ impl MarkdownWriter {
.any(|parent_element| parent_element.tag == tag)
}
fn is_inside_heading(&self) -> bool {
["h1", "h2", "h3", "h4", "h5", "h6"]
.into_iter()
.any(|heading| self.is_inside(heading))
}
/// Appends the given string slice onto the end of the Markdown output.
fn push_str(&mut self, str: &str) {
self.markdown.push_str(str);
@ -135,16 +129,14 @@ impl MarkdownWriter {
}
}
"div" | "span" => {
if tag.attrs.borrow().iter().any(|attr| {
attr.name.local.to_string() == "class"
&& attr.value.to_string() == "sidebar-elems"
}) {
return StartTagOutcome::Skip;
}
let classes_to_skip = ["nav-container", "sidebar-elems", "out-of-band"];
if tag.attrs.borrow().iter().any(|attr| {
attr.name.local.to_string() == "class"
&& attr.value.to_string() == "out-of-band"
&& attr
.value
.split(' ')
.any(|class| classes_to_skip.contains(&class.trim()))
}) {
return StartTagOutcome::Skip;
}
@ -189,10 +181,6 @@ impl MarkdownWriter {
return Ok(());
}
if self.is_inside_heading() && self.is_inside("a") {
return Ok(());
}
let trimmed_text = text.trim_matches(|char| char == '\n' || char == '\r' || char == '§');
self.push_str(trimmed_text);

View File

@ -4,6 +4,8 @@
mod markdown_writer;
use std::io::Read;
use anyhow::{Context, Result};
use html5ever::driver::ParseOpts;
use html5ever::parse_document;
@ -14,7 +16,7 @@ use markup5ever_rcdom::RcDom;
use crate::markdown_writer::MarkdownWriter;
/// Converts the provided rustdoc HTML to Markdown.
pub fn convert_rustdoc_to_markdown(html: &str) -> Result<String> {
pub fn convert_rustdoc_to_markdown(mut html: impl Read) -> Result<String> {
let parse_options = ParseOpts {
tree_builder: TreeBuilderOpts {
drop_doctype: true,
@ -24,7 +26,7 @@ pub fn convert_rustdoc_to_markdown(html: &str) -> Result<String> {
};
let dom = parse_document(RcDom::default(), parse_options)
.from_utf8()
.read_from(&mut html.as_bytes())
.read_from(&mut html)
.context("failed to parse rustdoc HTML")?;
let markdown_writer = MarkdownWriter::new();