rustdoc_command.rs

  1use std::path::Path;
  2use std::sync::atomic::AtomicBool;
  3use std::sync::Arc;
  4
  5use anyhow::{anyhow, bail, Context, Result};
  6use assistant_slash_command::{SlashCommand, SlashCommandOutput, SlashCommandOutputSection};
  7use fs::Fs;
  8use futures::AsyncReadExt;
  9use gpui::{AppContext, Model, Task, WeakView};
 10use html_to_markdown::convert_rustdoc_to_markdown;
 11use http::{AsyncBody, HttpClient, HttpClientWithUrl};
 12use language::LspAdapterDelegate;
 13use project::{Project, ProjectPath};
 14use ui::{prelude::*, ButtonLike, ElevationIndex};
 15use workspace::Workspace;
 16
 17#[derive(Debug, Clone, Copy)]
 18enum RustdocSource {
 19    /// The docs were sourced from local `cargo doc` output.
 20    Local,
 21    /// The docs were sourced from `docs.rs`.
 22    DocsDotRs,
 23}
 24
 25pub(crate) struct RustdocSlashCommand;
 26
 27impl RustdocSlashCommand {
 28    async fn build_message(
 29        fs: Arc<dyn Fs>,
 30        http_client: Arc<HttpClientWithUrl>,
 31        crate_name: String,
 32        module_path: Vec<String>,
 33        path_to_cargo_toml: Option<&Path>,
 34    ) -> Result<(RustdocSource, String)> {
 35        let cargo_workspace_root = path_to_cargo_toml.and_then(|path| path.parent());
 36        if let Some(cargo_workspace_root) = cargo_workspace_root {
 37            let mut local_cargo_doc_path = cargo_workspace_root.join("target/doc");
 38            local_cargo_doc_path.push(&crate_name);
 39            if !module_path.is_empty() {
 40                local_cargo_doc_path.push(module_path.join("/"));
 41            }
 42            local_cargo_doc_path.push("index.html");
 43
 44            if let Ok(contents) = fs.load(&local_cargo_doc_path).await {
 45                let (markdown, _items) = convert_rustdoc_to_markdown(contents.as_bytes())?;
 46
 47                return Ok((RustdocSource::Local, markdown));
 48            }
 49        }
 50
 51        let version = "latest";
 52        let path = format!(
 53            "{crate_name}/{version}/{crate_name}/{module_path}",
 54            module_path = module_path.join("/")
 55        );
 56
 57        let mut response = http_client
 58            .get(
 59                &format!("https://docs.rs/{path}"),
 60                AsyncBody::default(),
 61                true,
 62            )
 63            .await?;
 64
 65        let mut body = Vec::new();
 66        response
 67            .body_mut()
 68            .read_to_end(&mut body)
 69            .await
 70            .context("error reading docs.rs response body")?;
 71
 72        if response.status().is_client_error() {
 73            let text = String::from_utf8_lossy(body.as_slice());
 74            bail!(
 75                "status error {}, response: {text:?}",
 76                response.status().as_u16()
 77            );
 78        }
 79
 80        let (markdown, _items) = convert_rustdoc_to_markdown(&body[..])?;
 81
 82        Ok((RustdocSource::DocsDotRs, markdown))
 83    }
 84
 85    fn path_to_cargo_toml(project: Model<Project>, cx: &mut AppContext) -> Option<Arc<Path>> {
 86        let worktree = project.read(cx).worktrees().next()?;
 87        let worktree = worktree.read(cx);
 88        let entry = worktree.entry_for_path("Cargo.toml")?;
 89        let path = ProjectPath {
 90            worktree_id: worktree.id(),
 91            path: entry.path.clone(),
 92        };
 93        Some(Arc::from(
 94            project.read(cx).absolute_path(&path, cx)?.as_path(),
 95        ))
 96    }
 97}
 98
 99impl SlashCommand for RustdocSlashCommand {
100    fn name(&self) -> String {
101        "rustdoc".into()
102    }
103
104    fn description(&self) -> String {
105        "insert Rust docs".into()
106    }
107
108    fn menu_text(&self) -> String {
109        "Insert Rust Documentation".into()
110    }
111
112    fn requires_argument(&self) -> bool {
113        true
114    }
115
116    fn complete_argument(
117        &self,
118        _query: String,
119        _cancel: Arc<AtomicBool>,
120        _workspace: Option<WeakView<Workspace>>,
121        _cx: &mut AppContext,
122    ) -> Task<Result<Vec<String>>> {
123        Task::ready(Ok(Vec::new()))
124    }
125
126    fn run(
127        self: Arc<Self>,
128        argument: Option<&str>,
129        workspace: WeakView<Workspace>,
130        _delegate: Arc<dyn LspAdapterDelegate>,
131        cx: &mut WindowContext,
132    ) -> Task<Result<SlashCommandOutput>> {
133        let Some(argument) = argument else {
134            return Task::ready(Err(anyhow!("missing crate name")));
135        };
136        let Some(workspace) = workspace.upgrade() else {
137            return Task::ready(Err(anyhow!("workspace was dropped")));
138        };
139
140        let project = workspace.read(cx).project().clone();
141        let fs = project.read(cx).fs().clone();
142        let http_client = workspace.read(cx).client().http_client();
143        let mut path_components = argument.split("::");
144        let crate_name = match path_components
145            .next()
146            .ok_or_else(|| anyhow!("missing crate name"))
147        {
148            Ok(crate_name) => crate_name.to_string(),
149            Err(err) => return Task::ready(Err(err)),
150        };
151        let module_path = path_components.map(ToString::to_string).collect::<Vec<_>>();
152        let path_to_cargo_toml = Self::path_to_cargo_toml(project, cx);
153
154        let text = cx.background_executor().spawn({
155            let crate_name = crate_name.clone();
156            let module_path = module_path.clone();
157            async move {
158                Self::build_message(
159                    fs,
160                    http_client,
161                    crate_name,
162                    module_path,
163                    path_to_cargo_toml.as_deref(),
164                )
165                .await
166            }
167        });
168
169        let crate_name = SharedString::from(crate_name);
170        let module_path = if module_path.is_empty() {
171            None
172        } else {
173            Some(SharedString::from(module_path.join("::")))
174        };
175        cx.foreground_executor().spawn(async move {
176            let (source, text) = text.await?;
177            let range = 0..text.len();
178            Ok(SlashCommandOutput {
179                text,
180                sections: vec![SlashCommandOutputSection {
181                    range,
182                    render_placeholder: Arc::new(move |id, unfold, _cx| {
183                        RustdocPlaceholder {
184                            id,
185                            unfold,
186                            source,
187                            crate_name: crate_name.clone(),
188                            module_path: module_path.clone(),
189                        }
190                        .into_any_element()
191                    }),
192                }],
193                run_commands_in_text: false,
194            })
195        })
196    }
197}
198
199#[derive(IntoElement)]
200struct RustdocPlaceholder {
201    pub id: ElementId,
202    pub unfold: Arc<dyn Fn(&mut WindowContext)>,
203    pub source: RustdocSource,
204    pub crate_name: SharedString,
205    pub module_path: Option<SharedString>,
206}
207
208impl RenderOnce for RustdocPlaceholder {
209    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
210        let unfold = self.unfold;
211
212        let crate_path = self
213            .module_path
214            .map(|module_path| format!("{crate_name}::{module_path}", crate_name = self.crate_name))
215            .unwrap_or(self.crate_name.to_string());
216
217        ButtonLike::new(self.id)
218            .style(ButtonStyle::Filled)
219            .layer(ElevationIndex::ElevatedSurface)
220            .child(Icon::new(IconName::FileRust))
221            .child(Label::new(format!(
222                "rustdoc ({source}): {crate_path}",
223                source = match self.source {
224                    RustdocSource::Local => "local",
225                    RustdocSource::DocsDotRs => "docs.rs",
226                }
227            )))
228            .on_click(move |_, cx| unfold(cx))
229    }
230}