Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Update blame links with selected line, fix bug 1007293
Browse files Browse the repository at this point in the history
  • Loading branch information
pelmers committed Jun 9, 2016
1 parent 224cec2 commit f308589
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
3 changes: 3 additions & 0 deletions dxr/indexers.py
Expand Up @@ -224,6 +224,9 @@ def links(self):
(sort order, heading, [(icon, title, href), ...])
File views will replace any {{line}} within the href with the
last-selected line number.
"""
return []

Expand Down
43 changes: 37 additions & 6 deletions dxr/static_unhashed/js/code-highlighter.js
Expand Up @@ -7,13 +7,15 @@
* 1) Multi-select highlight lines with shift key and update window.location.hash
* 2) Multi-select highlight lines with command/control key and update window.location.hash
* 3) Highlight lines when page loads, if window.location.hash exists
* In addition, we update the permalink link to keep it synchronized with window.location.
* In addition, we update the permalink and other nav links to keep them
* synchronized with window.location.
*/

$(function () {
'use strict';
var container = $('#line-numbers'),
permalink = $('.permalink'), // whenever we update window.location, update this href too
navlinks = $('.panel a'), // whenever we update window.location, maybe update these too
permalink = $('.permalink'), // a subset of navlinks, but it has more specific update rules
lastModifierKey = null, // use this as a sort of canary/state indicator showing the last user action
singleLinesArray = [], //track single highlighted lines here
rangesArray = []; // track ranges of highlighted lines here
Expand Down Expand Up @@ -86,6 +88,7 @@ $(function () {
var selectedArray = generateSelectedArrays(); // generates sorted arrays
var singleLinesArray = selectedArray[0];
var rangesArray = selectedArray[1];
var lastNumber;
// eliminate duplication
for (s = 0; s < singleLinesArray.length; s++) {
for (r = 0; r < rangesArray.length; r++) {
Expand All @@ -103,22 +106,25 @@ $(function () {
// if no singleLines left or range < singleLine add range to hash
if ((r == rangesArray.length) || (singleLinesArray[s] < rangesArray[r][0])) {
windowHash += singleLinesArray[s] + ',';
lastNumber = singleLinesArray[s];
s++;
} else if (( s == singleLinesArray.length) || (rangesArray[r][0] < singleLinesArray[s])) {
windowHash += rangesArray[r][0] + '-' + rangesArray[r][1] + ',';
lastNumber = rangesArray[r][1];
r++;
}
}
if (windowHash) {
windowHash = windowHash.replace(reCleanup, '');
updateHash(windowHash);
updateHash(windowHash, lastNumber);
}
}

//update places where hash location is used: window, permalink
function updateHash(hash) {
//update places where hash location is used: window, permalink, other nav links
function updateHash(hash, lastNumber) {
if (permalink.length > 0)
updatePermalink(hash);
updateNavLinks(lastNumber);
history.replaceState(null, '', hash);
}

Expand All @@ -132,6 +138,27 @@ $(function () {
permalink.attr('href', permalink_href + windowHash);
}

//replace any occurrence of {{line}} in hrefs with the last-selected line.
//unless the last-selected line is undefined, then remove {{line}} from the
//displayed url.
function updateNavLinks(lastNumber) {
navlinks.each(function() {
console.log(lastNumber);
var jqthis = $(this);
// Copy the href to a template attr, since we will be updating it
// and don't want to lose the template. This behavior will happen
// only the first time this function is called.
if (!jqthis.data('template')) {
jqthis.data('template', jqthis.attr('href'));
}
if (lastNumber) {
jqthis.attr('href', jqthis.data('template').replace(/{{line}}/g, lastNumber));
} else {
jqthis.attr('href', jqthis.data('template').replace(/{{line}}/g, ''));
}
});
}

//parse window.location.hash on new requests into two arrays
//one of single lines and one multilines
//use with singleLinesArray and rangesArray for adding/changing new highlights
Expand Down Expand Up @@ -327,9 +354,13 @@ $(function () {
}

// Highlight any lines specified by hash in either a direct page load or a history pop.
$(document).ready(processHash);
$(document).ready(function() {
updateNavLinks();
processHash();
});
$(window).on('popstate', function() {
removeAllHighlighting();
updateNavLinks();
processHash();
});
});
4 changes: 2 additions & 2 deletions dxr/vcs.py
Expand Up @@ -147,7 +147,7 @@ def generate_diff(self, path):
return self.upstream + 'diff/' + self.previous_revisions[path] + '/' + path

def generate_blame(self, path):
return self.upstream + 'annotate/' + self.revision + '/' + path
return self.upstream + 'annotate/' + self.revision + '/' + path + '#l{{line}}'

def generate_log(self, path):
return self.upstream + 'filelog/' + self.revision + '/' + path
Expand Down Expand Up @@ -210,7 +210,7 @@ def generate_diff(self, path):
return self.upstream + "/commit/" + self.revision

def generate_blame(self, path):
return self.upstream + "/blame/" + self.revision + "/" + path
return self.upstream + "/blame/" + self.revision + "/" + path + '#L{{line}}'

def generate_log(self, path):
return self.upstream + "/commits/" + self.revision + "/" + path
Expand Down

0 comments on commit f308589

Please sign in to comment.