Skip to content

Implement to support intra-doc link #6980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2021

Conversation

sasurau4
Copy link
Contributor

Helps with #6168

This PR is very limited implementation to support intra-doc. It only support links indicate same file function.

I want someone to feedback me about this implementation. If the approach is good, I will continue this PR to support other symbols like enum and struct.

@kjeremy
Copy link
Contributor

kjeremy commented Dec 21, 2020

I'm not sure if goto def is the place for this. It makes more sense to either massage the markdown that is returned to the server (from hover, goto def etc.) to point to the right location or implement LSP's documentLink request.

@matklad
Copy link
Member

matklad commented Dec 23, 2020

@kjeremy why not both? I personally would totally expect gotoDefinition on link text to open the corresponding def in the editor. As I don't really use in-editor documentation (hover, etc) and prefer to view the raw source, that would be the single interface here for me.

As far as I understand, documentLink can only point to a web URL, you can't really plug that into goto definition?

@kjeremy
Copy link
Contributor

kjeremy commented Dec 23, 2020

@matklad I've re-read the documentLink documentation and it looks like it doesn't support target ranges so I think that you are correct with goto def.

The docs say

/**

  • A document link is a range in a text document that links to an internal or
  • external resource, like another text document or a web site.
    */

and target is a DocumentUri so we can use this to point at internal documents just fine. The problem is that it doesn't return a target range (which goto def solves).

@sasurau4 sasurau4 force-pushed the feature/support-infradoc-link branch 2 times, most recently from d8d6b76 to 1bf34fd Compare January 7, 2021 14:48
@sasurau4 sasurau4 requested a review from matklad January 7, 2021 15:32
pub(crate) fn extract_definitions_from_markdown(
markdown: &str,
) -> Vec<(String, Option<hir::Namespace>, Range<usize>)> {
let mut defs = vec![];
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let mut defs = vec![];
let mut res = vec![];

token: SyntaxToken,
) -> Option<(String, Option<hir::Namespace>)> {
let comment_range = token.text_range();
if let Some(doc_comment) = comment.doc_comment() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if let Some(doc_comment) = comment.doc_comment() {
if let Some(doc_comment) = comment.doc_comment()? {

Comment on lines 62 to 71
for (link, ns, def_link_range) in def_links {
let matched_position =
comment_range.start() + TextSize::from(def_link_range.start as u32);
if let Some(distance) = position.offset.checked_sub(matched_position) {
if let Some(_) = min_distance.checked_sub(distance) {
min_distance = distance;
positioned_link = Some((link, ns))
};
};
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks like Iterator::min_by

Comment on lines 85 to 91
let name = match_ast! {
match parent {
ast::NameRef(name_ref) => {
reference_definition(&sema, Either::Right(&name_ref)).to_vec()
},
ast::Name(name) => {
let def = NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db);
let nav = def.try_to_nav(sema.db)?;
vec![nav]
},
ast::SelfParam(self_param) => {
vec![self_to_nav_target(self_param, position.file_id)?]
},
ast::PathSegment(segment) => {
segment.self_token()?;
let path = segment.parent_path();
if path.qualifier().is_some() && !ast::PathExpr::can_cast(path.syntax().parent()?.kind()) {
return None;
}
let func = segment.syntax().ancestors().find_map(ast::Fn::cast)?;
let self_param = func.param_list()?.self_param()?;
vec![self_to_nav_target(self_param, position.file_id)?]
},
ast::Lifetime(lt) => if let Some(name_class) = NameClass::classify_lifetime(&sema, &lt) {
let def = name_class.referenced_or_defined(sema.db);
let nav = def.try_to_nav(sema.db)?;
vec![nav]
} else {
reference_definition(&sema, Either::Left(&lt)).to_vec()
},
_ => return None,
ast::Name(name) => Some(name),
ast::Fn(func) => func.name(),
_ => None,
}
};
Copy link
Member

Choose a reason for hiding this comment

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

let's add a helper fn here:

fn def_for_doc_comment(sema: &Semantics, doc_comment: &ast::Comment) -> Option<hir::ModuleDef>


let nav_targets = match_ast! {
fn nav_target_from_positioned_link(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it makes sense to extract this function, it is just the common case for goto_definition

@sasurau4 sasurau4 force-pushed the feature/support-infradoc-link branch from 1bf34fd to 65c4508 Compare January 8, 2021 13:26
@sasurau4 sasurau4 requested a review from matklad January 8, 2021 13:27
Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

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

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 10, 2021

@bors bors bot merged commit 6a0a47d into rust-lang:master Jan 10, 2021
@sasurau4 sasurau4 deleted the feature/support-infradoc-link branch January 11, 2021 11:31
@jyn514
Copy link
Member

jyn514 commented Jan 13, 2021

This is awesome ❤️ you guys are the best :)

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.

4 participants