-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BE: add InFunctionClauseBody::ast_fun_decl helper function
Summary: Add a helper function to return the AST that was the lowered to generate this function clause. Reviewed By: jcpetruzza Differential Revision: D52164542 fbshipit-source-id: 8a4793bdcb7efd63ae638419a1f7f4fc08ffb452
- Loading branch information
1 parent
4fc1cbd
commit 02e99be
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1223,6 +1223,15 @@ impl<T> InFunctionClauseBody<T> { | |
self.function_id.file_id | ||
} | ||
|
||
pub fn ast_fun_decl(&self, db: &dyn MinDefDatabase) -> ast::FunDecl { | ||
let form_list = db.file_form_list(self.function_id.file_id); | ||
let function = &form_list[self.function_id.value]; | ||
let function_ast = function | ||
.form_id | ||
.get(&self.function_id.file_syntax(db.upcast())); | ||
function_ast | ||
} | ||
|
||
pub fn body_exprs(&self) -> impl Iterator<Item = (ExprId, &Expr)> { | ||
self.body.body.exprs.iter() | ||
} | ||
|
@@ -1466,6 +1475,7 @@ mod tests { | |
use crate::test_db::TestDB; | ||
use crate::AnyExprId; | ||
use crate::InFile; | ||
use crate::InFunctionClauseBody; | ||
use crate::Semantic; | ||
|
||
#[track_caller] | ||
|
@@ -1671,4 +1681,86 @@ mod tests { | |
"#, | ||
) | ||
} | ||
|
||
#[track_caller] | ||
fn check_in_clause_ast(fixture_before: &str, expect: Expect) { | ||
let (db, position) = TestDB::with_position(fixture_before); | ||
let sema = Semantic::new(&db); | ||
|
||
let file_syntax = db.parse(position.file_id).syntax_node(); | ||
let token = file_syntax | ||
.token_at_offset(position.offset) | ||
.right_biased() | ||
.unwrap(); | ||
let node = token.parent().unwrap(); | ||
let (_clause_id, clause) = sema | ||
.to_clause_body(InFile::new(position.file_id, &node)) | ||
.unwrap(); | ||
let function_id = sema | ||
.find_enclosing_function_id(position.file_id, &node) | ||
.unwrap(); | ||
let in_clause = | ||
InFunctionClauseBody::new(clause, InFile::new(position.file_id, function_id), None, ()); | ||
let ast = in_clause.ast_fun_decl(&db); | ||
expect.assert_debug_eq(&ast); | ||
} | ||
|
||
#[test] | ||
fn infunctionclausebody_get_ast() { | ||
check_in_clause_ast( | ||
r#"foo(0) -> ok; | ||
foo(~A) -> this. | ||
"#, | ||
expect![[r#" | ||
FunDecl { | ||
syntax: [email protected] | ||
[email protected] | ||
[email protected] | ||
[email protected] "foo" | ||
[email protected] | ||
[email protected] "(" | ||
[email protected] | ||
[email protected] "A" | ||
[email protected] ")" | ||
[email protected] " " | ||
[email protected] | ||
[email protected] "->" | ||
[email protected] " " | ||
[email protected] | ||
[email protected] "this" | ||
[email protected] "." | ||
, | ||
} | ||
"#]], | ||
); | ||
} | ||
|
||
#[test] | ||
fn infunctionclausebody_get_ast_from_macro() { | ||
check_in_clause_ast( | ||
r#" | ||
-define(CLAUSE(Res), foo(_) -> Res). | ||
foo() -> 1; | ||
?CLAUSE(~V). | ||
"#, | ||
expect![[r#" | ||
FunDecl { | ||
syntax: [email protected] | ||
[email protected] | ||
[email protected] "?" | ||
[email protected] | ||
[email protected] "CLAUSE" | ||
[email protected] | ||
[email protected] "(" | ||
[email protected] | ||
[email protected] | ||
[email protected] "V" | ||
[email protected] ")" | ||
[email protected] "." | ||
, | ||
} | ||
"#]], | ||
); | ||
} | ||
} |