Skip to content

Commit

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

Reviewed By: alanz

Differential Revision: D55205079

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

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -294,6 +295,14 @@ pub(crate) struct RecordTarget {
name: String,
}

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

#[derive(Serialize, Debug)]
pub(crate) struct DeclarationKey {
key: FileDeclaration,
Expand Down Expand Up @@ -885,6 +894,10 @@ impl GleanIndexer {
let def = macro_def.as_ref()?;
Self::resolve_macro_v2(&sema, def, &source_file, &ctx)
}
hir::AnyExpr::TypeExpr(TypeExpr::Call { target, args }) => {
let arity = args.len() as u32;
Self::resolve_type_v2(&sema, target, arity, file_id, &ctx)
}
_ => None,
};
acc.push(target?);
Expand Down Expand Up @@ -1087,6 +1100,25 @@ impl GleanIndexer {
Some(XRefFactVal::new(range.into(), mfa))
}

fn resolve_type_v2(
sema: &Semantic,
target: &CallTarget<TypeExprId>,
arity: u32,
file_id: FileId,
ctx: &AnyCallBackCtx,
) -> Option<XRef> {
let (body, range) = ctx.find_range(sema)?;
let def = resolve_type_target(sema, target, arity, file_id, &body)?;
Some(XRef {
source: range.into(),
target: XRefTarget::Ttype(TypeTarget {
file_id: def.file.file_id.into(),
name: def.type_alias.name().name().to_string(),
arity,
}),
})
}

fn resolve_record(
sema: &Semantic,
name: hir::Atom,
Expand Down Expand Up @@ -1470,6 +1502,27 @@ mod tests {
xref_check(&spec);
}

#[test]
fn xref_types_v2_test() {
let spec = r#"
//- /glean/app_glean/src/glean_module81.erl
-type small() :: #{non_neg_integer() | infinity}.
//- /glean/app_glean/src/glean_module8.erl
-type huuuge() :: #{non_neg_integer() | infinity}.
-spec baz(
A :: huuuge(),
%% ^^^^^^^^ glean_module8.erl/type/huuuge/0
B :: glean_module81:small()
%% ^^^^^^^^^^^^^^^^^^^^^^ glean_module81.erl/type/small/0
) -> huuuge().
%% ^^^^^^^^ glean_module8.erl/type/huuuge/0
baz(A, B) ->
A + B."#;

xref_v2_check(&spec);
}

#[test]
fn xref_record_test() {
let spec = r#"
Expand Down Expand Up @@ -1939,6 +1992,9 @@ mod tests {
}
XRefTarget::Header(_) => f.write_str("header"),
XRefTarget::Record(xref) => f.write_str(format!("rec/{}", xref.name).as_str()),
XRefTarget::Ttype(xref) => {
f.write_str(format!("type/{}/{}", xref.name, xref.arity).as_str())
}
}
}
}
Expand All @@ -1950,6 +2006,7 @@ mod tests {
XRefTarget::Macro(xref) => &xref.file_id,
XRefTarget::Header(xref) => &xref.file_id,
XRefTarget::Record(xref) => &xref.file_id,
XRefTarget::Ttype(xref) => &xref.file_id,
}
}
}
Expand Down

0 comments on commit d521cda

Please sign in to comment.