Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 113 additions & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,119 @@ impl<'db> Semantics<'db> {
let idx = *methods_named(db, class, &name).first()?;
Some(Definition::Method(MethodId { class, idx }))
}
_ => self.body_resolution(file, node),
_ => self
.body_resolution(file, node)
.or_else(|| self.annotation_value_name(file, name_ref)),
}
}

/// 注解元素值里的名字(`@Produces(MediaType.TEXT_PLAIN)`、`@Foo(BAR)`):
/// 不在任何体的推断里,按常量表达式的名字静态解析。
fn annotation_value_name(
&self,
file: SourceFile,
name_ref: &ast::NameRef,
) -> Option<Definition> {
let node = name_ref.syntax();
node.ancestors()
.find(|n| n.kind() == SyntaxKind::ANNOTATION_ARG_LIST)?;
let parent = node.parent()?;
let name = name_ref.text();
match parent.kind() {
SyntaxKind::NAME_EXPR => self.annotation_simple_name(file, node, &name),
SyntaxKind::FIELD_ACCESS => {
let qualifier = parent.children().find(|c| c != node)?;
let qualifier = self.annotation_qualifier(file, &qualifier)?;
self.annotation_member(&qualifier, &name)
}
_ => None,
}
}

/// 限定名 `a.b.C.FIELD` 里 `.` 前面的部分:类或包。
fn annotation_qualifier(&self, file: SourceFile, expr: &SyntaxNode) -> Option<Definition> {
let def = match expr.kind() {
SyntaxKind::NAME_EXPR => {
let name_ref = expr.children().find_map(ast::NameRef::cast)?;
self.annotation_simple_name(file, name_ref.syntax(), &name_ref.text())?
}
SyntaxKind::FIELD_ACCESS => {
let mut children = expr.children();
let qualifier = children.next()?;
let name_ref = children.find_map(ast::NameRef::cast)?;
let qualifier = self.annotation_qualifier(file, &qualifier)?;
self.annotation_member(&qualifier, &name_ref.text())?
}
_ => return None,
};
matches!(def, Definition::Class(_) | Definition::Package(_)).then_some(def)
}

/// 类的字段或成员类、包里的类或子包。
fn annotation_member(&self, qualifier: &Definition, name: &str) -> Option<Definition> {
let db = self.db;
match qualifier {
Definition::Class(class) => {
if let Some(field) = lookup_field(db, &Ty::class(*class), name) {
return Some(Definition::Field(field.id));
}
member_type(db, *class, name).map(Definition::Class)
}
Definition::Package(package) => {
let mut segments: Vec<Name> = package.split('/').map(Name::new).collect();
segments.push(Name::new(name));
if let Some(class) = resolve_qualified_class(db, &segments) {
return Some(Definition::Class(class));
}
let package = segments.join("/");
package_exists(db, &package).then(|| Definition::Package(SmolStr::new(package)))
}
_ => None,
}
}

/// 简单名:外围各层类的字段(含继承来的)、static import 的字段,然后是类型、包。
fn annotation_simple_name(
&self,
file: SourceFile,
node: &SyntaxNode,
name: &str,
) -> Option<Definition> {
let db = self.db;
let scope = self.type_scope(file, node);
let mut class = scope.class;
while let Some(current) = class {
if let Some(field) = lookup_field(db, &Ty::class(current), name) {
return Some(Definition::Field(field.id));
}
class = hir_def::outer_class(db, current);
}
let file_scope = file_scope(db, file);
let mut imports: Vec<_> = file_scope.static_imports().collect();
// 单个成员的 static import 遮蔽按需的
imports.sort_by_key(|import| import.is_on_demand);
for import in imports {
let prefix = if import.is_on_demand {
&import.segments[..]
} else {
match import.segments.split_last() {
Some((last, prefix)) if last == name => prefix,
_ => continue,
}
};
let Some(owner) = resolve_qualified_class_visible(db, file, prefix)
.or_else(|| resolve_qualified_class(db, prefix))
else {
continue;
};
if let Some(field) = lookup_field(db, &Ty::class(owner), name) {
return Some(Definition::Field(field.id));
}
}
match resolve_type_path(db, &scope, &[name]) {
Some(TypeNs::Class(class)) => Some(Definition::Class(class)),
Some(TypeNs::TypeParam(_)) => None,
None => package_exists(db, name).then(|| Definition::Package(SmolStr::new(name))),
}
}

Expand Down
82 changes: 82 additions & 0 deletions crates/ide/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,3 +1603,85 @@ class Main extends spring.Holder {
.collect();
assert!(errors.is_empty(), "{errors:?}");
}

fn goto_in_annotation(marker: &str) -> (String, SymbolKind) {
let fx = fixture(&format!(
r#"
//- /m/MediaType.java
package m;
public class MediaType {{
public static final String TEXT_PLAIN = "text/plain";
public static class Nested {{ public static final int N = 1; }}
}}
//- /m/Mode.java
package m;
public enum Mode {{ FAST, SLOW }}
//- /m/Produces.java
package m;
public @interface Produces {{ String[] value() default "*/*"; Mode mode() default Mode.FAST; int n() default 0; }}
//- /b/Main.java
package b;
import m.MediaType;
import m.Mode;
import m.Produces;
import static m.MediaType.TEXT_PLAIN;
{marker}
"#
));
let result = navigation::goto_definition(&fx.db, position(&fx)).expect("没有跳转目标");
let nav = &result.info[0];
(slice(&fx, &nav.file, nav.focus_range).to_string(), nav.kind)
}

#[test]
fn goto_definition_in_annotation_values() {
let cases = [
(
"class Main { @Produces(MediaType.TEXT_PL$0AIN) String hello() { return \"\"; } }",
"TEXT_PLAIN",
SymbolKind::Field,
),
(
"class Main { @Produces(Media$0Type.TEXT_PLAIN) String hello() { return \"\"; } }",
"MediaType",
SymbolKind::Class,
),
(
"@Produces(m.MediaType.TEXT_PL$0AIN) class Main {}",
"TEXT_PLAIN",
SymbolKind::Field,
),
(
"@Produces(TEXT_PL$0AIN) class Main {}",
"TEXT_PLAIN",
SymbolKind::Field,
),
(
"@Produces(value = {MediaType.TEXT_PL$0AIN}, mode = Mode.SLOW) class Main {}",
"TEXT_PLAIN",
SymbolKind::Field,
),
(
"@Produces(mode = Mode.SL$0OW) class Main {}",
"SLOW",
SymbolKind::EnumConstant,
),
(
"@Produces(n = MediaType.Nested.$0N) class Main {}",
"N",
SymbolKind::Field,
),
(
"class Main { static final String LOCAL = \"x\"; @Produces(LOC$0AL) void f() {} }",
"LOCAL",
SymbolKind::Field,
),
];
for (source, name, kind) in cases {
assert_eq!(
goto_in_annotation(source),
(name.to_string(), kind),
"{source}"
);
}
}
Loading