@@ -26,8 +26,11 @@ pub(crate) fn render(cx: &mut Context<'_>, krate: &clean::Crate) -> Result<(), E
26
26
27
27
let dst = cx. dst . join ( "src" ) . join ( krate. name ( cx. tcx ( ) ) . as_str ( ) ) ;
28
28
cx. shared . ensure_dir ( & dst) ?;
29
+ let crate_name = krate. name ( cx. tcx ( ) ) ;
30
+ let crate_name = crate_name. as_str ( ) ;
29
31
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 } ;
31
34
collector. visit_crate ( krate) ;
32
35
Ok ( ( ) )
33
36
}
@@ -115,6 +118,8 @@ struct SourceCollector<'a, 'tcx> {
115
118
/// Root destination to place all HTML output into
116
119
dst : PathBuf ,
117
120
emitted_local_sources : FxHashSet < PathBuf > ,
121
+
122
+ crate_name : & ' a str ,
118
123
}
119
124
120
125
impl DocVisitor for SourceCollector < ' _ , ' _ > {
@@ -210,19 +215,22 @@ impl SourceCollector<'_, '_> {
210
215
} ,
211
216
) ;
212
217
218
+ let src_fname = p. file_name ( ) . expect ( "source has no filename" ) . to_os_string ( ) ;
219
+ let mut fname = src_fname. clone ( ) ;
220
+
213
221
let root_path = PathBuf :: from ( "../../" ) . join ( root_path. into_inner ( ) ) ;
214
222
let mut root_path = root_path. to_string_lossy ( ) ;
215
223
if let Some ( c) = root_path. as_bytes ( ) . last ( )
216
224
&& * c != b'/'
217
225
{
218
226
root_path += "/" ;
219
227
}
228
+ let mut file_path = Path :: new ( & self . crate_name ) . join ( & * cur. borrow ( ) ) ;
229
+ file_path. push ( & fname) ;
230
+ fname. push ( ".html" ) ;
220
231
let mut cur = self . dst . join ( cur. into_inner ( ) ) ;
221
232
shared. ensure_dir ( & cur) ?;
222
233
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" ) ;
226
234
cur. push ( & fname) ;
227
235
228
236
let title = format ! ( "{} - source" , src_fname. to_string_lossy( ) ) ;
@@ -250,7 +258,7 @@ impl SourceCollector<'_, '_> {
250
258
cx,
251
259
& root_path,
252
260
highlight:: DecorationInfo :: default ( ) ,
253
- SourceContext :: Standalone ,
261
+ SourceContext :: Standalone { file_path } ,
254
262
)
255
263
} ,
256
264
& shared. style_files ,
@@ -312,10 +320,11 @@ struct ScrapedSource<'a, Code: std::fmt::Display> {
312
320
struct Source < Code : std:: fmt:: Display > {
313
321
lines : RangeInclusive < usize > ,
314
322
code_html : Code ,
323
+ file_path : Option < ( String , String ) > ,
315
324
}
316
325
317
326
pub ( crate ) enum SourceContext < ' a > {
318
- Standalone ,
327
+ Standalone { file_path : PathBuf } ,
319
328
Embedded ( ScrapedInfo < ' a > ) ,
320
329
}
321
330
@@ -344,9 +353,19 @@ pub(crate) fn print_src(
344
353
} ) ;
345
354
let lines = s. lines ( ) . count ( ) ;
346
355
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
+ } ,
349
366
}
367
+ . render_into ( & mut writer)
368
+ . unwrap ( ) ,
350
369
SourceContext :: Embedded ( info) => {
351
370
let lines = ( 1 + info. offset ) ..=( lines + info. offset ) ;
352
371
ScrapedSource { info, lines, code_html : code } . render_into ( & mut writer) . unwrap ( ) ;
0 commit comments