Skip to content

Commit 4f33a90

Browse files
committed
Add code snippets
1 parent b295c7f commit 4f33a90

File tree

11 files changed

+119
-10
lines changed

11 files changed

+119
-10
lines changed

Manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ resources/css/bootstrap.min.css
4747
resources/css/datatables.min.css
4848
resources/css/pmd-tester.css
4949
resources/js/bootstrap.min.js
50+
resources/js/code-snippets.js
5051
resources/js/datatables.min.js
5152
resources/js/jquery-3.2.1.slim.min.js
5253
resources/js/jquery.min.js

lib/pmdtester/builders/liquid_renderer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def write_project_index(project, root)
6060

6161
def dump_violations_json(project)
6262
h = {
63+
'source_link_base' => project.webview_url,
6364
'source_link_template' => link_template(project),
6465
**violations_to_hash(project)
6566
}

resources/css/pmd-tester.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ body, td, select, input, li{
55
font-family: Verdana, Helvetica, Arial, sans-serif;
66
font-size: 13pt;
77
}
8-
code{
8+
code {
99
font-family: Courier, monospace;
1010
font-size: 13pt;
1111
}
12+
code.highlight {
13+
background-color: yellow;
14+
}
1215
a {
1316
text-decoration: none;
1417
}

resources/js/code-snippets.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Simple module to fetch code snippets via ajax.
3+
* Only supports github hosted code repos.
4+
*
5+
* Usage:
6+
* var el = document.createElement('p');
7+
* var url = 'https://github.com/checkstyle/checkstyle/tree/checkstyle-8.0/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4822variabledistance/InputVariableDeclarationUsageDistanceCheck.java'
8+
* var line = 3;
9+
* var weburl = 'https://github.com/checkstyle/checkstyle/tree/checkstyle-8.0/src/it/resources/com/google/checkstyle/test/chapter4formatting/rule4822variabledistance/InputVariableDeclarationUsageDistanceCheck.java#L3'
10+
* window.pmd_code_snippets.fetch(el, url, line, weburl);
11+
*/
12+
(function() {
13+
const contextLines = 3;
14+
15+
function formatLineNumber(number) {
16+
var prefix = "";
17+
if (number < 10) {
18+
prefix = "&nbsp;&nbsp;&nbsp;";
19+
} else if (number < 100) {
20+
prefix = "&nbsp;&nbsp;";
21+
} else if (number < 1000) {
22+
prefix = "&nbsp;";
23+
}
24+
return prefix + number;
25+
}
26+
27+
function fetchSnippet(el, url, line, weburl) {
28+
var weburl, requestUrl, oReq;
29+
30+
requestUrl = url.replace(/github.com/, "raw.githubusercontent.com");
31+
requestUrl = requestUrl.replace(/(tree|blob)\//, "");
32+
33+
oReq = new XMLHttpRequest();
34+
oReq.addEventListener("load", function() {
35+
var html, lines, start, deleteCount;
36+
37+
html = '<p><a href="' + weburl + '" target="_blank" rel="noopener noreferrer">' + weburl + '</a></p>';
38+
lines = this.responseText.split(/\r\n|\n/);
39+
start = line - contextLines;
40+
if (start > 0) {
41+
lines.splice(0, start);
42+
}
43+
deleteCount = lines.length - (2 * contextLines) + 1;
44+
lines.splice(2 * contextLines - 1, deleteCount);
45+
46+
lines.forEach(element => {
47+
start++;
48+
if (start == line) {
49+
html += "<code class=\"highlight\">";
50+
} else {
51+
html += "<code>";
52+
}
53+
html += formatLineNumber(start) + "&nbsp;" + element + "</code><br>";
54+
});
55+
el.innerHTML = html;
56+
});
57+
oReq.open("GET", requestUrl);
58+
oReq.send();
59+
60+
el.innerHTML = "<tt>fetching...</tt>";
61+
}
62+
63+
window.pmd_code_snippets = {
64+
fetch: fetchSnippet
65+
}
66+
})();

resources/js/project-report.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ $(document).ready(function () {
2020
return pathArray[pathArray.length - 1];
2121
}
2222

23+
function renderCodeSnippet(violation) {
24+
var node = document.createElement('p');
25+
var url = project.source_link_base + '/' + project.file_index[violation.f];
26+
window.pmd_code_snippets.fetch(node, url, violation.l, makeCodeLink(violation));
27+
return node;
28+
}
29+
2330
const cssClass = {
2431
"+": "added",
2532
"-": "removed",
@@ -32,10 +39,10 @@ $(document).ready(function () {
3239
"~": "Changed",
3340
}
3441

35-
$('#violationsTable').DataTable({
42+
var table = $('#violationsTable').DataTable({
3643
data: project.violations,
3744
columns: [
38-
// other attributes:
45+
// other attributes in data:
3946
// l: line
4047
// ol: old line
4148
{"data": "f"}, // file
@@ -45,12 +52,12 @@ $(document).ready(function () {
4552
],
4653
deferRender: true,
4754
// scrollY: "6000px",
48-
dom: 'Pfrtip',
55+
dom: 'Pfrtip', // Search Panes, filtering input, processing display element, table, table information summary, pagination control
4956
searchPanes: {
5057
viewTotal: true,
5158
cascadePanes: true,
5259
columns: [0, 1, 3],
53-
order: ['Rule', 'Location', 'Type']
60+
order: ['Rule', 'Location (click row to expand)', 'Type']
5461
},
5562
// scrollCollapse: true,
5663
// paging: false,
@@ -110,4 +117,20 @@ $(document).ready(function () {
110117
},
111118
});
112119

120+
$('#violationsTable tbody').on('click', 'tr', function() {
121+
var tr = $(this).closest('tr');
122+
var row = table.row( tr );
123+
124+
if ( row.child.isShown() ) {
125+
// This row is already open - close it
126+
row.child.hide();
127+
tr.removeClass('shown');
128+
}
129+
else {
130+
// Open this row
131+
row.child( renderCodeSnippet(row.data()) ).show();
132+
tr.addClass('shown');
133+
}
134+
});
135+
113136
});

resources/project_diff_report.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
<!DOCTYPE html>
12
<html lang="en">
23
<head>
4+
<meta charset="utf-8">
35
<title>Diff report for {{project_name}}</title>
46

57
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
@@ -10,6 +12,7 @@
1012
<script src="../js/popper.min.js"></script>
1113
<script src="../js/bootstrap.min.js"></script>
1214
<script src="../js/datatables.min.js"></script>
15+
<script src="../js/code-snippets.js"></script>
1316
<!-- This is generated -->
1417
<script src="./project_data.js"></script>
1518

@@ -111,7 +114,7 @@ <h2>Violations</h2>
111114
<table id="violationsTable" width="100%" class="table">
112115
<thead>
113116
<tr>
114-
<th>Location</th>
117+
<th>Location (click row to expand)</th>
115118
<th>Rule</th>
116119
<th>Message</th>
117120
<th>Type</th>

resources/project_index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
<!DOCTYPE html>
12
<html lang="en">
23
<head>
4+
<meta charset="utf-8">
35
<title>Project report index</title>
46

57
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>

test/integration_test_runner.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ def setup
88
end
99

1010
def test_local_mode
11-
argv = '-r target/repositories/pmd -b master -bc config/design.xml' \
12-
' -p pmd_releases/6.7.0 -pc config/design.xml -l test/resources/project-test.xml'
11+
argv = '-r target/repositories/pmd -b pmd_releases/6.7.0 -bc config/design.xml' \
12+
' -p master -pc config/design.xml -l test/resources/project-test.xml'
1313

1414
system("bundle exec bin/pmdtester #{argv}")
1515

@@ -21,7 +21,9 @@ def test_local_mode
2121
assert_path_exist('target/reports/pmd_releases_6.7.0/pmd/pmd_report.xml')
2222
assert_path_exist('target/reports/pmd_releases_6.7.0/pmd/config.xml')
2323
assert_path_exist('target/reports/diff/checkstyle/index.html')
24+
assert_path_exist('target/reports/diff/checkstyle/project_data.js')
2425
assert_path_exist('target/reports/diff/pmd/index.html')
26+
assert_path_exist('target/reports/diff/pmd/project_data.js')
2527
assert_path_exist('target/reports/diff/index.html')
2628
end
2729

test/resources/html_report_builder/expected_diff_report_index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
<!DOCTYPE html>
12
<html lang="en">
23
<head>
4+
<meta charset="utf-8">
35
<title>Diff report for spring-framework</title>
46

57
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
@@ -10,6 +12,7 @@
1012
<script src="../js/popper.min.js"></script>
1113
<script src="../js/bootstrap.min.js"></script>
1214
<script src="../js/datatables.min.js"></script>
15+
<script src="../js/code-snippets.js"></script>
1316
<!-- This is generated -->
1417
<script src="./project_data.js"></script>
1518

@@ -171,7 +174,7 @@ <h2>Violations</h2>
171174
<table id="violationsTable" width="100%" class="table">
172175
<thead>
173176
<tr>
174-
<th>Location</th>
177+
<th>Location (click row to expand)</th>
175178
<th>Rule</th>
176179
<th>Message</th>
177180
<th>Type</th>

test/resources/html_report_builder/expected_empty_diff_report.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
<!DOCTYPE html>
12
<html lang="en">
23
<head>
4+
<meta charset="utf-8">
35
<title>Diff report for openjdk10</title>
46

57
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css"/>
@@ -10,6 +12,7 @@
1012
<script src="../js/popper.min.js"></script>
1113
<script src="../js/bootstrap.min.js"></script>
1214
<script src="../js/datatables.min.js"></script>
15+
<script src="../js/code-snippets.js"></script>
1316
<!-- This is generated -->
1417
<script src="./project_data.js"></script>
1518

@@ -111,7 +114,7 @@ <h2>Violations</h2>
111114
<table id="violationsTable" width="100%" class="table">
112115
<thead>
113116
<tr>
114-
<th>Location</th>
117+
<th>Location (click row to expand)</th>
115118
<th>Rule</th>
116119
<th>Message</th>
117120
<th>Type</th>

0 commit comments

Comments
 (0)