22
33use std:: { fmt, fs, io, path:: PathBuf } ;
44
5- use sourcegen:: project_root;
5+ use crate :: {
6+ codegen:: { add_preamble, list_rust_files, CommentBlock , Location } ,
7+ project_root,
8+ } ;
69
7- #[ test]
8- fn sourcegen_diagnostic_docs ( ) {
10+ fn generate ( check : bool ) {
911 let diagnostics = Diagnostic :: collect ( ) . unwrap ( ) ;
10- let contents =
11- diagnostics. into_iter ( ) . map ( |it| it. to_string ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n \n " ) ;
12- let contents = sourcegen:: add_preamble ( "sourcegen_diagnostic_docs" , contents) ;
13- let dst = project_root ( ) . join ( "docs/user/generated_diagnostic.adoc" ) ;
14- fs:: write ( dst, contents) . unwrap ( ) ;
12+ if !check {
13+ let contents =
14+ diagnostics. into_iter ( ) . map ( |it| it. to_string ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n \n " ) ;
15+ let contents = add_preamble ( "sourcegen_diagnostic_docs" , contents) ;
16+ let dst = project_root ( ) . join ( "docs/user/generated_diagnostic.adoc" ) ;
17+ fs:: write ( dst, contents) . unwrap ( ) ;
18+ }
1519}
1620
1721#[ derive( Debug ) ]
1822struct Diagnostic {
1923 id : String ,
20- location : sourcegen :: Location ,
24+ location : Location ,
2125 doc : String ,
2226}
2327
@@ -26,23 +30,23 @@ impl Diagnostic {
2630 let handlers_dir = project_root ( ) . join ( "crates/ide-diagnostics/src/handlers" ) ;
2731
2832 let mut res = Vec :: new ( ) ;
29- for path in sourcegen :: list_rust_files ( & handlers_dir) {
33+ for path in list_rust_files ( & handlers_dir) {
3034 collect_file ( & mut res, path) ?;
3135 }
3236 res. sort_by ( |lhs, rhs| lhs. id . cmp ( & rhs. id ) ) ;
3337 return Ok ( res) ;
3438
3539 fn collect_file ( acc : & mut Vec < Diagnostic > , path : PathBuf ) -> io:: Result < ( ) > {
3640 let text = fs:: read_to_string ( & path) ?;
37- let comment_blocks = sourcegen :: CommentBlock :: extract ( "Diagnostic" , & text) ;
41+ let comment_blocks = CommentBlock :: extract ( "Diagnostic" , & text) ;
3842
3943 for block in comment_blocks {
4044 let id = block. id ;
4145 if let Err ( msg) = is_valid_diagnostic_name ( & id) {
4246 panic ! ( "invalid diagnostic name: {id:?}:\n {msg}" )
4347 }
4448 let doc = block. contents . join ( "\n " ) ;
45- let location = sourcegen :: Location { file : path. clone ( ) , line : block. line } ;
49+ let location = Location { file : path. clone ( ) , line : block. line } ;
4650 acc. push ( Diagnostic { id, location, doc } )
4751 }
4852
0 commit comments