<commit_before>import React from 'react';
import FileTree from './FileTree.jsx'
import TextEditorPane from './TextEditorPane.jsx';
import TextEditor from './TextEditor.jsx';
const {remote, ipcRenderer, dialog} = require('electron');
const fs = require('fs');


export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      nextTabId: 0,
      openTabs: [],
      activeTab: null,
      openedProjectPath: '',
    }
    this.openFile = this.openFile.bind(this);
    this.setActiveTab = this.setActiveTab.bind(this);
    this.checkIfAlreadyOpened = this.checkIfAlreadyOpened.bind(this);
    this.saveTab = this.saveTab.bind(this);
    this.addEditorInstance = this.addEditorInstance.bind(this);
    this.closeTab = this.closeTab.bind(this);

    //reset tabs, should save tabs before doing this though
    ipcRenderer.on('openDir', (err, arg) => {
      if (this.state.openedProjectPath !== arg) {
        this.setState({ openTabs: [], activeTab: null, openedProjectPath: arg, nextTabId: 0 });
      }
    });
    ipcRenderer.on('saveFile', (err, arg) => {
      console.log(this.state.activeTab);
      if (this.state.activeTab !== null) {
        this.saveTab(this.state.activeTab);
      }
    })
  }
  closeTab(id) {
    const temp = this.state.openTabs;
    for (var i = 0; i < temp.length; i++) {
      if (temp[i].id === id) {
        temp.splice(i, 1);
        break;
      }
    }
    this.setState({ openTabs: temp });
  }
  addEditorInstance(editor, id) {
    const temp = this.state.openTabs;
    let i;
    for (i = 0; this.state.openTabs[i].id !== id; i++) {
      console.log('openTabId:', this.state.openTabs[i].id);
      console.log('id:', id);
    }
    temp[i].editor = editor;
    this.setState({
      openTabs: temp
    })
  }
  saveTab(tabIndex) {
    console.log("Should Save");
    fs.writeFileSync(this.state.openTabs[tabIndex].path, this.state.openTabs[tabIndex].editor.getValue(), { encoding: 'utf8' });
  }
  setActiveTab(id) {
    this.setState({ activeTab: id });
  }
  openFile(file) {
    let id = this.checkIfAlreadyOpened(file);
    if (id === -1) {
      const openTabs = this.state.openTabs;
      id = this.state.nextTabId;
      // openTabs[this.state.id] = {
      //   path: file.path,
      //   id,
      //   name: file.name,
      //   modified: false,<commit_msg>closing tab opens first tab in array<commit_after>import React from 'react';
import FileTree from './FileTree.jsx'
import TextEditorPane from './TextEditorPane.jsx';
import TextEditor from './TextEditor.jsx';
const {remote, ipcRenderer, dialog} = require('electron');
const fs = require('fs');


export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      nextTabId: 0,
      openTabs: [],
      activeTab: null,
      openedProjectPath: '',
    }
    this.openFile = this.openFile.bind(this);
    this.setActiveTab = this.setActiveTab.bind(this);
    this.checkIfAlreadyOpened = this.checkIfAlreadyOpened.bind(this);
    this.saveTab = this.saveTab.bind(this);
    this.addEditorInstance = this.addEditorInstance.bind(this);
    this.closeTab = this.closeTab.bind(this);

    //reset tabs, should save tabs before doing this though
    ipcRenderer.on('openDir', (err, arg) => {
      if (this.state.openedProjectPath !== arg) {
        this.setState({ openTabs: [], activeTab: null, openedProjectPath: arg, nextTabId: 0 });
      }
    });
    ipcRenderer.on('saveFile', (err, arg) => {
      if (this.state.activeTab !== null) {
        this.saveTab(this.state.activeTab);
      }
    })
  }
  closeTab(id, event) {
    const temp = this.state.openTabs;
    for (var i = 0; i < temp.length; i++) {
      if (temp[i].id === id) {
        temp.splice(i, 1);
        break;
      }
    }
    event.stopPropagation();
    const activeTab = temp[0].id;
    this.setState({ openTabs: temp, activeTab });
  }
  addEditorInstance(editor, id) {
    const temp = this.state.openTabs;
    let i;
    for (i = 0; this.state.openTabs[i].id !== id; i++) {}
    temp[i].editor = editor;
    this.setState({
      openTabs: temp
    })
  }
  saveTab(tabIndex) {
    fs.writeFileSync(this.state.openTabs[tabIndex].path, this.state.openTabs[tabIndex].editor.getValue(), { encoding: 'utf8' });
  }
  setActiveTab(id) {
    this.setState({ activeTab: id });
  }
  openFile(file) {
    let id = this.checkIfAlreadyOpened(file);
    if (id === -1) {
      const openTabs = this.state.openTabs;
      id = this.state.nextTabId;
      // openTabs[this.state.id] = {
      //   path: file.path,
      //   id,
      //   name: file.name,
      //   modified: false,
{'fraction_non_alphanumeric': 0.15682895303748384, 'fraction_numerical': 0.003231365790607497, 'mean_word_length': 2.759514170040486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '5187929', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1569, 'n_words': 314}