]> git.lizzy.rs Git - rust.git/commitdiff
Treat BlockExpr as a potential module origin
authorJonas Schievink <jonasschievink@gmail.com>
Wed, 20 Jan 2021 19:05:48 +0000 (20:05 +0100)
committerJonas Schievink <jonasschievink@gmail.com>
Wed, 20 Jan 2021 19:05:48 +0000 (20:05 +0100)
crates/assists/src/handlers/generate_function.rs
crates/hir_def/src/attr.rs
crates/hir_def/src/nameres.rs
crates/ide/src/display/navigation_target.rs
crates/ide/src/display/short_label.rs
crates/ide/src/hover.rs
crates/ide/src/runnables.rs
crates/ide_db/src/search.rs

index 06ac85f670e0ec544b485dd487490a9c858d303c..1805c1dfdf71a566cef3c9e20b6019e351f99816 100644 (file)
@@ -158,11 +158,11 @@ fn render(self) -> FunctionTemplate {
                 it.text_range().end()
             }
             GeneratedFunctionTarget::InEmptyItemList(it) => {
-                let indent = IndentLevel::from_node(it.syntax());
+                let indent = IndentLevel::from_node(&it);
                 leading_ws = format!("\n{}", indent + 1);
                 fn_def = fn_def.indent(indent + 1);
                 trailing_ws = format!("\n{}", indent);
-                it.syntax().text_range().start() + TextSize::of('{')
+                it.text_range().start() + TextSize::of('{')
             }
         };
 
@@ -179,14 +179,14 @@ fn render(self) -> FunctionTemplate {
 
 enum GeneratedFunctionTarget {
     BehindItem(SyntaxNode),
-    InEmptyItemList(ast::ItemList),
+    InEmptyItemList(SyntaxNode),
 }
 
 impl GeneratedFunctionTarget {
     fn syntax(&self) -> &SyntaxNode {
         match self {
             GeneratedFunctionTarget::BehindItem(it) => it,
-            GeneratedFunctionTarget::InEmptyItemList(it) => it.syntax(),
+            GeneratedFunctionTarget::InEmptyItemList(it) => it,
         }
     }
 }
@@ -323,7 +323,16 @@ fn next_space_for_fn_in_module(
             if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
                 GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
             } else {
-                GeneratedFunctionTarget::InEmptyItemList(it.item_list()?)
+                GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone())
+            }
+        }
+        hir::ModuleSource::BlockExpr(it) => {
+            if let Some(last_item) =
+                it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
+            {
+                GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
+            } else {
+                GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
             }
         }
     };
index 1b09ff816e0af8c8a0ddbfccfdf807c6a05d6b3e..c72649c41e4d183f8456db60326ab27ae2f135a1 100644 (file)
@@ -207,6 +207,7 @@ pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
                         mod_data.definition_source(db).as_ref().map(|src| match src {
                             ModuleSource::SourceFile(file) => file as &dyn AttrsOwner,
                             ModuleSource::Module(module) => module as &dyn AttrsOwner,
+                            ModuleSource::BlockExpr(block) => block as &dyn AttrsOwner,
                         }),
                     ),
                 }
index 23f960ad490f99f753a9e0f00495c3798dbf96ff..a3200c710ae576f42372acc048ccc19b39b4bb4f 100644 (file)
@@ -109,6 +109,10 @@ pub enum ModuleOrigin {
     Inline {
         definition: AstId<ast::Module>,
     },
+    /// Pseudo-module introduced by a block scope (contains only inner items).
+    BlockExpr {
+        block: AstId<ast::BlockExpr>,
+    },
 }
 
 impl Default for ModuleOrigin {
@@ -122,7 +126,7 @@ fn declaration(&self) -> Option<AstId<ast::Module>> {
         match self {
             ModuleOrigin::File { declaration: module, .. }
             | ModuleOrigin::Inline { definition: module, .. } => Some(*module),
-            ModuleOrigin::CrateRoot { .. } => None,
+            ModuleOrigin::CrateRoot { .. } | ModuleOrigin::BlockExpr { .. } => None,
         }
     }
 
@@ -137,7 +141,7 @@ pub fn file_id(&self) -> Option<FileId> {
 
     pub fn is_inline(&self) -> bool {
         match self {
-            ModuleOrigin::Inline { .. } => true,
+            ModuleOrigin::Inline { .. } | ModuleOrigin::BlockExpr { .. } => true,
             ModuleOrigin::CrateRoot { .. } | ModuleOrigin::File { .. } => false,
         }
     }
@@ -155,6 +159,9 @@ fn definition_source(&self, db: &dyn DefDatabase) -> InFile<ModuleSource> {
                 definition.file_id,
                 ModuleSource::Module(definition.to_node(db.upcast())),
             ),
+            ModuleOrigin::BlockExpr { block } => {
+                InFile::new(block.file_id, ModuleSource::BlockExpr(block.to_node(db.upcast())))
+            }
         }
     }
 }
@@ -300,6 +307,7 @@ pub fn declaration_source(&self, db: &dyn DefDatabase) -> Option<InFile<ast::Mod
 pub enum ModuleSource {
     SourceFile(ast::SourceFile),
     Module(ast::Module),
+    BlockExpr(ast::BlockExpr),
 }
 
 mod diagnostics {
index 671aa13736d4fa06012f548fada817d30e00fbfd..9c568c90c3882f36f8831d0de203959b2415ace9 100644 (file)
@@ -294,6 +294,7 @@ fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
             ModuleSource::Module(node) => {
                 (node.syntax(), node.name().map(|it| it.syntax().text_range()))
             }
+            ModuleSource::BlockExpr(node) => (node.syntax(), None),
         };
         let frange = src.with_value(syntax).original_file_range(db);
         NavigationTarget::from_syntax(frange.file_id, name, focus, frange.range, SymbolKind::Module)
index b8e4cc181126a5938483cf458ebb963dee603cc4..7ac050473f1bddf29b8531d0e7cb3d3863c8c5bc 100644 (file)
@@ -53,6 +53,12 @@ fn short_label(&self) -> Option<String> {
     }
 }
 
+impl ShortLabel for ast::BlockExpr {
+    fn short_label(&self) -> Option<String> {
+        None
+    }
+}
+
 impl ShortLabel for ast::TypeAlias {
     fn short_label(&self) -> Option<String> {
         short_label_from_node(self, "type ")
index 44ebdbd35db3d4db30a91b977a2bf87c33672a48..ec163148658d43d09b1eda65d3fc8064713d49ff 100644 (file)
@@ -321,6 +321,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
                 match it.definition_source(db).value {
                     ModuleSource::Module(it) => it.short_label(),
                     ModuleSource::SourceFile(it) => it.short_label(),
+                    ModuleSource::BlockExpr(it) => it.short_label(),
                 },
                 mod_path,
             ),
index 47a85dc4568f54f3713d5bf70adf42e553b2f2db..975abf47f97c2d8415ac02e1caefc6bd2dab5419 100644 (file)
@@ -131,6 +131,7 @@ fn runnables_mod(sema: &Semantics<RootDatabase>, acc: &mut Vec<Runnable>, module
             match submodule.definition_source(sema.db).value {
                 hir::ModuleSource::Module(_) => runnables_mod(sema, acc, submodule),
                 hir::ModuleSource::SourceFile(_) => mark::hit!(dont_recurse_in_outline_submodules),
+                hir::ModuleSource::BlockExpr(_) => {} // inner items aren't runnable
             }
         }
     }
index 0ecb13a64b0ae44903b25eb29cd0e37d7f5342cd..b9ba0aed5d328e0d1e7373c01929905da811beee 100644 (file)
@@ -228,6 +228,15 @@ fn search_scope(&self, db: &RootDatabase) -> SearchScope {
                             // so do nothing.
                         }
                     }
+                    ModuleSource::BlockExpr(b) => {
+                        if is_first {
+                            let range = Some(b.syntax().text_range());
+                            res.insert(file_id, range);
+                        } else {
+                            // We have already added the enclosing file to the search scope,
+                            // so do nothing.
+                        }
+                    }
                     ModuleSource::SourceFile(_) => {
                         res.insert(file_id, None);
                     }
@@ -257,6 +266,7 @@ fn search_scope(&self, db: &RootDatabase) -> SearchScope {
         let mut res = FxHashMap::default();
         let range = match module_src.value {
             ModuleSource::Module(m) => Some(m.syntax().text_range()),
+            ModuleSource::BlockExpr(b) => Some(b.syntax().text_range()),
             ModuleSource::SourceFile(_) => None,
         };
         res.insert(file_id, range);