Skip to content

Commit

Permalink
fix: HTML line visibility is saved in local storage #1123
Browse files Browse the repository at this point in the history
Seems like we could unify the two different uses of localStorage, but
that's for another time.

Fixes: #1123
  • Loading branch information
nedbat committed Feb 27, 2021
1 parent c7052bb commit 8750045
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
11 changes: 9 additions & 2 deletions CHANGES.rst
Expand Up @@ -29,13 +29,20 @@ Unreleased
they have been combined. This was requested in `issue 1108`_ and implemented
in `pull request 1110`_. Thanks, Éric Larivière.

- The HTML report has a little more room for line numbers so that 4-digit
numbers work well, fixing `issue 1124`_.
- Minor improvements to the HTML report:

- The state of the line visibility selector buttons is saved in local storage
so you don't have to fiddle with them so often, fixing `issue 1123`_.

- It has a little more room for line numbers so that 4-digit numbers work
well, fixing `issue 1124`_.

.. _issue 1108: https://github.com/nedbat/coveragepy/issues/1108
.. _pull request 1110: https://github.com/nedbat/coveragepy/pull/1110
.. _issue 1123: https://github.com/nedbat/coveragepy/issues/1123
.. _issue 1124: https://github.com/nedbat/coveragepy/issues/1124


.. _changes_54:

Version 5.4 --- 2021-01-24
Expand Down
43 changes: 35 additions & 8 deletions coverage/htmlfiles/coverage_html.js
Expand Up @@ -233,6 +233,8 @@ coverage.index_ready = function ($) {

// -- pyfile stuff --

coverage.LINE_FILTERS_STORAGE = "COVERAGE_LINE_FILTERS";

coverage.pyfile_ready = function ($) {
// If we're directed to a particular line number, highlight the line.
var frag = location.hash;
Expand All @@ -256,6 +258,22 @@ coverage.pyfile_ready = function ($) {
$(".button_toggle_mis").click(function (evt) {coverage.toggle_lines(evt.target, "mis");});
$(".button_toggle_par").click(function (evt) {coverage.toggle_lines(evt.target, "par");});

coverage.filters = undefined;
try {
coverage.filters = localStorage.getItem(coverage.LINE_FILTERS_STORAGE);
} catch(err) {}

if (coverage.filters) {
coverage.filters = JSON.parse(coverage.filters);
}
else {
coverage.filters = {run: false, exc: true, mis: true, par: true};
}

for (cls in coverage.filters) {
coverage.set_line_visibilty(cls, coverage.filters[cls]);
}

coverage.assign_shortkeys();
coverage.wire_up_help_panel();

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

coverage.toggle_lines = function (btn, cls) {
btn = $(btn);
var show = "show_"+cls;
if (btn.hasClass(show)) {
$("#source ." + cls).removeClass(show);
btn.removeClass(show);
}
else {
var onoff = !$(btn).hasClass("show_" + cls);
coverage.set_line_visibilty(cls, onoff);
coverage.build_scroll_markers();
coverage.filters[cls] = onoff;
try {
localStorage.setItem(coverage.LINE_FILTERS_STORAGE, JSON.stringify(coverage.filters));
} catch(err) {}
};

coverage.set_line_visibilty = function (cls, onoff) {
var show = "show_" + cls;
var btn = $(".button_toggle_" + cls);
if (onoff) {
$("#source ." + cls).addClass(show);
btn.addClass(show);
}
coverage.build_scroll_markers();
else {
$("#source ." + cls).removeClass(show);
btn.removeClass(show);
}
};

// Return the nth line div.
Expand Down

0 comments on commit 8750045

Please sign in to comment.