Skip to content

Commit 574e3dd

Browse files
Finish porting lint functionalities to vanilla JS
1 parent 0055ceb commit 574e3dd

File tree

3 files changed

+140
-113
lines changed

3 files changed

+140
-113
lines changed

util/gh-pages/index_template.html

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,13 @@ <h1>Clippy Lints</h1>
199199
</div>
200200
</div>
201201
{% for lint in lints %}
202-
<article class="panel panel-default" id="{(lint.id)}">
203-
<header class="panel-heading" ng-click="open[lint.id] = !open[lint.id]">
202+
<article class="panel panel-default collapsed" id="{(lint.id)}">
203+
<header class="panel-heading" onclick="expandLint('{(lint.id)}')">
204204
<h2 class="panel-title">
205-
<div class="panel-title-name">
205+
<div class="panel-title-name" id="lint-{(lint.id)}">
206206
<span>{(lint.id)}</span>
207-
<a href="#{(lint.id)}" class="anchor label label-default"
208-
ng-click="openLint(lint); $event.preventDefault(); $event.stopPropagation()">&para;</a>
209-
<a href="" id="clipboard-{(lint.id)}" class="anchor label label-default" ng-click="copyToClipboard(lint); $event.stopPropagation()">
207+
<a href="#{(lint.id)}" class="anchor label label-default" onclick="openLint(event)">&para;</a>
208+
<a href="" class="anchor label label-default" onclick="copyToClipboard(event)">
210209
&#128203;
211210
</a>
212211
</div>
@@ -217,8 +216,7 @@ <h2 class="panel-title">
217216
<span class="label label-lint-level label-lint-level-{(lint.level)}">{(lint.level)}</span>
218217

219218

220-
<span class="label label-doc-folding" ng-show="open[lint.id]">&minus;</span>
221-
<span class="label label-doc-folding" ng-hide="open[lint.id]">&plus;</span>
219+
<span class="label label-doc-folding">&plus;</span>
222220
</div>
223221
</h2>
224222
</header>
@@ -239,13 +237,13 @@ <h2 class="panel-title">
239237
</div>
240238
{# Open related issues #}
241239
<div class="lint-additional-info-item">
242-
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a>
240+
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{(lint.id)}">Related Issues</a>
243241
</div>
244242

245243
{# Jump to source #}
246244
{% if let Some(id_location) = lint.id_location %}
247245
<div class="lint-additional-info-item">
248-
<a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/clippy_lints/{{id_location}}">View Source</a>
246+
<a href="https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/{(id_location)}">View Source</a>
249247
{% endif %}
250248
</div>
251249
</div>
@@ -315,7 +313,6 @@ <h2 class="panel-title">
315313
</style>
316314
</a>
317315

318-
<script src="https://cdnjs.cloudflare.com/ajax/libs/markdown-it/12.3.2/markdown-it.min.js"></script>
319316
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>
320317
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/languages/rust.min.js"></script>
321318
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.12/angular.min.js"></script>

util/gh-pages/script.js

Lines changed: 128 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
(function () {
2-
function scrollToLint(lintId) {
3-
const target = document.getElementById(lintId);
4-
if (!target) {
5-
return;
6-
}
7-
target.scrollIntoView();
8-
}
9-
10-
function scrollToLintByURL($scope, $location) {
11-
const removeListener = $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
12-
scrollToLint($location.path().substring(1));
13-
removeListener();
14-
});
15-
}
16-
172
function selectGroup($scope, selectedGroup) {
183
const groups = $scope.groups;
194
for (const group in groups) {
@@ -365,39 +350,12 @@
365350
return $scope.applicabilities[lint.applicability];
366351
};
367352

368-
// Show details for one lint
369-
$scope.openLint = function (lint) {
370-
$scope.open[lint.id] = true;
371-
$location.path(lint.id);
372-
};
373-
374353
$scope.toggleExpansion = function(lints, isExpanded) {
375354
lints.forEach(lint => {
376355
$scope.open[lint.id] = isExpanded;
377356
});
378357
}
379358

380-
$scope.copyToClipboard = function (lint) {
381-
const clipboard = document.getElementById("clipboard-" + lint.id);
382-
if (clipboard) {
383-
let resetClipboardTimeout = null;
384-
const resetClipboardIcon = clipboard.innerHTML;
385-
386-
function resetClipboard() {
387-
resetClipboardTimeout = null;
388-
clipboard.innerHTML = resetClipboardIcon;
389-
}
390-
391-
navigator.clipboard.writeText("clippy::" + lint.id);
392-
393-
clipboard.innerHTML = "&#10003;";
394-
if (resetClipboardTimeout !== null) {
395-
clearTimeout(resetClipboardTimeout);
396-
}
397-
resetClipboardTimeout = setTimeout(resetClipboard, 1000);
398-
}
399-
}
400-
401359
// Get data
402360
$scope.open = {};
403361
$scope.loading = true;
@@ -413,8 +371,6 @@
413371
selectGroup($scope, selectedGroup.toLowerCase());
414372
}
415373

416-
scrollToLintByURL($scope, $location);
417-
418374
setTimeout(function () {
419375
const el = document.getElementById('filter-input');
420376
if (el) { el.focus() }
@@ -433,6 +389,65 @@ function getQueryVariable(variable) {
433389
}
434390
}
435391

392+
function storeValue(settingName, value) {
393+
try {
394+
localStorage.setItem(`clippy-lint-list-${settingName}`, value);
395+
} catch (e) { }
396+
}
397+
398+
function loadValue(settingName) {
399+
return localStorage.getItem(`clippy-lint-list-${settingName}`);
400+
}
401+
402+
function setTheme(theme, store) {
403+
let enableHighlight = false;
404+
let enableNight = false;
405+
let enableAyu = false;
406+
407+
switch(theme) {
408+
case "ayu":
409+
enableAyu = true;
410+
break;
411+
case "coal":
412+
case "navy":
413+
enableNight = true;
414+
break;
415+
case "rust":
416+
enableHighlight = true;
417+
break;
418+
default:
419+
enableHighlight = true;
420+
theme = "light";
421+
break;
422+
}
423+
424+
document.getElementsByTagName("body")[0].className = theme;
425+
426+
document.getElementById("githubLightHighlight").disabled = enableNight || !enableHighlight;
427+
document.getElementById("githubDarkHighlight").disabled = !enableNight && !enableAyu;
428+
429+
document.getElementById("styleHighlight").disabled = !enableHighlight;
430+
document.getElementById("styleNight").disabled = !enableNight;
431+
document.getElementById("styleAyu").disabled = !enableAyu;
432+
433+
if (store) {
434+
storeValue("theme", theme);
435+
} else {
436+
document.getElementById(`theme-choice`).value = theme;
437+
}
438+
}
439+
440+
// loading the theme after the initial load
441+
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
442+
const theme = loadValue('theme');
443+
if (prefersDark.matches && !theme) {
444+
setTheme("coal", false);
445+
} else {
446+
setTheme(theme, false);
447+
}
448+
let disableShortcuts = loadValue('disable-shortcuts') === "true";
449+
document.getElementById("disable-shortcuts").checked = disableShortcuts;
450+
436451
window.searchState = {
437452
timeout: null,
438453
inputElem: document.getElementById("search-input"),
@@ -486,6 +501,11 @@ window.searchState = {
486501
lint.style.display = "none";
487502
}
488503
});
504+
if (searchStr.length > 0) {
505+
window.location.hash = `/${searchStr}`;
506+
} else {
507+
window.location.hash = '';
508+
}
489509
},
490510
};
491511

@@ -496,54 +516,6 @@ function handleInputChanged(event) {
496516
searchState.resetInputTimeout();
497517
}
498518

499-
function storeValue(settingName, value) {
500-
try {
501-
localStorage.setItem(`clippy-lint-list-${settingName}`, value);
502-
} catch (e) { }
503-
}
504-
505-
function loadValue(settingName) {
506-
return localStorage.getItem(`clippy-lint-list-${settingName}`);
507-
}
508-
509-
function setTheme(theme, store) {
510-
let enableHighlight = false;
511-
let enableNight = false;
512-
let enableAyu = false;
513-
514-
switch(theme) {
515-
case "ayu":
516-
enableAyu = true;
517-
break;
518-
case "coal":
519-
case "navy":
520-
enableNight = true;
521-
break;
522-
case "rust":
523-
enableHighlight = true;
524-
break;
525-
default:
526-
enableHighlight = true;
527-
theme = "light";
528-
break;
529-
}
530-
531-
document.getElementsByTagName("body")[0].className = theme;
532-
533-
document.getElementById("githubLightHighlight").disabled = enableNight || !enableHighlight;
534-
document.getElementById("githubDarkHighlight").disabled = !enableNight && !enableAyu;
535-
536-
document.getElementById("styleHighlight").disabled = !enableHighlight;
537-
document.getElementById("styleNight").disabled = !enableNight;
538-
document.getElementById("styleAyu").disabled = !enableAyu;
539-
540-
if (store) {
541-
storeValue("theme", theme);
542-
} else {
543-
document.getElementById(`theme-choice`).value = theme;
544-
}
545-
}
546-
547519
function handleShortcut(ev) {
548520
if (ev.ctrlKey || ev.altKey || ev.metaKey || disableShortcuts) {
549521
return;
@@ -584,6 +556,52 @@ function onEachLazy(lazyArray, func) {
584556
}
585557
}
586558

559+
function expandLintId(lintId) {
560+
searchState.inputElem.value = lintId;
561+
searchState.filterLints();
562+
563+
// Expand the lint.
564+
const lintElem = document.getElementById(lintId);
565+
const isCollapsed = lintElem.classList.remove("collapsed");
566+
lintElem.querySelector(".label-doc-folding").innerText = "-";
567+
}
568+
569+
// Show details for one lint
570+
function openLint(event) {
571+
event.preventDefault();
572+
event.stopPropagation();
573+
expandLintId(event.target.getAttribute("href").slice(1));
574+
}
575+
576+
function expandLint(lintId) {
577+
const lintElem = document.getElementById(lintId);
578+
const isCollapsed = lintElem.classList.toggle("collapsed");
579+
lintElem.querySelector(".label-doc-folding").innerText = isCollapsed ? "+" : "-";
580+
}
581+
582+
function copyToClipboard(event) {
583+
event.preventDefault();
584+
event.stopPropagation();
585+
586+
const clipboard = event.target;
587+
588+
let resetClipboardTimeout = null;
589+
const resetClipboardIcon = clipboard.innerHTML;
590+
591+
function resetClipboard() {
592+
resetClipboardTimeout = null;
593+
clipboard.innerHTML = resetClipboardIcon;
594+
}
595+
596+
navigator.clipboard.writeText("clippy::" + clipboard.parentElement.id.slice(5));
597+
598+
clipboard.innerHTML = "&#10003;";
599+
if (resetClipboardTimeout !== null) {
600+
clearTimeout(resetClipboardTimeout);
601+
}
602+
resetClipboardTimeout = setTimeout(resetClipboard, 1000);
603+
}
604+
587605
function handleBlur(event) {
588606
const parent = document.getElementById("settings-dropdown");
589607
if (!parent.contains(document.activeElement) &&
@@ -617,15 +635,23 @@ function generateSearch() {
617635
generateSettings();
618636
generateSearch();
619637

620-
// loading the theme after the initial load
621-
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
622-
const theme = loadValue('theme');
623-
if (prefersDark.matches && !theme) {
624-
setTheme("coal", false);
625-
} else {
626-
setTheme(theme, false);
638+
function scrollToLint(lintId) {
639+
const target = document.getElementById(lintId);
640+
if (!target) {
641+
return;
642+
}
643+
target.scrollIntoView();
644+
expandLintId(lintId);
627645
}
628-
let disableShortcuts = loadValue('disable-shortcuts') === "true";
629-
document.getElementById("disable-shortcuts").checked = disableShortcuts;
630646

631-
hljs.highlightAll();
647+
// If the page we arrive on has link to a given lint, we scroll to it.
648+
function scrollToLintByURL() {
649+
const lintId = window.location.hash.substring(2);
650+
if (lintId.length > 0) {
651+
scrollToLint(lintId);
652+
}
653+
}
654+
655+
scrollToLintByURL();
656+
657+
onEachLazy(document.querySelectorAll("pre > code.language-rust"), el => hljs.highlightElement(el));

util/gh-pages/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,7 @@ body {
396396
background: var(--bg);
397397
color: var(--fg);
398398
}
399+
400+
article.collapsed .lint-docs {
401+
display: none;
402+
}

0 commit comments

Comments
 (0)