Skip to content

Commit

Permalink
WIP: Command to open docs under cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
zacps committed Aug 30, 2020
1 parent f647edc commit 929a967
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 7 deletions.
31 changes: 31 additions & 0 deletions crates/hir/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ pub fn resolve_doc_link<T: Resolvable + Clone>(
resolve_doc_link_impl(db, &resolver, module_def, link_text, link_target)
}

pub fn get_doc_link<T: Resolvable + Clone>(db: &dyn HirDatabase, definition: &T) -> Option<String> {
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<String> {
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,
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down
8 changes: 8 additions & 0 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<link_rewrite::DocumentationLink>> {
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<Option<CallInfo>> {
self.with_db(|db| call_info::call_info(db, position))
Expand Down
52 changes: 49 additions & 3 deletions crates/ide/src/link_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<DocumentationLink> {
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<Item = Event<'e>>,
callback: impl Fn(&str, &str) -> (String, String),
Expand Down Expand Up @@ -79,3 +113,15 @@ fn map_links<'e>(
_ => evt,
})
}

fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
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,
}
}
}
15 changes: 14 additions & 1 deletion crates/rust-analyzer/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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<DocumentationLink> {
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()
Expand Down
28 changes: 28 additions & 0 deletions crates/rust-analyzer/src/lsp_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,31 @@ pub struct CommandLink {
#[serde(skip_serializing_if = "Option::is_none")]
pub tooltip: Option<String>,
}

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<String>,
// #[serde(skip_serializing_if = "Option::is_none")]
// pub local: Option<String>
}
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ impl GlobalState {
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)?
.on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)?
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)?
.on::<lsp_ext::OpenDocs>(handlers::handle_open_docs)?
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)?
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)?
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)?
Expand Down
9 changes: 9 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -909,6 +914,10 @@
{
"command": "rust-analyzer.toggleInlayHints",
"when": "inRustProject"
},
{
"command": "rust-analyzer.openDocs",
"when": "inRustProject"
}
]
}
Expand Down
25 changes: 23 additions & 2 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 11 additions & 0 deletions editors/code/src/lsp_ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenDocsParams, DocumentationLink, void>('rust-analyzer/openDocs');
1 change: 1 addition & 0 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 929a967

Please sign in to comment.