@@ -32,6 +32,118 @@ fn is_custom_comment(comment: &str) -> bool {
32
32
}
33
33
}
34
34
35
+ #[ derive( PartialEq , Eq ) ]
36
+ pub enum CommentStyle {
37
+ DoubleSlash ,
38
+ TripleSlash ,
39
+ Doc ,
40
+ SingleBullet ,
41
+ DoubleBullet ,
42
+ Exclamation ,
43
+ Custom ,
44
+ }
45
+
46
+ impl CommentStyle {
47
+ pub fn opener < ' a > ( & self , orig : & ' a str ) -> & ' a str {
48
+ match * self {
49
+ CommentStyle :: DoubleSlash => "// " ,
50
+ CommentStyle :: TripleSlash => "/// " ,
51
+ CommentStyle :: Doc => "//! " ,
52
+ CommentStyle :: SingleBullet => "/* " ,
53
+ CommentStyle :: DoubleBullet => "/** " ,
54
+ CommentStyle :: Exclamation => "/*! " ,
55
+ CommentStyle :: Custom => {
56
+ if orig. chars ( ) . nth ( 3 ) == Some ( ' ' ) {
57
+ & orig[ 0 ..4 ]
58
+ } else {
59
+ & orig[ 0 ..3 ]
60
+ }
61
+ }
62
+ }
63
+ }
64
+
65
+ pub fn closer < ' a > ( & self ) -> & ' a str {
66
+ match * self {
67
+ CommentStyle :: DoubleSlash |
68
+ CommentStyle :: TripleSlash |
69
+ CommentStyle :: Custom |
70
+ CommentStyle :: Doc => "" ,
71
+ CommentStyle :: DoubleBullet => " **/" ,
72
+ CommentStyle :: SingleBullet |
73
+ CommentStyle :: Exclamation => " */" ,
74
+ }
75
+ }
76
+
77
+ pub fn line_start < ' a > ( & self , orig : & ' a str ) -> & ' a str {
78
+ match * self {
79
+ CommentStyle :: DoubleSlash => "// " ,
80
+ CommentStyle :: TripleSlash => "/// " ,
81
+ CommentStyle :: Doc => "//! " ,
82
+ CommentStyle :: SingleBullet |
83
+ CommentStyle :: Exclamation => " * " ,
84
+ CommentStyle :: DoubleBullet => " ** " ,
85
+ CommentStyle :: Custom => {
86
+ if orig. chars ( ) . nth ( 3 ) == Some ( ' ' ) {
87
+ & orig[ 0 ..4 ]
88
+ } else {
89
+ & orig[ 0 ..3 ]
90
+ }
91
+ }
92
+ }
93
+ }
94
+
95
+ pub fn to_str_tuplet < ' a > ( & self , orig : & ' a str ) -> ( & ' a str , & ' a str , & ' a str ) {
96
+ ( self . opener ( orig) , self . closer ( ) , self . line_start ( orig) )
97
+ }
98
+
99
+ pub fn to_pattern < ' a > ( & self , line : & str , orig : & ' a str , normalize_comments : bool ) -> bool {
100
+ match * self {
101
+ CommentStyle :: DoubleSlash |
102
+ CommentStyle :: TripleSlash |
103
+ CommentStyle :: Custom |
104
+ CommentStyle :: Doc => {
105
+ line. trim_left ( )
106
+ . starts_with ( self . line_start ( orig) . trim_left ( ) ) ||
107
+ comment_style ( line, normalize_comments) == * self
108
+ }
109
+ CommentStyle :: DoubleBullet |
110
+ CommentStyle :: SingleBullet |
111
+ CommentStyle :: Exclamation => {
112
+ line. trim_left ( ) . starts_with ( self . closer ( ) . trim_left ( ) ) ||
113
+ line. trim_left ( )
114
+ . starts_with ( self . line_start ( orig) . trim_left ( ) ) ||
115
+ comment_style ( line, normalize_comments) == * self
116
+ }
117
+ }
118
+ }
119
+ }
120
+
121
+ fn comment_style ( orig : & str , normalize_comments : bool ) -> CommentStyle {
122
+ if !normalize_comments {
123
+ if orig. starts_with ( "/**" ) && !orig. starts_with ( "/**/" ) {
124
+ CommentStyle :: DoubleBullet
125
+ } else if orig. starts_with ( "/*!" ) {
126
+ CommentStyle :: Exclamation
127
+ } else if orig. starts_with ( "/*" ) {
128
+ CommentStyle :: SingleBullet
129
+ } else if orig. starts_with ( "///" ) {
130
+ CommentStyle :: TripleSlash
131
+ } else if orig. starts_with ( "//!" ) {
132
+ CommentStyle :: Doc
133
+ } else {
134
+ CommentStyle :: DoubleSlash
135
+ }
136
+ } else if orig. starts_with ( "///" ) || ( orig. starts_with ( "/**" ) && !orig. starts_with ( "/**/" ) ) {
137
+ CommentStyle :: TripleSlash
138
+ } else if orig. starts_with ( "//!" ) || orig. starts_with ( "/*!" ) {
139
+ CommentStyle :: Doc
140
+ } else if is_custom_comment ( orig) {
141
+ CommentStyle :: Custom
142
+ } else {
143
+ CommentStyle :: DoubleSlash
144
+ }
145
+ }
146
+
35
147
pub fn rewrite_comment ( orig : & str ,
36
148
block_style : bool ,
37
149
shape : Shape ,
@@ -47,39 +159,44 @@ pub fn rewrite_comment(orig: &str,
47
159
if num_bare_lines > 0 && !config. normalize_comments ( ) {
48
160
return Some ( orig. to_owned ( ) ) ;
49
161
}
50
-
51
162
if !config. normalize_comments ( ) && !config. wrap_comments ( ) {
52
163
return light_rewrite_comment ( orig, shape. indent , config) ;
53
164
}
54
165
166
+ let style = comment_style ( orig, false ) ;
167
+ let first_group = orig. lines ( )
168
+ . take_while ( |l| style. to_pattern ( l, orig, false ) )
169
+ . collect :: < Vec < _ > > ( )
170
+ . join ( "\n " ) ;
171
+ let rest = orig. lines ( )
172
+ . skip ( first_group. lines ( ) . count ( ) )
173
+ . collect :: < Vec < _ > > ( )
174
+ . join ( "\n " ) ;
175
+
176
+ let first_group_str = try_opt ! ( rewrite_comment_inner( & first_group, block_style, shape, config) ) ;
177
+ if rest. is_empty ( ) {
178
+ Some ( first_group_str)
179
+ } else {
180
+ rewrite_comment ( & rest, block_style, shape, config) . map ( |rest_str| {
181
+ format ! ( "{}\n {}{}" ,
182
+ first_group_str,
183
+ shape
184
+ . indent
185
+ . to_string( config) ,
186
+ rest_str)
187
+ } )
188
+ }
189
+ }
190
+
191
+ fn rewrite_comment_inner ( orig : & str ,
192
+ block_style : bool ,
193
+ shape : Shape ,
194
+ config : & Config )
195
+ -> Option < String > {
55
196
let ( opener, closer, line_start) = if block_style {
56
- ( "/* " , " */" , " * " )
57
- } else if !config. normalize_comments ( ) {
58
- if orig. starts_with ( "/**" ) && !orig. starts_with ( "/**/" ) {
59
- ( "/** " , " **/" , " ** " )
60
- } else if orig. starts_with ( "/*!" ) {
61
- ( "/*! " , " */" , " * " )
62
- } else if orig. starts_with ( "/*" ) {
63
- ( "/* " , " */" , " * " )
64
- } else if orig. starts_with ( "///" ) {
65
- ( "/// " , "" , "/// " )
66
- } else if orig. starts_with ( "//!" ) {
67
- ( "//! " , "" , "//! " )
68
- } else {
69
- ( "// " , "" , "// " )
70
- }
71
- } else if orig. starts_with ( "///" ) || ( orig. starts_with ( "/**" ) && !orig. starts_with ( "/**/" ) ) {
72
- ( "/// " , "" , "/// " )
73
- } else if orig. starts_with ( "//!" ) || orig. starts_with ( "/*!" ) {
74
- ( "//! " , "" , "//! " )
75
- } else if is_custom_comment ( orig) {
76
- if orig. chars ( ) . nth ( 3 ) == Some ( ' ' ) {
77
- ( & orig[ 0 ..4 ] , "" , & orig[ 0 ..4 ] )
78
- } else {
79
- ( & orig[ 0 ..3 ] , "" , & orig[ 0 ..3 ] )
80
- }
197
+ CommentStyle :: SingleBullet . to_str_tuplet ( "" )
81
198
} else {
82
- ( "// " , "" , "// " )
199
+ comment_style ( orig , config . normalize_comments ( ) ) . to_str_tuplet ( orig )
83
200
} ;
84
201
85
202
let max_chars = shape
0 commit comments