Skip to content

Commit e8b268d

Browse files
committed
feat(front): include info lines in refactoring
1 parent 2c63b2d commit e8b268d

File tree

10 files changed

+431
-59
lines changed

10 files changed

+431
-59
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
action
1+
action/
2+
.cache
3+
node_modules/
File renamed without changes.
File renamed without changes.

js/build/background.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// modules are defined as an array
2+
// [ module function, map of requires ]
3+
//
4+
// map of requires is short require name -> numeric require
5+
//
6+
// anything defined in a previous bundle is accessed via the
7+
// orig method which is the require for previous bundles
8+
parcelRequire = (function (modules, cache, entry, globalName) {
9+
// Save the require from previous bundle to this closure if any
10+
var previousRequire = typeof parcelRequire === 'function' && parcelRequire;
11+
var nodeRequire = typeof require === 'function' && require;
12+
13+
function newRequire(name, jumped) {
14+
if (!cache[name]) {
15+
if (!modules[name]) {
16+
// if we cannot find the module within our internal map or
17+
// cache jump to the current global require ie. the last bundle
18+
// that was added to the page.
19+
var currentRequire = typeof parcelRequire === 'function' && parcelRequire;
20+
if (!jumped && currentRequire) {
21+
return currentRequire(name, true);
22+
}
23+
24+
// If there are other bundles on this page the require from the
25+
// previous one is saved to 'previousRequire'. Repeat this as
26+
// many times as there are bundles until the module is found or
27+
// we exhaust the require chain.
28+
if (previousRequire) {
29+
return previousRequire(name, true);
30+
}
31+
32+
// Try the node require function if it exists.
33+
if (nodeRequire && typeof name === 'string') {
34+
return nodeRequire(name);
35+
}
36+
37+
var err = new Error('Cannot find module \'' + name + '\'');
38+
err.code = 'MODULE_NOT_FOUND';
39+
throw err;
40+
}
41+
42+
localRequire.resolve = resolve;
43+
localRequire.cache = {};
44+
45+
var module = cache[name] = new newRequire.Module(name);
46+
47+
modules[name][0].call(module.exports, localRequire, module, module.exports, this);
48+
}
49+
50+
return cache[name].exports;
51+
52+
function localRequire(x){
53+
return newRequire(localRequire.resolve(x));
54+
}
55+
56+
function resolve(x){
57+
return modules[name][1][x] || x;
58+
}
59+
}
60+
61+
function Module(moduleName) {
62+
this.id = moduleName;
63+
this.bundle = newRequire;
64+
this.exports = {};
65+
}
66+
67+
newRequire.isParcelRequire = true;
68+
newRequire.Module = Module;
69+
newRequire.modules = modules;
70+
newRequire.cache = cache;
71+
newRequire.parent = previousRequire;
72+
newRequire.register = function (id, exports) {
73+
modules[id] = [function (require, module) {
74+
module.exports = exports;
75+
}, {}];
76+
};
77+
78+
var error;
79+
for (var i = 0; i < entry.length; i++) {
80+
try {
81+
newRequire(entry[i]);
82+
} catch (e) {
83+
// Save first error but execute all entries
84+
if (!error) {
85+
error = e;
86+
}
87+
}
88+
}
89+
90+
if (entry.length) {
91+
// Expose entry point to Node, AMD or browser globals
92+
// Based on https://github.com/ForbesLindesay/umd/blob/master/template.js
93+
var mainExports = newRequire(entry[entry.length - 1]);
94+
95+
// CommonJS
96+
if (typeof exports === "object" && typeof module !== "undefined") {
97+
module.exports = mainExports;
98+
99+
// RequireJS
100+
} else if (typeof define === "function" && define.amd) {
101+
define(function () {
102+
return mainExports;
103+
});
104+
105+
// <script>
106+
} else if (globalName) {
107+
this[globalName] = mainExports;
108+
}
109+
}
110+
111+
// Override the current require with this new one
112+
parcelRequire = newRequire;
113+
114+
if (error) {
115+
// throw error from earlier, _after updating parcelRequire_
116+
throw error;
117+
}
118+
119+
return newRequire;
120+
})({"background.js":[function(require,module,exports) {
121+
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
122+
if (!changeInfo.url) {
123+
return;
124+
}
125+
126+
fetchData(changeInfo.url, function (data) {
127+
chrome.tabs.sendMessage(tabId, {
128+
message: "data",
129+
data: data
130+
});
131+
});
132+
});
133+
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
134+
if (request.message === "fetch") {
135+
fetchData(request.url, function (data) {
136+
chrome.tabs.query({
137+
active: true,
138+
currentWindow: true
139+
}, function (tabs) {
140+
chrome.tabs.sendMessage(tabs[0].id, {
141+
message: "data",
142+
data: data
143+
});
144+
});
145+
});
146+
}
147+
});
148+
149+
function fetchData(url, callback) {
150+
var regex = /github\.com\/([\w_-]+)\/([\w_-]+)\/pull\/(\d+)/g;
151+
var urlParts = regex.exec(url);
152+
153+
if (!urlParts) {
154+
return;
155+
}
156+
157+
fetch("https://refdiff.brito.com.br/".concat(urlParts[1], "/").concat(urlParts[2], "/").concat(urlParts[3])).then(function (response) {
158+
return response.json();
159+
}).then(function (data) {
160+
callback(data);
161+
});
162+
}
163+
},{}]},{},["background.js"], null)

js/build/content-script.js

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// modules are defined as an array
2+
// [ module function, map of requires ]
3+
//
4+
// map of requires is short require name -> numeric require
5+
//
6+
// anything defined in a previous bundle is accessed via the
7+
// orig method which is the require for previous bundles
8+
parcelRequire = (function (modules, cache, entry, globalName) {
9+
// Save the require from previous bundle to this closure if any
10+
var previousRequire = typeof parcelRequire === 'function' && parcelRequire;
11+
var nodeRequire = typeof require === 'function' && require;
12+
13+
function newRequire(name, jumped) {
14+
if (!cache[name]) {
15+
if (!modules[name]) {
16+
// if we cannot find the module within our internal map or
17+
// cache jump to the current global require ie. the last bundle
18+
// that was added to the page.
19+
var currentRequire = typeof parcelRequire === 'function' && parcelRequire;
20+
if (!jumped && currentRequire) {
21+
return currentRequire(name, true);
22+
}
23+
24+
// If there are other bundles on this page the require from the
25+
// previous one is saved to 'previousRequire'. Repeat this as
26+
// many times as there are bundles until the module is found or
27+
// we exhaust the require chain.
28+
if (previousRequire) {
29+
return previousRequire(name, true);
30+
}
31+
32+
// Try the node require function if it exists.
33+
if (nodeRequire && typeof name === 'string') {
34+
return nodeRequire(name);
35+
}
36+
37+
var err = new Error('Cannot find module \'' + name + '\'');
38+
err.code = 'MODULE_NOT_FOUND';
39+
throw err;
40+
}
41+
42+
localRequire.resolve = resolve;
43+
localRequire.cache = {};
44+
45+
var module = cache[name] = new newRequire.Module(name);
46+
47+
modules[name][0].call(module.exports, localRequire, module, module.exports, this);
48+
}
49+
50+
return cache[name].exports;
51+
52+
function localRequire(x){
53+
return newRequire(localRequire.resolve(x));
54+
}
55+
56+
function resolve(x){
57+
return modules[name][1][x] || x;
58+
}
59+
}
60+
61+
function Module(moduleName) {
62+
this.id = moduleName;
63+
this.bundle = newRequire;
64+
this.exports = {};
65+
}
66+
67+
newRequire.isParcelRequire = true;
68+
newRequire.Module = Module;
69+
newRequire.modules = modules;
70+
newRequire.cache = cache;
71+
newRequire.parent = previousRequire;
72+
newRequire.register = function (id, exports) {
73+
modules[id] = [function (require, module) {
74+
module.exports = exports;
75+
}, {}];
76+
};
77+
78+
var error;
79+
for (var i = 0; i < entry.length; i++) {
80+
try {
81+
newRequire(entry[i]);
82+
} catch (e) {
83+
// Save first error but execute all entries
84+
if (!error) {
85+
error = e;
86+
}
87+
}
88+
}
89+
90+
if (entry.length) {
91+
// Expose entry point to Node, AMD or browser globals
92+
// Based on https://github.com/ForbesLindesay/umd/blob/master/template.js
93+
var mainExports = newRequire(entry[entry.length - 1]);
94+
95+
// CommonJS
96+
if (typeof exports === "object" && typeof module !== "undefined") {
97+
module.exports = mainExports;
98+
99+
// RequireJS
100+
} else if (typeof define === "function" && define.amd) {
101+
define(function () {
102+
return mainExports;
103+
});
104+
105+
// <script>
106+
} else if (globalName) {
107+
this[globalName] = mainExports;
108+
}
109+
}
110+
111+
// Override the current require with this new one
112+
parcelRequire = newRequire;
113+
114+
if (error) {
115+
// throw error from earlier, _after updating parcelRequire_
116+
throw error;
117+
}
118+
119+
return newRequire;
120+
})({"content-script.js":[function(require,module,exports) {
121+
var fileMap = {};
122+
var popup = document.createElement("div");
123+
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
124+
switch (request.message) {
125+
case "data":
126+
console.log(request.data);
127+
request.data.refactorings.forEach(function (refactoring) {
128+
var file = fileMap[refactoring.after_file_name]; // TODO: change for before
129+
130+
if (file) {
131+
addRefactorings(file.ref, file.link, refactoring);
132+
} else {
133+
console.error("not found reference for " + refactoring.after_file_name);
134+
}
135+
});
136+
break;
137+
138+
default:
139+
console.log("others: " + request);
140+
}
141+
});
142+
window.addEventListener("load", function () {
143+
popup.setAttribute("class", "diff-refector-popup");
144+
popup.innerHTML = "\n <button class=\"diff-refector-popup-close btn btn-sm btn-default\">x</button>\n <p><b>Type:</b> <span class=\"refactor-type\">Move Method</span></p>\n <div class=\"refactor-diff\"></div>\n <a class=\"btn btn-sm btn-primary refactor-link\" href=\"#\">Go to block</a>\n ";
145+
146+
popup.showDiff = function (element, type, diffHTML, interval) {
147+
popup.style.setProperty("display", "block");
148+
popup.querySelector(".refactor-diff").innerHTML = diffHTML;
149+
popup.querySelector(".refactor-type").innerText = type;
150+
151+
if (interval) {
152+
popup.querySelector(".refactor-link").setAttribute("href", interval);
153+
}
154+
155+
var offset = (popupPosition = popup.getBoundingClientRect().width) + 10;
156+
var pos = element.getBoundingClientRect();
157+
popup.style.setProperty("top", pos.top + "px");
158+
popup.style.setProperty("left", pos.left - offset + "px");
159+
};
160+
161+
document.body.appendChild(popup);
162+
document.querySelector(".diff-refector-popup-close").addEventListener("click", function () {
163+
popup.style.setProperty("display", "none");
164+
});
165+
var files = document.querySelectorAll(".file");
166+
files.forEach(function (file) {
167+
var header = file.querySelector(".file-info > a");
168+
var fileName = header.textContent;
169+
var link = header.getAttribute("href");
170+
console.log("File=" + fileName + " Link=" + link);
171+
fileMap[fileName] = {
172+
ref: file,
173+
link: link
174+
};
175+
});
176+
chrome.runtime.sendMessage({
177+
message: "fetch",
178+
url: document.location.href
179+
});
180+
});
181+
182+
function addRefactorings(file, link, refactoring) {
183+
console.log("adding refactoring for ", refactoring);
184+
console.log("REF = ", file);
185+
file.querySelectorAll(".code-review.blob-code.blob-code-deletion").forEach(function (line) {
186+
console.log("seraching for ", refactoring.before_line_number);
187+
188+
if (!line.querySelector("[data-line=\"".concat(refactoring.before_line_number, "\"]"))) {
189+
return;
190+
}
191+
192+
console.log("found line!!!!");
193+
var button = document.createElement("button");
194+
button.setAttribute("class", "btn-refector");
195+
button.addEventListener("click", function () {
196+
var moveMethodDiff = "diff --git a/Example.java b/Example.java\nindex aa5aefd..36cbde8 100644\n--- a/Example.java\n+++ b/Example.java\n@@ -1,20 +1,9 @@\n import java.io.*;\n \n public class Example {\n\tpublic void DoNothing() {\n-\t\tSystem.out.println(\"do nothing\");\n+\t\tSystem.out.println(\"do something\");\n\t}\n";
197+
var moveMethodHTML = Diff2Html.getPrettyHtml(moveMethodDiff, {
198+
drawFileList: true,
199+
matching: "lines"
200+
});
201+
popup.showDiff(button, "".concat(refactoring.type, " - ").concat(refactoring.object_type), moveMethodHTML, link);
202+
});
203+
button.innerText = "R";
204+
line.appendChild(button);
205+
});
206+
}
207+
},{}]},{},["content-script.js"], null)

0 commit comments

Comments
 (0)