1
1
//! Generates descriptors structure for unstable feature from Unstable Book
2
+ use std:: fmt:: Write ;
2
3
use std:: path:: { Path , PathBuf } ;
3
4
4
- use quote:: quote;
5
5
use walkdir:: WalkDir ;
6
6
use xshell:: { cmd, read_file} ;
7
7
@@ -15,16 +15,13 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
15
15
cmd ! ( "git clone --depth=1 https://github.com/rust-lang/rust ./target/rust" ) . run ( ) ?;
16
16
}
17
17
18
- let ts_features = generate_descriptor ( "./target/rust/src/doc/unstable-book/src" . into ( ) ) ?;
19
- cmd ! ( "curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json" ) . run ( ) ?;
18
+ let mut contents = String :: from ( "use crate::completions::attribute::LintCompletion;\n \n " ) ;
19
+ generate_descriptor ( & mut contents, "./target/rust/src/doc/unstable-book/src" . into ( ) ) ?;
20
+ contents. push ( '\n' ) ;
20
21
21
- let ts_clippy = generate_descriptor_clippy ( & Path :: new ( "./target/clippy_lints.json" ) ) ?;
22
- let ts = quote ! {
23
- use crate :: completions:: attribute:: LintCompletion ;
24
- #ts_features
25
- #ts_clippy
26
- } ;
27
- let contents = reformat ( ts. to_string ( ) . as_str ( ) ) ?;
22
+ cmd ! ( "curl http://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json" ) . run ( ) ?;
23
+ generate_descriptor_clippy ( & mut contents, & Path :: new ( "./target/clippy_lints.json" ) ) ?;
24
+ let contents = reformat ( & contents) ?;
28
25
29
26
let destination =
30
27
project_root ( ) . join ( "crates/ide_completion/src/generated_lint_completions.rs" ) ;
@@ -34,30 +31,26 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
34
31
Ok ( ( ) )
35
32
}
36
33
37
- fn generate_descriptor ( src_dir : PathBuf ) -> Result < proc_macro2:: TokenStream > {
38
- let definitions = [ "language-features" , "library-features" ]
34
+ fn generate_descriptor ( buf : & mut String , src_dir : PathBuf ) -> Result < ( ) > {
35
+ buf. push_str ( r#"pub(super) const FEATURES: &[LintCompletion] = &["# ) ;
36
+ buf. push ( '\n' ) ;
37
+ [ "language-features" , "library-features" ]
39
38
. iter ( )
40
39
. flat_map ( |it| WalkDir :: new ( src_dir. join ( it) ) )
41
40
. filter_map ( |e| e. ok ( ) )
42
41
. filter ( |entry| {
43
42
// Get all `.md ` files
44
43
entry. file_type ( ) . is_file ( ) && entry. path ( ) . extension ( ) . unwrap_or_default ( ) == "md"
45
44
} )
46
- . map ( |entry| {
45
+ . for_each ( |entry| {
47
46
let path = entry. path ( ) ;
48
47
let feature_ident = path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . replace ( "-" , "_" ) ;
49
48
let doc = read_file ( path) . unwrap ( ) ;
50
49
51
- quote ! { LintCompletion { label : # feature_ident, description : # doc } }
50
+ push_lint_completion ( buf , & feature_ident, & doc) ;
52
51
} ) ;
53
-
54
- let ts = quote ! {
55
- pub ( super ) const FEATURES : & [ LintCompletion ] = & [
56
- #( #definitions) , *
57
- ] ;
58
- } ;
59
-
60
- Ok ( ts)
52
+ buf. push_str ( "];\n " ) ;
53
+ Ok ( ( ) )
61
54
}
62
55
63
56
#[ derive( Default ) ]
@@ -66,7 +59,7 @@ struct ClippyLint {
66
59
id : String ,
67
60
}
68
61
69
- fn generate_descriptor_clippy ( path : & Path ) -> Result < proc_macro2 :: TokenStream > {
62
+ fn generate_descriptor_clippy ( buf : & mut String , path : & Path ) -> Result < ( ) > {
70
63
let file_content = read_file ( path) ?;
71
64
let mut clippy_lints: Vec < ClippyLint > = vec ! [ ] ;
72
65
@@ -97,18 +90,27 @@ fn generate_descriptor_clippy(path: &Path) -> Result<proc_macro2::TokenStream> {
97
90
}
98
91
}
99
92
100
- let definitions = clippy_lints. into_iter ( ) . map ( |clippy_lint| {
93
+ buf. push_str ( r#"pub(super) const CLIPPY_LINTS: &[LintCompletion] = &["# ) ;
94
+ buf. push ( '\n' ) ;
95
+ clippy_lints. into_iter ( ) . for_each ( |clippy_lint| {
101
96
let lint_ident = format ! ( "clippy::{}" , clippy_lint. id) ;
102
97
let doc = clippy_lint. help ;
103
-
104
- quote ! { LintCompletion { label: #lint_ident, description: #doc } }
98
+ push_lint_completion ( buf, & lint_ident, & doc) ;
105
99
} ) ;
106
100
107
- let ts = quote ! {
108
- pub ( super ) const CLIPPY_LINTS : & [ LintCompletion ] = & [
109
- #( #definitions) , *
110
- ] ;
111
- } ;
101
+ buf. push_str ( "];\n " ) ;
102
+
103
+ Ok ( ( ) )
104
+ }
112
105
113
- Ok ( ts)
106
+ fn push_lint_completion ( buf : & mut String , label : & str , description : & str ) {
107
+ writeln ! (
108
+ buf,
109
+ r###" LintCompletion {{
110
+ label: "{}",
111
+ description: r##"{}"##
112
+ }},"### ,
113
+ label, description
114
+ )
115
+ . unwrap ( ) ;
114
116
}
0 commit comments