Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 522016

Summary: Compare widget V15: Allow the widget users to calculate the line added, line removed and line changed on the diff.
Product: [ECD] Orion Reporter: libing wang <libingw>
Component: ClientAssignee: libing wang <libingw>
Status: RESOLVED FIXED QA Contact:
Severity: critical    
Priority: P2    
Version: 15.0   
Target Milestone: 16.0   
Hardware: PC   
OS: Mac OS X   
See Also: https://github.com/eclipse/orion.client/pull/174
Whiteboard:

Description libing wang CLA 2017-09-07 16:03:21 EDT

    
Comment 1 libing wang CLA 2017-09-07 16:07:59 EDT
The idea is to use "orion/compare/jsdiffAdapter" module in the compare widget requirejs build, without creating the widget instance itself.
Comment 2 libing wang CLA 2017-09-07 16:22:13 EDT
Here is the snippet to calculate:
************************************************

	require.config({
	    bundles: {
	        "built-compare-amd": ["orion/compare", "orion/compare/jsdiffAdapter"]
	    }	    
	});    
        require(["orion/compare", "orion/compare/jsdiffAdapter"], function(Compare, mJSDiffAdapter) {
		var diffAdapter = new mJSDiffAdapter.JSDiffAdapter(true/*ignore white space*/);
		var maps = diffAdapter.adapt(sampleRight/*A string representing the old File contents, including line delimeters*/, 
									 sampleLeft/*A string representing the new File contents, including line delimeters*/, 
									 "\n"/*line delimeter*/);
		
		var totalLinesInNew = 0, totalLinesInOld = 0, changedLines = 0, removedLines = 0, addedLines = 0;
		
		maps.mapper.forEach(function(singleDiffBlock){
			/*
			 * A singleDiffBock is a simple array of 3 numbers
			 * [0]: how many lines are there in the new file in this diff block
			 * [1]: how many lines are there in the old file in this diff block
			 * [3]: a number(0 or none zero). 0 means both new and old files are the same in this diff block. Just ignore the 0 case
			 */
			
			totalLinesInNew = totalLinesInNew + singleDiffBlock[0];
			totalLinesInOld = totalLinesInOld + singleDiffBlock[1];
			if(singleDiffBlock[2] !== 0) {
				if(singleDiffBlock[0] === singleDiffBlock[1]) {//New and old files have the same lines in this diff block
					changedLines = changedLines + singleDiffBlock[0];
				} else if(singleDiffBlock[0] === 0) {// The new file side has no lines in this diff block
					removedLines = removedLines + singleDiffBlock[1];
				} else if (singleDiffBlock[1] === 0) {// The old file side has no lines in this diff block
					addedLines = addedLines + singleDiffBlock[0];
				} else {//New and old files have the diffrent lines(non zero) in this diff block
					//E.g. If old file has 5 lines and new file has 2 lines, we should say it is a changedLine
					changedLines = changedLines + singleDiffBlock[1];
				}
			}
		});
		alert("total lines in the old file: " + totalLinesInOld + "\n" + "total lines in the new file: " + totalLinesInNew + "\n" +
			  "changed lines: " + changedLines + "\n" + "removed lines: " + removedLines + "\n" + "added lines: " + addedLines);
        });
Comment 3 Eclipse Genie CLA 2017-09-07 16:33:10 EDT
GitHub Pull Request 174 created by [libingw]
https://github.com/eclipse/orion.client/pull/174
Comment 4 libing wang CLA 2017-09-07 16:34:32 EDT
Pushed the demo