-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_diff.html
More file actions
245 lines (213 loc) · 7.32 KB
/
code_diff.html
File metadata and controls
245 lines (213 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/codemirror.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/codemirror.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.16.0/mode/javascript/javascript.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/diff_match_patch/20121119/diff_match_patch.js"></script>
<script>
var originalCodeMirror = undefined;
var modifiedCodeMirror = undefined;
var difflib = new diff_match_patch();
var DIFFS = null;
window.onload = function() {
originalCodeMirror = CodeMirror.fromTextArea(document.getElementById('original'), {
mode: "string",
theme: "default",
smartIndent: false,
lineWrapping: true,
lineNumbers: true,
electricChars: false,
showCursorWhenSelecting: true,
});
originalCodeMirror.setSize("100%", "100%");
// Add event handlers
originalCodeMirror.on("beforeSelectionChange", function(cm, change) {
range = change.ranges[0];
anchor = range.anchor;
head = range.head;
// return if something goes wrong...
if (!(anchor && head && modifiedCodeMirror)) {
return;
}
console.log(anchor);
});
originalCodeMirror.on("change", function(cm, change) {
DIFFS = null;
});
modifiedCodeMirror = CodeMirror.fromTextArea(document.getElementById('modified'), {
mode: "string",
theme: "default",
smartIndent: false,
lineWrapping: true,
lineNumbers: true,
electricChars: false,
});
modifiedCodeMirror.setSize("100%", "100%");
}
function copyValues(flag) {
if (flag === 1) {
modifiedCodeMirror.setValue(originalCodeMirror.getValue());
} else if (flag === -1) {
originalCodeMirror.setValue(modifiedCodeMirror.getValue());
}
}
function getLineDiffs() {
var text_orig = originalCodeMirror.getValue();
var text_modi = modifiedCodeMirror.getValue();
/* // uncomment for line diff
var a = difflib.diff_linesToChars_(text_orig, text_modi); // as of Neil Fraser's example
var lineText1 = a['chars1'];
var lineText2 = a['chars2'];
var lineArray = a['lineArray'];
var diffs = difflib.diff_main(lineText1, lineText2, false);
var lineDiffs = difflib.diff_charsToLines_(diffs, lineArray);
*/
var diffs = difflib.diff_main(text_orig, text_modi);
difflib.diff_cleanupSemantic(diffs);
DIFFS = diffs;
return diffs;
}
function runDiff() {
var diffs = getLineDiffs();
origLineIndex = {line: 0, ch: 0};
modiLineIndex = {line: 0, ch: 0};
for (linediff of diffs) {
var diffChars = linediff[1].split('\n');
if (linediff[0] == 0) {
origLineIndex.line += diffChars.length - 1;
modiLineIndex.line += diffChars.length - 1;
if (diffChars.length == 1) {
origLineIndex.ch += diffChars[diffChars.length - 1].length;
modiLineIndex.ch += diffChars[diffChars.length - 1].length;
} else {
origLineIndex.ch = diffChars[diffChars.length - 1].length;
modiLineIndex.ch = diffChars[diffChars.length - 1].length;
}
} else if (linediff[0] == -1) {
newOrigLineIndex = {line: origLineIndex.line + diffChars.length - 1,
ch: origLineIndex.ch + diffChars[diffChars.length - 1].length}
originalCodeMirror.markText(origLineIndex,
newOrigLineIndex,
{className: 'deleted'});
console.log("Marking from ");
console.log(origLineIndex);
console.log("to")
console.log(newOrigLineIndex);
origLineIndex = newOrigLineIndex;
} else if (linediff[0] == 1) {
newModiLineIndex = {line: modiLineIndex.line + diffChars.length - 1,
ch: modiLineIndex.ch + diffChars[diffChars.length - 1].length}
modifiedCodeMirror.markText(modiLineIndex,
newModiLineIndex,
{className: 'added'});
modiLineIndex = newModiLineIndex;
}
}
}
/** Function count the occurrences of substring in a string;
* @param {String} string Required. The string;
* @param {String} subString Required. The string to search for;
* @param {Boolean} allowOverlapping Optional. Default: false;
* @author Vitim.us http://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string/7924240#7924240
*/
function occurrences(string, subString, allowOverlapping) {
string += "";
subString += "";
if (subString.length <= 0) return (string.length + 1);
var n = 0,
pos = 0,
step = allowOverlapping ? 1 : subString.length;
while (true) {
pos = string.indexOf(subString, pos);
if (pos >= 0) {
++n;
pos += step;
} else break;
}
return n;
}
</script>
<style>
.text-table {
width: 100%;
height: 100%;
}
.text-editor {
width: 50%;
height: 100%;
}
.highlighted {
background-color: yellow;
}
.deleted {
background-color: #FFAAAA;
}
.added {
background-color: #AAFFAA;
}
.not-highlighted {
background-color: inherit;
}
</style>
</head>
<body>
<table class="text-table">
<tr>
<td>
Original
</td>
<td>
</td>
<td>
Modified
</td>
</tr>
<tr>
<td class="text-editor">
<textarea rows="4" cols="50" id="original" name="original">package cs206b;
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
String rev = reverse();
}
public static String reverse() {
String s = "";
// Implement here
return s;
}
}</textarea>
</td>
<td>
<input type="button" value="->" onClick="copyValues(1)">
<br>
<input type="button" value="<-" onClick="copyValues(-1)">
<br>
<input type="button" value="Diff" onClick="runDiff()">
</td>
<td class="text-editor">
<textarea rows="4" cols="50" id="modified" name="modified">package cs206b;
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
String rev = reverse();
}
public static String reverse() {
String revString = "";
String inputString = "";
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()){
inputString = scanner.nextLine();
revString = inputString + "\n" + revString;
}
System.out.println(revString);
return revString;
}
}</textarea>
</td>
</tr>
</table>
</body>
</html>