From 8750045b3dd2cd7530bb2795f2d7a36f89353225 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 27 Feb 2021 15:44:20 -0500 Subject: [PATCH] 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 --- CHANGES.rst | 11 ++++++-- coverage/htmlfiles/coverage_html.js | 43 +++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7eaf22a2e..201f1d069 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js index 6bc9fdf59..27b49b36f 100644 --- a/coverage/htmlfiles/coverage_html.js +++ b/coverage/htmlfiles/coverage_html.js @@ -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; @@ -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(); @@ -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.