package refdiff.examples;

import java.io.File;

import refdiff.core.RefDiff;
import refdiff.core.diff.CstDiff;
import refdiff.core.diff.Relationship;
import refdiff.parsers.c.CParser;
import refdiff.parsers.java.JavaParser;
import refdiff.parsers.js.JsParser;

public class RefDiffExample {
	
	public static void main(String[] args) throws Exception {
		runExamples();
	}
	
	private static void runExamples() throws Exception {
		// This is a temp folder to clone or checkout git repositories
		File tempFolder = new File("temp");
		
		JsParser jsParser = new JsParser();
		RefDiff refDiffJs = new RefDiff(jsParser);
		
		File angularJsRepo = refDiffJs.cloneGitRepository(
			new File(tempFolder, "angular.js"),
			"https://github.com/refdiff-study/angular.js.git");
		
		CstDiff diffForCommit = refDiffJs.computeDiffForCommit(angularJsRepo, "2636105");
		printRefactorings("Refactorings found in angular.js 2636105", diffForCommit);
		
		refDiffJs.computeDiffForCommitHistory(angularJsRepo, 5, (commit, diff) -> {
			printRefactorings("Refactorings found in angular.js " + commit.getId().name(), diff);
		});
		
		jsParser.close();
		
		
		CParser cParser = new CParser();
		RefDiff refDiffC = new RefDiff(cParser);
		
		File gitRepo = refDiffC.cloneGitRepository(
			new File(tempFolder, "git"),
			"https://github.com/refdiff-study/git.git");
		
		printRefactorings(
			"Refactorings found in git ba97aea",
			refDiffC.computeDiffForCommit(gitRepo, "ba97aea1659e249a3a58ecc5f583ee2056a90ad8"));
		
		JavaParser javaParser = new JavaParser(tempFolder);
		RefDiff refDiffJava = new RefDiff(javaParser);
		
		File eclipseThemesRepo = refDiffC.cloneGitRepository(
			new File(tempFolder, "eclipse-themes"),
			"https://github.com/icse18-refactorings/eclipse-themes.git");
		
		printRefactorings(
			"Refactorings found in eclipse-themes 72f61ec",
			refDiffJava.computeDiffForCommit(eclipseThemesRepo, "72f61ec"));
	}
	
	private static void printRefactorings(String headLine, CstDiff diff) {
		System.out.println(headLine);
		for (Relationship rel : diff.getRefactoringRelationships()) {
			System.out.println(rel.getStandardDescription());
		}
	}
	
}

@@ -486,16 +486,17 @@
 sitories
+.
 %0A%09%09File 
@@ -529,16 +529,87 @@
 p%22);%0A%09%09%0A
+%09%09// Creates a RefDiff instance configured with the JavaScript parser.%0A
 %09%09JsPars
@@ -682,24 +682,63 @@
 Parser);%0A%09%09%0A
+%09%09// Clone the angular.js GitHub repo.%0A
 %09%09File angul
@@ -875,16 +875,319 @@
 t%22);%0A%09%09%0A
+%09%09// You can compute the relationships between the code elements in a commit with%0A%09%09// its previous commit. The result of this operation is a CstDiff object, which%0A%09%09// contains all relationships between CstNode's. Relationships whose type is different%0A%09%09// from RelationshipType.SAME are refactorings.%0A
 %09%09CstDif
@@ -1345,16 +1345,180 @@
 it);%0A%09%09%0A
+%09%09// You can also mine refactoring from the commit history. In this example we navigate%0A%09%09// the commit graph backwards up to 5 commits. Merge commits are skipped.%0A
 %09%09refDif
@@ -1691,31 +1691,182 @@
 %09%0A%09%09
-jsParser.close();%0A%09%09%0A%09%09
+// The JsParser initializes JavaScript runtime to run the Babel parser. We should close it shut down.%0A%09%09jsParser.close();%0A%09%09%0A%09%09%0A%09%09// In this example, we use the parser for C.
 %0A%09%09C
@@ -2224,16 +2224,57 @@
 %22));%0A%09%09%0A
+%09%09%0A%09%09// Now, we use the parser for Java.%0A
 %09%09JavaPa

Add comments to the example.