Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
assistant: Add `/rustdoc` slash command (#12453)
Marshall Bowers08881828ce61
diff --git a/Cargo.lock b/Cargo.lockindex 809a7f59c05..f91ce427606 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -366,6 +366,7 @@ dependencies = ["rand 0.8.5","regex","rope",+ "rustdoc_to_markdown","schemars","search","semantic_index",diff --git a/crates/assistant/Cargo.toml b/crates/assistant/Cargo.tomlindex 6e41f514f94..939fd682733 100644--- a/crates/assistant/Cargo.toml+++ b/crates/assistant/Cargo.toml@@ -39,6 +39,7 @@ parking_lot.workspace = trueproject.workspace = trueregex.workspace = truerope.workspace = true+rustdoc_to_markdown.workspace = trueschemars.workspace = truesearch.workspace = truesemantic_index.workspace = truediff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rsindex 0cb202ecc6e..591f4d8b11c 100644--- a/crates/assistant/src/assistant_panel.rs+++ b/crates/assistant/src/assistant_panel.rs@@ -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,diff --git a/crates/assistant/src/slash_command.rs b/crates/assistant/src/slash_command.rsindex 5f975a89640..ce99ca141a0 100644--- a/crates/assistant/src/slash_command.rs+++ b/crates/assistant/src/slash_command.rs@@ -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;diff --git a/crates/assistant/src/slash_command/rustdoc_command.rs b/crates/assistant/src/slash_command/rustdoc_command.rsnew file mode 100644index 00000000000..c8e03bcbe4f--- /dev/null+++ b/crates/assistant/src/slash_command/rustdoc_command.rs@@ -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))+ }+}diff --git a/crates/rustdoc_to_markdown/examples/test.rs b/crates/rustdoc_to_markdown/examples/test.rsindex 3df85b2b1ff..38a85874dfd 100644--- a/crates/rustdoc_to_markdown/examples/test.rs+++ b/crates/rustdoc_to_markdown/examples/test.rs@@ -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}");}diff --git a/crates/rustdoc_to_markdown/src/markdown_writer.rs b/crates/rustdoc_to_markdown/src/markdown_writer.rsindex 59aa7e1b379..4b69594bc8e 100644--- a/crates/rustdoc_to_markdown/src/markdown_writer.rs+++ b/crates/rustdoc_to_markdown/src/markdown_writer.rs@@ -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);diff --git a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rsindex d3afe2a264a..dbbafa2c5c1 100644--- a/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs+++ b/crates/rustdoc_to_markdown/src/rustdoc_to_markdown.rs@@ -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();