Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): add goto definition for structs #3718

Merged
merged 9 commits into from
Dec 13, 2023
43 changes: 43 additions & 0 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,9 +1286,52 @@ impl NodeInterner {
_ => None,
}
}
HirExpression::Constructor(expr) => {
let struct_type = &expr.r#type.borrow();

eprintln!("\n -> Resolve Constructor {struct_type:?}\n");

Some(struct_type.location)
}
HirExpression::MemberAccess(expr_member_access) => {
self.resolve_struct_member_access(expr_member_access)
}
HirExpression::Call(expr_call) => {
let func = expr_call.func;
self.resolve_location(func)
}

_ => None,
}
}

fn resolve_struct_member_access(
&self,
expr_member_access: &crate::hir_def::expr::HirMemberAccess,
) -> Option<Location> {
let expr_lhs = &expr_member_access.lhs;
let expr_rhs = &expr_member_access.rhs;

let found_ident = self.nodes.get(expr_lhs.into())?;

if let Node::Expression(HirExpression::Ident(ident)) = found_ident {
let definition_info = self.definition(ident.id);
if let DefinitionKind::Local(Some(local_id)) = definition_info.kind {
if let Some(Node::Expression(HirExpression::Constructor(constructor_expression))) =
self.nodes.get(local_id.into())
{
let struct_type = constructor_expression.r#type.borrow();
let field_names = struct_type.field_names();
if let Some(found) =
kobyhallx marked this conversation as resolved.
Show resolved Hide resolved
field_names.iter().find(|field_name| field_name.0 == expr_rhs.0)
{
return Some(Location::new(found.span(), struct_type.location.file));
}
}
}
}
None
}
}

impl Methods {
Expand Down
Loading