@@ -120,32 +120,37 @@ parcelRequire = (function (modules, cache, entry, globalName) {
120120} ) ( { "content-script.js" :[ function ( require , module , exports ) {
121121var fileMap = { } ;
122122var popup = document . createElement ( "div" ) ;
123+ var currentPage = "" ;
124+
125+ function urlEqual ( baseURL , reference ) {
126+ return baseURL . split ( "#diff" ) [ 0 ] === reference . split ( "#diff" ) [ 0 ] ;
127+ }
128+
123129chrome . runtime . onMessage . addListener ( function ( request , sender , sendResponse ) {
124130 switch ( request . message ) {
125131 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
132+ if ( urlEqual ( request . url , currentPage ) ) {
133+ console . log ( "same page, skipping..." ) ;
134+ return ;
135+ }
129136
130- if ( file ) {
131- addRefactorings ( file . ref , file . link , refactoring ) ;
132- } else {
133- console . error ( "not found reference for " + refactoring . after_file_name ) ;
134- }
137+ currentPage = request . url . split ( "#diff" ) [ 0 ] ;
138+ console . log ( "DATA = " , request . data ) ;
139+ request . data . refactorings . forEach ( function ( refactoring ) {
140+ var beforeFile = fileMap [ refactoring . before_file_name ] ;
141+ var afterFile = fileMap [ refactoring . after_file_name ] ;
142+ addRefactorings ( beforeFile . ref , "" . concat ( afterFile . link , "R" ) . concat ( refactoring . after_line_number ) , refactoring , "L" ) ;
143+ addRefactorings ( afterFile . ref , "" . concat ( beforeFile . link , "L" ) . concat ( refactoring . before_line_number ) , refactoring , "R" ) ;
135144 } ) ;
136- break ;
137-
138- default :
139- console . log ( "others: " + request ) ;
140145 }
141146} ) ;
142147window . addEventListener ( "load" , function ( ) {
143148 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 " ;
149+ popup . innerHTML = "\n <button class=\"diff-refector-popup-close btn btn-sm btn-default\">x</button>\n <p><b class=\"refactor-type\"></b ></p>\n <div class=\"refactor-content \"></div>\n <a class=\"btn btn-sm btn-primary refactor-link\" href=\"#\">Go to block</a>\n " ;
145150
146151 popup . showDiff = function ( element , type , diffHTML , interval ) {
147152 popup . style . setProperty ( "display" , "block" ) ;
148- popup . querySelector ( ".refactor-diff " ) . innerHTML = diffHTML ;
153+ popup . querySelector ( ".refactor-content " ) . innerHTML = diffHTML ;
149154 popup . querySelector ( ".refactor-type" ) . innerText = type ;
150155
151156 if ( interval ) {
@@ -175,30 +180,47 @@ window.addEventListener("load", function () {
175180 } ) ;
176181 chrome . runtime . sendMessage ( {
177182 message : "fetch" ,
178- url : document . location . href
183+ url : document . location . href . split ( "#diff" ) [ 0 ]
179184 } ) ;
180185} ) ;
181186
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+ function addRefactorings ( file , link , refactoring , side ) {
188+ console . log ( "adding refactoring for " , refactoring ) ; // right side (addiction)
189+
190+ var lineNumber = refactoring . after_line_number ;
191+ var selector = ".code-review.blob-code.blob-code-addition" ; // left side (deletion)
187192
188- if ( ! line . querySelector ( "[data-line=\"" . concat ( refactoring . before_line_number , "\"]" ) ) ) {
193+ if ( side === "L" ) {
194+ lineNumber = refactoring . before_line_number ;
195+ selector = ".code-review.blob-code.blob-code-deletion" ;
196+ }
197+
198+ file . querySelectorAll ( selector ) . forEach ( function ( line ) {
199+ console . log ( "seraching for " , lineNumber , "side = " , side ) ;
200+
201+ if ( ! line . querySelector ( "[data-line=\"" . concat ( lineNumber , "\"]" ) ) ) {
189202 return ;
190203 }
191204
205+ var contentHTML ;
206+
207+ switch ( refactoring . type ) {
208+ case "RENAME" :
209+ contentHTML = "<p>" . concat ( refactoring . before_local_name , " to " ) . concat ( refactoring . after_local_name , "</p>" ) ;
210+ break ;
211+
212+ case "MOVE" :
213+ contentHTML = "<p>" . concat ( refactoring . object_type , " " ) . concat ( refactoring . before_local_name , " moved.</p>" ) ;
214+ contentHTML += "<p>Origin: " . concat ( refactoring . before_file_name , ":" ) . concat ( refactoring . before_line_number , "</p>" ) ;
215+ contentHTML += "<p>Destiny: " . concat ( refactoring . after_file_name , ":" ) . concat ( refactoring . after_line_number , "</p>" ) ;
216+ break ;
217+ }
218+
192219 console . log ( "found line!!!!" ) ;
193220 var button = document . createElement ( "button" ) ;
194221 button . setAttribute ( "class" , "btn-refector" ) ;
195222 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 ) ;
223+ popup . showDiff ( button , "" . concat ( refactoring . type , " " ) . concat ( refactoring . object_type ) , contentHTML , link ) ;
202224 } ) ;
203225 button . innerText = "R" ;
204226 line . appendChild ( button ) ;
0 commit comments