From 929a967a77e756aed47d58b6b391b3bb56953eff Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Sun, 30 Aug 2020 20:02:29 +1200 Subject: [PATCH] WIP: Command to open docs under cursor --- crates/hir/src/doc_links.rs | 31 ++++++++++++++++ crates/hir/src/lib.rs | 2 +- crates/ide/src/lib.rs | 8 +++++ crates/ide/src/link_rewrite.rs | 52 +++++++++++++++++++++++++-- crates/rust-analyzer/src/handlers.rs | 15 +++++++- crates/rust-analyzer/src/lsp_ext.rs | 28 +++++++++++++++ crates/rust-analyzer/src/main_loop.rs | 1 + editors/code/package.json | 9 +++++ editors/code/src/commands.ts | 25 +++++++++++-- editors/code/src/lsp_ext.ts | 11 ++++++ editors/code/src/main.ts | 1 + 11 files changed, 176 insertions(+), 7 deletions(-) diff --git a/crates/hir/src/doc_links.rs b/crates/hir/src/doc_links.rs index ddaffbec2554..309b21405e0d 100644 --- a/crates/hir/src/doc_links.rs +++ b/crates/hir/src/doc_links.rs @@ -20,6 +20,37 @@ pub fn resolve_doc_link( resolve_doc_link_impl(db, &resolver, module_def, link_text, link_target) } +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 resolve_doc_link_impl( db: &dyn HirDatabase, resolver: &Resolver, diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 03915ea1bc4f..7fa48b95c68d 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -39,7 +39,7 @@ pub use crate::{ GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, }, - doc_links::resolve_doc_link, + doc_links::{get_doc_link, resolve_doc_link}, has_source::HasSource, semantics::{original_range, PathResolution, Semantics, SemanticsScope}, }; diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 570790384ee5..0536236e2ad1 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -372,6 +372,14 @@ impl Analysis { self.with_db(|db| hover::hover(db, position)) } + /// 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 ff3200eefb4d..9496f2d9b00d 100644 --- a/crates/ide/src/link_rewrite.rs +++ b/crates/ide/src/link_rewrite.rs @@ -5,8 +5,15 @@ use pulldown_cmark::{CowStr, Event, Options, Parser, Tag}; use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions}; -use hir::resolve_doc_link; -use ide_db::{defs::Definition, RootDatabase}; +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 { @@ -48,7 +55,34 @@ pub fn rewrite_links(db: &RootDatabase, markdown: &str, definition: &Definition) out } -// Rewrites a markdown document, resolving links using `callback` and additionally striping prefixes/suffixes on link titles. +// FIXME: This should either be moved, or the module should be renamed. +/// Retrieve a link to documentation for the given symbol. +pub fn get_doc_url(db: &RootDatabase, position: &FilePosition) -> 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), @@ -79,3 +113,15 @@ fn map_links<'e>( _ => evt, }) } + +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 33e60b500a16..e9d1a88e6858 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -33,7 +33,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, }; @@ -1235,6 +1235,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 e1a28b1b4bad..0a44bfce6fe1 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -337,3 +337,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 355caaee200b..d3cf26f91145 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -380,6 +380,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)? diff --git a/editors/code/package.json b/editors/code/package.json index f079f73b8009..3638a18dee6b 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -177,6 +177,11 @@ "command": "rust-analyzer.toggleInlayHints", "title": "Toggle inlay hints", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.openDocs", + "title": "Open docs under cursor", + "category": "Rust Analyzer" } ], "keybindings": [ @@ -909,6 +914,10 @@ { "command": "rust-analyzer.toggleInlayHints", "when": "inRustProject" + }, + { + "command": "rust-analyzer.openDocs", + "when": "inRustProject" } ] } diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts index d0faf4745a2c..e604f5205785 100644 --- a/editors/code/src/commands.ts +++ b/editors/code/src/commands.ts @@ -414,10 +414,31 @@ export function gotoLocation(ctx: Ctx): Cmd { }; } +export function openDocs(ctx: Ctx): Cmd { + return async () => { + console.log("running openDocs"); + + const client = ctx.client; + const editor = vscode.window.activeTextEditor; + if (!editor || !client) { + console.log("not yet ready"); + return + }; + + const position = editor.selection.active; + const textDocument = { uri: editor.document.uri.toString() }; + + const doclink = await client.sendRequest(ra.openDocs, { position, textDocument }); + + vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(doclink.remote)); + }; + +} + export function resolveCodeAction(ctx: Ctx): Cmd { const client = ctx.client; - return async (params: ra.ResolveCodeActionParams) => { - const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params); + return async () => { + const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, null); if (!item) { return; } diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 8663737a6849..1e4f250ebea6 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -113,3 +113,14 @@ export interface CommandLinkGroup { title?: string; commands: CommandLink[]; } + +export interface DocumentationLink { + remote: string; +} + +export interface OpenDocsParams { + textDocument: lc.TextDocumentIdentifier; + position: lc.Position; +} + +export const openDocs = new lc.RequestType('rust-analyzer/openDocs'); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index bd99d696ad86..a694a7c6316b 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -106,6 +106,7 @@ async function tryActivate(context: vscode.ExtensionContext) { ctx.registerCommand('run', commands.run); ctx.registerCommand('debug', commands.debug); ctx.registerCommand('newDebugConfig', commands.newDebugConfig); + ctx.registerCommand('openDocs', commands.openDocs); defaultOnEnter.dispose(); ctx.registerCommand('onEnter', commands.onEnter);