<commit_before>import * as Action from 'common/actions';
import { Writer } from 'common/connection';
import { fileBuffer } from 'common/stores';

let fileTreeHandler = null;

/**
 * Takes an instance of the file tree as input and use it to handle the file creation actions.
 * @param {object} ref - file tree component instance
 * @return {void}
 */
export const setFileTreeHandler = (ref) => fileTreeHandler = ref;

export const readProjectFiles = () => Writer.readProjectFiles();

export const loadFile = ({ path }) => (fileBuffer.exists(path)) ?
    Action.viewCode(path) : Writer.readFile({ path });

export const closeFile = (filePath) => {
    fileBuffer.close(filePath);
    const lastOpenedFile = fileBuffer.lastOpenedFile();
    if (lastOpenedFile) Action.viewCode(lastOpenedFile);
    else Action.cleanCode();
};

export const closeCurrentFile = () => {
    const currentFilePath = fileBuffer.activeFilePath;
    closeFile(currentFilePath);
};

export const closeAllFiles = () => fileBuffer.fileStates.map(({ path }) => closeFile(path));

export const createNewFile = (path) => Writer.createFile({ path });

export const triggerNewFile = () => {
    if (!fileBuffer.activeFile) return;
    let path = fileBuffer.activeFile.path.split('/');
    path.pop();
    path = path.join('/');
    fileTreeHandler.createFile(path);
};

// TODO: Implement folder creation
export const createNewFolder = () => {};

export const saveFile = () => {
    const activeFile = fileBuffer.activeFile;
    if (!activeFile) return;
    const content = Action.getCode();
    Writer.updateFile({ ...activeFile, content });
};

export const saveAllFiles = () => fileBuffer.openedFiles.map(file => saveFile(file));

export const viewNextFile = () => {
    const nextFile = fileBuffer.nextFile;
    loadFile(nextFile);
};

export const viewPreviousFile = () => {
    const previousFile = fileBuffer.previousFile;
    loadFile(previousFile);
};<commit_msg>Creating file on root if no active file<commit_after>import * as Action from 'common/actions';
import { Writer } from 'common/connection';
import { project, fileBuffer } from 'common/stores';

let fileTreeHandler = null;

/**
 * Takes an instance of the file tree as input and use it to handle the file creation actions.
 * @param {object} ref - file tree component instance
 * @return {void}
 */
export const setFileTreeHandler = (ref) => fileTreeHandler = ref;

export const readProjectFiles = () => Writer.readProjectFiles();

export const loadFile = ({ path }) => (fileBuffer.exists(path)) ?
    Action.viewCode(path) : Writer.readFile({ path });

export const closeFile = (filePath) => {
    fileBuffer.close(filePath);
    const lastOpenedFile = fileBuffer.lastOpenedFile();
    if (lastOpenedFile) Action.viewCode(lastOpenedFile);
    else Action.cleanCode();
};

export const closeCurrentFile = () => {
    const currentFilePath = fileBuffer.activeFilePath;
    closeFile(currentFilePath);
};

export const closeAllFiles = () => fileBuffer.fileStates.map(({ path }) => closeFile(path));

export const createNewFile = (path) => Writer.createFile({ path });

export const triggerNewFile = () => {
    let path = '';
    if (fileBuffer.activeFile) {
        let path = fileBuffer.activeFile.path.split('/');
        path.pop();
        path = path.join('/');
    } else {
        path = project.path;
    }
    fileTreeHandler.createFile(path);
};

// TODO: Implement folder creation
export const createNewFolder = () => {};

export const saveFile = () => {
    const activeFile = fileBuffer.activeFile;
    if (!activeFile) return;
    const content = Action.getCode();
    Writer.updateFile({ ...activeFile, content });
};

export const saveAllFiles = () => fileBuffer.openedFiles.map(file => saveFile(file));

export const viewNextFile = () => {
    const nextFile = fileBuffer.nextFile;
    loadFile(nextFile);
};

export const viewPreviousFile = () => {
    const previousFile = fileBuffer.previousFile;
    loadFile(previousFile);
};
{'fraction_non_alphanumeric': 0.13765385581512182, 'fraction_numerical': 0.0, 'mean_word_length': 4.098591549295775, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '3715753', 'n_tokens_mistral': 1130, 'n_tokens_neox': 1128, 'n_words': 306}