Skip to content

Commit 828179d

Browse files
Add a button to copy the "use statement"
1 parent f35e587 commit 828179d

File tree

8 files changed

+57
-13
lines changed

8 files changed

+57
-13
lines changed

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,7 @@ fn init_id_map() -> FxHashMap<String, usize> {
13541354
map.insert("default-settings".to_owned(), 1);
13551355
map.insert("rustdoc-vars".to_owned(), 1);
13561356
map.insert("sidebar-vars".to_owned(), 1);
1357+
map.insert("copy-path".to_owned(), 1);
13571358
// This is the list of IDs used by rustdoc sections.
13581359
map.insert("fields".to_owned(), 1);
13591360
map.insert("variants".to_owned(), 1);

src/librustdoc/html/render/print_item.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
7373
}
7474
}
7575
write!(buf, "<a class=\"{}\" href=\"\">{}</a>", item.type_(), item.name.as_ref().unwrap());
76+
write!(buf, "<button id=\"copy-path\" onclick=\"copy_path(this)\">⎘</button>");
7677

7778
buf.write_str("</span>"); // in-band
7879
buf.write_str("<span class=\"out-of-band\">");

src/librustdoc/html/static/main.js

+25
Original file line numberDiff line numberDiff line change
@@ -3061,3 +3061,28 @@ function hideThemeButtonState() {
30613061
window.onhashchange = onHashChange;
30623062
setupSearchLoader();
30633063
}());
3064+
3065+
function copy_path(but) {
3066+
var parent = but.parentElement;
3067+
var path = [];
3068+
3069+
onEach(parent.childNodes, function(child) {
3070+
if (child.tagName === 'A') {
3071+
path.push(child.textContent);
3072+
}
3073+
});
3074+
3075+
var el = document.createElement('textarea');
3076+
el.value = 'use ' + path.join('::') + ';';
3077+
el.setAttribute('readonly', '');
3078+
// To not make it appear on the screen.
3079+
el.style.position = 'absolute';
3080+
el.style.left = '-9999px';
3081+
3082+
document.body.appendChild(el);
3083+
el.select();
3084+
document.execCommand('copy');
3085+
document.body.removeChild(el);
3086+
3087+
but.textContent = '✓';
3088+
}

src/librustdoc/html/static/noscript.css

+5
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ rules.
3333
/* Since there is no toggle (the "[-]") when JS is disabled, no need for this margin either. */
3434
margin-left: 0 !important;
3535
}
36+
37+
#copy-path {
38+
/* It requires JS to work so no need to display it in this case. */
39+
display: none;
40+
}

src/librustdoc/html/static/rustdoc.css

+16-7
Original file line numberDiff line numberDiff line change
@@ -1316,20 +1316,29 @@ h4 > .notable-traits {
13161316
outline: none;
13171317
}
13181318

1319+
#theme-picker, #settings-menu, .help-button, #copy-path {
1320+
padding: 4px;
1321+
width: 27px;
1322+
height: 29px;
1323+
border: 1px solid;
1324+
border-radius: 3px;
1325+
cursor: pointer;
1326+
}
1327+
13191328
.help-button {
13201329
right: 30px;
13211330
font-family: "Fira Sans", Arial, sans-serif;
13221331
text-align: center;
13231332
font-size: 17px;
1333+
padding-top: 2px;
13241334
}
13251335

1326-
#theme-picker, #settings-menu, .help-button {
1327-
padding: 4px;
1328-
width: 27px;
1329-
height: 29px;
1330-
border: 1px solid;
1331-
border-radius: 3px;
1332-
cursor: pointer;
1336+
#copy-path {
1337+
height: 30px;
1338+
font-size: 18px;
1339+
margin-left: 10px;
1340+
padding: 0 6px;
1341+
width: 28px;
13331342
}
13341343

13351344
#theme-choices {

src/librustdoc/html/static/themes/ayu.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ kbd {
498498
box-shadow-color: #c6cbd1;
499499
}
500500

501-
#theme-picker, #settings-menu, .help-button {
501+
#theme-picker, #settings-menu, .help-button, #copy-path {
502502
border-color: #5c6773;
503503
background-color: #0f1419;
504504
color: #fff;
@@ -510,7 +510,8 @@ kbd {
510510

511511
#theme-picker:hover, #theme-picker:focus,
512512
#settings-menu:hover, #settings-menu:focus,
513-
.help-button:hover, .help-button:focus {
513+
.help-button:hover, .help-button:focus,
514+
#copy-path:hover, #copy-path:focus {
514515
border-color: #e0e0e0;
515516
}
516517

src/librustdoc/html/static/themes/dark.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,16 @@ kbd {
388388
box-shadow-color: #c6cbd1;
389389
}
390390

391-
#theme-picker, #settings-menu, .help-button {
391+
#theme-picker, #settings-menu, .help-button, #copy-path {
392392
border-color: #e0e0e0;
393393
background: #f0f0f0;
394394
color: #000;
395395
}
396396

397397
#theme-picker:hover, #theme-picker:focus,
398398
#settings-menu:hover, #settings-menu:focus,
399-
.help-button:hover, .help-button:focus {
399+
.help-button:hover, .help-button:focus,
400+
#copy-path:hover, #copy-path:focus {
400401
border-color: #ffb900;
401402
}
402403

src/librustdoc/html/static/themes/light.css

+3-2
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,15 @@ kbd {
380380
box-shadow-color: #c6cbd1;
381381
}
382382

383-
#theme-picker, #settings-menu, .help-button {
383+
#theme-picker, #settings-menu, .help-button, #copy-path {
384384
border-color: #e0e0e0;
385385
background-color: #fff;
386386
}
387387

388388
#theme-picker:hover, #theme-picker:focus,
389389
#settings-menu:hover, #settings-menu:focus,
390-
.help-button:hover, .help-button:focus {
390+
.help-button:hover, .help-button:focus,
391+
#copy-path:hover, #copy-path:focus {
391392
border-color: #717171;
392393
}
393394

0 commit comments

Comments
 (0)