<commit_before>
var lastSyncedContent;
var lastSyncedCursor;
var cursors = {};

jQuery.fn.contentsNoCursors = function()
{
	var cont = $(this).contents();
	//console.log(cont);
	var ret = $();
	for( var i = 0; i < cont.length; i++ ){
		var n = cont.eq(i);
		if( n.get().className != 'cursor' )
			ret = ret.add(cont.eq(i));
	}
	
	//console.log(ret);

	return ret;
	//return $(this).contents();
}

function getPath(start_node, decendant)
{
	var pathstr = [];
	while( decendant.parent().length > 0 ){
		if( decendant.get(0) === start_node.get(0) ) break;
		var par = decendant.parent();
		pathstr.push(par.contentsNoCursors().index(decendant));
		decendant = par;
	}
	//console.log(pathstr);
	return pathstr.reverse();
}

function resolvePath(start_node, path)
{
	for( var i = 0; i < path.length; i++ ){
		if( start_node.get(0) instanceof Text )
			break;
		var cont = start_node.contentsNoCursors();
		start_node = cont.eq(path[i]);
	}
	return start_node.get(0);
}

function updateRemoteCursors()
{
	$('#cursors').empty();
	for( c in cursors ){
		$('#cursors').append("<div class=\"cursor\" id=\"cursor-"+c+"\"></div>");
		$('#cursor-'+c).css('left', cursors[c].left);
		$('#cursor-'+c).css('top', cursors[c].top);
	}
}

function onServerMessageReceived(json)
{
	var commands = JSON.parse(json);
	
	var edit = $('#edit');

	/*if( commands.applyPatch ){
		var cur = getCursorPos();
		var curstartpath = getPath(edit, $(cur.startContainer));
		var curendpath = getPath(edit, $(cur.endContainer));
		var dmp = new diff_match_patch();
		var newcont = edit.html();
		console.log(newcont);
		newcont = dmp.patch_apply(dmp.patch_fromText(commands.applyPatch), newcont);
		newcont = newcont[0];
		edit.html(newcont);
		cur.endContainer = resolvePath(edit, curpath);
		cur.startContainer = resolvePath(edit, curpath);
		setCursorPos(cur);
	}*/

	if( commands.updateCursor ){
		var cur = getCursorPos();
		var curid = commands.updateCursor.id;
		var curpath = commands.updateCursor.path;
		var curstartpath = getPath(edit, $(cur.startContainer));
		var curendpath = getPath(edit, $(cur.endContainer));
		
		//
		// Mirror the current cursor
		//
		var dst = edit;
		var oldContents = edit.html();
		
		log += "START "+dst.get(0).tagName+"\n";
		for( var i = 0; i < curpath.length; i++ ){
			if( dst.get(0) instanceof Text )
				break;
			log += "GO TO "+dst.get(0).tagName+"["+curpath[i]+"]\n";
			var cont = dst.contentsNoCursors();
			dst = cont.eq(curpath[i]);
			log += " -> "+dst.get(0)+"\n";
		}
		var idx = curpath[curpath.length-1];
		
		var cursorText = "<div id=\"cursor-dummy\" class=\"cursor\"></div>";
		
		var handled = false;
		if( dst.get(0).tagName == "BR" ){
			var siblings = dst.parent().contentsNoCursors();
			var pred = siblings.eq(siblings.index(dst)-1);
			if( pred.get(0) instanceof Text ){
				dst = dst.before();
			} else {
				log += "PRED: "+pred.get(0) + "\n";
				dst.before(cursorText);
				handled = true;
			}
		}
		if( !handled ){
			if( dst.get(0) instanceof Text ){
				var oldText = dst.text().trim();
				var newText = oldText.substr(0, idx) + cursorText + oldText.substr(idx);
				dst.replaceWith(newText);
			} else {<commit_msg>Fixed js compile error due to not declared variable.<commit_after>
var lastSyncedContent;
var lastSyncedCursor;
var cursors = {};

jQuery.fn.contentsNoCursors = function()
{
	var cont = $(this).contents();
	//console.log(cont);
	var ret = $();
	for( var i = 0; i < cont.length; i++ ){
		var n = cont.eq(i);
		if( n.get().className != 'cursor' )
			ret = ret.add(cont.eq(i));
	}
	
	//console.log(ret);

	return ret;
	//return $(this).contents();
}

function getPath(start_node, decendant)
{
	var pathstr = [];
	while( decendant.parent().length > 0 ){
		if( decendant.get(0) === start_node.get(0) ) break;
		var par = decendant.parent();
		pathstr.push(par.contentsNoCursors().index(decendant));
		decendant = par;
	}
	//console.log(pathstr);
	return pathstr.reverse();
}

function resolvePath(start_node, path)
{
	for( var i = 0; i < path.length; i++ ){
		if( start_node.get(0) instanceof Text )
			break;
		var cont = start_node.contentsNoCursors();
		start_node = cont.eq(path[i]);
	}
	return start_node.get(0);
}

function updateRemoteCursors()
{
	$('#cursors').empty();
	for( c in cursors ){
		$('#cursors').append("<div class=\"cursor\" id=\"cursor-"+c+"\"></div>");
		$('#cursor-'+c).css('left', cursors[c].left);
		$('#cursor-'+c).css('top', cursors[c].top);
	}
}

function onServerMessageReceived(json)
{
	var commands = JSON.parse(json);
	
	var edit = $('#edit');

	/*if( commands.applyPatch ){
		var cur = getCursorPos();
		var curstartpath = getPath(edit, $(cur.startContainer));
		var curendpath = getPath(edit, $(cur.endContainer));
		var dmp = new diff_match_patch();
		var newcont = edit.html();
		console.log(newcont);
		newcont = dmp.patch_apply(dmp.patch_fromText(commands.applyPatch), newcont);
		newcont = newcont[0];
		edit.html(newcont);
		cur.endContainer = resolvePath(edit, curpath);
		cur.startContainer = resolvePath(edit, curpath);
		setCursorPos(cur);
	}*/

	if( commands.updateCursor ){
		var cur = getCursorPos();
		var curid = commands.updateCursor.id;
		var curpath = commands.updateCursor.path;
		var curstartpath = getPath(edit, $(cur.startContainer));
		var curendpath = getPath(edit, $(cur.endContainer));
		
		//
		// Mirror the current cursor
		//
		var dst = edit;
		var oldContents = edit.html();
		
		var log += "START "+dst.get(0).tagName+"\n";
		for( var i = 0; i < curpath.length; i++ ){
			if( dst.get(0) instanceof Text )
				break;
			log += "GO TO "+dst.get(0).tagName+"["+curpath[i]+"]\n";
			var cont = dst.contentsNoCursors();
			dst = cont.eq(curpath[i]);
			log += " -> "+dst.get(0)+"\n";
		}
		var idx = curpath[curpath.length-1];
		
		var cursorText = "<div id=\"cursor-dummy\" class=\"cursor\"></div>";
		
		var handled = false;
		if( dst.get(0).tagName == "BR" ){
			var siblings = dst.parent().contentsNoCursors();
			var pred = siblings.eq(siblings.index(dst)-1);
			if( pred.get(0) instanceof Text ){
				dst = dst.before();
			} else {
				log += "PRED: "+pred.get(0) + "\n";
				dst.before(cursorText);
				handled = true;
			}
		}
		if( !handled ){
			if( dst.get(0) instanceof Text ){
				var oldText = dst.text().trim();
				var newText = oldText.substr(0, idx) + cursorText + oldText.substr(idx);
				dst.replaceWith(newText);
			} else {
{'fraction_non_alphanumeric': 0.192458758837392, 'fraction_numerical': 0.006284367635506678, 'mean_word_length': 3.7542942494398805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3481585', 'n_tokens_mistral': 2550, 'n_tokens_neox': 2303, 'n_words': 489}