-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Conversation
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. |
@kjeremy why not both? I personally would totally expect As far as I understand, |
@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
and |
d8d6b76
to
1bf34fd
Compare
crates/ide/src/doc_links.rs
Outdated
pub(crate) fn extract_definitions_from_markdown( | ||
markdown: &str, | ||
) -> Vec<(String, Option<hir::Namespace>, Range<usize>)> { | ||
let mut defs = vec![]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let mut defs = vec![]; | |
let mut res = vec![]; |
crates/ide/src/goto_definition.rs
Outdated
token: SyntaxToken, | ||
) -> Option<(String, Option<hir::Namespace>)> { | ||
let comment_range = token.text_range(); | ||
if let Some(doc_comment) = comment.doc_comment() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if let Some(doc_comment) = comment.doc_comment() { | |
if let Some(doc_comment) = comment.doc_comment()? { |
crates/ide/src/goto_definition.rs
Outdated
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)) | ||
}; | ||
}; | ||
} |
There was a problem hiding this comment.
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
crates/ide/src/goto_definition.rs
Outdated
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, <) { | ||
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(<)).to_vec() | ||
}, | ||
_ => return None, | ||
ast::Name(name) => Some(name), | ||
ast::Fn(func) => func.name(), | ||
_ => None, | ||
} | ||
}; |
There was a problem hiding this comment.
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>
crates/ide/src/goto_definition.rs
Outdated
|
||
let nav_targets = match_ast! { | ||
fn nav_target_from_positioned_link( |
There was a problem hiding this comment.
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
1bf34fd
to
65c4508
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bors r+
This is awesome ❤️ you guys are the best :) |
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.