Auto merge of #17936 - Veykril:module_path, r=Veykril

feat: Implement `module_path` macro

Turns out this is a pain to implement because of our hir-def hir-expand split :)
This commit is contained in:
bors 2024-08-23 09:32:27 +00:00
commit 33c1f57a1a
20 changed files with 278 additions and 71 deletions

View file

@ -24,9 +24,11 @@ pub(crate) fn goto_type_definition(
let file: ast::SourceFile = sema.parse_guess_edition(file_id);
let token: SyntaxToken =
pick_best_token(file.syntax().token_at_offset(offset), |kind| match kind {
IDENT | INT_NUMBER | T![self] => 2,
kind if kind.is_trivia() => 0,
_ => 1,
IDENT | INT_NUMBER | T![self] => 3,
// operators
T!['('] | T!['['] | T!['{'] | T![')'] | T![']'] | T!['}'] | T![!] | T![?] => 2,
kind if !kind.is_trivia() => 1,
_ => 0,
})?;
let mut res = Vec::new();

View file

@ -8702,3 +8702,68 @@ fn foo() {
"#]],
);
}
#[test]
fn module_path_macro() {
check(
r##"
//- minicore: module_path
const C$0: &'static str = module_path!();
"##,
expect![[r#"
*C*
```rust
test
```
```rust
const C: &'static str = "test"
```
"#]],
);
check(
r##"
//- minicore: module_path
mod foo {
const C$0: &'static str = module_path!();
}
"##,
expect![[r#"
*C*
```rust
test::foo
```
```rust
const C: &'static str = "test::foo"
```
"#]],
);
check(
r##"
//- minicore: module_path
mod baz {
const _: () = {
mod bar {
const C$0: &'static str = module_path!();
}
}
}
"##,
expect![[r#"
*C*
```rust
test::bar
```
```rust
const C: &'static str = "test::baz::bar"
```
"#]],
);
}

View file

@ -69,6 +69,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
dependencies,
origin,
is_proc_macro,
..
} = &crate_graph[crate_id];
format_to!(
buf,