Skip to content

Commit 8750045

Browse files
committed
fix: HTML line visibility is saved in local storage #1123
Seems like we could unify the two different uses of localStorage, but that's for another time. Fixes: #1123
1 parent c7052bb commit 8750045

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

CHANGES.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,20 @@ Unreleased
2929
they have been combined. This was requested in `issue 1108`_ and implemented
3030
in `pull request 1110`_. Thanks, Éric Larivière.
3131

32-
- The HTML report has a little more room for line numbers so that 4-digit
33-
numbers work well, fixing `issue 1124`_.
32+
- Minor improvements to the HTML report:
33+
34+
- The state of the line visibility selector buttons is saved in local storage
35+
so you don't have to fiddle with them so often, fixing `issue 1123`_.
36+
37+
- It has a little more room for line numbers so that 4-digit numbers work
38+
well, fixing `issue 1124`_.
3439

3540
.. _issue 1108: https://github.com/nedbat/coveragepy/issues/1108
3641
.. _pull request 1110: https://github.com/nedbat/coveragepy/pull/1110
42+
.. _issue 1123: https://github.com/nedbat/coveragepy/issues/1123
3743
.. _issue 1124: https://github.com/nedbat/coveragepy/issues/1124
3844

45+
3946
.. _changes_54:
4047

4148
Version 5.4 --- 2021-01-24

coverage/htmlfiles/coverage_html.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ coverage.index_ready = function ($) {
233233

234234
// -- pyfile stuff --
235235

236+
coverage.LINE_FILTERS_STORAGE = "COVERAGE_LINE_FILTERS";
237+
236238
coverage.pyfile_ready = function ($) {
237239
// If we're directed to a particular line number, highlight the line.
238240
var frag = location.hash;
@@ -256,6 +258,22 @@ coverage.pyfile_ready = function ($) {
256258
$(".button_toggle_mis").click(function (evt) {coverage.toggle_lines(evt.target, "mis");});
257259
$(".button_toggle_par").click(function (evt) {coverage.toggle_lines(evt.target, "par");});
258260

261+
coverage.filters = undefined;
262+
try {
263+
coverage.filters = localStorage.getItem(coverage.LINE_FILTERS_STORAGE);
264+
} catch(err) {}
265+
266+
if (coverage.filters) {
267+
coverage.filters = JSON.parse(coverage.filters);
268+
}
269+
else {
270+
coverage.filters = {run: false, exc: true, mis: true, par: true};
271+
}
272+
273+
for (cls in coverage.filters) {
274+
coverage.set_line_visibilty(cls, coverage.filters[cls]);
275+
}
276+
259277
coverage.assign_shortkeys();
260278
coverage.wire_up_help_panel();
261279

@@ -266,17 +284,26 @@ coverage.pyfile_ready = function ($) {
266284
};
267285

268286
coverage.toggle_lines = function (btn, cls) {
269-
btn = $(btn);
270-
var show = "show_"+cls;
271-
if (btn.hasClass(show)) {
272-
$("#source ." + cls).removeClass(show);
273-
btn.removeClass(show);
274-
}
275-
else {
287+
var onoff = !$(btn).hasClass("show_" + cls);
288+
coverage.set_line_visibilty(cls, onoff);
289+
coverage.build_scroll_markers();
290+
coverage.filters[cls] = onoff;
291+
try {
292+
localStorage.setItem(coverage.LINE_FILTERS_STORAGE, JSON.stringify(coverage.filters));
293+
} catch(err) {}
294+
};
295+
296+
coverage.set_line_visibilty = function (cls, onoff) {
297+
var show = "show_" + cls;
298+
var btn = $(".button_toggle_" + cls);
299+
if (onoff) {
276300
$("#source ." + cls).addClass(show);
277301
btn.addClass(show);
278302
}
279-
coverage.build_scroll_markers();
303+
else {
304+
$("#source ." + cls).removeClass(show);
305+
btn.removeClass(show);
306+
}
280307
};
281308

282309
// Return the nth line div.

0 commit comments

Comments
 (0)