<commit_before>/* 
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
window.annis_gui_components_codemirror_AqlCodeEditor = function() {
  
    var connector = this;
    var rootDiv = this.getElement(this.getConnectorId());
    
    var changeDelayTimerID = null;
    
    var changeDelayTime = 500;
    
    var lastServerRequestCounter = 0;
    
    var errorList = [];

    CodeMirror.registerHelper("lint", "aql", function(text) {
      return errorList;
    });
    
    var cmTextArea = CodeMirror(rootDiv,
    {
      mode: {
        name : 'aql',
        nodeMappings : {}
      }, 
      lineNumbers: false,
      lineWrapping: true,
      matchBrackets: true,
      gutters: ["CodeMirror-lint-markers"],
      lint: true,
      placeholder: "",
      specialChars: /[\t\u0000-\u0019\u00ad\u200b\u200c\200d\u200f\u2028\u2029\ufeff]/g,
      inputStyle: 'textarea',
      rtlMoveVisually : true
    });
        
    this.sendTextIfNecessary = function() 
    {
      //var current = cmTextArea.getValue();
      var current = cmTextArea.getValue().replace(/\u200e/g, "");
            
      if(changeDelayTimerID)
      {
        window.clearTimeout(changeDelayTimerID);
      }
      
      if(connector.getState().text !== current)
      {
        var cursor = cmTextArea.getCursor();
        // calculate the absolute cursor position
        var absPos = 0;
        for(var i=0; i < cursor.line; i++)
        {
          absPos += cmTextArea.getLine(i).length;
          absPos++; // add one for the newline
        }
        absPos += cursor.ch;
        
        connector.textChanged(current, absPos);
      }
    };
    
       
    
    this.onStateChange = function() 
    {    
     
    	//TODO mark LRM and " as atomic (see CodeMirror mark method)
      // test
         
      var current = cmTextArea.getValue();
      var currentCursor = cmTextArea.getCursor();      
       
      var cursorLine  = currentCursor.line;
      var cursorPos = currentCursor.ch;    

      var lastChar = cmTextArea.getRange({line: cursorLine, ch: cursorPos - 2}, {line: cursorLine, ch: cursorPos - 1});
      var currentChar = cmTextArea.getRange({line: cursorLine, ch: cursorPos - 1}, {line: cursorLine, ch: cursorPos});


      
      if (currentChar == "\"" & lastChar != '\u200E')
      {
       //var modifiedText = current.substring(0, current.length - 1).concat('\u200E', "\"");
      // var modifiedText = cmTextArea.getValue().replace(/([^\u200E])\"/gm, '$1'.concat("\u200E\""));
       // add the LRM control character before \" 
       //cmTextArea.setValue(modifiedText);
       
       var replacement = '\u200E';
       cmTextArea.replaceRange(replacement, {line: cursorLine, ch: cursorPos - 1});      
       cmTextArea.markText({line: cursorLine, ch: cursorPos - 2}, {line: cursorLine, ch: cursorPos + 0}, {atomic: true});
       cmTextArea.setCursor({line: cursorLine, ch: cursorPos + 1});  
    
   // test ende
      } 

    
     
       var cursor = cmTextArea.getCursor();
      
      var newMode = {
        name: 'aql',
        nodeMappings : connector.getState().nodeMappings
      };
      
      cmTextArea.setOption('mode', newMode);
      cmTextArea.setOption("placeholder", connector.getState().inputPrompt);
      
      // set the text from the server defined state if a new request was made
      if(lastServerRequestCounter < connector.getState().serverRequestCounter)
      {
        lastServerRequestCounter = connector.getState().serverRequestCounter;
        cmTextArea.setValue(connector.getState().text);

        // restore the cursor position
        cmTextArea.setCursor(cursor);
      }
      
      // apply parent code class
      if(connector.getState().textareaClass && connector.getState().textareaClass !== "") {
        var c = connector.getState().textareaClass;
        if(!$(cmTextArea.getWrapperElement()).find("pre").hasClass(c)) {
          $(cmTextArea.getWrapperElement()).find("pre").addClass(c);<commit_msg>changed LRM replacement routine<commit_after>/* 
 * Copyright 2013 Corpuslinguistic working group Humboldt University Berlin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
window.annis_gui_components_codemirror_AqlCodeEditor = function() {
  
    var connector = this;
    var rootDiv = this.getElement(this.getConnectorId());
    
    var changeDelayTimerID = null;
    
    var changeDelayTime = 500;
    
    var lastServerRequestCounter = 0;
    
    var errorList = [];

    CodeMirror.registerHelper("lint", "aql", function(text) {
      return errorList;
    });
    
    var cmTextArea = CodeMirror(rootDiv,
    {
      mode: {
        name : 'aql',
        nodeMappings : {}
      }, 
      lineNumbers: false,
      lineWrapping: true,
      matchBrackets: true,
      gutters: ["CodeMirror-lint-markers"],
      lint: true,
      placeholder: "",
      specialChars: /[\t\u0000-\u0019\u00ad\u200b\u200c\200d\u200f\u2028\u2029\ufeff]/g,
      inputStyle: 'textarea',
      rtlMoveVisually : true
    });
        
    this.sendTextIfNecessary = function() 
    {
      //var current = cmTextArea.getValue();
      var current = cmTextArea.getValue().replace(/\u200e/g, "");
            
      if(changeDelayTimerID)
      {
        window.clearTimeout(changeDelayTimerID);
      }
      
      if(connector.getState().text !== current)
      {
        var cursor = cmTextArea.getCursor();
        // calculate the absolute cursor position
        var absPos = 0;
        for(var i=0; i < cursor.line; i++)
        {
          absPos += cmTextArea.getLine(i).length;
          absPos++; // add one for the newline
        }
        absPos += cursor.ch;
        
        connector.textChanged(current, absPos);
      }
    };
    
       
    
    this.onStateChange = function() 
    {    
     
    	//TODO mark LRM and " as atomic (see CodeMirror mark method)
      // test         
     /* var current = cmTextArea.getValue();
      var currentCursor = cmTextArea.getCursor();      
       
      var cursorLine  = currentCursor.line;
      var cursorPos = currentCursor.ch;    

      var lastChar = cmTextArea.getRange({line: cursorLine, ch: cursorPos - 2}, {line: cursorLine, ch: cursorPos - 1});
      var currentChar = cmTextArea.getRange({line: cursorLine, ch: cursorPos - 1}, {line: cursorLine, ch: cursorPos});
           
      if (currentChar == "\"" & lastChar != '\u200E')
      {       
       // add the LRM control character before \"    
       var replacement = '\u200E';
       cmTextArea.replaceRange(replacement, {line: cursorLine, ch: cursorPos - 1});           
       cmTextArea.markText({line: cursorLine, ch: cursorPos - 1}, {line: cursorLine, ch: cursorPos + 1}, {atomic: true});
       //cmTextArea.setCursor({line: cursorLine, ch: cursorPos + 1});    
   
      } // test ende*/
      
           
    
            
	  var lineCount = cmTextArea.lineCount();
	  var j = 0;
	  var pos = 0;
	  
	  for (var i = 0; i < lineCount; i++) 
	  {
		  var lineValue = cmTextArea.getLine(i);
		  //find all occurrences of \"
		  while ((pos = lineValue.indexOf("\"", j)) != -1)
		  {
			  j = pos + 1;
			  if (lineValue.charAt(pos - 1) == undefined || lineValue.charAt(pos - 1) != '\u200e'){				
		    	 var replacement = '\u200E';
		         cmTextArea.replaceRange(replacement, {line: i, ch: pos});   
		         cmTextArea.markText({line: i, ch: pos}, {line: i, ch: (pos + 2)}, {atomic: true});
		         j += 1;
			 }
			  else
			  {
				  cmTextArea.markText({line: i, ch: (pos - 1)}, {line: i, ch: (pos + 1)}, {atomic: true});  
			  }
			
	      }
	        
		  
	  }
    	
    
     
      var cursor = cmTextArea.getCursor();
      
      var newMode = {
        name: 'aql',
        nodeMappings : connector.getState().nodeMappings
      };
      
      cmTextArea.setOption('mode', newMode);
      cmTextArea.setOption("placeholder", connector.getState().inputPrompt);
      
      // set the text from the server defined state if a new request was made
      if(lastServerRequestCounter < connector.getState().serverRequestCounter)
      {
        lastServerRequestCounter = connector.getState().serverRequestCounter;
        cmTextArea.setValue(connector.getState().text);

        // restore the cursor position
        cmTextArea.setCursor(cursor);
      }
      
      // apply parent code class
      if(connector.getState().textareaClass && connector.getState().textareaClass !== "") {
        var c = connector.getState().textareaClass;
        if(!$(cmTextArea.getWrapperElement()).find("pre").hasClass(c)) {
          $(cmTextArea.getWrapperElement()).find("pre").addClass(c);
{'fraction_non_alphanumeric': 0.12216728471363823, 'fraction_numerical': 0.01534816646065101, 'mean_word_length': 2.2978940217391304, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '19627', 'n_tokens_mistral': 3031, 'n_tokens_neox': 2814, 'n_words': 702}