Skip to content

Commit 929a967

Browse files
committed
WIP: Command to open docs under cursor
1 parent f647edc commit 929a967

File tree

11 files changed

+176
-7
lines changed

11 files changed

+176
-7
lines changed

crates/hir/src/doc_links.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ pub fn resolve_doc_link<T: Resolvable + Clone>(
2020
resolve_doc_link_impl(db, &resolver, module_def, link_text, link_target)
2121
}
2222

23+
pub fn get_doc_link<T: Resolvable + Clone>(db: &dyn HirDatabase, definition: &T) -> Option<String> {
24+
eprintln!("hir::doc_links::get_doc_link");
25+
let module_def = definition.clone().try_into_module_def()?;
26+
27+
get_doc_link_impl(db, &module_def)
28+
}
29+
30+
// TODO:
31+
// BUG: For Option
32+
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
33+
// Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html
34+
//
35+
// BUG: For methods
36+
// import_map.path_of(ns) fails, is not designed to resolve methods
37+
fn get_doc_link_impl(db: &dyn HirDatabase, moddef: &ModuleDef) -> Option<String> {
38+
eprintln!("get_doc_link_impl: {:#?}", moddef);
39+
let ns = ItemInNs::Types(moddef.clone().into());
40+
41+
let module = moddef.module(db)?;
42+
let krate = module.krate();
43+
let import_map = db.import_map(krate.into());
44+
let base = once(krate.display_name(db).unwrap())
45+
.chain(import_map.path_of(ns).unwrap().segments.iter().map(|name| format!("{}", name)))
46+
.join("/");
47+
48+
get_doc_url(db, &krate)
49+
.and_then(|url| url.join(&base).ok())
50+
.and_then(|url| get_symbol_filename(db, &moddef).as_deref().and_then(|f| url.join(f).ok()))
51+
.map(|url| url.into_string())
52+
}
53+
2354
fn resolve_doc_link_impl(
2455
db: &dyn HirDatabase,
2556
resolver: &Resolver,

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use crate::{
3939
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
4040
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
4141
},
42-
doc_links::resolve_doc_link,
42+
doc_links::{get_doc_link, resolve_doc_link},
4343
has_source::HasSource,
4444
semantics::{original_range, PathResolution, Semantics, SemanticsScope},
4545
};

crates/ide/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ impl Analysis {
372372
self.with_db(|db| hover::hover(db, position))
373373
}
374374

375+
/// Return URL(s) for the documentation of the symbol under the cursor.
376+
pub fn get_doc_url(
377+
&self,
378+
position: FilePosition,
379+
) -> Cancelable<Option<link_rewrite::DocumentationLink>> {
380+
self.with_db(|db| link_rewrite::get_doc_url(db, &position))
381+
}
382+
375383
/// Computes parameter information for the given call expression.
376384
pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
377385
self.with_db(|db| call_info::call_info(db, position))

crates/ide/src/link_rewrite.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
use pulldown_cmark::{CowStr, Event, Options, Parser, Tag};
66
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
77

8-
use hir::resolve_doc_link;
9-
use ide_db::{defs::Definition, RootDatabase};
8+
use crate::{FilePosition, Semantics};
9+
use hir::{get_doc_link, resolve_doc_link};
10+
use ide_db::{
11+
defs::{classify_name, classify_name_ref, Definition},
12+
RootDatabase,
13+
};
14+
use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T};
15+
16+
pub type DocumentationLink = String;
1017

1118
/// Rewrite documentation links in markdown to point to an online host (e.g. docs.rs)
1219
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)
4855
out
4956
}
5057

51-
// Rewrites a markdown document, resolving links using `callback` and additionally striping prefixes/suffixes on link titles.
58+
// FIXME: This should either be moved, or the module should be renamed.
59+
/// Retrieve a link to documentation for the given symbol.
60+
pub fn get_doc_url(db: &RootDatabase, position: &FilePosition) -> Option<DocumentationLink> {
61+
let sema = Semantics::new(db);
62+
let file = sema.parse(position.file_id).syntax().clone();
63+
let token = pick_best(file.token_at_offset(position.offset))?;
64+
let token = sema.descend_into_macros(token);
65+
66+
let node = token.parent();
67+
let definition = match_ast! {
68+
match node {
69+
ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)),
70+
ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)),
71+
_ => None,
72+
}
73+
};
74+
75+
match definition? {
76+
Definition::Macro(t) => get_doc_link(db, &t),
77+
Definition::Field(t) => get_doc_link(db, &t),
78+
Definition::ModuleDef(t) => get_doc_link(db, &t),
79+
Definition::SelfType(t) => get_doc_link(db, &t),
80+
Definition::Local(t) => get_doc_link(db, &t),
81+
Definition::TypeParam(t) => get_doc_link(db, &t),
82+
}
83+
}
84+
85+
/// Rewrites a markdown document, applying 'callback' to each link.
5286
fn map_links<'e>(
5387
events: impl Iterator<Item = Event<'e>>,
5488
callback: impl Fn(&str, &str) -> (String, String),
@@ -79,3 +113,15 @@ fn map_links<'e>(
79113
_ => evt,
80114
})
81115
}
116+
117+
fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
118+
return tokens.max_by_key(priority);
119+
fn priority(n: &SyntaxToken) -> usize {
120+
match n.kind() {
121+
IDENT | INT_NUMBER => 3,
122+
T!['('] | T![')'] => 2,
123+
kind if kind.is_trivia() => 0,
124+
_ => 1,
125+
}
126+
}
127+
}

crates/rust-analyzer/src/handlers.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
config::RustfmtConfig,
3434
from_json, from_proto,
3535
global_state::{GlobalState, GlobalStateSnapshot},
36-
lsp_ext::{self, InlayHint, InlayHintsParams},
36+
lsp_ext::{self, DocumentationLink, InlayHint, InlayHintsParams, OpenDocsParams},
3737
to_proto, LspError, Result,
3838
};
3939

@@ -1235,6 +1235,19 @@ pub(crate) fn handle_semantic_tokens_range(
12351235
Ok(Some(semantic_tokens.into()))
12361236
}
12371237

1238+
pub(crate) fn handle_open_docs(
1239+
snap: GlobalStateSnapshot,
1240+
params: OpenDocsParams,
1241+
) -> Result<DocumentationLink> {
1242+
let _p = profile::span("handle_open_docs");
1243+
let position = from_proto::file_position(&snap, params.position)?;
1244+
1245+
// FIXME: Propogate or ignore this error instead of panicking.
1246+
let remote = snap.analysis.get_doc_url(position)?.unwrap();
1247+
1248+
Ok(DocumentationLink { remote })
1249+
}
1250+
12381251
fn implementation_title(count: usize) -> String {
12391252
if count == 1 {
12401253
"1 implementation".into()

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,31 @@ pub struct CommandLink {
337337
#[serde(skip_serializing_if = "Option::is_none")]
338338
pub tooltip: Option<String>,
339339
}
340+
341+
pub enum OpenDocs {}
342+
343+
impl Request for OpenDocs {
344+
type Params = OpenDocsParams;
345+
type Result = DocumentationLink;
346+
const METHOD: &'static str = "rust-analyzer/openDocs";
347+
}
348+
349+
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
350+
#[serde(rename_all = "camelCase")]
351+
pub struct OpenDocsParams {
352+
// TODO: I don't know the difference between these two methods of passing position.
353+
#[serde(flatten)]
354+
pub position: lsp_types::TextDocumentPositionParams,
355+
// pub textDocument: lsp_types::TextDocumentIdentifier,
356+
// pub position: lsp_types::Position,
357+
}
358+
359+
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
360+
#[serde(rename_all = "camelCase")]
361+
pub struct DocumentationLink {
362+
pub remote: String, // TODO: Better API?
363+
// #[serde(skip_serializing_if = "Option::is_none")]
364+
// pub remote: Option<String>,
365+
// #[serde(skip_serializing_if = "Option::is_none")]
366+
// pub local: Option<String>
367+
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ impl GlobalState {
380380
.on::<lsp_ext::CodeActionRequest>(handlers::handle_code_action)?
381381
.on::<lsp_ext::ResolveCodeActionRequest>(handlers::handle_resolve_code_action)?
382382
.on::<lsp_ext::HoverRequest>(handlers::handle_hover)?
383+
.on::<lsp_ext::OpenDocs>(handlers::handle_open_docs)?
383384
.on::<lsp_types::request::OnTypeFormatting>(handlers::handle_on_type_formatting)?
384385
.on::<lsp_types::request::DocumentSymbolRequest>(handlers::handle_document_symbol)?
385386
.on::<lsp_types::request::WorkspaceSymbol>(handlers::handle_workspace_symbol)?

editors/code/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@
177177
"command": "rust-analyzer.toggleInlayHints",
178178
"title": "Toggle inlay hints",
179179
"category": "Rust Analyzer"
180+
},
181+
{
182+
"command": "rust-analyzer.openDocs",
183+
"title": "Open docs under cursor",
184+
"category": "Rust Analyzer"
180185
}
181186
],
182187
"keybindings": [
@@ -909,6 +914,10 @@
909914
{
910915
"command": "rust-analyzer.toggleInlayHints",
911916
"when": "inRustProject"
917+
},
918+
{
919+
"command": "rust-analyzer.openDocs",
920+
"when": "inRustProject"
912921
}
913922
]
914923
}

editors/code/src/commands.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,31 @@ export function gotoLocation(ctx: Ctx): Cmd {
414414
};
415415
}
416416

417+
export function openDocs(ctx: Ctx): Cmd {
418+
return async () => {
419+
console.log("running openDocs");
420+
421+
const client = ctx.client;
422+
const editor = vscode.window.activeTextEditor;
423+
if (!editor || !client) {
424+
console.log("not yet ready");
425+
return
426+
};
427+
428+
const position = editor.selection.active;
429+
const textDocument = { uri: editor.document.uri.toString() };
430+
431+
const doclink = await client.sendRequest(ra.openDocs, { position, textDocument });
432+
433+
vscode.commands.executeCommand("vscode.open", vscode.Uri.parse(doclink.remote));
434+
};
435+
436+
}
437+
417438
export function resolveCodeAction(ctx: Ctx): Cmd {
418439
const client = ctx.client;
419-
return async (params: ra.ResolveCodeActionParams) => {
420-
const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, params);
440+
return async () => {
441+
const item: lc.WorkspaceEdit = await client.sendRequest(ra.resolveCodeAction, null);
421442
if (!item) {
422443
return;
423444
}

editors/code/src/lsp_ext.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,14 @@ export interface CommandLinkGroup {
113113
title?: string;
114114
commands: CommandLink[];
115115
}
116+
117+
export interface DocumentationLink {
118+
remote: string;
119+
}
120+
121+
export interface OpenDocsParams {
122+
textDocument: lc.TextDocumentIdentifier;
123+
position: lc.Position;
124+
}
125+
126+
export const openDocs = new lc.RequestType<OpenDocsParams, DocumentationLink, void>('rust-analyzer/openDocs');

0 commit comments

Comments
 (0)