diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2011-03-27 16:44:00 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2011-03-27 16:44:00 -0400 |
commit | 527ba191faed1cb5a2d4fd2206114de2748a3dfe (patch) | |
tree | ea62ca0770d1a9a220144609204aaeee50cf4e97 /coverage/htmlfiles | |
parent | f1d758c7351e0d1a4c97c2ce262ae183cb6f8565 (diff) | |
download | python-coveragepy-git-527ba191faed1cb5a2d4fd2206114de2748a3dfe.tar.gz |
HTML hotkeys for jumping to next/prev highlighted source section.
Diffstat (limited to 'coverage/htmlfiles')
-rw-r--r-- | coverage/htmlfiles/coverage_html.js | 101 | ||||
-rw-r--r-- | coverage/htmlfiles/jquery.isonscreen.js | 53 | ||||
-rw-r--r-- | coverage/htmlfiles/pyfile.html | 1 |
3 files changed, 155 insertions, 0 deletions
diff --git a/coverage/htmlfiles/coverage_html.js b/coverage/htmlfiles/coverage_html.js index 7b6db427..3e2ed71d 100644 --- a/coverage/htmlfiles/coverage_html.js +++ b/coverage/htmlfiles/coverage_html.js @@ -90,8 +90,15 @@ coverage.pyfile_ready = function($) { var frag = location.hash; if (frag.length > 2 && frag[1] === 'n') { $(frag).addClass('highlight'); + coverage.sel_begin = parseInt(frag.substr(2)); + coverage.sel_end = coverage.sel_begin + 1; } + $(document).bind('keydown', 'j', coverage.to_next_chunk); + $(document).bind('keydown', 'k', coverage.to_prev_chunk); + $(document).bind('keydown', '0', coverage.to_top); + $(document).bind('keydown', '1', coverage.to_first_chunk); + coverage.assign_shortkeys(); }; @@ -108,3 +115,97 @@ coverage.toggle_lines = function(btn, cls) { } }; +// The first line selected, and the next line not selected. +coverage.sel_begin = 0; +coverage.sel_end = 1; + +coverage.to_top = function() { + coverage.sel_begin = 0; + coverage.sel_end = 1; + $("html").animate({scrollTop: 0}, 200); +} + +coverage.to_first_chunk = function() { + coverage.sel_begin = 0; + coverage.sel_end = 1; + coverage.to_next_chunk(); +} + +coverage.to_next_chunk = function() { + var c = coverage; + + // Find the start of the next colored chunk. + var probe = c.sel_end; + var color = $("#t" + probe).css("background-color"); + while (color === "transparent") { + probe += 1; + var probe_line = $("#t" + probe); + if (probe_line.length === 0) { + return; + } + color = probe_line.css("background-color"); + } + + // There's a next chunk, `probe` points to it. + c.sel_begin = probe; + + // Find the end of this chunk. + var next_color = color; + while (next_color === color) { + probe += 1; + next_color = $("#t" + probe).css("background-color"); + } + c.sel_end = probe; + coverage.show_selected_chunk(); +}; + +coverage.to_prev_chunk = function() { + var c = coverage; + + // Find the end of the prev colored chunk. + var probe = c.sel_begin-1; + var color = $("#t" + probe).css("background-color"); + while (probe > 0 && color === "transparent") { + probe -= 1; + var probe_line = $("#t" + probe); + if (probe_line.length === 0) { + return; + } + color = probe_line.css("background-color"); + } + + // There's a prev chunk, `probe` points to its last line. + c.sel_end = probe+1; + + // Find the beginning of this chunk. + var prev_color = color; + while (prev_color === color) { + probe -= 1; + prev_color = $("#t" + probe).css("background-color"); + } + c.sel_begin = probe+1; + coverage.show_selected_chunk(); +}; + +coverage.show_selected_chunk = function() { + var c = coverage; + + // Highlight the lines in the chunk + $(".linenos p").removeClass("highlight"); + var probe = c.sel_begin; + while (probe > 0 && probe < c.sel_end) { + $("#n" + probe).addClass("highlight"); + probe += 1; + } + + // Scroll the page if the chunk isn't fully visible. + var top = $("#t" + c.sel_begin); + var bot = $("#t" + (c.sel_end-1)); + + if (!top.isOnScreen() || !bot.isOnScreen()) { + // Need to move the page. + var top_pos = parseInt(top.offset().top); + $("html").animate({scrollTop: top_pos-30}, 300); + } +}; + diff --git a/coverage/htmlfiles/jquery.isonscreen.js b/coverage/htmlfiles/jquery.isonscreen.js new file mode 100644 index 00000000..b147aff0 --- /dev/null +++ b/coverage/htmlfiles/jquery.isonscreen.js @@ -0,0 +1,53 @@ +/* Copyright (c) 2010
+ * @author Laurence Wheway
+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
+ *
+ * @version 1.2.0
+ */
+(function($) {
+ jQuery.extend({
+ isOnScreen: function(box, container) {
+ //ensure numbers come in as intgers (not strings) and remove 'px' is it's there
+ for(var i in box){box[i] = parseFloat(box[i])};
+ for(var i in container){container[i] = parseFloat(container[i])};
+
+ if(!container){
+ container = {
+ left: $(window).scrollLeft(),
+ top: $(window).scrollTop(),
+ width: $(window).width(),
+ height: $(window).height()
+ }
+ }
+
+ if( box.left+box.width-container.left > 0 &&
+ box.left < container.width+container.left &&
+ box.top+box.height-container.top > 0 &&
+ box.top < container.height+container.top
+ ) return true;
+ return false;
+ }
+ })
+
+
+ jQuery.fn.isOnScreen = function (container) {
+ for(var i in container){container[i] = parseFloat(container[i])};
+
+ if(!container){
+ container = {
+ left: $(window).scrollLeft(),
+ top: $(window).scrollTop(),
+ width: $(window).width(),
+ height: $(window).height()
+ }
+ }
+
+ if( $(this).offset().left+$(this).width()-container.left > 0 &&
+ $(this).offset().left < container.width+container.left &&
+ $(this).offset().top+$(this).height()-container.top > 0 &&
+ $(this).offset().top < container.height+container.top
+ ) return true;
+ return false;
+ }
+})(jQuery);
diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html index d9d0e4c6..6f99e6a6 100644 --- a/coverage/htmlfiles/pyfile.html +++ b/coverage/htmlfiles/pyfile.html @@ -9,6 +9,7 @@ <link rel='stylesheet' href='style.css' type='text/css'> <script type='text/javascript' src='jquery-1.4.3.min.js'></script> <script type='text/javascript' src='jquery.hotkeys.js'></script> + <script type='text/javascript' src='jquery.isonscreen.js'></script> <script type='text/javascript' src='coverage_html.js'></script> <script type='text/javascript' charset='utf-8'> jQuery(document).ready(coverage.pyfile_ready); |