@@ -914,7 +914,6 @@ crate struct DocFragment {
914
914
crate parent_module : Option < DefId > ,
915
915
crate doc : Symbol ,
916
916
crate kind : DocFragmentKind ,
917
- crate need_backline : bool ,
918
917
crate indent : usize ,
919
918
}
920
919
@@ -930,20 +929,16 @@ crate enum DocFragmentKind {
930
929
RawDoc ,
931
930
}
932
931
933
- // The goal of this function is to apply the `DocFragment` transformations that are required when
934
- // transforming into the final markdown. So the transformations in here are:
935
- //
936
- // * Applying the computed indent to each lines in each doc fragment (a `DocFragment` can contain
937
- // multiple lines in case of `#[doc = ""]`).
938
- // * Adding backlines between `DocFragment`s and adding an extra one if required (stored in the
939
- // `need_backline` field).
932
+ /// The goal of this function is to apply the `DocFragment` transformation that is required when
933
+ /// transforming into the final Markdown, which is applying the computed indent to each line in
934
+ /// each doc fragment (a `DocFragment` can contain multiple lines in case of `#[doc = ""]`).
935
+ ///
936
+ /// Note: remove the trailing newline where appropriate
940
937
fn add_doc_fragment ( out : & mut String , frag : & DocFragment ) {
941
938
let s = frag. doc . as_str ( ) ;
942
939
let mut iter = s. lines ( ) ;
943
940
if s == "" {
944
- if frag. need_backline {
945
- out. push ( '\n' ) ;
946
- }
941
+ out. push ( '\n' ) ;
947
942
return ;
948
943
}
949
944
while let Some ( line) = iter. next ( ) {
@@ -955,9 +950,6 @@ fn add_doc_fragment(out: &mut String, frag: &DocFragment) {
955
950
}
956
951
out. push ( '\n' ) ;
957
952
}
958
- if !frag. need_backline {
959
- out. pop ( ) ;
960
- }
961
953
}
962
954
963
955
/// Collapse a collection of [`DocFragment`]s into one string,
@@ -967,6 +959,7 @@ crate fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
967
959
for frag in doc_strings {
968
960
add_doc_fragment ( & mut acc, frag) ;
969
961
}
962
+ acc. pop ( ) ;
970
963
acc
971
964
}
972
965
@@ -1032,7 +1025,6 @@ impl Attributes {
1032
1025
additional_attrs : Option < ( & [ ast:: Attribute ] , DefId ) > ,
1033
1026
) -> Attributes {
1034
1027
let mut doc_strings: Vec < DocFragment > = vec ! [ ] ;
1035
-
1036
1028
let clean_attr = |( attr, parent_module) : ( & ast:: Attribute , Option < DefId > ) | {
1037
1029
if let Some ( value) = attr. doc_str ( ) {
1038
1030
trace ! ( "got doc_str={:?}" , value) ;
@@ -1043,18 +1035,8 @@ impl Attributes {
1043
1035
DocFragmentKind :: RawDoc
1044
1036
} ;
1045
1037
1046
- let frag = DocFragment {
1047
- span : attr. span ,
1048
- doc : value,
1049
- kind,
1050
- parent_module,
1051
- need_backline : false ,
1052
- indent : 0 ,
1053
- } ;
1054
-
1055
- if let Some ( prev) = doc_strings. last_mut ( ) {
1056
- prev. need_backline = true ;
1057
- }
1038
+ let frag =
1039
+ DocFragment { span : attr. span , doc : value, kind, parent_module, indent : 0 } ;
1058
1040
1059
1041
doc_strings. push ( frag) ;
1060
1042
@@ -1090,6 +1072,7 @@ impl Attributes {
1090
1072
}
1091
1073
add_doc_fragment ( & mut out, new_frag) ;
1092
1074
}
1075
+ out. pop ( ) ;
1093
1076
if out. is_empty ( ) { None } else { Some ( out) }
1094
1077
}
1095
1078
@@ -1098,10 +1081,17 @@ impl Attributes {
1098
1081
/// The module can be different if this is a re-export with added documentation.
1099
1082
crate fn collapsed_doc_value_by_module_level ( & self ) -> FxHashMap < Option < DefId > , String > {
1100
1083
let mut ret = FxHashMap :: default ( ) ;
1084
+ if self . doc_strings . len ( ) == 0 {
1085
+ return ret;
1086
+ }
1087
+ let last_index = self . doc_strings . len ( ) - 1 ;
1101
1088
1102
- for new_frag in self . doc_strings . iter ( ) {
1089
+ for ( i , new_frag) in self . doc_strings . iter ( ) . enumerate ( ) {
1103
1090
let out = ret. entry ( new_frag. parent_module ) . or_default ( ) ;
1104
1091
add_doc_fragment ( out, new_frag) ;
1092
+ if i == last_index {
1093
+ out. pop ( ) ;
1094
+ }
1105
1095
}
1106
1096
ret
1107
1097
}
0 commit comments