Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions dxr/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,23 @@ def _browse_file(tree, path, line_docs, file_doc, config, is_binary,
:arg image_rev: revision number of a textual or binary image, for images
displayed at a certain rev
"""
def process_link_templates(sections):
"""Look for {{line}} in the links of given sections, and duplicate them onto
a 'template' field.
"""
for section in sections:
for link in section['items']:
if '{{line}}' in link['href']:
link['template'] = link['href']
link['href'] = link['href'].replace('{{line}}', '')

def sidebar_links(sections):
"""Return data structure to build nav sidebar from. ::

[('Section Name', [{'icon': ..., 'title': ..., 'href': ...}])]

"""
process_link_templates(sections)
# Sort by order, resolving ties by section name:
return sorted(sections, key=lambda section: (section['order'],
section['heading']))
Expand Down
3 changes: 3 additions & 0 deletions dxr/indexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we scroll to the first-selected line but blame to the last. Any reason for the inconsistency?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, actually when I was writing this I hadn't realized that we scrolled to the first selected line.


"""
return []

Expand Down
32 changes: 27 additions & 5 deletions dxr/static_unhashed/js/code-highlighter.js
Original file line number Diff line number Diff line change
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 firstNumber;
// eliminate duplication
for (s = 0; s < singleLinesArray.length; s++) {
for (r = 0; r < rangesArray.length; r++) {
Expand All @@ -103,22 +106,29 @@ $(function () {
// if no singleLines left or range < singleLine add range to hash
if ((r == rangesArray.length) || (singleLinesArray[s] < rangesArray[r][0])) {
windowHash += singleLinesArray[s] + ',';
if (!firstNumber) {
firstNumber = singleLinesArray[s];
}
s++;
} else if (( s == singleLinesArray.length) || (rangesArray[r][0] < singleLinesArray[s])) {
windowHash += rangesArray[r][0] + '-' + rangesArray[r][1] + ',';
if (!firstNumber) {
firstNumber = rangesArray[r][0];
}
r++;
}
}
if (windowHash) {
windowHash = windowHash.replace(reCleanup, '');
updateHash(windowHash);
updateHash(windowHash, firstNumber);
}
}

//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, lineNumber) {
if (permalink.length > 0)
updatePermalink(hash);
updateNavLinks(lineNumber);
history.replaceState(null, '', hash);
}

Expand All @@ -132,6 +142,18 @@ $(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(lineNumber) {
navlinks.each(function() {
const $this = $(this);
if ($this.data('template')) {
$this.attr('href', $this.data('template').replace(/{{line}}/g, lineNumber || ''));
}
});
}

//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
2 changes: 1 addition & 1 deletion dxr/templates/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h4>{{ section.heading }}</h4>
<ul>
{% for item in section['items'] %}
<li>
<a href="{{ item.href }}" title="{{ item.title }}" class="{{ item.icon }} icon">{{ item.title }}</a>
<a href="{{ item.href }}" title="{{ item.title }}" class="{{ item.icon }} icon" {%- if 'template' in item %} data-template="{{ item.template }}" {%- endif %}>{{ item.title }}</a>
</li>
{%- endfor %}
</ul>
Expand Down
25 changes: 12 additions & 13 deletions dxr/vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,17 @@ def last_modified_date(self, path):
return self.previous_revisions[path][1]

def generate_raw(self, path):
return self.upstream + 'raw-file/' + self.revision + '/' + path
return "{}raw-file/{}/{}".format(self.upstream, self.revision, path)

def generate_diff(self, path):
# We generate link to diff with the last revision in which the file changed.
return self.upstream + 'diff/' + self.previous_revisions[path][0] + '/' + path
return "{}diff/{}/{}".format(self.upstream, self.previous_revisions[path][0], path)

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

def generate_log(self, path):
return self.upstream + 'filelog/' + self.revision + '/' + path
return "{}filelog/{}/{}".format(self.upstream, self.revision, path)

@classmethod
def get_contents(cls, path, revision, stderr=None):
Expand Down Expand Up @@ -273,18 +273,18 @@ def is_tracked(self, path):
return path in self.tracked_files

def generate_raw(self, path):
return self.upstream + "/raw/" + self.revision + "/" + path
return "{}/raw/{}/{}".format(self.upstream, self.revision, path)

def generate_diff(self, path):
# I really want to make this anchor on the file in question, but github
# doesn't seem to do that nicely
return self.upstream + "/commit/" + self.revision
return "{}/commit/{}".format(self.upstream, self.revision)

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

def generate_log(self, path):
return self.upstream + "/commits/" + self.revision + "/" + path
return "{}/commits/{}/{}".format(self.upstream, self.revision, path)

@classmethod
def get_contents(cls, path, revision, stderr=None):
Expand Down Expand Up @@ -335,22 +335,21 @@ def is_tracked(self, path):

def generate_raw(self, path):
info = self.have[path]
return self.upstream + info['depotFile'] + '?ac=98&rev1=' + info['haveRev']
return "{}{}?ac=98&rev1={}".format(self.upstream, info['depotFile'], info['haveRev'])

def generate_diff(self, path):
info = self.have[path]
haveRev = info['haveRev']
prevRev = str(int(haveRev) - 1)
return (self.upstream + info['depotFile'] + '?ac=19&rev1=' + prevRev +
'&rev2=' + haveRev)
return "{}{}?ac=19&rev1={}&rev2={}".format(self.upstream, info['depotFile'], prevRev, haveRev)

def generate_blame(self, path):
info = self.have[path]
return self.upstream + info['depotFile'] + '?ac=193'
return "{}{}?ac=193".format(self.upstream, info['depotFile'])

def generate_log(self, path):
info = self.have[path]
return self.upstream + info['depotFile'] + '?ac=22#' + info['haveRev']
return "{}{}?ac=22#{}".format(self.upstream, info['depotFile'], info['haveRev'])

def display_rev(self, path):
info = self.have[path]
Expand Down
3 changes: 2 additions & 1 deletion tests/test_vcs_git/test_vcs_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def test_diff(self):
def test_blame(self):
"""Make sure the blame link exists and goes to the right place."""
response = self.client().get('/code/source/main.c')
ok_('/blame/%s/main.c" title="Blame" class="blame icon">Blame</a>' % LATEST_REVISION in response.data)
ok_('/blame/%s/main.c#L" title="Blame" class="blame icon"' % LATEST_REVISION in response.data)
ok_('/blame/%s/main.c#L{{line}}">Blame' % LATEST_REVISION in response.data)

def test_raw(self):
"""Make sure the raw link exists and goes to the right place."""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_vcs_hg/test_vcs_hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_diff_file3(self):
def test_blame(self):
"""Make sure the blame link goes to the right place."""
response = self.client().get('/code/source/ChangedInCommit1')
ok_('/annotate/84798105c9ab5897f8c7d630d133d9003b44a62f/ChangedInCommit1" title="Blame" class="blame icon">Blame</a>' in response.data)
ok_('/annotate/84798105c9ab5897f8c7d630d133d9003b44a62f/ChangedInCommit1#l" title="Blame" class="blame icon"' in response.data)
ok_('/annotate/84798105c9ab5897f8c7d630d133d9003b44a62f/ChangedInCommit1#l{{line}}">Blame' in response.data)

def test_raw(self):
"""Make sure the raw link goes to the right place."""
Expand Down