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 {
30 } else if orig.starts_with("///") {
32 } else if orig.starts_with("//!") {
38 let max_chars = width.checked_sub(closer.len() + opener.len()).unwrap_or(1);
40 let fmt = StringFormat {
43 line_start: line_start,
46 offset: offset + (opener.len() - line_start.len()),
51 let indent_str = offset.to_string(config);
52 let line_breaks = s.chars().filter(|&c| c == '\n').count();
56 .map(|(i, mut line)| {
59 if i == line_breaks && line.ends_with("*/") && !line.starts_with("//") {
60 line = &line[..(line.len() - 2)];
65 .map(left_trim_comment_line)
74 let mut result = opener.to_owned();
80 result.push_str(&indent_str);
81 result.push_str(line_start);
84 if config.wrap_comments && line.len() > max_chars {
85 let rewrite = try_opt!(rewrite_string(line, &fmt));
86 result.push_str(&rewrite);
89 // Remove space if this is an empty comment or a doc comment.
92 result.push_str(line);
98 result.push_str(closer);
103 fn left_trim_comment_line(line: &str) -> &str {
104 if line.starts_with("//! ") || line.starts_with("/// ") {
106 } else if line.starts_with("/* ") || line.starts_with("// ") || line.starts_with("//!") ||
107 line.starts_with("///") {
109 } else if line.starts_with("/*") || line.starts_with("* ") || line.starts_with("//") {
111 } else if line.starts_with("*") {
118 pub trait FindUncommented {
119 fn find_uncommented(&self, pat: &str) -> Option<usize>;
122 impl FindUncommented for str {
123 fn find_uncommented(&self, pat: &str) -> Option<usize> {
124 let mut needle_iter = pat.chars();
125 for (kind, (i, b)) in CharClasses::new(self.char_indices()) {
126 match needle_iter.next() {
128 return Some(i - pat.len());
132 CodeCharKind::Normal if b == c => {}
134 needle_iter = pat.chars();
141 // Handle case where the pattern is a suffix of the search string
142 match needle_iter.next() {
144 None => Some(self.len() - pat.len()),
149 // Returns the first byte position after the first comment. The given string
150 // is expected to be prefixed by a comment, including delimiters.
151 // Good: "/* /* inner */ outer */ code();"
152 // Bad: "code(); // hello\n world!"
153 pub fn find_comment_end(s: &str) -> Option<usize> {
154 let mut iter = CharClasses::new(s.char_indices());
155 for (kind, (i, _c)) in &mut iter {
156 if kind == CodeCharKind::Normal {
161 // Handle case where the comment ends at the end of s.
162 if iter.status == CharClassesStatus::Normal {
169 /// Returns true if text contains any comment.
170 pub fn contains_comment(text: &str) -> bool {
171 CharClasses::new(text.chars()).any(|(kind, _)| kind == CodeCharKind::Comment)
174 struct CharClasses<T>
178 base: iter::Peekable<T>,
179 status: CharClassesStatus,
183 fn get_char(&self) -> char;
186 impl RichChar for char {
187 fn get_char(&self) -> char {
192 impl RichChar for (usize, char) {
193 fn get_char(&self) -> char {
198 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
199 enum CharClassesStatus {
205 // The u32 is the nesting deepness of the comment
207 // Status when the '/' has been consumed, but not yet the '*', deepness is
208 // the new deepness (after the comment opening).
209 BlockCommentOpening(u32),
210 // Status when the '*' has been consumed, but not yet the '/', deepness is
211 // the new deepness (after the comment closing).
212 BlockCommentClosing(u32),
216 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
217 pub enum CodeCharKind {
222 impl<T> CharClasses<T> where T: Iterator, T::Item: RichChar {
223 fn new(base: T) -> CharClasses<T> {
225 base: base.peekable(),
226 status: CharClassesStatus::Normal,
231 impl<T> Iterator for CharClasses<T> where T: Iterator, T::Item: RichChar {
232 type Item = (CodeCharKind, T::Item);
234 fn next(&mut self) -> Option<(CodeCharKind, T::Item)> {
235 let item = try_opt!(self.base.next());
236 let chr = item.get_char();
237 self.status = match self.status {
238 CharClassesStatus::LitString => {
240 '"' => CharClassesStatus::Normal,
241 '\\' => CharClassesStatus::LitStringEscape,
242 _ => CharClassesStatus::LitString,
245 CharClassesStatus::LitStringEscape => CharClassesStatus::LitString,
246 CharClassesStatus::LitChar => {
248 '\\' => CharClassesStatus::LitCharEscape,
249 '\'' => CharClassesStatus::Normal,
250 _ => CharClassesStatus::LitChar,
253 CharClassesStatus::LitCharEscape => CharClassesStatus::LitChar,
254 CharClassesStatus::Normal => {
256 '"' => CharClassesStatus::LitString,
257 '\'' => CharClassesStatus::LitChar,
259 match self.base.peek() {
260 Some(next) if next.get_char() == '*' => {
261 self.status = CharClassesStatus::BlockCommentOpening(1);
262 return Some((CodeCharKind::Comment, item));
264 Some(next) if next.get_char() == '/' => {
265 self.status = CharClassesStatus::LineComment;
266 return Some((CodeCharKind::Comment, item));
268 _ => CharClassesStatus::Normal,
271 _ => CharClassesStatus::Normal,
274 CharClassesStatus::BlockComment(deepness) => {
276 // This is the closing '/'
277 assert_eq!(chr, '/');
278 self.status = CharClassesStatus::Normal;
279 return Some((CodeCharKind::Comment, item));
281 self.status = match self.base.peek() {
282 Some(next) if next.get_char() == '/' && chr == '*' => {
283 CharClassesStatus::BlockCommentClosing(deepness - 1)
285 Some(next) if next.get_char() == '*' && chr == '/' => {
286 CharClassesStatus::BlockCommentOpening(deepness + 1)
288 _ => CharClassesStatus::BlockComment(deepness),
290 return Some((CodeCharKind::Comment, item));
292 CharClassesStatus::BlockCommentOpening(deepness) => {
293 assert_eq!(chr, '*');
294 self.status = CharClassesStatus::BlockComment(deepness);
295 return Some((CodeCharKind::Comment, item));
297 CharClassesStatus::BlockCommentClosing(deepness) => {
298 assert_eq!(chr, '/');
299 self.status = if deepness == 0 {
300 CharClassesStatus::Normal
302 CharClassesStatus::BlockComment(deepness)
304 return Some((CodeCharKind::Comment, item));
306 CharClassesStatus::LineComment => {
307 self.status = match chr {
308 '\n' => CharClassesStatus::Normal,
309 _ => CharClassesStatus::LineComment,
311 return Some((CodeCharKind::Comment, item));
314 Some((CodeCharKind::Normal, item))
318 /// Iterator over an alternating sequence of functional and commented parts of
319 /// a string. The first item is always a, possibly zero length, subslice of
320 /// functional text. Line style comments contain their ending newlines.
321 pub struct CommentCodeSlices<'a> {
323 last_slice_kind: CodeCharKind,
324 last_slice_end: usize,
327 impl<'a> CommentCodeSlices<'a> {
328 pub fn new(slice: &'a str) -> CommentCodeSlices<'a> {
331 last_slice_kind: CodeCharKind::Comment,
337 impl<'a> Iterator for CommentCodeSlices<'a> {
338 type Item = (CodeCharKind, usize, &'a str);
340 fn next(&mut self) -> Option<Self::Item> {
341 if self.last_slice_end == self.slice.len() {
345 let mut sub_slice_end = self.last_slice_end;
346 let mut first_whitespace = None;
347 let subslice = &self.slice[self.last_slice_end..];
348 let mut iter = CharClasses::new(subslice.char_indices());
350 for (kind, (i, c)) in &mut iter {
351 let is_comment_connector = self.last_slice_kind == CodeCharKind::Normal &&
352 &subslice[..2] == "//" &&
353 [' ', '\t'].contains(&c);
355 if is_comment_connector && first_whitespace.is_none() {
356 first_whitespace = Some(i);
359 if kind == self.last_slice_kind && !is_comment_connector {
360 let last_index = match first_whitespace {
364 sub_slice_end = self.last_slice_end + last_index;
368 if !is_comment_connector {
369 first_whitespace = None;
373 if let (None, true) = (iter.next(), sub_slice_end == self.last_slice_end) {
374 // This was the last subslice.
375 sub_slice_end = match first_whitespace {
376 Some(i) => self.last_slice_end + i,
377 None => self.slice.len(),
381 let kind = match self.last_slice_kind {
382 CodeCharKind::Comment => CodeCharKind::Normal,
383 CodeCharKind::Normal => CodeCharKind::Comment,
387 &self.slice[self.last_slice_end..sub_slice_end]);
388 self.last_slice_end = sub_slice_end;
389 self.last_slice_kind = kind;
397 use super::{CharClasses, CodeCharKind, contains_comment, rewrite_comment, FindUncommented,
403 let mut iter = CharClasses::new("//\n\n".chars());
405 assert_eq!((CodeCharKind::Comment, '/'), iter.next().unwrap());
406 assert_eq!((CodeCharKind::Comment, '/'), iter.next().unwrap());
407 assert_eq!((CodeCharKind::Comment, '\n'), iter.next().unwrap());
408 assert_eq!((CodeCharKind::Normal, '\n'), iter.next().unwrap());
409 assert_eq!(None, iter.next());
413 fn comment_code_slices() {
414 let input = "code(); /* test */ 1 + 1";
415 let mut iter = CommentCodeSlices::new(input);
417 assert_eq!((CodeCharKind::Normal, 0, "code(); "), iter.next().unwrap());
418 assert_eq!((CodeCharKind::Comment, 8, "/* test */"),
419 iter.next().unwrap());
420 assert_eq!((CodeCharKind::Normal, 18, " 1 + 1"), iter.next().unwrap());
421 assert_eq!(None, iter.next());
425 fn comment_code_slices_two() {
426 let input = "// comment\n test();";
427 let mut iter = CommentCodeSlices::new(input);
429 assert_eq!((CodeCharKind::Normal, 0, ""), iter.next().unwrap());
430 assert_eq!((CodeCharKind::Comment, 0, "// comment\n"),
431 iter.next().unwrap());
432 assert_eq!((CodeCharKind::Normal, 11, " test();"),
433 iter.next().unwrap());
434 assert_eq!(None, iter.next());
438 fn comment_code_slices_three() {
439 let input = "1 // comment\n // comment2\n\n";
440 let mut iter = CommentCodeSlices::new(input);
442 assert_eq!((CodeCharKind::Normal, 0, "1 "), iter.next().unwrap());
443 assert_eq!((CodeCharKind::Comment, 2, "// comment\n // comment2\n"),
444 iter.next().unwrap());
445 assert_eq!((CodeCharKind::Normal, 29, "\n"), iter.next().unwrap());
446 assert_eq!(None, iter.next());
450 #[cfg_attr(rustfmt, rustfmt_skip)]
451 fn format_comments() {
452 let mut config: ::config::Config = Default::default();
453 config.wrap_comments = true;
454 assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
456 assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
457 Indent::empty(), &config).unwrap());
459 assert_eq!("// A multi line comment\n // between args.",
460 rewrite_comment("// A multi line comment\n // between args.",
466 let input = "// comment";
471 assert_eq!(expected, rewrite_comment(input, true, 9, Indent::new(0, 69), &config).unwrap());
473 assert_eq!("/* trimmed */", rewrite_comment("/* trimmed */", true, 100,
474 Indent::new(0, 100), &config).unwrap());
477 // This is probably intended to be a non-test fn, but it is not used. I'm
478 // keeping it around unless it helps us test stuff.
479 fn uncommented(text: &str) -> String {
480 CharClasses::new(text.chars())
481 .filter_map(|(s, c)| {
483 CodeCharKind::Normal => Some(c),
484 CodeCharKind::Comment => None,
491 fn test_uncommented() {
492 assert_eq!(&uncommented("abc/*...*/"), "abc");
493 assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"),
495 assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf");
499 fn test_contains_comment() {
500 assert_eq!(contains_comment("abc"), false);
501 assert_eq!(contains_comment("abc // qsdf"), true);
502 assert_eq!(contains_comment("abc /* kqsdf"), true);
503 assert_eq!(contains_comment("abc \" /* */\" qsdf"), false);
507 fn test_find_uncommented() {
508 fn check(haystack: &str, needle: &str, expected: Option<usize>) {
509 assert_eq!(expected, haystack.find_uncommented(needle));
512 check("/*/ */test", "test", Some(6));
513 check("//test\ntest", "test", Some(7));
514 check("/* comment only */", "whatever", None);
515 check("/* comment */ some text /* more commentary */ result",
518 check("sup // sup", "p", Some(2));
519 check("sup", "x", None);
520 check(r#"π? /**/ π is nice!"#, r#"π is nice"#, Some(9));
521 check("/*sup yo? \n sup*/ sup", "p", Some(20));
522 check("hel/*lohello*/lo", "hello", None);
523 check("acb", "ab", None);
524 check(",/*A*/ ", ",", Some(0));
525 check("abc", "abc", Some(0));
526 check("/* abc */", "abc", None);
527 check("/**/abc/* */", "abc", Some(4));
528 check("\"/* abc */\"", "abc", Some(4));
529 check("\"/* abc", "abc", Some(4));