<commit_before>/**
 * Functions for syntax coloring.
 */
 function isSyntaxColoring() {
  var prefs = Chickenfoot.getPrefBranch();
  try {
    return prefs.getBoolPref("syntaxColor");
  } catch (e) {
    var defaultValue = false;
    prefs.setBoolPref("syntaxColor", defaultValue);
    return defaultValue;
  }
}

 
 /**
 * If syntaxColor is on and the event is not a ctrl, or ctrl+m, look at the anchor
 * and focus to see if it is a case of regular syntax coloring or just autoIndent
 * 
 *  returns void
 */
 function syntaxColorEvent(event) {
	 if(Chickenfoot.getPrefBranch().getBoolPref("syntaxColor")){
	 	if (event.keyCode !=16 && event.keyCode !=17) {//don't activate on shift key
	 		//debug(event.keyCode);
	 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
	    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
	    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
	    	var focusNodeCaret = sbwin.getSelectedBuffer().api.selection.focusNode;
	    	var focusOffsetCaret = sbwin.getSelectedBuffer().api.selection.focusOffset;
	    	
	    	if(anchorNodeCaret==focusNodeCaret &&anchorOffsetCaret==focusOffsetCaret){
	    		regularSyntaxColoringAndAutoIndent(event);
	    	}else{
	    		selectionAutoIndent(event);
	    	}
	    	
	 	}
	}
}

/**
 * Syntax-color text after every keystroke.  First get the cursor position,
 * then count how many characters and carrige returns in the cursor was,
 * then take out all spans then recolor the text, then replace the caret 
 * by counting characters again. This is followed by autoIndent if the event was 
 * a return, }, or control mask + m, which has some furthur caret placement
 *
 *  returns void
 */
function regularSyntaxColoringAndAutoIndent(event){

 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
    	var ed = sbwin.getSelectedBuffer().editor;
    	var doc = ed.contentDocument;
    	var pre = doc.getElementById("pre");   
    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
    	/*
    	debug('PRE - BEGINNING !!!!!');
    	debug(pre.childNodes);
    	debug(pre.innerHTML);
    	debug("anchorNodeCaret");
    	debug(anchorNodeCaret);
    	debug("anchorOffsetCaret");
    	debug(anchorOffsetCaret);
    	*/
    	
    	//count characters
    	var chars=0;
    	/*
    	if (event.keyCode ==13 & anchorNodeCaret.nodeName=="SPAN") {
    		debug("charCount SPAN BR");
    		chars=charCountSpanBr(anchorNodeCaret,pre.childNodes);
    	}else{
    		chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	}*/
    	chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	
     	//eliminate spans, specifically:
     	//<span style="color: blue;">
     	//<span style="color: purple;">
		//<span style="color: green;">
		//</span>
		
		var spanRegEx = /(<span style="color: purple;">|<span style="color: blue;">|<span style="color: green;">|<\/span>)/gm;
    	
    	pre.innerHTML=pre.innerHTML.replace(spanRegEx,"");
    	//recolor
    	syntaxColor(pre, CF_JAVASCRIPT_RULES); 
    	
		//replace caret
		caretPlacement(chars,pre,sbwin);
		
		//autoIndent : only on return, } , and ctrl+tab
    	var pre2 = doc.getElementById("pre"); 
    	//debug('event.keyCode');
    	//debug(event.keyCode);
    	//if(false){
    	if(event.keyCode==13||event.keyCode==221||(event.ctrlKey && event.keyCode==9)){
    		var lines = makeLines(pre2);
    		//debug('lines');
    		//debug(lines);
    		for (i in lines){
    		//debug(lines[i]);
    		}
    		var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    		var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
   			var lineNum=findNodeInLines(lines,anchorNodeCaret2,anchorOffsetCaret2);
   			//debug('lineNum= '+lineNum);
			//this is how many spaces we need to indent our line by
			
			if (lines[lineNum][0].parentNode.nodeName=="SPAN"){
				//do nothing
				//debug('inside a span node, dont change indent');
			}else{
				var indents=calculateIndents(lines,lineNum);
				
				//debug('INDENTS: '+indents)
				/*
				debug("event.keycode");
				debug(lines[lineNum]);
				debug(doc);
				debug(pre);
				debug(sbwin);
				debug(anchorNodeCaret2);
				debug(anchorOffsetCaret2);
				debug("event.keycode");
				*/
				//debug("changeIndent");
				changeIndent(lines[lineNum],indents,doc,pre,sbwin,anchorNodeCaret2,anchorOffsetCaret2);
			}
		}
		/*
		var pre2 = doc.getElementById("pre");  
    	var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    	var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
		
		debug('pre');
    	debug(pre.childNodes);
    	debug(anchorNodeCaret2.data+"+end");
    	debug(anchorNodeCaret2);
    	debug(anchorOffsetCaret2);
    	//debug(pre.innerHTML)
    	*/
    	//debug("FINISHED");
    	
 	
}

function selectionAutoIndent(event){

 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
    	var ed = sbwin.getSelectedBuffer().editor;
    	var doc = ed.contentDocument;
    	var pre = doc.getElementById("pre");   
    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
    	/*
    	debug('PRE - BEGINNING !!!!!');
    	debug(pre.childNodes);
    	debug(pre.innerHTML);
    	debug("anchorNodeCaret");
    	debug(anchorNodeCaret);
    	debug("anchorOffsetCaret");
    	debug(anchorOffsetCaret);
    	*/
    	
    	//count characters
    	var chars=0;
    	//debug(event.keyCode);
    	//debug(anchorNodeCaret.parentNode.nodeName);
    	//if (event.keyCode ==13 & anchorNodeCaret.nodeName=="SPAN") {
    		//debug("charCount SPAN BR");
    		//chars=charCountSpanBr(anchorNodeCaret,pre.childNodes);
    		//chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	//}else{
    		chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	//}
    	
    	/*
    	debug("charCount");
    	debug(chars);
    	debug("pre.innerHTML");
    	debug(pre.innerHTML);
    	*/
    	
    	
     	//eliminate spans, specifically:
     	//<span style="color: blue;">
     	//<span style="color: purple;">
		//<span style="color: green;">
		//</span>
		
		var spanRegEx = /(<span style="color: purple;">|<span style="color: blue;">|<span style="color: green;">|<\/span>)/gm;
    	//debug("replace innerHTML");
    	//debug(pre.innerHTML.replace(spanRegEx,""));
    	pre.innerHTML=pre.innerHTML.replace(spanRegEx,"");
    	//recolor
    	syntaxColor(pre, CF_JAVASCRIPT_RULES); 
    	
    	
		//replace caret
		caretPlacement(chars,pre,sbwin);
		
		//autoIndent : only on return, } , and alt
    	var pre2 = doc.getElementById("pre"); 
    	
    	if(event.keyCode==13||event.keyCode==221||(event.ctrlKey && event.keyCode==9)){
    		var lines = makeLines(pre2);
    		//debug('lines');
    		//debug(lines);
    		var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    		var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
   			var lineNum=findNodeInLines(lines,anchorNodeCaret2,anchorOffsetCaret2);
   			//debug('lineNum= '+lineNum);
			//this is how many spaces we need to indent our line by
			var indents=calculateIndents(lines,lineNum);
			changeIndent(lines[lineNum],indents,doc,pre,sbwin,anchorNodeCaret2,anchorOffsetCaret2);
		}
		
		
		
		
		/*
		var pre2 = doc.getElementById("pre");  
    	var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    	var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
		
		debug('pre');
    	debug(pre.childNodes);
    	debug(anchorNodeCaret2.data+"+end");
    	debug(anchorNodeCaret2);
    	debug(anchorOffsetCaret2);
    	//debug(pre.innerHTML)
    	*/
    	debug("FINISHED");
    	
 	
}
/*
*Creates a count of the number of text characters and carridge returns 
*at before and before the given n, offset. 
*
*/
function charCount(n,offset,nodes){
	//debug("counting chars function");
	//debug(n);
	//debug(offset);
	var node;
	var off;
	if(n.nodeName=="PRE"){
		//debug("PRE");
		node=n.childNodes[offset];
		off=0;
	}else{
		node=n;
		off=offset;
	}
	//debug(node);
	//debug(offset);
	var count=0;
	var currNodeNum=0;<commit_msg>Fixed arrow key bug.  Syntaxcoloring/autoIndent no longer happen on arrow keys (and the debug "FINISHED") is removed.  <commit_after>/**
 * Functions for syntax coloring.
 */
 function isSyntaxColoring() {
  var prefs = Chickenfoot.getPrefBranch();
  try {
    return prefs.getBoolPref("syntaxColor");
  } catch (e) {
    var defaultValue = false;
    prefs.setBoolPref("syntaxColor", defaultValue);
    return defaultValue;
  }
}

 
 /**
 * If syntaxColor is on and the event is not a ctrl, or ctrl+m, look at the anchor
 * and focus to see if it is a case of regular syntax coloring or just autoIndent
 * 
 *  returns void
 */
 function syntaxColorEvent(event) {
	 if(Chickenfoot.getPrefBranch().getBoolPref("syntaxColor")){
	 	if (event.keyCode !=16 && event.keyCode !=17 && event.keyCode !=34 && event.keyCode !=37 && event.keyCode !=38 && event.keyCode !=34) {//don't activate on shift, control, and the 4 arrow keys
	 		//debug(event.keyCode);
	 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
	    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
	    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
	    	var focusNodeCaret = sbwin.getSelectedBuffer().api.selection.focusNode;
	    	var focusOffsetCaret = sbwin.getSelectedBuffer().api.selection.focusOffset;
	    	
	    	if(anchorNodeCaret==focusNodeCaret &&anchorOffsetCaret==focusOffsetCaret){
	    		regularSyntaxColoringAndAutoIndent(event);
	    	}else{
	    		selectionAutoIndent(event);
	    	}
	    	
	 	}
	}
}

/**
 * Syntax-color text after every keystroke.  First get the cursor position,
 * then count how many characters and carrige returns in the cursor was,
 * then take out all spans then recolor the text, then replace the caret 
 * by counting characters again. This is followed by autoIndent if the event was 
 * a return, }, or control mask + m, which has some furthur caret placement
 *
 *  returns void
 */
function regularSyntaxColoringAndAutoIndent(event){

 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
    	var ed = sbwin.getSelectedBuffer().editor;
    	var doc = ed.contentDocument;
    	var pre = doc.getElementById("pre");   
    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
      	
    	//count characters
    	var chars=0;
    	/*
    	if (event.keyCode ==13 & anchorNodeCaret.nodeName=="SPAN") {
    		debug("charCount SPAN BR");
    		chars=charCountSpanBr(anchorNodeCaret,pre.childNodes);
    	}else{
    		chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	}*/
    	chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	
     	//eliminate spans, specifically:
     	//<span style="color: blue;">
     	//<span style="color: purple;">
		//<span style="color: green;">
		//</span>
		
		var spanRegEx = /(<span style="color: purple;">|<span style="color: blue;">|<span style="color: green;">|<\/span>)/gm;
    	
    	pre.innerHTML=pre.innerHTML.replace(spanRegEx,"");
    	//recolor
    	syntaxColor(pre, CF_JAVASCRIPT_RULES); 
    	
		//replace caret
		caretPlacement(chars,pre,sbwin);
		
		//autoIndent : only on return, } , and ctrl+tab
    	var pre2 = doc.getElementById("pre"); 
    	//debug('event.keyCode');
    	//debug(event.keyCode);
    	//if(false){
    	if(event.keyCode==13||event.keyCode==221||(event.ctrlKey && event.keyCode==9)){
    		var lines = makeLines(pre2);
    		//debug('lines');
    		//debug(lines);
    		for (i in lines){
    		//debug(lines[i]);
    		}
    		var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    		var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
   			var lineNum=findNodeInLines(lines,anchorNodeCaret2,anchorOffsetCaret2);
   			//debug('lineNum= '+lineNum);
			//this is how many spaces we need to indent our line by
			
			if (lines[lineNum][0].parentNode.nodeName=="SPAN"){
				//do nothing
				//debug('inside a span node, dont change indent');
			}else{
				var indents=calculateIndents(lines,lineNum);
				
				//debug('INDENTS: '+indents)
				/*
				debug("event.keycode");
				debug(lines[lineNum]);
				debug(doc);
				debug(pre);
				debug(sbwin);
				debug(anchorNodeCaret2);
				debug(anchorOffsetCaret2);
				debug("event.keycode");
				*/
				//debug("changeIndent");
				changeIndent(lines[lineNum],indents,doc,pre,sbwin,anchorNodeCaret2,anchorOffsetCaret2);
			}
		}
		/*
		var pre2 = doc.getElementById("pre");  
    	var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    	var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
		
		debug('pre');
    	debug(pre.childNodes);
    	debug(anchorNodeCaret2.data+"+end");
    	debug(anchorNodeCaret2);
    	debug(anchorOffsetCaret2);
    	//debug(pre.innerHTML)
    	*/
    	//debug("FINISHED");
    	
 	
}

function selectionAutoIndent(event){

 		var sbwin = Chickenfoot.getSidebarWindow(chromeWindow);
    	var ed = sbwin.getSelectedBuffer().editor;
    	var doc = ed.contentDocument;
    	var pre = doc.getElementById("pre");   
    	var anchorNodeCaret = sbwin.getSelectedBuffer().api.selection.anchorNode;
    	var anchorOffsetCaret = sbwin.getSelectedBuffer().api.selection.anchorOffset;
     	
    	//count characters
    	var chars=0;
    	//debug(event.keyCode);
    	//debug(anchorNodeCaret.parentNode.nodeName);
    	//if (event.keyCode ==13 & anchorNodeCaret.nodeName=="SPAN") {
    		//debug("charCount SPAN BR");
    		//chars=charCountSpanBr(anchorNodeCaret,pre.childNodes);
    		//chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	//}else{
    		chars=charCount(anchorNodeCaret,anchorOffsetCaret,pre.childNodes);
    	//}
    	
    	/*
    	debug("charCount");
    	debug(chars);
    	debug("pre.innerHTML");
    	debug(pre.innerHTML);
    	*/
    	
    	
     	//eliminate spans, specifically:
     	//<span style="color: blue;">
     	//<span style="color: purple;">
		//<span style="color: green;">
		//</span>
		
		var spanRegEx = /(<span style="color: purple;">|<span style="color: blue;">|<span style="color: green;">|<\/span>)/gm;
    	//debug("replace innerHTML");
    	//debug(pre.innerHTML.replace(spanRegEx,""));
    	pre.innerHTML=pre.innerHTML.replace(spanRegEx,"");
    	//recolor
    	syntaxColor(pre, CF_JAVASCRIPT_RULES); 
    	
    	
		//replace caret
		caretPlacement(chars,pre,sbwin);
		
		//autoIndent : only on return, } , and alt
    	var pre2 = doc.getElementById("pre"); 
    	
    	if(event.keyCode==13||event.keyCode==221||(event.ctrlKey && event.keyCode==9)){
    		var lines = makeLines(pre2);
    		//debug('lines');
    		//debug(lines);
    		var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    		var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
   			var lineNum=findNodeInLines(lines,anchorNodeCaret2,anchorOffsetCaret2);
   			//debug('lineNum= '+lineNum);
			//this is how many spaces we need to indent our line by
			var indents=calculateIndents(lines,lineNum);
			changeIndent(lines[lineNum],indents,doc,pre,sbwin,anchorNodeCaret2,anchorOffsetCaret2);
		}
		
		
		
		
		/*
		var pre2 = doc.getElementById("pre");  
    	var anchorNodeCaret2 = sbwin.getSelectedBuffer().api.selection.focusNode;
    	var anchorOffsetCaret2 = sbwin.getSelectedBuffer().api.selection.focusOffset;
		
		debug('pre');
    	debug(pre.childNodes);
    	debug(anchorNodeCaret2.data+"+end");
    	debug(anchorNodeCaret2);
    	debug(anchorOffsetCaret2);
    	//debug(pre.innerHTML)
    	*/
    	//debug("FINISHED");
    	
 	
}
/*
*Creates a count of the number of text characters and carridge returns 
*at before and before the given n, offset. 
*
*/
function charCount(n,offset,nodes){
	//debug("counting chars function");
	//debug(n);
	//debug(offset);
	var node;
	var off;
	if(n.nodeName=="PRE"){
		//debug("PRE");
		node=n.childNodes[offset];
		off=0;
	}else{
		node=n;
		off=offset;
	}
	//debug(node);
	//debug(offset);
	var count=0;
	var currNodeNum=0;
{'fraction_non_alphanumeric': 0.14043303734338888, 'fraction_numerical': 0.007359202043547014, 'mean_word_length': 3.402409638554217, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '2604332', 'n_tokens_mistral': 5898, 'n_tokens_neox': 5443, 'n_words': 1003}