From bfda0d25834250a3adbcd0d26953a1cdc6662e7f Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Sun, 30 Aug 2020 20:02:29 +1200 Subject: WIP: Command to open docs under cursor --- crates/ide/src/lib.rs | 8 ++++ crates/ide/src/link_rewrite.rs | 82 ++++++++++++++++++++++++++++++++++- crates/rust-analyzer/src/handlers.rs | 15 ++++++- crates/rust-analyzer/src/lsp_ext.rs | 28 ++++++++++++ crates/rust-analyzer/src/main_loop.rs | 1 + 5 files changed, 132 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 57f3581b6..645369597 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -382,6 +382,14 @@ impl Analysis { self.with_db(|db| hover::hover(db, position, links_in_hover, markdown)) } + /// Return URL(s) for the documentation of the symbol under the cursor. + pub fn get_doc_url( + &self, + position: FilePosition, + ) -> Cancelable> { + self.with_db(|db| link_rewrite::get_doc_url(db, &position)) + } + /// Computes parameter information for the given call expression. pub fn call_info(&self, position: FilePosition) -> Cancelable> { self.with_db(|db| call_info::call_info(db, position)) diff --git a/crates/ide/src/link_rewrite.rs b/crates/ide/src/link_rewrite.rs index c317a2379..80005c06e 100644 --- a/crates/ide/src/link_rewrite.rs +++ b/crates/ide/src/link_rewrite.rs @@ -8,6 +8,16 @@ use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag}; use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; use url::Url; +use crate::{FilePosition, Semantics}; +use hir::{get_doc_link, resolve_doc_link}; +use ide_db::{ + defs::{classify_name, classify_name_ref, Definition}, + RootDatabase, +}; +use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; + +pub type DocumentationLink = String; + /// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs) pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) -> String { let doc = Parser::new_with_broken_link_callback( @@ -80,6 +90,37 @@ pub fn remove_links(markdown: &str) -> String { out } +pub fn get_doc_link(db: &dyn HirDatabase, definition: &T) -> Option { + eprintln!("hir::doc_links::get_doc_link"); + let module_def = definition.clone().try_into_module_def()?; + + get_doc_link_impl(db, &module_def) +} + +// TODO: +// BUG: For Option +// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some +// Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html +// +// BUG: For methods +// import_map.path_of(ns) fails, is not designed to resolve methods +fn get_doc_link_impl(db: &dyn HirDatabase, moddef: &ModuleDef) -> Option { + eprintln!("get_doc_link_impl: {:#?}", moddef); + let ns = ItemInNs::Types(moddef.clone().into()); + + let module = moddef.module(db)?; + let krate = module.krate(); + let import_map = db.import_map(krate.into()); + let base = once(krate.display_name(db).unwrap()) + .chain(import_map.path_of(ns).unwrap().segments.iter().map(|name| format!("{}", name))) + .join("/"); + + get_doc_url(db, &krate) + .and_then(|url| url.join(&base).ok()) + .and_then(|url| get_symbol_filename(db, &moddef).as_deref().and_then(|f| url.join(f).ok())) + .map(|url| url.into_string()) +} + fn rewrite_intra_doc_link( db: &RootDatabase, def: Definition, @@ -138,7 +179,34 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option Option { + let sema = Semantics::new(db); + let file = sema.parse(position.file_id).syntax().clone(); + let token = pick_best(file.token_at_offset(position.offset))?; + let token = sema.descend_into_macros(token); + + let node = token.parent(); + let definition = match_ast! { + match node { + ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)), + ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)), + _ => None, + } + }; + + match definition? { + Definition::Macro(t) => get_doc_link(db, &t), + Definition::Field(t) => get_doc_link(db, &t), + Definition::ModuleDef(t) => get_doc_link(db, &t), + Definition::SelfType(t) => get_doc_link(db, &t), + Definition::Local(t) => get_doc_link(db, &t), + Definition::TypeParam(t) => get_doc_link(db, &t), + } +} + +/// Rewrites a markdown document, applying 'callback' to each link. fn map_links<'e>( events: impl Iterator>, callback: impl Fn(&str, &str) -> (String, String), @@ -275,3 +343,15 @@ fn get_symbol_filename(db: &RootDatabase, definition: &ModuleDef) -> Option format!("static.{}.html", s.name(db)?), }) } + +fn pick_best(tokens: TokenAtOffset) -> Option { + return tokens.max_by_key(priority); + fn priority(n: &SyntaxToken) -> usize { + match n.kind() { + IDENT | INT_NUMBER => 3, + T!['('] | T![')'] => 2, + kind if kind.is_trivia() => 0, + _ => 1, + } + } +} diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 468655f9c..ec8c8fecd 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -34,7 +34,7 @@ use crate::{ config::RustfmtConfig, from_json, from_proto, global_state::{GlobalState, GlobalStateSnapshot}, - lsp_ext::{self, InlayHint, InlayHintsParams}, + lsp_ext::{self, DocumentationLink, InlayHint, InlayHintsParams, OpenDocsParams}, to_proto, LspError, Result, }; @@ -1310,6 +1310,19 @@ pub(crate) fn handle_semantic_tokens_range( Ok(Some(semantic_tokens.into())) } +pub(crate) fn handle_open_docs( + snap: GlobalStateSnapshot, + params: OpenDocsParams, +) -> Result { + let _p = profile::span("handle_open_docs"); + let position = from_proto::file_position(&snap, params.position)?; + + // FIXME: Propogate or ignore this error instead of panicking. + let remote = snap.analysis.get_doc_url(position)?.unwrap(); + + Ok(DocumentationLink { remote }) +} + fn implementation_title(count: usize) -> String { if count == 1 { "1 implementation".into() diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index fee0bb69c..83a20802f 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -347,3 +347,31 @@ pub struct CommandLink { #[serde(skip_serializing_if = "Option::is_none")] pub tooltip: Option, } + +pub enum OpenDocs {} + +impl Request for OpenDocs { + type Params = OpenDocsParams; + type Result = DocumentationLink; + const METHOD: &'static str = "rust-analyzer/openDocs"; +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct OpenDocsParams { + // TODO: I don't know the difference between these two methods of passing position. + #[serde(flatten)] + pub position: lsp_types::TextDocumentPositionParams, + // pub textDocument: lsp_types::TextDocumentIdentifier, + // pub position: lsp_types::Position, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DocumentationLink { + pub remote: String, // TODO: Better API? + // #[serde(skip_serializing_if = "Option::is_none")] + // pub remote: Option, + // #[serde(skip_serializing_if = "Option::is_none")] + // pub local: Option +} diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 4b7ac8224..75f85011d 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -384,6 +384,7 @@ impl GlobalState { .on::(handlers::handle_code_action)? .on::(handlers::handle_resolve_code_action)? .on::(handlers::handle_hover)? + .on::(handlers::handle_open_docs)? .on::(handlers::handle_on_type_formatting)? .on::(handlers::handle_document_symbol)? .on::(handlers::handle_workspace_symbol)? -- cgit v1.2.3