Skip to content

Commit

Permalink
xref for records
Browse files Browse the repository at this point in the history
Summary: as per title

Reviewed By: alanz

Differential Revision: D55201992

fbshipit-source-id: d2b20f9d47d2423538dd3eca3d871479eeb56a93
  • Loading branch information
perehonchuk authored and facebook-github-bot committed Apr 5, 2024
1 parent e07190b commit a7dcc43
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions crates/elp/src/bin/glean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ pub(crate) enum XRefTarget {
Function(FunctionTarget),
Macro(MacroTarget),
Header(HeaderTarget),
Record(RecordTarget),
}

#[derive(Serialize, Debug)]
Expand All @@ -286,6 +287,13 @@ pub(crate) struct HeaderTarget {
file_id: GleanFileId,
}

#[derive(Serialize, Debug)]
pub(crate) struct RecordTarget {
#[serde(rename = "file")]
file_id: GleanFileId,
name: String,
}

#[derive(Serialize, Debug)]
pub(crate) struct DeclarationKey {
key: FileDeclaration,
Expand Down Expand Up @@ -861,6 +869,9 @@ impl GleanIndexer {
};
Self::resolve_call_v2(&sema, target, arity?, file_id, &body, range)
}
hir::AnyExpr::Expr(Expr::Record { name, .. }) => {
Self::resolve_record_v2(&sema, *name, file_id, ctx)
}
hir::AnyExpr::Expr(Expr::MacroCall { macro_def, .. })
| hir::AnyExpr::Pat(Pat::MacroCall { macro_def, .. })
| hir::AnyExpr::TypeExpr(TypeExpr::MacroCall { macro_def, .. })
Expand Down Expand Up @@ -1087,6 +1098,27 @@ impl GleanIndexer {
Some(XRefFactVal::new(range.into(), mfa))
}

fn resolve_record_v2(
sema: &Semantic,
name: hir::Atom,
file_id: FileId,
ctx: &AnyCallBackCtx,
) -> Option<XRef> {
let record_name = sema.db.lookup_atom(name);
let def_map = sema.db.def_map(file_id);
let def = def_map.get_record(&record_name)?;
let (_, expr_source) = ctx.body_with_expr_source(&sema)?;
let source_file = sema.parse(file_id);
let range = Self::find_range(sema, ctx, &source_file, &expr_source)?;
Some(XRef {
source: range.into(),
target: XRefTarget::Record(RecordTarget {
file_id: def.file.file_id.into(),
name: def.record.name.to_string(),
}),
})
}

fn find_range(
sema: &Semantic,
ctx: &AnyCallBackCtx,
Expand Down Expand Up @@ -1447,6 +1479,21 @@ mod tests {
xref_check(&spec);
}

#[test]
fn xref_record_v2_test() {
let spec = r#"
//- /glean/app_glean/src/glean_module9.erl
-record(query, {
size :: non_neg_integer()
}).
baz(A) ->
#query{ size = A }.
%% ^^^^^^ glean_module9.erl/rec/query
"#;

xref_v2_check(&spec);
}

#[test]
fn xref_record_index_test() {
let spec = r#"
Expand Down Expand Up @@ -1802,6 +1849,7 @@ mod tests {
f.write_str(format!("macro/{}/{}", xref.name, arity).as_str())
}
XRefTarget::Header(_) => f.write_str("header"),
XRefTarget::Record(xref) => f.write_str(format!("rec/{}", xref.name).as_str()),
}
}
}
Expand All @@ -1812,6 +1860,7 @@ mod tests {
XRefTarget::Function(xref) => &xref.file_id,
XRefTarget::Macro(xref) => &xref.file_id,
XRefTarget::Header(xref) => &xref.file_id,
XRefTarget::Record(xref) => &xref.file_id,
}
}
}
Expand Down

0 comments on commit a7dcc43

Please sign in to comment.