@@ -8,7 +8,7 @@ use crate::{
88} ;
99
1010pub fn generate_assists_docs ( mode : Mode ) -> Result < ( ) > {
11- let assists = collect_assists ( ) ?;
11+ let assists = Assist :: collect ( ) ?;
1212 generate_tests ( & assists, mode) ?;
1313 generate_docs ( & assists, mode) ?;
1414 Ok ( ( ) )
@@ -22,81 +22,61 @@ struct Assist {
2222 after : String ,
2323}
2424
25- fn hide_hash_comments ( text : & str ) -> String {
26- text. split ( '\n' ) // want final newline
27- . filter ( |& it| !( it. starts_with ( "# " ) || it == "#" ) )
28- . map ( |it| format ! ( "{}\n " , it) )
29- . collect ( )
30- }
31-
32- fn reveal_hash_comments ( text : & str ) -> String {
33- text. split ( '\n' ) // want final newline
34- . map ( |it| {
35- if it. starts_with ( "# " ) {
36- & it[ 2 ..]
37- } else if it == "#" {
38- ""
39- } else {
40- it
41- }
42- } )
43- . map ( |it| format ! ( "{}\n " , it) )
44- . collect ( )
45- }
46-
47- fn collect_assists ( ) -> Result < Vec < Assist > > {
48- let mut res = Vec :: new ( ) ;
49- for path in rust_files ( & project_root ( ) . join ( codegen:: ASSISTS_DIR ) ) {
50- collect_file ( & mut res, path. as_path ( ) ) ?;
51- }
52- res. sort_by ( |lhs, rhs| lhs. id . cmp ( & rhs. id ) ) ;
53- return Ok ( res) ;
54-
55- fn collect_file ( acc : & mut Vec < Assist > , path : & Path ) -> Result < ( ) > {
56- let text = fs:: read_to_string ( path) ?;
57- let comment_blocks = extract_comment_blocks_with_empty_lines ( & text) ;
58-
59- for block in comment_blocks {
60- // FIXME: doesn't support blank lines yet, need to tweak
61- // `extract_comment_blocks` for that.
62- let mut lines = block. iter ( ) ;
63- let first_line = lines. next ( ) . unwrap ( ) ;
64- if !first_line. starts_with ( "Assist: " ) {
65- continue ;
66- }
67- let id = first_line[ "Assist: " . len ( ) ..] . to_string ( ) ;
68- assert ! (
69- id. chars( ) . all( |it| it. is_ascii_lowercase( ) || it == '_' ) ,
70- "invalid assist id: {:?}" ,
71- id
72- ) ;
73-
74- let doc = take_until ( lines. by_ref ( ) , "```" ) . trim ( ) . to_string ( ) ;
75- assert ! (
76- doc. chars( ) . next( ) . unwrap( ) . is_ascii_uppercase( ) && doc. ends_with( '.' ) ,
77- "\n \n {}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n \n {}\n \n " ,
78- id, doc,
79- ) ;
80-
81- let before = take_until ( lines. by_ref ( ) , "```" ) ;
82-
83- assert_eq ! ( lines. next( ) . unwrap( ) . as_str( ) , "->" ) ;
84- assert_eq ! ( lines. next( ) . unwrap( ) . as_str( ) , "```" ) ;
85- let after = take_until ( lines. by_ref ( ) , "```" ) ;
86- acc. push ( Assist { id, doc, before, after } )
25+ impl Assist {
26+ fn collect ( ) -> Result < Vec < Assist > > {
27+ let mut res = Vec :: new ( ) ;
28+ for path in rust_files ( & project_root ( ) . join ( codegen:: ASSISTS_DIR ) ) {
29+ collect_file ( & mut res, path. as_path ( ) ) ?;
8730 }
31+ res. sort_by ( |lhs, rhs| lhs. id . cmp ( & rhs. id ) ) ;
32+ return Ok ( res) ;
33+
34+ fn collect_file ( acc : & mut Vec < Assist > , path : & Path ) -> Result < ( ) > {
35+ let text = fs:: read_to_string ( path) ?;
36+ let comment_blocks = extract_comment_blocks_with_empty_lines ( & text) ;
37+
38+ for block in comment_blocks {
39+ // FIXME: doesn't support blank lines yet, need to tweak
40+ // `extract_comment_blocks` for that.
41+ let mut lines = block. iter ( ) ;
42+ let first_line = lines. next ( ) . unwrap ( ) ;
43+ if !first_line. starts_with ( "Assist: " ) {
44+ continue ;
45+ }
46+ let id = first_line[ "Assist: " . len ( ) ..] . to_string ( ) ;
47+ assert ! (
48+ id. chars( ) . all( |it| it. is_ascii_lowercase( ) || it == '_' ) ,
49+ "invalid assist id: {:?}" ,
50+ id
51+ ) ;
52+
53+ let doc = take_until ( lines. by_ref ( ) , "```" ) . trim ( ) . to_string ( ) ;
54+ assert ! (
55+ doc. chars( ) . next( ) . unwrap( ) . is_ascii_uppercase( ) && doc. ends_with( '.' ) ,
56+ "\n \n {}: assist docs should be proper sentences, with capitalization and a full stop at the end.\n \n {}\n \n " ,
57+ id, doc,
58+ ) ;
59+
60+ let before = take_until ( lines. by_ref ( ) , "```" ) ;
61+
62+ assert_eq ! ( lines. next( ) . unwrap( ) . as_str( ) , "->" ) ;
63+ assert_eq ! ( lines. next( ) . unwrap( ) . as_str( ) , "```" ) ;
64+ let after = take_until ( lines. by_ref ( ) , "```" ) ;
65+ acc. push ( Assist { id, doc, before, after } )
66+ }
8867
89- fn take_until < ' a > ( lines : impl Iterator < Item = & ' a String > , marker : & str ) -> String {
90- let mut buf = Vec :: new ( ) ;
91- for line in lines {
92- if line == marker {
93- break ;
68+ fn take_until < ' a > ( lines : impl Iterator < Item = & ' a String > , marker : & str ) -> String {
69+ let mut buf = Vec :: new ( ) ;
70+ for line in lines {
71+ if line == marker {
72+ break ;
73+ }
74+ buf. push ( line. clone ( ) ) ;
9475 }
95- buf. push ( line . clone ( ) ) ;
76+ buf. join ( " \n " )
9677 }
97- buf . join ( " \n " )
78+ Ok ( ( ) )
9879 }
99- Ok ( ( ) )
10080 }
10181}
10282
@@ -157,3 +137,25 @@ fn generate_docs(assists: &[Assist], mode: Mode) -> Result<()> {
157137
158138 codegen:: update ( & project_root ( ) . join ( codegen:: ASSISTS_DOCS ) , & buf, mode)
159139}
140+
141+ fn hide_hash_comments ( text : & str ) -> String {
142+ text. split ( '\n' ) // want final newline
143+ . filter ( |& it| !( it. starts_with ( "# " ) || it == "#" ) )
144+ . map ( |it| format ! ( "{}\n " , it) )
145+ . collect ( )
146+ }
147+
148+ fn reveal_hash_comments ( text : & str ) -> String {
149+ text. split ( '\n' ) // want final newline
150+ . map ( |it| {
151+ if it. starts_with ( "# " ) {
152+ & it[ 2 ..]
153+ } else if it == "#" {
154+ ""
155+ } else {
156+ it
157+ }
158+ } )
159+ . map ( |it| format ! ( "{}\n " , it) )
160+ . collect ( )
161+ }
0 commit comments