Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Scroll to proper position in annotations

If there is a selection instead of a single line, use the first line in the
selection range.

Takes offset from the navigation box into account and scrolls
to the first row of the commit (scrolling to the **selected** row is
pretty useless, as there is no author or revision info if you are in
the middle of the document).
  • Loading branch information...
commit 74567b13695cc2b7c3e0853779ea554ba4a548e0 1 parent 87c1506
@kaiwood kaiwood authored
View
1  Support/app/controllers/annotate_controller.rb
@@ -2,6 +2,7 @@
class AnnotateController < ApplicationController
include DateHelpers
+ include AnnotateHelper
layout "application", :except => "update"
def index
@file_path = params[:file_path] || ENV['TM_FILEPATH']
View
29 Support/app/helpers/annotate_helper.rb
@@ -0,0 +1,29 @@
+module AnnotateHelper
+ def selected_line_range
+ lines = parse_selection_string(ENV['TM_SELECTION']).flatten
+ [lines.min, lines.max]
+ end
+
+ # Parses the selection string and includes an array containing the lines
+ # selected in the document
+ def parse_selection_string(str)
+ str.split('&').map do |range|
+ if range =~ /(\d+)(?::(\d+))?(?:\+\d+)?(?:([-x])(\d+)(?::(\d+))?(?:\+\d+)?)?/
+ l1, l2, c1, c2 = $1.to_i, ($4 ? $4.to_i : nil), ($2 || 1).to_i, ($5 || 1).to_i
+ l1, l2, c1, c2 = l2, l1, c2, c1 if l2 && (l1 > l2 || l1 == l2 && c1 > c2)
+
+ case $3
+ when 'x'
+ [ l1, l2 ]
+ when '-'
+ l2 -= 1 if c2 == 1
+ [ l1, l2 ]
+ else
+ [ l1, l1 ]
+ end
+ else
+ abort "unsupported selection string syntax: ‘#{range}"
+ end
+ end
+ end
+end
View
2  Support/app/views/annotate/_content.html.erb
@@ -48,7 +48,7 @@ EOF
</thead>
<tbody>
<% for display in formatted_annotations %>
- <tr>
+ <tr id="line-<%= display[:ln] %>">
<td class="line-numbers">
<% if display[:rev]=="" %>
View
2  Support/app/views/annotate/_navigate_box.html.erb
@@ -1,4 +1,4 @@
-<div style='position:fixed; top:0px; background: #fff; width: 100%; '>
+<div style='position:fixed; top:0px; background: #fff; width: 100%; ' id="navigate-box">
<div id='debug'></div>
<div style='float:right; margin-right: 15px;'>
<b>Commands</b>: <%= %w[next previous open branch tag].map { |word| "<strong>(#{word[0..0]})</strong>#{word[1..-1]}" } * ' / ' %>
View
25 Support/app/views/annotate/index.html.erb
@@ -7,7 +7,28 @@
{
$('content').update( dispatch({controller: 'annotate', action: "update", revision: revision}) );
}
-
+
+ function scroll_to_proper_position()
+ {
+ var line_number = <%= selected_line_range.min %>;
+ var info_row = $('line-' + line_number);
+
+ // Find row with last commit info
+ for (i = parseInt(line_number); i >= 1; i--) {
+ if (info_row.innerText.substr(0, 8).match(/[a-zA-Z0-9\-]{8}/)) { // Match revision hash or the word -current
+ break;
+ } else {
+ info_row = $('line-' + i);
+ }
+ }
+
+ var position = info_row.getBoundingClientRect().top;
+ var offset_from_navigate_box = document.getElementById('navigate-box').getBoundingClientRect().bottom;
+ var offset_from_border = 2
+
+ window.scroll(0, position - offset_from_navigate_box - offset_from_border);
+ }
+
function keypress_listener(e)
{
// if (e.keyCode==Event.KEY_LEFT)
@@ -59,5 +80,7 @@
catch (e) {
$('debug').update(e)
}
+
+ scroll_to_proper_position();
</script>
<% end %>
Please sign in to comment.
Something went wrong with that request. Please try again.