Skip to content

Commit 0055ceb

Browse files
Replace search with vanilla JS
1 parent 9661ba0 commit 0055ceb

File tree

3 files changed

+43
-56
lines changed

3 files changed

+43
-56
lines changed

tests/compile-test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use clippy_lints::declared_lints::LINTS;
1010
use clippy_lints::deprecated_lints::{DEPRECATED, DEPRECATED_VERSION, RENAMED};
1111
use pulldown_cmark::{Options, Parser, html};
1212
use rinja::{Template, filters::Safe};
13-
use serde::{Deserialize, Serialize};
13+
use serde::Deserialize;
1414
use test_utils::IS_RUSTC_TEST_SUITE;
1515
use ui_test::custom_flags::Flag;
1616
use ui_test::custom_flags::rustfix::RustfixMode;

util/gh-pages/index_template.html

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ <h1>Clippy Lints</h1>
180180
<div class="col-12 col-md-5 search-control">
181181
<div class="input-group">
182182
<label class="input-group-addon" id="filter-label" for="search-input">Filter:</label>
183-
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input" onblur="updatePath()" onchange="handleInputChanged()" />
183+
<input type="text" class="form-control filter-input" placeholder="Keywords or search string (`S` or `/` to focus)" id="search-input" />
184184
<span class="input-group-btn">
185-
<button class="filter-clear btn" type="button" ng-click="search = ''; updatePath();">
185+
<button class="filter-clear btn" type="button" onclick="searchState.clearInput(event)">
186186
Clear
187187
</button>
188188
</span>
@@ -199,22 +199,22 @@ <h1>Clippy Lints</h1>
199199
</div>
200200
</div>
201201
{% for lint in lints %}
202-
<article class="panel panel-default" id="{{lint.id}}">
202+
<article class="panel panel-default" id="{(lint.id)}">
203203
<header class="panel-heading" ng-click="open[lint.id] = !open[lint.id]">
204204
<h2 class="panel-title">
205205
<div class="panel-title-name">
206206
<span>{(lint.id)}</span>
207-
<a href="#{{lint.id}}" class="anchor label label-default"
207+
<a href="#{(lint.id)}" class="anchor label label-default"
208208
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()">
209+
<a href="" id="clipboard-{(lint.id)}" class="anchor label label-default" ng-click="copyToClipboard(lint); $event.stopPropagation()">
210210
&#128203;
211211
</a>
212212
</div>
213213

214214
<div class="panel-title-addons">
215-
<span class="label label-lint-group label-default label-group-{{lint.group}}">{(lint.group)}</span>
215+
<span class="label label-lint-group label-default label-group-{(lint.group)}">{(lint.group)}</span>
216216

217-
<span class="label label-lint-level label-lint-level-{{lint.level}}">{(lint.level)}</span>
217+
<span class="label label-lint-level label-lint-level-{(lint.level)}">{(lint.level)}</span>
218218

219219

220220
<span class="label label-doc-folding" ng-show="open[lint.id]">&minus;</span>
@@ -223,7 +223,7 @@ <h2 class="panel-title">
223223
</h2>
224224
</header>
225225

226-
<div class="list-group lint-docs" ng-class="{collapse: true, in: open[lint.id]}">
226+
<div class="list-group lint-docs">
227227
<div class="list-group-item lint-doc-md">{(markdown(lint.docs))}</div>
228228
<div class="lint-additional-info-container">
229229
{# Applicability #}
@@ -232,18 +232,21 @@ <h2 class="panel-title">
232232
<span class="label label-default label-applicability">{( lint.applicability_str() )}</span>
233233
<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.Applicability.html#variants">(?)</a>
234234
</div>
235-
<!-- Clippy version -->
235+
{# Clippy version #}
236236
<div class="lint-additional-info-item">
237237
<span>{% if lint.group == "deprecated" %}Deprecated{% else %} Added{% endif %} in: </span>
238238
<span class="label label-default label-version">{(lint.version)}</span>
239239
</div>
240-
<!-- Open related issues -->
240+
{# Open related issues #}
241241
<div class="lint-additional-info-item">
242242
<a href="https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+{{lint.id}}">Related Issues</a>
243243
</div>
244-
<!-- Jump to source -->
245-
<div class="lint-additional-info-item" ng-if="lint.id_location">
246-
<a href="https://github.com/rust-lang/rust-clippy/blob/{{docVersion}}/clippy_lints/{{lint.id_location.path}}">View Source</a>
244+
245+
{# Jump to source #}
246+
{% if let Some(id_location) = lint.id_location %}
247+
<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>
249+
{% endif %}
247250
</div>
248251
</div>
249252
</div>

util/gh-pages/script.js

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -361,42 +361,6 @@
361361
return $scope.groups[lint.group];
362362
};
363363

364-
$scope.bySearch = function (lint, index, array) {
365-
let searchStr = $scope.search;
366-
// It can be `null` I haven't missed this value
367-
if (searchStr == null) {
368-
return true;
369-
}
370-
searchStr = searchStr.toLowerCase();
371-
if (searchStr.startsWith("clippy::")) {
372-
searchStr = searchStr.slice(8);
373-
}
374-
375-
// Search by id
376-
if (lint.id.indexOf(searchStr.replaceAll("-", "_")) !== -1) {
377-
return true;
378-
}
379-
380-
// Search the description
381-
// The use of `for`-loops instead of `foreach` enables us to return early
382-
const terms = searchStr.split(" ");
383-
const docsLowerCase = lint.docs.toLowerCase();
384-
for (index = 0; index < terms.length; index++) {
385-
// This is more likely and will therefore be checked first
386-
if (docsLowerCase.indexOf(terms[index]) !== -1) {
387-
continue;
388-
}
389-
390-
if (lint.id.indexOf(terms[index]) !== -1) {
391-
continue;
392-
}
393-
394-
return false;
395-
}
396-
397-
return true;
398-
}
399-
400364
$scope.byApplicabilities = function (lint) {
401365
return $scope.applicabilities[lint.applicability];
402366
};
@@ -472,6 +436,11 @@ function getQueryVariable(variable) {
472436
window.searchState = {
473437
timeout: null,
474438
inputElem: document.getElementById("search-input"),
439+
lastSearch: '',
440+
clearInput: () => {
441+
searchState.inputElem.value = "";
442+
searchState.filterLints();
443+
},
475444
clearInputTimeout: () => {
476445
if (searchState.timeout !== null) {
477446
clearTimeout(searchState.timeout);
@@ -483,32 +452,38 @@ window.searchState = {
483452
setTimeout(searchState.filterLints, 50);
484453
},
485454
filterLints: () => {
486-
let searchStr = searchState.value.trim().toLowerCase();
455+
searchState.clearInputTimeout();
456+
457+
let searchStr = searchState.inputElem.value.trim().toLowerCase();
487458
if (searchStr.startsWith("clippy::")) {
488459
searchStr = searchStr.slice(8);
489460
}
461+
if (searchState.lastSearch === searchStr) {
462+
return;
463+
}
464+
searchState.lastSearch = searchStr;
490465
const terms = searchStr.split(" ");
491466

492467
onEachLazy(document.querySelectorAll("article"), lint => {
493468
// Search by id
494469
if (lint.id.indexOf(searchStr.replaceAll("-", "_")) !== -1) {
495-
el.style.display = "";
470+
lint.style.display = "";
496471
return;
497472
}
498473
// Search the description
499474
// The use of `for`-loops instead of `foreach` enables us to return early
500-
const docsLowerCase = lint.docs.toLowerCase();
475+
const docsLowerCase = lint.textContent.toLowerCase();
501476
for (index = 0; index < terms.length; index++) {
502477
// This is more likely and will therefore be checked first
503478
if (docsLowerCase.indexOf(terms[index]) !== -1) {
504-
continue;
479+
return;
505480
}
506481

507482
if (lint.id.indexOf(terms[index]) !== -1) {
508-
continue;
483+
return;
509484
}
510485

511-
return false;
486+
lint.style.display = "none";
512487
}
513488
});
514489
},
@@ -631,7 +606,16 @@ function generateSettings() {
631606
);
632607
}
633608

609+
function generateSearch() {
610+
searchState.inputElem.addEventListener("change", handleInputChanged);
611+
searchState.inputElem.addEventListener("input", handleInputChanged);
612+
searchState.inputElem.addEventListener("keydown", handleInputChanged);
613+
searchState.inputElem.addEventListener("keyup", handleInputChanged);
614+
searchState.inputElem.addEventListener("paste", handleInputChanged);
615+
}
616+
634617
generateSettings();
618+
generateSearch();
635619

636620
// loading the theme after the initial load
637621
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");

0 commit comments

Comments
 (0)