1
1
use anyhow:: Context ;
2
2
use ra_ap_ide_db:: line_index:: LineIndex ;
3
+ use ra_ap_parser:: Edition ;
3
4
mod archive;
4
5
mod config;
5
6
pub mod generated;
6
7
mod translate;
7
8
pub mod trap;
9
+ use ra_ap_syntax:: ast:: SourceFile ;
10
+ use ra_ap_syntax:: AstNode ;
8
11
12
+ fn extract (
13
+ archiver : & archive:: Archiver ,
14
+ traps : & trap:: TrapFileProvider ,
15
+ file : std:: path:: PathBuf ,
16
+ ) -> anyhow:: Result < ( ) > {
17
+ let file = std:: path:: absolute ( & file) . unwrap_or ( file) ;
18
+ let file = std:: fs:: canonicalize ( & file) . unwrap_or ( file) ;
19
+ archiver. archive ( & file) ;
20
+ let input = std:: fs:: read ( & file) ?;
21
+ let input = String :: from_utf8 ( input) ?;
22
+ let line_index = LineIndex :: new ( & input) ;
23
+ let display_path = file. to_string_lossy ( ) ;
24
+ let mut trap = traps. create ( "source" , & file) ;
25
+ let label = trap. emit_file ( & file) ;
26
+ let mut translator = translate:: Translator :: new ( trap, label, line_index) ;
27
+
28
+ let parse = ra_ap_syntax:: ast:: SourceFile :: parse ( & input, Edition :: CURRENT ) ;
29
+ for err in parse. errors ( ) {
30
+ let ( start, _) = translator. location ( err. range ( ) ) ;
31
+ log:: warn!( "{}:{}:{}: {}" , display_path, start. line, start. col, err) ;
32
+ }
33
+ if let Some ( ast) = SourceFile :: cast ( parse. syntax_node ( ) ) {
34
+ translator. emit_source_file ( ast) ;
35
+ translator. trap . commit ( ) ?
36
+ } else {
37
+ log:: warn!( "Skipped {}" , display_path) ;
38
+ }
39
+ Ok ( ( ) )
40
+ }
9
41
fn main ( ) -> anyhow:: Result < ( ) > {
10
42
let cfg = config:: Config :: extract ( ) . context ( "failed to load configuration" ) ?;
11
43
stderrlog:: new ( )
@@ -18,18 +50,7 @@ fn main() -> anyhow::Result<()> {
18
50
root : cfg. source_archive_dir ,
19
51
} ;
20
52
for file in cfg. inputs {
21
- let file = std:: path:: absolute ( & file) . unwrap_or ( file) ;
22
- let file = std:: fs:: canonicalize ( & file) . unwrap_or ( file) ;
23
- archiver. archive ( & file) ;
24
- let input = std:: fs:: read ( & file) ?;
25
- let input = String :: from_utf8 ( input) ?;
26
- let line_index = LineIndex :: new ( & input) ;
27
- let display_path = file. to_string_lossy ( ) ;
28
- let mut trap = traps. create ( "source" , & file) ;
29
- let label = trap. emit_file ( & file) ;
30
- translate:: SourceFileTranslator :: new ( trap, label, line_index)
31
- . extract ( & display_path, & input)
32
- . context ( "writing trap file" ) ?;
53
+ extract ( & archiver, & traps, file) ?;
33
54
}
34
55
35
56
Ok ( ( ) )
0 commit comments