1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
17 use string::{StringFormat, rewrite_string};
19 pub fn rewrite_comment(orig: &str,
27 // Edge case: block comments. Let's not trim their lines (for now).
28 let (opener, closer, line_start) = if block_style {
34 let max_chars = width.checked_sub(closer.len() + opener.len()).unwrap_or(1);
36 let fmt = StringFormat {
39 line_start: line_start,
42 offset: offset + (opener.len() - line_start.len()),
47 let indent_str = offset.to_string(config);
48 let line_breaks = s.chars().filter(|&c| c == '\n').count();
52 .map(|(i, mut line)| {
55 if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
56 line = &line[..(line.len() - 2)];
61 .map(left_trim_comment_line)
70 let mut result = opener.to_owned();
76 result.push_str(&indent_str);
77 result.push_str(line_start);
80 if line.len() > max_chars {
81 let rewrite = try_opt!(rewrite_string(line, &fmt));
82 result.push_str(&rewrite);
85 result.pop(); // Remove space if this is an empty comment.
87 result.push_str(line);
94 result.push_str(closer);
99 fn left_trim_comment_line(line: &str) -> &str {
100 if line.starts_with("/* ") || line.starts_with("// ") {
102 } else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//") {
104 } else if line.starts_with("*") {
111 pub trait FindUncommented {
112 fn find_uncommented(&self, pat: &str) -> Option<usize>;
115 impl FindUncommented for str {
116 fn find_uncommented(&self, pat: &str) -> Option<usize> {
117 let mut needle_iter = pat.chars();
118 for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
119 match needle_iter.next() {
121 return Some(i - pat.len());
123 Some(c) => match kind {
124 CodeCharKind::Normal if b == c => {}
126 needle_iter = pat.chars();
132 // Handle case where the pattern is a suffix of the search string
133 match needle_iter.next() {
135 None => Some(self.len() - pat.len()),
140 // Returns the first byte position after the first comment. The given string
141 // is expected to be prefixed by a comment, including delimiters.
142 // Good: "/* /* inner */ outer */ code();"
143 // Bad: "code(); // hello\n world!"
144 pub fn find_comment_end(s: &str) -> Option<usize> {
145 let mut iter = CharClasses::new(s.char_indices());
146 for (kind, (i, _c)) in &mut iter {
147 if kind == CodeCharKind::Normal {
152 // Handle case where the comment ends at the end of s.
153 if iter.status == CharClassesStatus::Normal {
160 /// Returns true if text contains any comment.
161 pub fn contains_comment(text: &str) -> bool {
162 CharClasses::new(text.chars()).any(|(kind, _)| kind == CodeCharKind::Comment)
165 pub struct CharClasses<T>
169 base: iter::Peekable<T>,
170 status: CharClassesStatus,
174 fn get_char(&self) -> char;
177 impl RichChar for char {
178 fn get_char(&self) -> char {
183 impl RichChar for (usize, char) {
184 fn get_char(&self) -> char {
189 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
190 enum CharClassesStatus {
196 // The u32 is the nesting deepness of the comment
198 // Status when the '/' has been consumed, but not yet the '*', deepness is
199 // the new deepness (after the comment opening).
200 BlockCommentOpening(u32),
201 // Status when the '*' has been consumed, but not yet the '/', deepness is
202 // the new deepness (after the comment closing).
203 BlockCommentClosing(u32),
207 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
208 pub enum CodeCharKind {
213 impl<T> CharClasses<T> where T: Iterator, T::Item: RichChar {
214 pub fn new(base: T) -> CharClasses<T> {
216 base: base.peekable(),
217 status: CharClassesStatus::Normal,
222 impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
223 type Item = (CodeCharKind, T::Item);
225 fn next(&mut self) -> Option<(CodeCharKind, T::Item)> {
226 let item = try_opt!(self.base.next());
227 let chr = item.get_char();
228 self.status = match self.status {
229 CharClassesStatus::LitString => match chr {
230 '"' => CharClassesStatus::Normal,
231 '\\' => CharClassesStatus::LitStringEscape,
232 _ => CharClassesStatus::LitString,
234 CharClassesStatus::LitStringEscape => CharClassesStatus::LitString,
235 CharClassesStatus::LitChar => match chr {
236 '\\' => CharClassesStatus::LitCharEscape,
237 '\'' => CharClassesStatus::Normal,
238 _ => CharClassesStatus::LitChar,
240 CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
241 CharClassesStatus::Normal => {
243 '"' => CharClassesStatus::LitString,
244 '\'' => CharClassesStatus::LitChar,
245 '/' => match self.base.peek() {
246 Some(next) if next.get_char() == '*' => {
247 self.status = CharClassesStatus::BlockCommentOpening(1);
248 return Some((CodeCharKind::Comment, item));
250 Some(next) if next.get_char() == '/' => {
251 self.status = CharClassesStatus::LineComment;
252 return Some((CodeCharKind::Comment, item));
254 _ => CharClassesStatus::Normal,
256 _ => CharClassesStatus::Normal,
259 CharClassesStatus::BlockComment(deepness) => {
261 // This is the closing '/'
262 assert_eq!(chr, '/');
263 self.status = CharClassesStatus::Normal;
264 return Some((CodeCharKind::Comment, item));
266 self.status = match self.base.peek() {
267 Some(next) if next.get_char() == '/' && chr == '*' =>
268 CharClassesStatus::BlockCommentClosing(deepness - 1),
269 Some(next) if next.get_char() == '*' && chr == '/' =>
270 CharClassesStatus::BlockCommentOpening(deepness + 1),
271 _ => CharClassesStatus::BlockComment(deepness),
273 return Some((CodeCharKind::Comment, item));
275 CharClassesStatus::BlockCommentOpening(deepness) => {
276 assert_eq!(chr, '*');
277 self.status = CharClassesStatus::BlockComment(deepness);
278 return Some((CodeCharKind::Comment, item));
280 CharClassesStatus::BlockCommentClosing(deepness) => {
281 assert_eq!(chr, '/');
282 self.status = if deepness == 0 {
283 CharClassesStatus::Normal
285 CharClassesStatus::BlockComment(deepness)
287 return Some((CodeCharKind::Comment, item));
289 CharClassesStatus::LineComment => {
290 self.status = match chr {
291 '\n' => CharClassesStatus::Normal,
292 _ => CharClassesStatus::LineComment,
294 // let code_char_kind = match chr {
295 // '\n' => CodeCharKind::Normal,
296 // _ => CodeCharKind::Comment,
298 return Some((CodeCharKind::Comment, item));
301 Some((CodeCharKind::Normal, item))
305 pub struct CommentCodeSlices<'a> {
307 last_slice_type: CodeCharKind,
308 last_slice_end: usize,
311 impl<'a> CommentCodeSlices<'a> {
312 pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
315 last_slice_type: CodeCharKind::Comment,
321 impl<'a> Iterator for CommentCodeSlices<'a> {
322 type Item = (CodeCharKind, usize, &'a str);
324 fn next(&mut self) -> Option<Self::Item> {
325 if self.last_slice_end == self.slice.len() {
329 let mut sub_slice_end = self.last_slice_end;
330 for (kind, (i, _)) in CharClasses::new(self.slice[self.last_slice_end..].char_indices()) {
331 if kind == self.last_slice_type {
332 sub_slice_end = self.last_slice_end + i;
337 let kind = match self.last_slice_type {
338 CodeCharKind::Comment => CodeCharKind::Normal,
339 CodeCharKind::Normal => CodeCharKind::Comment,
341 self.last_slice_type = kind;
343 // FIXME: be consistent in use of kind vs type.
344 if sub_slice_end == self.last_slice_end {
345 // This was the last subslice.
346 self.last_slice_end = self.slice.len();
348 Some((kind, sub_slice_end, &self.slice[sub_slice_end..]))
352 &self.slice[self.last_slice_end..sub_slice_end]);
353 self.last_slice_end = sub_slice_end;
361 use super::{CharClasses, CodeCharKind, contains_comment, rewrite_comment, FindUncommented,
366 fn comment_code_slices() {
367 let input = "code(); /* test */ 1 + 1";
369 let mut iter = CommentCodeSlices::new(input);
371 assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
372 assert_eq!((CodeCharKind::Comment, 8, "/* test */"),
373 iter.next().unwrap());
374 assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
375 assert_eq!(None, iter.next());
380 fn format_comments() {
381 let config = Default::default();
382 assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
384 assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
385 Indent::empty(), &config).unwrap());
387 assert_eq!("// A multi line comment\n // between args.",
388 rewrite_comment("// A multi line comment\n // between args.",
394 let input = "// comment";
399 assert_eq!(expected, rewrite_comment(input, true, 9, Indent::new(0, 69), &config).unwrap());
401 assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100,
402 Indent::new(0, 100), &config).unwrap());
405 // This is probably intended to be a non-test fn, but it is not used. I'm
406 // keeping it around unless it helps us test stuff.
407 fn uncommented(text: &str) -> String {
408 CharClasses::new(text.chars())
409 .filter_map(|(s, c)| {
411 CodeCharKind::Normal => Some(c),
412 CodeCharKind::Comment => None,
419 fn test_uncommented() {
420 assert_eq!(&uncommented("abc/*...*/"), "abc");
421 assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
423 assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
427 fn test_contains_comment() {
428 assert_eq!(contains_comment("abc"), false);
429 assert_eq!(contains_comment("abc // qsdf"), true);
430 assert_eq!(contains_comment("abc /* kqsdf"), true);
431 assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
435 fn test_find_uncommented() {
436 fn check(haystack: &str, needle: &str, expected: Option<usize>) {
437 println!("haystack {:?}, needle: {:?}", haystack, needle);
438 assert_eq!(expected, haystack.find_uncommented(needle));
441 check("/*/ */test", "test", Some(6));
442 check("//test\ntest", "test", Some(7));
443 check("/* comment only */", "whatever", None);
444 check("/* comment */ some text /* more commentary */ result",
447 check("sup // sup", "p", Some(2));
448 check("sup", "x", None);
449 check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
450 check("/*sup yo? \n sup*/ sup", "p", Some(20));
451 check("hel/*lohello*/lo", "hello", None);
452 check("acb", "ab", None);
453 check(",/*A*/ ", ",", Some(0));
454 check("abc", "abc", Some(0));
455 check("/* abc */", "abc", None);
456 check("/**/abc/* */", "abc", Some(4));
457 check("\"/* abc */\"", "abc", Some(4));
458 check("\"/* abc", "abc", Some(4));