Skip to content

Resolve links in hover documentation - #4873

Merged
bors[bot] merged 35 commits into
rust-lang:masterfrom
zacps:503-hover-doc-links
Aug 25, 2020
Merged

bors[bot] merged 35 commits into
rust-lang:masterfrom
zacps:503-hover-doc-links

Conversation

@zacps

@zacps zacps commented Jun 14, 2020

Copy link
Copy Markdown
Contributor

This PR resolves links in hover documentation. Both the upcoming intra-doc-links style and the old "path-based" style.

Todo

  • More tests
  • Benchmark (Is there an easy way to benchmark this?)
  • Resolve issues with the markdown parser/get rid of it Migrate to pulldown_cmark_to_cmark
  • Reorganise code (Tips appreciated)

Fixes #503

@zacps
zacps force-pushed the 503-hover-doc-links branch from 9b06786 to ff60cda Compare June 16, 2020 02:57
Comment thread crates/ra_hir/src/code_model.rs Outdated
ModuleDef::Static(s) => Into::<StaticId>::into(s.clone()).resolver(db),
ModuleDef::Trait(t) => Into::<TraitId>::into(t.clone()).resolver(db),
ModuleDef::TypeAlias(t) => Into::<ModuleId>::into(t.module(db)).resolver(db),
// FIXME: This should be a resolver relative to `std/core`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an easy way to get the std Module?

Comment thread crates/ra_ide/src/hover.rs Outdated
@zacps
zacps marked this pull request as ready for review June 16, 2020 06:37
@zacps
zacps marked this pull request as draft June 27, 2020 03:35
@zacps
zacps force-pushed the 503-hover-doc-links branch from ea89336 to 6b9bd86 Compare June 30, 2020 07:58
@zacps
zacps force-pushed the 503-hover-doc-links branch from 6b9bd86 to 48d858f Compare June 30, 2020 08:10
@zacps
zacps marked this pull request as ready for review June 30, 2020 09:28
@zacps
zacps force-pushed the 503-hover-doc-links branch from c89e617 to d6f9e88 Compare June 30, 2020 09:42
Comment thread crates/ra_hir/src/code_model.rs Outdated

@matklad matklad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a review.

I think the core interface is sound, but the implementation does a really uncomfortable amount of stuff. Luckely, it seems most of this is not required :)

Comment thread crates/ra_hir/src/code_model.rs Outdated
Comment thread crates/ra_ide/Cargo.toml Outdated
Comment thread crates/ra_ide/Cargo.toml Outdated
Comment thread crates/ra_ide/Cargo.toml Outdated
Comment thread xtask/tests/tidy.rs
Comment thread crates/ra_ide/src/hover.rs Outdated
Comment thread crates/ra_ide/src/hover.rs Outdated
Comment thread crates/ra_ide/src/hover.rs Outdated
let namespace = Namespace::from_intra_spec(link_target);

// Strip prefixes/suffixes
let link_target = strip_prefixes_suffixes(link_target);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could use some types?

struct DocLink {
    path: &str,
    namespace: Option<Namespace>,
}

impl FromStr for DocLink { ... }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've debated this for a little bit and I'm not sure I think it's worth the mental overhead of another type for the increased type safety.

More than happy to defer to your judgement here if you still think it's a good idea.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always err on the side of adding types, on the grounds that removing type later is easier than adding it.

Comment thread crates/ra_ide/src/hover.rs Outdated
Comment thread crates/ra_ide/src/hover.rs Outdated
@zacps
zacps force-pushed the 503-hover-doc-links branch from df618c0 to b34ad4b Compare August 2, 2020 08:32
Comment thread crates/hir/src/code_model.rs Outdated
Comment thread crates/hir/src/code_model.rs Outdated
Comment thread crates/hir/src/code_model.rs Outdated
Comment thread crates/hir/src/code_model.rs Outdated
use hir_def::{db::DefDatabase, resolver::Resolver};
use syntax::ast::Path;

pub fn resolve_doc_link<T: Resolvable + Clone, D: DefDatabase + HirDatabase>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a method of Docs trait?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs trait is implemented for T: Into<AttrDef> which is a different set of types from those that implement Resolvable.

Conceptually they're not really the same thing. Resolvable really just means that we can find a base which we can then use to resolve documentation links. There isn't a strict requirement that something implementing Resolvable has, or could have, documentation.

/// Try to resolve path to local documentation via intra-doc-links (i.e. `super::gateway::Shard`).
///
/// See [RFC1946](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md).
fn try_resolve_intra<T: Resolvable, D: DefDatabase + HirDatabase>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we can make this on-generic:

https://github.com/rust-analyzer/rust-analyzer/pull/5853/files

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but I'm not sure it's easy/possible with the DefDatabase + HirDatabase bound (which also exists in Resolvable).

I know you can declare a trait:

trait DB: DefDatabase + HirDatabase {}

And then create &dyn DB objects, but I don't think there's an easy way to get &dyn DefDatabase/&dyn HirDatabase out of those?

@matklad

matklad commented Aug 25, 2020

Copy link
Copy Markdown
Contributor

bors r+

Ok, this looks good now!

@matklad

matklad commented Aug 25, 2020

Copy link
Copy Markdown
Contributor

I'll look into reshufling APIs here later today

@bors

bors Bot commented Aug 25, 2020

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clickable links in markdown don't do anything

3 participants