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.
4
- //
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.
10
-
11
1
// Formatting and tools for comments.
12
2
13
3
use std:: { self , borrow:: Cow , iter} ;
@@ -54,7 +44,7 @@ fn custom_opener(s: &str) -> &str {
54
44
}
55
45
56
46
impl < ' a > CommentStyle < ' a > {
57
- /// Returns true if the commenting style covers a line only.
47
+ /// Returns ` true` if the commenting style covers a line only.
58
48
pub fn is_line_comment ( & self ) -> bool {
59
49
match * self {
60
50
CommentStyle :: DoubleSlash
@@ -65,7 +55,7 @@ impl<'a> CommentStyle<'a> {
65
55
}
66
56
}
67
57
68
- /// Returns true if the commenting style can span over multiple lines.
58
+ /// Returns ` true` if the commenting style can span over multiple lines.
69
59
pub fn is_block_comment ( & self ) -> bool {
70
60
match * self {
71
61
CommentStyle :: SingleBullet | CommentStyle :: DoubleBullet | CommentStyle :: Exclamation => {
@@ -75,7 +65,7 @@ impl<'a> CommentStyle<'a> {
75
65
}
76
66
}
77
67
78
- /// Returns true if the commenting style is for documentation.
68
+ /// Returns ` true` if the commenting style is for documentation.
79
69
pub fn is_doc_comment ( & self ) -> bool {
80
70
match * self {
81
71
CommentStyle :: TripleSlash | CommentStyle :: Doc => true ,
@@ -439,7 +429,7 @@ struct ItemizedBlock {
439
429
}
440
430
441
431
impl ItemizedBlock {
442
- /// Returns true if the line is formatted as an item
432
+ /// Returns ` true` if the line is formatted as an item
443
433
fn is_itemized_line ( line : & str ) -> bool {
444
434
let trimmed = line. trim_start ( ) ;
445
435
trimmed. starts_with ( "* " ) || trimmed. starts_with ( "- " )
@@ -458,7 +448,7 @@ impl ItemizedBlock {
458
448
}
459
449
}
460
450
461
- /// Returns a `StringFormat` used for formatting the content of an item
451
+ /// Returns a `StringFormat` used for formatting the content of an item.
462
452
fn create_string_format < ' a > ( & ' a self , fmt : & ' a StringFormat < ' _ > ) -> StringFormat < ' a > {
463
453
StringFormat {
464
454
opener : "" ,
@@ -471,8 +461,8 @@ impl ItemizedBlock {
471
461
}
472
462
}
473
463
474
- /// Returns true if the line is part of the current itemized block.
475
- /// If it is, then it is added to the internal lines vec .
464
+ /// Returns ` true` if the line is part of the current itemized block.
465
+ /// If it is, then it is added to the internal lines list .
476
466
fn add_line ( & mut self , line : & str ) -> bool {
477
467
if !ItemizedBlock :: is_itemized_line ( line)
478
468
&& self . indent <= line. chars ( ) . take_while ( |c| c. is_whitespace ( ) ) . count ( )
@@ -491,7 +481,7 @@ impl ItemizedBlock {
491
481
. collect :: < String > ( )
492
482
}
493
483
494
- /// Returns the block as a string under its original form
484
+ /// Returns the block as a string under its original form.
495
485
fn original_block_as_string ( & self ) -> String {
496
486
self . lines . join ( "\n " )
497
487
}
@@ -842,7 +832,7 @@ fn trim_custom_comment_prefix(s: &str) -> String {
842
832
. join ( "\n " )
843
833
}
844
834
845
- /// Returns true if the given string MAY include URLs or alike.
835
+ /// Returns ` true` if the given string MAY include URLs or alike.
846
836
fn has_url ( s : & str ) -> bool {
847
837
// This function may return false positive, but should get its job done in most cases.
848
838
s. contains ( "https://" ) || s. contains ( "http://" ) || s. contains ( "ftp://" ) || s. contains ( "file://" )
@@ -1000,8 +990,8 @@ impl FindUncommented for str {
1000
990
1001
991
// Returns the first byte position after the first comment. The given string
1002
992
// is expected to be prefixed by a comment, including delimiters.
1003
- // Good: " /* /* inner */ outer */ code();"
1004
- // Bad: " code(); // hello\n world!"
993
+ // Good: ` /* /* inner */ outer */ code();`
994
+ // Bad: ` code(); // hello\n world!`
1005
995
pub fn find_comment_end ( s : & str ) -> Option < usize > {
1006
996
let mut iter = CharClasses :: new ( s. char_indices ( ) ) ;
1007
997
for ( kind, ( i, _c) ) in & mut iter {
@@ -1010,15 +1000,15 @@ pub fn find_comment_end(s: &str) -> Option<usize> {
1010
1000
}
1011
1001
}
1012
1002
1013
- // Handle case where the comment ends at the end of s .
1003
+ // Handle case where the comment ends at the end of `s` .
1014
1004
if iter. status == CharClassesStatus :: Normal {
1015
1005
Some ( s. len ( ) )
1016
1006
} else {
1017
1007
None
1018
1008
}
1019
1009
}
1020
1010
1021
- /// Returns true if text contains any comment.
1011
+ /// Returns ` true` if text contains any comment.
1022
1012
pub fn contains_comment ( text : & str ) -> bool {
1023
1013
CharClasses :: new ( text. chars ( ) ) . any ( |( kind, _) | kind. is_comment ( ) )
1024
1014
}
@@ -1540,7 +1530,7 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
1540
1530
}
1541
1531
1542
1532
/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
1543
- /// (if it fits in the width/offset, else return None), else return `new`
1533
+ /// (if it fits in the width/offset, else return ` None` ), else return `new`
1544
1534
pub fn recover_comment_removed (
1545
1535
new : String ,
1546
1536
span : Span ,
@@ -1583,14 +1573,14 @@ pub fn filter_normal_code(code: &str) -> String {
1583
1573
buffer
1584
1574
}
1585
1575
1586
- /// Return true if the two strings of code have the same payload of comments.
1576
+ /// Returns ` true` if the two strings of code have the same payload of comments.
1587
1577
/// The payload of comments is everything in the string except:
1588
- /// - actual code (not comments)
1589
- /// - comment start/end marks
1590
- /// - whitespace
1591
- /// - '*' at the beginning of lines in block comments
1578
+ /// - actual code (not comments),
1579
+ /// - comment start/end marks,
1580
+ /// - whitespace,
1581
+ /// - '*' at the beginning of lines in block comments.
1592
1582
fn changed_comment_content ( orig : & str , new : & str ) -> bool {
1593
- // Cannot write this as a fn since we cannot return types containing closures
1583
+ // Cannot write this as a fn since we cannot return types containing closures.
1594
1584
let code_comment_content = |code| {
1595
1585
let slices = UngroupedCommentCodeSlices :: new ( code) ;
1596
1586
slices
@@ -1625,7 +1615,8 @@ impl<'a> CommentReducer<'a> {
1625
1615
let comment = remove_comment_header ( comment) ;
1626
1616
CommentReducer {
1627
1617
is_block,
1628
- at_start_line : false , // There are no supplementary '*' on the first line
1618
+ // There are no supplementary '*' on the first line.
1619
+ at_start_line : false ,
1629
1620
iter : comment. chars ( ) ,
1630
1621
}
1631
1622
}
@@ -1641,7 +1632,7 @@ impl<'a> Iterator for CommentReducer<'a> {
1641
1632
while c. is_whitespace ( ) {
1642
1633
c = self . iter . next ( ) ?;
1643
1634
}
1644
- // Ignore leading '*'
1635
+ // Ignore leading '*'.
1645
1636
if c == '*' {
1646
1637
c = self . iter . next ( ) ?;
1647
1638
}
@@ -1777,7 +1768,7 @@ mod test {
1777
1768
& wrap_normalize_config) . unwrap ( ) ;
1778
1769
assert_eq ! ( "/* trimmed */" , comment) ;
1779
1770
1780
- // check that different comment style are properly recognised
1771
+ // Check that different comment style are properly recognised.
1781
1772
let comment = rewrite_comment ( r#"/// test1
1782
1773
/// test2
1783
1774
/*
@@ -1788,7 +1779,7 @@ mod test {
1788
1779
& wrap_normalize_config) . unwrap ( ) ;
1789
1780
assert_eq ! ( "/// test1\n /// test2\n // test3" , comment) ;
1790
1781
1791
- // check that the blank line marks the end of a commented paragraph
1782
+ // Check that the blank line marks the end of a commented paragraph.
1792
1783
let comment = rewrite_comment ( r#"// test1
1793
1784
1794
1785
// test2"# ,
@@ -1797,7 +1788,7 @@ mod test {
1797
1788
& wrap_normalize_config) . unwrap ( ) ;
1798
1789
assert_eq ! ( "// test1\n \n // test2" , comment) ;
1799
1790
1800
- // check that the blank line marks the end of a custom-commented paragraph
1791
+ // Check that the blank line marks the end of a custom-commented paragraph.
1801
1792
let comment = rewrite_comment ( r#"//@ test1
1802
1793
1803
1794
//@ test2"# ,
@@ -1806,7 +1797,7 @@ mod test {
1806
1797
& wrap_normalize_config) . unwrap ( ) ;
1807
1798
assert_eq ! ( "//@ test1\n \n //@ test2" , comment) ;
1808
1799
1809
- // check that bare lines are just indented but left unchanged otherwise
1800
+ // Check that bare lines are just indented but otherwise left unchanged.
1810
1801
let comment = rewrite_comment ( r#"// test1
1811
1802
/*
1812
1803
a bare line!
@@ -1819,8 +1810,8 @@ mod test {
1819
1810
assert_eq ! ( "// test1\n /*\n a bare line!\n \n another bare line!\n */" , comment) ;
1820
1811
}
1821
1812
1822
- // This is probably intended to be a non-test fn, but it is not used. I'm
1823
- // keeping it around unless it helps us test stuff.
1813
+ // This is probably intended to be a non-test fn, but it is not used.
1814
+ // We should keep this around unless it helps us test stuff to remove it .
1824
1815
fn uncommented ( text : & str ) -> String {
1825
1816
CharClasses :: new ( text. chars ( ) )
1826
1817
. filter_map ( |( s, c) | match s {
0 commit comments