@@ -26,8 +26,11 @@ pub(crate) fn render(cx: &mut Context<'_>, krate: &clean::Crate) -> Result<(), E
2626
2727 let dst = cx. dst . join ( "src" ) . join ( krate. name ( cx. tcx ( ) ) . as_str ( ) ) ;
2828 cx. shared . ensure_dir ( & dst) ?;
29+ let crate_name = krate. name ( cx. tcx ( ) ) ;
30+ let crate_name = crate_name. as_str ( ) ;
2931
30- let mut collector = SourceCollector { dst, cx, emitted_local_sources : FxHashSet :: default ( ) } ;
32+ let mut collector =
33+ SourceCollector { dst, cx, emitted_local_sources : FxHashSet :: default ( ) , crate_name } ;
3134 collector. visit_crate ( krate) ;
3235 Ok ( ( ) )
3336}
@@ -115,6 +118,8 @@ struct SourceCollector<'a, 'tcx> {
115118 /// Root destination to place all HTML output into
116119 dst : PathBuf ,
117120 emitted_local_sources : FxHashSet < PathBuf > ,
121+
122+ crate_name : & ' a str ,
118123}
119124
120125impl DocVisitor for SourceCollector < ' _ , ' _ > {
@@ -210,19 +215,22 @@ impl SourceCollector<'_, '_> {
210215 } ,
211216 ) ;
212217
218+ let src_fname = p. file_name ( ) . expect ( "source has no filename" ) . to_os_string ( ) ;
219+ let mut fname = src_fname. clone ( ) ;
220+
213221 let root_path = PathBuf :: from ( "../../" ) . join ( root_path. into_inner ( ) ) ;
214222 let mut root_path = root_path. to_string_lossy ( ) ;
215223 if let Some ( c) = root_path. as_bytes ( ) . last ( )
216224 && * c != b'/'
217225 {
218226 root_path += "/" ;
219227 }
228+ let mut file_path = Path :: new ( & self . crate_name ) . join ( & * cur. borrow ( ) ) ;
229+ file_path. push ( & fname) ;
230+ fname. push ( ".html" ) ;
220231 let mut cur = self . dst . join ( cur. into_inner ( ) ) ;
221232 shared. ensure_dir ( & cur) ?;
222233
223- let src_fname = p. file_name ( ) . expect ( "source has no filename" ) . to_os_string ( ) ;
224- let mut fname = src_fname. clone ( ) ;
225- fname. push ( ".html" ) ;
226234 cur. push ( & fname) ;
227235
228236 let title = format ! ( "{} - source" , src_fname. to_string_lossy( ) ) ;
@@ -250,7 +258,7 @@ impl SourceCollector<'_, '_> {
250258 cx,
251259 & root_path,
252260 highlight:: DecorationInfo :: default ( ) ,
253- SourceContext :: Standalone ,
261+ SourceContext :: Standalone { file_path } ,
254262 )
255263 } ,
256264 & shared. style_files ,
@@ -312,10 +320,11 @@ struct ScrapedSource<'a, Code: std::fmt::Display> {
312320struct Source < Code : std:: fmt:: Display > {
313321 lines : RangeInclusive < usize > ,
314322 code_html : Code ,
323+ file_path : Option < ( String , String ) > ,
315324}
316325
317326pub ( crate ) enum SourceContext < ' a > {
318- Standalone ,
327+ Standalone { file_path : PathBuf } ,
319328 Embedded ( ScrapedInfo < ' a > ) ,
320329}
321330
@@ -344,9 +353,19 @@ pub(crate) fn print_src(
344353 } ) ;
345354 let lines = s. lines ( ) . count ( ) ;
346355 match source_context {
347- SourceContext :: Standalone => {
348- Source { lines : ( 1 ..=lines) , code_html : code } . render_into ( & mut writer) . unwrap ( )
356+ SourceContext :: Standalone { file_path } => Source {
357+ lines : ( 1 ..=lines) ,
358+ code_html : code,
359+ file_path : if let Some ( file_name) = file_path. file_name ( )
360+ && let Some ( file_path) = file_path. parent ( )
361+ {
362+ Some ( ( file_path. display ( ) . to_string ( ) , file_name. display ( ) . to_string ( ) ) )
363+ } else {
364+ None
365+ } ,
349366 }
367+ . render_into ( & mut writer)
368+ . unwrap ( ) ,
350369 SourceContext :: Embedded ( info) => {
351370 let lines = ( 1 + info. offset ) ..=( lines + info. offset ) ;
352371 ScrapedSource { info, lines, code_html : code } . render_into ( & mut writer) . unwrap ( ) ;
0 commit comments