Skip to content

Commit 080aa37

Browse files
authored
Rollup merge of #83721 - GuillaumeGomez:copy-use, r=Nemo157
Add a button to copy the "use statement" Fixes #50239 When clicking on the button, it'll add the elements prepended by "use " and will end with a ";". So in the images below, I now have in my clipboard `use std::fs::OpenOptions;`. A screenshot of the newly added button: ![Screenshot from 2021-03-31 22-12-12](https://user-images.githubusercontent.com/3050060/113205430-90e64500-926e-11eb-8538-529829f611ec.png) A screenshot after it was clicked: ![Screenshot from 2021-03-31 22-15-31](https://user-images.githubusercontent.com/3050060/113205532-ad827d00-926e-11eb-893d-35f2f8f92696.png) r? `@Nemo157`
2 parents 03ba8ab + 828179d commit 080aa37

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
@@ -1355,6 +1355,7 @@ fn init_id_map() -> FxHashMap<String, usize> {
13551355
map.insert("default-settings".to_owned(), 1);
13561356
map.insert("rustdoc-vars".to_owned(), 1);
13571357
map.insert("sidebar-vars".to_owned(), 1);
1358+
map.insert("copy-path".to_owned(), 1);
13581359
// This is the list of IDs used by rustdoc sections.
13591360
map.insert("fields".to_owned(), 1);
13601361
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
@@ -1318,20 +1318,29 @@ h4 > .notable-traits {
13181318
outline: none;
13191319
}
13201320

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

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

13371346
#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)