<commit_before>package eg.document;

import java.awt.EventQueue;

import javax.swing.JTextPane;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

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

import eg.utils.FileUtils;
import eg.ui.EditArea;

/**
 * Class represents the text document which a file and language
 * can be assigned to
 */
public final class TextDocument {

   private final static String LINE_SEP = System.lineSeparator();
   private final static Preferences PREFS = new Preferences();

   private final JTextPane textArea;
   private final EditArea editArea;
   private final TypingEdit type;

   private File docFile = null;
   private String filename = "";
   private String filepath = "";
   private String dir = "";
   private String content = "";
   boolean isPlainText = false;

   /**
    * Creates a TextDocument
    * @param editArea  the reference to the {@link EditArea}
    */
  public TextDocument(EditArea editArea) {
      this.editArea = editArea;
      this.textArea = editArea.textArea();
      type = new TypingEdit(editArea);
      type.addAllLineNumbers(content);
      PREFS.readPrefs();
      String indentUnit = PREFS.getProperty("indentUnit");
      changeIndentUnit(indentUnit);    
   }

   /**
    * Creates a TextDocument with a specified language
    *
    * @param editArea  the reference to an {@link EditArea}
    * @param lang  the language that is one of the constants in
    * {@link Languages}
    */
   public TextDocument(EditArea editArea, Languages lang) {
      this(editArea);
      type.setUpEditing(lang);
      isPlainText = Languages.PLAIN_TEXT == lang;
   }

   /**
    * Returns this text area
    *
    * @return  this text area which is of type {@link EditArea}
    */
    public JTextPane getTextArea() {
       return textArea;
    }

   /**
    * Returns the name of this file
    *
    * @return  the name of this file
    */
   public String filename() {
      return filename;
   }

   /**
    * Returns the path of this file
    *
    * @return  the full path of this file
    */
   public String filepath() {
      return filepath;
   }

   /**
    * Returns the parent directory of this file
    *
    * @return  the parent directory of this file
    */
   public String dir() {
      return dir;
   }
   
   /**
    * If the set Language is a coding language, i.e. not plain text
    *
    * @return  the set Language is any coding language, i.e. not plain text
    */
   public boolean isCodingLanguage() {
      return !isPlainText;
   }

   /**
    * Sets the specified file and displays the file content
    *
    * @param file  the file whose content is displayed in this text area
    */
   public void openFile(File file) {
      if (this.filepath().length() != 0) {
         throw new IllegalStateException(
               "Illegal attempt to assign a file to a "
               + " TextDocument which a file was assigned to before");
      }
      assignFileStrings(file);
      EventQueue.invokeLater(() -> {
         displayFileContent();
         setLanguageBySuffix();
         setContent();
         type.addAllLineNumbers(content);
      });
   }

   /**
    * Saves the current text content to this file
    *
    * @return  if the content was saved
    */
   public boolean saveToFile() {
      return writeToFile(docFile);
   }

   /**
    * Sets the specified file and saves the current text content
    *
    * @param file  the new file
    * @return  if the content was saved
    */
   public boolean saveFileAs(File file) {
      assignFileStrings(file);
      setLanguageBySuffix();
      return writeToFile(file);
   }
   
   /**
    * Saves the current content to the specified file but does not
    * assign the file to this
    *
    * @param file  the file which the current content is saved to
    */
   public void saveCopy(File file) {
      writeToFile(file);
   }

   /**
    * Returns if the text content equals the content since the last
    * saving point
    *
    * @return  if the text content is saved
    */
   public boolean isContentSaved() {
      return content.equals(editArea.getDocText());
   }

   /**
    * Changes the indentation unit
    *
    * @param indentUnit  the String that consists of a certain number of
    * white spaces
    */
   public void changeIndentUnit(String indentUnit) {
      if (indentUnit == null || !indentUnit.matches("[\\s]+")) {
         throw new IllegalArgumentException("Argument indentUnit is"
               + " incorrect");
      }
      type.changeIndentUnit(indentUnit);
      PREFS.storePrefs("indentUnit", type.getIndentUnit());
   }

   /**
    * Returns the current indentation unit<commit_msg>remove superficial invokeLater in openFile() methof<commit_after>package eg.document;

import java.awt.EventQueue;

import javax.swing.JTextPane;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

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

import eg.utils.FileUtils;
import eg.ui.EditArea;

/**
 * Class represents the text document which a file and language
 * can be assigned to
 */
public final class TextDocument {

   private final static String LINE_SEP = System.lineSeparator();
   private final static Preferences PREFS = new Preferences();

   private final JTextPane textArea;
   private final EditArea editArea;
   private final TypingEdit type;

   private File docFile = null;
   private String filename = "";
   private String filepath = "";
   private String dir = "";
   private String content = "";
   boolean isPlainText = false;

   /**
    * Creates a TextDocument
    * @param editArea  the reference to the {@link EditArea}
    */
  public TextDocument(EditArea editArea) {
      this.editArea = editArea;
      this.textArea = editArea.textArea();
      type = new TypingEdit(editArea);
      type.addAllLineNumbers(content);
      PREFS.readPrefs();
      String indentUnit = PREFS.getProperty("indentUnit");
      setIndentUnit(indentUnit);    
   }

   /**
    * Creates a TextDocument with a specified language
    *
    * @param editArea  the reference to an {@link EditArea}
    * @param lang  the language that is one of the constants in
    * {@link Languages}
    */
   public TextDocument(EditArea editArea, Languages lang) {
      this(editArea);
      type.setUpEditing(lang);
      isPlainText = Languages.PLAIN_TEXT == lang;
   }

   /**
    * Returns this text area
    *
    * @return  this text area which is of type {@link EditArea}
    */
    public JTextPane getTextArea() {
       return textArea;
    }

   /**
    * Returns the name of this file
    *
    * @return  the name of this file
    */
   public String filename() {
      return filename;
   }

   /**
    * Returns the path of this file
    *
    * @return  the full path of this file
    */
   public String filepath() {
      return filepath;
   }

   /**
    * Returns the parent directory of this file
    *
    * @return  the parent directory of this file
    */
   public String dir() {
      return dir;
   }
   
   /**
    * If the set Language is a coding language, i.e. not plain text
    *
    * @return  the set Language is any coding language, i.e. not plain text
    */
   public boolean isCodingLanguage() {
      return !isPlainText;
   }

   /**
    * Sets the specified file and displays the file content
    *
    * @param file  the file whose content is displayed in this text area
    */
   public void openFile(File file) {
      if (this.filepath().length() != 0) {
         throw new IllegalStateException(
               "Illegal attempt to assign a file to a "
               + " TextDocument which a file was assigned to before");
      }
      assignFileStrings(file);
      displayFileContent();
      setLanguageBySuffix();
      setContent();
      type.addAllLineNumbers(content);
   }

   /**
    * Saves the current text content to this file
    *
    * @return  if the content was saved
    */
   public boolean saveToFile() {
      return writeToFile(docFile);
   }

   /**
    * Sets the specified file and saves the current text content
    *
    * @param file  the new file
    * @return  if the content was saved
    */
   public boolean saveFileAs(File file) {
      assignFileStrings(file);
      setLanguageBySuffix();
      return writeToFile(file);
   }
   
   /**
    * Saves the current content to the specified file but does not
    * assign the file to this
    *
    * @param file  the file which the current content is saved to
    */
   public void saveCopy(File file) {
      writeToFile(file);
   }

   /**
    * Returns if the text content equals the content since the last
    * saving point
    *
    * @return  if the text content is saved
    */
   public boolean isContentSaved() {
      return content.equals(editArea.getDocText());
   }

   /**
    * Sets the indentation unit
    *
    * @param indentUnit  the String that consists of a certain number of
    * white spaces
    */
   public void setIndentUnit(String indentUnit) {
      if (indentUnit == null || !indentUnit.matches("[\\s]+")) {
         throw new IllegalArgumentException("Argument indentUnit is"
               + " incorrect");
      }
      type.setIndentUnit(indentUnit);
      PREFS.storePrefs("indentUnit", type.getIndentUnit());
   }

   /**
    * Returns the current indentation unit
{'fraction_non_alphanumeric': 0.09204844655081622, 'fraction_numerical': 0.00021063717746182202, 'mean_word_length': 2.453090909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '107071', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2668, 'n_words': 905}