Skip to content

Commit

Permalink
fold_file -define
Browse files Browse the repository at this point in the history
Summary: As title

Reviewed By: perehonchuk

Differential Revision: D49821862

fbshipit-source-id: be9376b6e73fd92bc519c5d0fa168d41ee03cd04
  • Loading branch information
alanz authored and facebook-github-bot committed Oct 3, 2023
1 parent a448a13 commit 39df75b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/hir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::FunType;
use crate::HirIdx;
use crate::InFile;
use crate::ListType;
use crate::PPDirective;
use crate::Pat;
use crate::PatId;
use crate::RecordFieldBody;
Expand Down Expand Up @@ -90,6 +91,18 @@ pub fn fold_file<'a, T>(
callback(acc, ctx)
})
}
FormIdx::PPDirective(idx) => {
if let PPDirective::Define(define_id) = &form_list[idx] {
sema.fold_define(
form_idx,
InFile::new(file_id, *define_id),
r,
&mut |acc, ctx| callback(acc, ctx),
)
} else {
r
}
}
_ => {
// Will have to do some time?
r
Expand Down Expand Up @@ -1667,4 +1680,13 @@ bar() ->
"#;
count_atom_foo(fixture_str, 2);
}

#[test]
fn traverse_define() {
let fixture_str = r#"
-module(foo).
-define(FOO(X), foo(X,fo~o)).
"#;
count_atom_foo(fixture_str, 2);
}
}
22 changes: 22 additions & 0 deletions crates/hir/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use crate::CallbackId;
use crate::Clause;
use crate::CompileOptionId;
use crate::DefMap;
use crate::DefineId;
use crate::Expr;
use crate::ExprId;
use crate::File;
Expand Down Expand Up @@ -673,6 +674,27 @@ impl<'db> Semantic<'db> {
)
}

pub fn fold_define<'a, T>(
&self,
form_id: FormIdx,
attribute_id: InFile<DefineId>,
initial: T,
callback: AnyCallBack<'a, T>,
) -> T {
if let Some(body) = self.db.define_body(attribute_id) {
FoldCtx::fold_expr(
&body.body,
Strategy::TopDown,
form_id,
body.expr,
initial,
callback,
)
} else {
initial
}
}

pub fn bound_vars_in_pattern_diagnostic(
&self,
file_id: FileId,
Expand Down

0 comments on commit 39df75b

Please sign in to comment.