Merge pull request #21376 from SomeoneToIgnore/doc-find-defs

Allow finding references from doc comments
This commit is contained in:
Lukas Wirth 2025-12-31 14:30:10 +00:00 committed by GitHub
commit 019d0164da
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -38,7 +38,8 @@ use syntax::{
};
use crate::{
Analysis, FilePosition, HighlightedRange, NavigationTarget, TryToNav, highlight_related,
Analysis, FilePosition, HighlightedRange, NavigationTarget, TryToNav,
doc_links::token_as_doc_comment, highlight_related,
};
/// Result of a reference search operation.
@ -211,6 +212,13 @@ pub(crate) fn find_defs(
syntax: &SyntaxNode,
offset: TextSize,
) -> Option<Vec<Definition>> {
if let Some(token) = syntax.token_at_offset(offset).left_biased()
&& let Some(doc_comment) = token_as_doc_comment(&token)
{
return doc_comment
.get_definition_with_descend_at(sema, offset, |def, _, _| Some(vec![def]));
}
let token = syntax.token_at_offset(offset).find(|t| {
matches!(
t.kind(),
@ -785,6 +793,23 @@ fn main() {
);
}
#[test]
fn test_find_all_refs_in_comments() {
check(
r#"
struct Foo;
/// $0[`Foo`] is just above
struct Bar;
"#,
expect![[r#"
Foo Struct FileId(0) 0..11 7..10
(no references)
"#]],
);
}
#[test]
fn search_filters_by_range() {
check(