diff options
| author | Jacob Pratt <jacob@jhpratt.dev> | 2025-02-24 02:11:32 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-24 02:11:32 -0500 |
| commit | 81336525628e4f8f1696c918ec3622c1e3bd315e (patch) | |
| tree | 5d6e2d7db740dd4dafe4b0e1f10243cf50b04e46 /src/librustdoc/html/static/js/main.js | |
| parent | 6aa015ae9d7c0ebf95f1daabc77c5cb147b54d26 (diff) | |
| parent | a1471f3aa68ad97fc809e37c3c2c7b8b9e2ae227 (diff) | |
| download | rust-81336525628e4f8f1696c918ec3622c1e3bd315e.tar.gz rust-81336525628e4f8f1696c918ec3622c1e3bd315e.zip | |
Rollup merge of #136991 - GuillaumeGomez:code-wrapping, r=notriddle
[rustdoc] Add new setting to wrap source code lines when too long Fixes https://github.com/rust-lang/rust/issues/127334. Wrapped lines look like this:  It works in both source code pages and doc pages. You can test it [here](https://rustdoc.crud.net/imperio/code-wrapping/bar/index.html). r? ``@notriddle``
Diffstat (limited to 'src/librustdoc/html/static/js/main.js')
| -rw-r--r-- | src/librustdoc/html/static/js/main.js | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index e46cc1897e9..edfcc1291b9 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -1112,35 +1112,32 @@ function preLoadCss(cssUrl) { // @ts-expect-error window.rustdoc_add_line_numbers_to_examples = () => { - if (document.querySelector(".rustdoc.src")) { - // We are in the source code page, nothing to be done here! - return; + // @ts-expect-error + function generateLine(nb) { + return `<span data-nosnippet>${nb}</span>`; } + onEachLazy(document.querySelectorAll( - ":not(.scraped-example) > .example-wrap > pre:not(.example-line-numbers)", - ), x => { - const parent = x.parentNode; - const line_numbers = parent.querySelectorAll(".example-line-numbers"); - if (line_numbers.length > 0) { + ".rustdoc:not(.src) :not(.scraped-example) > .example-wrap > pre > code", + ), code => { + if (hasClass(code.parentElement.parentElement, "hide-lines")) { + removeClass(code.parentElement.parentElement, "hide-lines"); return; } - const count = x.textContent.split("\n").length; - const elems = []; - for (let i = 0; i < count; ++i) { - elems.push(i + 1); - } - const node = document.createElement("pre"); - addClass(node, "example-line-numbers"); - node.innerHTML = elems.join("\n"); - parent.insertBefore(node, x); + const lines = code.innerHTML.split("\n"); + const digits = (lines.length + "").length; + // @ts-expect-error + code.innerHTML = lines.map((line, index) => generateLine(index + 1) + line).join("\n"); + addClass(code.parentElement.parentElement, `digits-${digits}`); }); }; // @ts-expect-error window.rustdoc_remove_line_numbers_from_examples = () => { - onEachLazy(document.querySelectorAll(".example-wrap > .example-line-numbers"), x => { - x.parentNode.removeChild(x); - }); + onEachLazy( + document.querySelectorAll(".rustdoc:not(.src) :not(.scraped-example) > .example-wrap"), + x => addClass(x, "hide-lines"), + ); }; if (getSettingValue("line-numbers") === "true") { |
