aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/body/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/body/tests.rs')
-rw-r--r--crates/hir_def/src/body/tests.rs116
1 files changed, 114 insertions, 2 deletions
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs
index 2e5d0a01e..da60072ce 100644
--- a/crates/hir_def/src/body/tests.rs
+++ b/crates/hir_def/src/body/tests.rs
@@ -1,7 +1,10 @@
1use base_db::{fixture::WithFixture, SourceDatabase}; 1mod block;
2
3use base_db::{fixture::WithFixture, FilePosition, SourceDatabase};
4use expect_test::Expect;
2use test_utils::mark; 5use test_utils::mark;
3 6
4use crate::{test_db::TestDB, ModuleDefId}; 7use crate::{test_db::TestDB, BlockId, ModuleDefId};
5 8
6use super::*; 9use super::*;
7 10
@@ -31,6 +34,115 @@ fn check_diagnostics(ra_fixture: &str) {
31 db.check_diagnostics(); 34 db.check_diagnostics();
32} 35}
33 36
37fn block_def_map_at(ra_fixture: &str) -> Arc<DefMap> {
38 let (db, position) = crate::test_db::TestDB::with_position(ra_fixture);
39
40 let krate = db.crate_graph().iter().next().unwrap();
41 let def_map = db.crate_def_map(krate);
42
43 let mut block =
44 block_at_pos(&db, &def_map, position).expect("couldn't find enclosing function or block");
45 loop {
46 let def_map = db.block_def_map(block);
47 let new_block = block_at_pos(&db, &def_map, position);
48 match new_block {
49 Some(new_block) => {
50 assert_ne!(block, new_block);
51 block = new_block;
52 }
53 None => {
54 return def_map;
55 }
56 }
57 }
58}
59
60fn block_at_pos(db: &dyn DefDatabase, def_map: &DefMap, position: FilePosition) -> Option<BlockId> {
61 let mut size = None;
62 let mut fn_def = None;
63 for (_, module) in def_map.modules() {
64 let file_id = module.definition_source(db).file_id;
65 if file_id != position.file_id.into() {
66 continue;
67 }
68 let root = db.parse_or_expand(file_id).unwrap();
69 let ast_map = db.ast_id_map(file_id);
70 let item_tree = db.item_tree(file_id);
71 for decl in module.scope.declarations() {
72 if let ModuleDefId::FunctionId(it) = decl {
73 let ast = ast_map.get(item_tree[it.lookup(db).id.value].ast_id).to_node(&root);
74 let range = ast.syntax().text_range();
75
76 // Find the smallest (innermost) function containing the cursor.
77 if !range.contains(position.offset) {
78 continue;
79 }
80
81 let new_size = match size {
82 None => range.len(),
83 Some(size) => {
84 if range.len() < size {
85 range.len()
86 } else {
87 size
88 }
89 }
90 };
91 if size != Some(new_size) {
92 size = Some(new_size);
93 fn_def = Some(it);
94 }
95 }
96 }
97 }
98
99 let (body, source_map) = db.body_with_source_map(fn_def?.into());
100
101 // Now find the smallest encompassing block expression in the function body.
102 let mut size = None;
103 let mut block_id = None;
104 for (expr_id, expr) in body.exprs.iter() {
105 if let Expr::Block { id, .. } = expr {
106 if let Ok(ast) = source_map.expr_syntax(expr_id) {
107 if ast.file_id != position.file_id.into() {
108 continue;
109 }
110
111 let root = db.parse_or_expand(ast.file_id).unwrap();
112 let ast = ast.value.to_node(&root);
113 let range = ast.syntax().text_range();
114
115 if !range.contains(position.offset) {
116 continue;
117 }
118
119 let new_size = match size {
120 None => range.len(),
121 Some(size) => {
122 if range.len() < size {
123 range.len()
124 } else {
125 size
126 }
127 }
128 };
129 if size != Some(new_size) {
130 size = Some(new_size);
131 block_id = Some(*id);
132 }
133 }
134 }
135 }
136
137 Some(block_id.expect("can't find block containing cursor"))
138}
139
140fn check_at(ra_fixture: &str, expect: Expect) {
141 let def_map = block_def_map_at(ra_fixture);
142 let actual = def_map.dump();
143 expect.assert_eq(&actual);
144}
145
34#[test] 146#[test]
35fn your_stack_belongs_to_me() { 147fn your_stack_belongs_to_me() {
36 mark::check!(your_stack_belongs_to_me); 148 mark::check!(your_stack_belongs_to_me);