aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/source_binder.rs14
-rw-r--r--crates/ra_hir/src/ty/tests.rs44
2 files changed, 58 insertions, 0 deletions
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 85bd84469..551f44d4e 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -87,6 +87,20 @@ fn module_from_source(
87 Ok(Some(Module::new(db, source_root_id, module_id)?)) 87 Ok(Some(Module::new(db, source_root_id, module_id)?))
88} 88}
89 89
90pub fn function_from_position(
91 db: &impl HirDatabase,
92 position: FilePosition,
93) -> Cancelable<Option<Function>> {
94 let file = db.source_file(position.file_id);
95 let fn_def = if let Some(f) = find_node_at_offset::<ast::FnDef>(file.syntax(), position.offset)
96 {
97 f
98 } else {
99 return Ok(None);
100 };
101 function_from_source(db, position.file_id, fn_def)
102}
103
90pub fn function_from_source( 104pub fn function_from_source(
91 db: &impl HirDatabase, 105 db: &impl HirDatabase,
92 file_id: FileId, 106 file_id: FileId,
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index fb53fcf0b..515c66e85 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -1,7 +1,10 @@
1use std::sync::Arc;
1use std::fmt::Write; 2use std::fmt::Write;
2use std::path::{PathBuf, Path}; 3use std::path::{PathBuf, Path};
3use std::fs; 4use std::fs;
4 5
6use salsa::Database;
7
5use ra_db::{SyntaxDatabase}; 8use ra_db::{SyntaxDatabase};
6use ra_syntax::ast::{self, AstNode}; 9use ra_syntax::ast::{self, AstNode};
7use test_utils::{project_dir, assert_eq_text, read_text}; 10use test_utils::{project_dir, assert_eq_text, read_text};
@@ -217,3 +220,44 @@ fn ellipsize(mut text: String, max_len: usize) -> String {
217fn test_data_dir() -> PathBuf { 220fn test_data_dir() -> PathBuf {
218 project_dir().join("crates/ra_hir/src/ty/tests/data") 221 project_dir().join("crates/ra_hir/src/ty/tests/data")
219} 222}
223
224#[test]
225#[should_panic] // TODO this should work once hir::Expr is used
226fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
227 let (mut db, pos) = MockDatabase::with_position(
228 "
229 //- /lib.rs
230 fn foo() -> i32 {
231 <|>1 + 1
232 }
233 ",
234 );
235 let func = source_binder::function_from_position(&db, pos)
236 .unwrap()
237 .unwrap();
238 {
239 let events = db.log_executed(|| {
240 func.infer(&db).unwrap();
241 });
242 assert!(format!("{:?}", events).contains("infer"))
243 }
244
245 let new_text = "
246 fn foo() -> i32 {
247 1
248 +
249 1
250 }
251 "
252 .to_string();
253
254 db.query_mut(ra_db::FileTextQuery)
255 .set(pos.file_id, Arc::new(new_text));
256
257 {
258 let events = db.log_executed(|| {
259 func.infer(&db).unwrap();
260 });
261 assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
262 }
263}