@@ -14,7 +14,7 @@ use crate::{
1414        range:: Pos , 
1515    } , 
1616    nomparser:: SourceProgram , 
17-     utils:: read_config:: { get_config ,  search_config_file,  Config } , 
17+     utils:: read_config:: { prepare_build_envs ,  search_config_file,  Config } , 
1818    Db , 
1919} ; 
2020
@@ -39,9 +39,13 @@ pub struct EmitParams {
3939#[ salsa:: input]  
4040pub  struct  MemDocsInput  { 
4141    pub  docs :  Arc < Mutex < RefCell < MemDocs > > > , 
42+     /// file is the absolute path of the input file to be build, 
43+      /// currently we will build the whole project instead of this file only. 
4244     #[ return_ref]  
4345    pub  file :  String , 
4446    pub  op :  Options , 
47+ 
48+     /// the following fields are used for lsp server to hold additional information 
4549     pub  action :  ActionType , 
4650    pub  params :  Option < ( Pos ,  Option < String > ) > , 
4751    pub  edit_pos :  Option < Pos > , 
@@ -50,6 +54,7 @@ pub struct MemDocsInput {
5054/// 必须有#[id],否则会导致lru cache失效 
5155#[ salsa:: tracked]  
5256pub  struct  FileCompileInput  { 
57+     /// file represents the entry file path to parse 
5358     #[ return_ref]  
5459    pub  file :  String , 
5560    #[ return_ref]  
@@ -61,6 +66,7 @@ pub struct FileCompileInput {
6166#[ salsa:: tracked]  
6267impl  FileCompileInput  { 
6368    #[ salsa:: tracked]  
69+     // get_file_content gets the file content from cache or reads from file with the self.file 
6470    pub  fn  get_file_content ( self ,  db :  & dyn  Db )  -> Option < SourceProgram >  { 
6571        // let f = self.file(db); 
6672        // eprintln!("get_file_content {}", f); 
@@ -71,11 +77,14 @@ impl FileCompileInput {
7177            . unwrap ( ) 
7278            . borrow_mut ( ) 
7379            . get_file_content ( db,  self . file ( db) ) ; 
74-         if  re. is_none ( )  { 
75-             let  f = self . file ( db) ; 
76-             log:: error!( "lsp error: get_file_content failed {}" ,  f) ; 
80+         match  re { 
81+             None  => { 
82+                 let  f = self . file ( db) ; 
83+                 log:: error!( "lsp error: get_file_content failed {}" ,  f) ; 
84+                 None 
85+             } 
86+             _ => re, 
7787        } 
78-         re
7988    } 
8089    #[ salsa:: tracked]  
8190    pub  fn  get_emit_params ( self ,  db :  & dyn  Db )  -> EmitParams  { 
@@ -138,47 +147,69 @@ impl MemDocsInput {
138147            . get_file_content ( db,  & f) ; 
139148        re
140149    } 
150+ 
151+     /// finalize_parser_input prepares the building environments according to the kagari.toml, 
152+      /// and generates the parser entry file. 
153+      /// it applies the entry_file as the parser entry if override_with_kagari_entry is false, 
154+      /// otherwise the entry field in kagari.toml will be applied. 
141155     #[ salsa:: tracked]  
142-     pub  fn  get_file_params ( self ,  db :  & dyn  Db ,  f :  String ,  entry :  bool )  -> Option < FileCompileInput >  { 
143-         let  f = crate :: utils:: canonicalize ( f) ; 
144-         if  f. is_err ( )  { 
145-             log:: debug!( "lsp error: {}" ,  f. err( ) . unwrap( ) ) ; 
146-             return  None ; 
156+     pub  fn  finalize_parser_input ( 
157+         self , 
158+         db :  & dyn  Db , 
159+         entry_file :  String , 
160+         override_with_kagari_entry :  bool , 
161+     )  -> Option < FileCompileInput >  { 
162+         let  mut  final_entry_file:  String ; 
163+         let  re_entry_file = crate :: utils:: canonicalize ( entry_file) ; 
164+         match  re_entry_file { 
165+             Err ( e)  => { 
166+                 log:: debug!( "lsp error: {}" ,  e) ; 
167+                 return  None ; 
168+             } 
169+             Ok ( f)  => { 
170+                 final_entry_file = f. to_string_lossy ( ) . to_string ( ) ; 
171+             } 
147172        } 
148-          let   mut  file = f . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ; 
149-         let  path  = search_config_file ( file . clone ( ) ) ; 
150-         if  path . is_err ( )  { 
151-             log:: debug!( "lsp error: {}" ,  path . err( ) . unwrap( ) ) ; 
173+ 
174+         let  re_kagari_path  = search_config_file ( final_entry_file . clone ( ) ) ; 
175+         if  re_kagari_path . is_err ( )  { 
176+             log:: debug!( "lsp error: {}" ,  re_kagari_path . err( ) . unwrap( ) ) ; 
152177            return  None ; 
153178        } 
154-         let  path = path. unwrap ( ) ; 
155-         let  buf = crate :: utils:: canonicalize ( PathBuf :: from ( path. clone ( ) ) ) . unwrap ( ) ; 
156-         let  parant = buf. parent ( ) . unwrap ( ) ; 
157-         let  re = get_config ( 
179+ 
180+         let  kagari_path = re_kagari_path. unwrap ( ) ; 
181+         let  re_config = prepare_build_envs ( 
158182            db, 
159183            self . docs ( db) 
160184                . lock ( ) 
161185                . unwrap ( ) 
162186                . borrow_mut ( ) 
163-                 . get_file_content ( db,  & path ) 
187+                 . get_file_content ( db,  & kagari_path ) 
164188                . unwrap ( ) , 
165189        ) ; 
166-         if  re. is_err ( )  { 
167-             log:: debug!( "lsp error: {}" ,  re. err( ) . unwrap( ) ) ; 
168-             return  None ; 
169-         } 
170-         let  config = re. unwrap ( ) ; 
171-         if  entry { 
172-             file = config. entry . clone ( ) ; 
190+ 
191+         match  re_config { 
192+             Err ( info)  => { 
193+                 log:: debug!( "lsp error: {}" ,  info) ; 
194+                 None 
195+             } 
196+             Ok ( config)  => { 
197+                 // use entry path inside kagari.toml instead of the input file 
198+                 if  override_with_kagari_entry { 
199+                     final_entry_file = config. entry . clone ( ) ; 
200+                 } 
201+                 let  buf = crate :: utils:: canonicalize ( PathBuf :: from ( kagari_path. clone ( ) ) ) . unwrap ( ) ; 
202+                 let  parant = buf. parent ( ) . unwrap ( ) ; 
203+                 Some ( FileCompileInput :: new ( 
204+                     db, 
205+                     final_entry_file, 
206+                     parant. to_str ( ) . unwrap ( ) . to_string ( ) , 
207+                     self , 
208+                     config, 
209+                     self . op ( db) . optimization , 
210+                 ) ) 
211+             } 
173212        } 
174-         Some ( FileCompileInput :: new ( 
175-             db, 
176-             file, 
177-             parant. to_str ( ) . unwrap ( ) . to_string ( ) , 
178-             self , 
179-             config, 
180-             self . op ( db) . optimization , 
181-         ) ) 
182213    } 
183214} 
184215
0 commit comments