@@ -3,7 +3,9 @@ use crate::namespace::items::{IngotId, Module, ModuleContext, ModuleFileContent,
33use crate :: AnalyzerDb ;
44use fe_common:: diagnostics:: { Diagnostic , Severity } ;
55use fe_parser:: ast;
6+ use indexmap:: map:: IndexMap ;
67use indexmap:: set:: IndexSet ;
8+ use smol_str:: SmolStr ;
79use std:: path:: Path ;
810use std:: rc:: Rc ;
911
@@ -42,25 +44,26 @@ pub fn ingot_all_modules(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Rc<Vec<Modul
4244 } )
4345 . collect :: < IndexSet < _ > > ( )
4446 . into_iter ( )
45- . map ( |dir| {
46- let module = Module {
47- name : dir
48- . file_name ( )
49- . expect ( "missing file name" )
50- . to_str ( )
51- . expect ( "could not convert dir name to string" )
52- . into ( ) ,
53- ast : ast:: Module { body : vec ! [ ] } ,
54- context : ModuleContext :: Ingot ( ingot_id) ,
55- file_content : ModuleFileContent :: Dir {
56- dir_path : dir
47+ . filter_map ( |dir| {
48+ if let Some ( file_name) = dir. file_name ( ) {
49+ let module = Module {
50+ name : file_name
5751 . to_str ( )
58- . expect ( "could not convert dir path to string" )
52+ . expect ( "could not convert dir name to string" )
5953 . into ( ) ,
60- } ,
61- } ;
62-
63- db. intern_module ( Rc :: new ( module) )
54+ ast : ast:: Module { body : vec ! [ ] } ,
55+ context : ModuleContext :: Ingot ( ingot_id) ,
56+ file_content : ModuleFileContent :: Dir {
57+ dir_path : dir
58+ . to_str ( )
59+ . expect ( "could not convert dir path to string" )
60+ . into ( ) ,
61+ } ,
62+ } ;
63+ Some ( db. intern_module ( Rc :: new ( module) ) )
64+ } else {
65+ None
66+ }
6467 } )
6568 . collect :: < Vec < ModuleId > > ( ) ;
6669
@@ -72,15 +75,7 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
7275 let main_id = ingot_id
7376 . all_modules ( db)
7477 . iter ( )
75- . find ( |module_id| {
76- module_id. name ( db) == "main" && {
77- if let Some ( parent_id) = module_id. parent_module ( db) {
78- parent_id. name ( db) == "src"
79- } else {
80- false
81- }
82- }
83- } )
78+ . find ( |module_id| module_id. name ( db) == "main" )
8479 . copied ( ) ;
8580
8681 Analysis {
@@ -103,3 +98,63 @@ pub fn ingot_main_module(db: &dyn AnalyzerDb, ingot_id: IngotId) -> Analysis<Opt
10398 } ) ,
10499 }
105100}
101+
102+ pub fn ingot_lib_module ( db : & dyn AnalyzerDb , ingot_id : IngotId ) -> Analysis < Option < ModuleId > > {
103+ let lib_id = ingot_id
104+ . all_modules ( db)
105+ . iter ( )
106+ . find ( |module_id| module_id. name ( db) == "lib" )
107+ . copied ( ) ;
108+
109+ Analysis {
110+ value : lib_id,
111+ diagnostics : Rc :: new ( {
112+ if lib_id. is_none ( ) {
113+ vec ! [ Diagnostic {
114+ severity: Severity :: Error ,
115+ message: format!(
116+ "The ingot named \" {}\" is missing a lib module. \
117+ \n Please add a `src/lib.fe` file to the base directory.",
118+ ingot_id. name( db)
119+ ) ,
120+ labels: vec![ ] ,
121+ notes: vec![ ] ,
122+ } ]
123+ } else {
124+ vec ! [ ]
125+ }
126+ } ) ,
127+ }
128+ }
129+
130+ pub fn ingot_root_module ( db : & dyn AnalyzerDb , ingot_id : IngotId ) -> Option < ModuleId > {
131+ db. ingot_main_module ( ingot_id)
132+ . value
133+ . or ( db. ingot_lib_module ( ingot_id) . value )
134+ }
135+
136+ pub fn ingot_root_sub_modules (
137+ db : & dyn AnalyzerDb ,
138+ ingot_id : IngotId ,
139+ ) -> Rc < IndexMap < SmolStr , ModuleId > > {
140+ let modules = ingot_id
141+ . all_modules ( db)
142+ . iter ( )
143+ . filter ( |module_id| {
144+ if Some ( * * module_id) == ingot_id. root_module ( db) {
145+ false
146+ } else {
147+ let mut in_src = false ;
148+ if let Some ( parent) = Path :: new ( & module_id. ingot_path ( db) . to_string ( ) ) . parent ( ) {
149+ if let Some ( name) = parent. file_name ( ) {
150+ in_src = name == "src"
151+ }
152+ }
153+ in_src
154+ }
155+ } )
156+ . map ( |module_id| ( module_id. name ( db) , * module_id) )
157+ . collect ( ) ;
158+
159+ Rc :: new ( modules)
160+ }
0 commit comments