<commit_before>package eg.document;

import javax.swing.JTextPane;

import javax.swing.text.StyledDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;

//--Eadgyth--//
import eg.Preferences;

/**
 * The auto indentation.
 * <p>
 * The indention of a previous line is added to a new line, is increased
 * upon typing an opening curly brackets and reduced upon typing a closing
 * curly bracked. <p>
 * The indentation method is so far very simple
 */
class AutoIndent {
   
   private final static Preferences prefs = new Preferences();

   private String indentUnit;
   private int indentLength;  
   private String indent = "";

   private JTextPane textArea;
   private StyledDocument doc;
   private SimpleAttributeSet normalSet;

   AutoIndent(JTextPane textArea, StyledDocument doc,
         SimpleAttributeSet normalSet)
   {
      this.textArea = textArea;
      this.doc = doc;
      this.normalSet = normalSet;
      
      prefs.readPrefs();
      indentUnit = prefs.prop.getProperty("indentUnit");
      indentLength = indentUnit.length();
      
      textArea.addKeyListener(listener);
   }
   
   String getIndentUnit() {
      return indentUnit;
   }
   
   /**
    * Assigns to this the indentation unit and the indentation
    * length. Saves the indentation unit to preferences.
    */
   void changeIndentUnit(String indentUnit) {
      this.indentUnit = indentUnit;
      indentLength = indentUnit.length();    
      prefs.storePrefs("indentUnit", indentUnit);
   }  

   /**
    * Assigns to this indent the indentation unit at the current line
    * and adds another unit if the symbol before the current position 
    * is and open bracket.
    */
   void openBracketIndent(String in, int pos) {
      String indent = currentIndent(in, pos);
      String atPrevPos = in.substring(pos - 1, pos);      
      if (atPrevPos.equals("{")) {
         indent += indentUnit;
      } 
      this.indent = indent;
   }

   /**
    * Reduces indentation by one indentation unit if close bracket was
    * typed and at least one indent unit is detected before the bracket 
    */
   void closeBracketIndent(String in, int pos) {
      int lastReturn = 0;

      if (pos > 0) {
         String atPos = in.substring(pos, pos + 1);
         if ("}".equals(atPos)) {
            if (in.substring(pos - indentLength, pos).equals(indentUnit)) {   
               removeIndent(pos - indentLength, indentLength);
            }   
         }
      }
   }
   
   void resetIndent() {
      indent = "";
   }

   /*
    * Returns the indentation at the current line
    */
   private String currentIndent(String in, int pos) {
      String currentIndent = "";<commit_msg>members final<commit_after>package eg.document;

import javax.swing.JTextPane;

import javax.swing.text.StyledDocument;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;

//--Eadgyth--//
import eg.Preferences;

/**
 * The auto indentation.
 * <p>
 * The indention of a previous line is added to a new line, is increased
 * upon typing an opening curly brackets and reduced upon typing a closing
 * curly bracked. <p>
 * The indentation is so far very simple
 */
class AutoIndent {
   
   private final static Preferences PREFS = new Preferences();
   private final JTextPane textArea;
   private final StyledDocument doc;
   private final SimpleAttributeSet normalSet;

   private String indentUnit;
   private int indentLength;  
   private String indent = "";

   AutoIndent(JTextPane textArea, StyledDocument doc,
         SimpleAttributeSet normalSet)
   {
      this.textArea = textArea;
      this.doc = doc;
      this.normalSet = normalSet;
      
      PREFS.readPrefs();
      indentUnit = PREFS.prop.getProperty("indentUnit");
      indentLength = indentUnit.length();
      
      textArea.addKeyListener(listener);
   }
   
   String getIndentUnit() {
      return indentUnit;
   }
   
   void resetIndent() {
      indent = "";
   }
   
   /**
    * Assigns to this the indentation unit and the indentation
    * length. Saves the indentation unit to preferences.
    */
   void changeIndentUnit(String indentUnit) {
      this.indentUnit = indentUnit;
      indentLength = indentUnit.length();    
      PREFS.storePrefs("indentUnit", indentUnit);
   }  

   /**
    * Assigns to this indent the indentation unit at the current line
    * and adds another unit if the symbol before the current position 
    * is and open bracket.
    */
   void openBracketIndent(String in, int pos) {
      String indent = currentIndent(in, pos);
      String atPrevPos = in.substring(pos - 1, pos);      
      if (atPrevPos.equals("{")) {
         indent += indentUnit;
      } 
      this.indent = indent;
   }

   /**
    * Reduces indentation by one indentation unit if close bracket was
    * typed and at least one indent unit is detected before the bracket 
    */
   void closeBracketIndent(String in, int pos) {
      int lastReturn = 0;

      if (pos > 0) {
         String atPos = in.substring(pos, pos + 1);
         if ("}".equals(atPos)) {
            if (in.substring(pos - indentLength, pos).equals(indentUnit)) {   
               removeIndent(pos - indentLength, indentLength);
            }   
         }
      }
   }

   /*
    * Returns the indentation at the current line
    */
   private String currentIndent(String in, int pos) {
      String currentIndent = "";
{'fraction_non_alphanumeric': 0.09164897381457891, 'fraction_numerical': 0.0014154281670205238, 'mean_word_length': 2.589206349206349, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4646056', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1607, 'n_words': 493}