<commit_before>'use babel';

function getPanes() {
    const panesFilter = (panes, prop, name) => panes.filter(x => x[prop]
                     && x[prop].constructor && x[prop].constructor.name === name);
    let allPanes = atom.workspace.getPanes(),
        panes = panesFilter(allPanes, 'parent', 'PaneAxis');
    if (!panes.length) panes = panesFilter(allPanes, 'activeItem', 'TextEditor');
    return panes;
}

function getOrientationPanes(horizontal) {
    let panes = getPanes(),
        orientation = (horizontal) ? 'horizontal' : 'vertical';
    return panes.filter(x => (x.parent.orientation || orientation) === orientation);
};

function addUntilNPanes(n, horizontal) {
    let panes = getOrientationPanes(horizontal),
        paneCount = panes.length,
        promise = Promise.resolve([panes, paneCount]);
    if (paneCount === 0) {
        promise = new Promise((accept,reject) => {
            atom.workspace.open()
            .then(() => {
                let panes = getOrientationPanes(horizontal);
                accept([panes, panes.length]);
            })
            .catch((err) => reject(err));
        });
    }
    return new Promise((accept,reject) => {
        promise.then(([panes, paneCount]) => {
            if (horizontal) panes.slice(-1)[0].splitRight();
            else panes.slice(-1)[0].splitDown();
            if (paneCount+1 < n) {
                addUntilNPanes(n, horizontal)
                .then(x => accept(x))
                .catch((err) => reject(err));
            }
            else accept();
        })
        .catch((err) => reject(err));
    });
};

function mergeTwoPanes(lastPane, secondToLastPane) {
    let lastPaneItems = lastPane.getItems();
    for (let item of Array.from(lastPaneItems)) {
        lastPane.moveItemToPane(item, secondToLastPane);
    }
    lastPane.destroy();
};

function mergeUntilNPanes(n, horizontal) {
    let panes = getOrientationPanes(horizontal);
    let result = [];
    while (panes.length > n) {
        mergeTwoPanes(panes.slice(-1)[0], panes.slice(-2)[0]);
        result.push(panes = getOrientationPanes(horizontal));
    }
    return Promise.resolve();
};

// Main functions

function ensurePanes(n, horizontal) {
    let revPaneCount = getOrientationPanes(!horizontal).length,
        paneCount = getOrientationPanes(horizontal).length;
    // If true, there is a number of horizontal panes within vertical panes or vice versa
    let forceToOneA = ((revPaneCount > 0) && (paneCount > 1))
                            || ((revPaneCount > 1) && (paneCount > 0));
    // If true, possible transition from grid to horizontal panes
    let forceToOneB = (!horizontal) && (paneCount >= 4) && ((paneCount % 2) === 0);
    if (forceToOneA || forceToOneB) {
        // We can't properly identify the panes, we'll merge all until only one pane is left
        let panes = getPanes();
            paneCount = 1;
        while (panes.length > 1) {
            mergeTwoPanes(panes.slice(-1)[0], panes.slice(-2)[0]);
            panes = getPanes();
        }
    } else if (revPaneCount > 1) {
        mergeUntilNPanes(1, !horizontal);
    }
    paneCount = getOrientationPanes(horizontal).length;
    if (paneCount < n && n > 1) return addUntilNPanes(n, horizontal);
    else if (paneCount > n) return mergeUntilNPanes(n, horizontal);
    return Promise.resolve();
}

function ensureGrid() {
    ensurePanes(2, true).then(() => {
        let panes = getPanes();
        Array.from(panes).map((item) => item.splitDown());
    })
    .catch((err) => {}); // let it die
}

export default { ensurePanes, ensureGrid };
<commit_msg>Changes to getPanes()<commit_after>'use babel';

function getPanes() {
    const allPanes = atom.workspace.getPanes(),
        panesFilter = (prop, name) => allPanes.filter(x => x[prop] && x[prop].constructor
                                                    && x[prop].constructor.name === name);
    let panes = panesFilter('parent', 'PaneAxis');
    if (!panes.length) panes = panesFilter('activeItem', 'TextEditor');
    return panes;
}

function getOrientationPanes(horizontal) {
    let panes = getPanes(),
        orientation = (horizontal) ? 'horizontal' : 'vertical';
    return panes.filter(x => (x.parent.orientation || orientation) === orientation);
};

function addUntilNPanes(n, horizontal) {
    let panes = getOrientationPanes(horizontal),
        paneCount = panes.length,
        promise = Promise.resolve([panes, paneCount]);
    if (paneCount === 0) {
        promise = new Promise((accept,reject) => {
            atom.workspace.open()
            .then(() => {
                let panes = getOrientationPanes(horizontal);
                accept([panes, panes.length]);
            })
            .catch((err) => reject(err));
        });
    }
    return new Promise((accept,reject) => {
        promise.then(([panes, paneCount]) => {
            if (horizontal) panes.slice(-1)[0].splitRight();
            else panes.slice(-1)[0].splitDown();
            if (paneCount+1 < n) {
                addUntilNPanes(n, horizontal)
                .then(x => accept(x))
                .catch((err) => reject(err));
            }
            else accept();
        })
        .catch((err) => reject(err));
    });
};

function mergeTwoPanes(lastPane, secondToLastPane) {
    let lastPaneItems = lastPane.getItems();
    for (let item of Array.from(lastPaneItems)) {
        lastPane.moveItemToPane(item, secondToLastPane);
    }
    lastPane.destroy();
};

function mergeUntilNPanes(n, horizontal) {
    let panes = getOrientationPanes(horizontal);
    let result = [];
    while (panes.length > n) {
        mergeTwoPanes(panes.slice(-1)[0], panes.slice(-2)[0]);
        result.push(panes = getOrientationPanes(horizontal));
    }
    return Promise.resolve();
};

// Main functions

function ensurePanes(n, horizontal) {
    let revPaneCount = getOrientationPanes(!horizontal).length,
        paneCount = getOrientationPanes(horizontal).length;
    // If true, there is a number of horizontal panes within vertical panes or vice versa
    let forceToOneA = ((revPaneCount > 0) && (paneCount > 1))
                            || ((revPaneCount > 1) && (paneCount > 0));
    // If true, possible transition from grid to horizontal panes
    let forceToOneB = (!horizontal) && (paneCount >= 4) && ((paneCount % 2) === 0);
    if (forceToOneA || forceToOneB) {
        // We can't properly identify the panes, we'll merge all until only one pane is left
        let panes = getPanes();
            paneCount = 1;
        while (panes.length > 1) {
            mergeTwoPanes(panes.slice(-1)[0], panes.slice(-2)[0]);
            panes = getPanes();
        }
    } else if (revPaneCount > 1) {
        mergeUntilNPanes(1, !horizontal);
    }
    paneCount = getOrientationPanes(horizontal).length;
    if (paneCount < n && n > 1) return addUntilNPanes(n, horizontal);
    else if (paneCount > n) return mergeUntilNPanes(n, horizontal);
    return Promise.resolve();
}

function ensureGrid() {
    ensurePanes(2, true).then(() => {
        let panes = getPanes();
        Array.from(panes).map((item) => item.splitDown());
    })
    .catch((err) => {}); // let it die
}

export default { ensurePanes, ensureGrid };

{'fraction_non_alphanumeric': 0.14676239127433385, 'fraction_numerical': 0.007455474251000967, 'mean_word_length': 2.5492405683488486, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3049383', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2158, 'n_words': 493}