<commit_before>'use strict';

var path = require('path'),
    p4 = require('node-perforce'),
    Q = require('q'),
    $ = require('jquery'),
    statusTileElements = {
        info: $('<span/>').addClass('perforce perforce-info'),
        error: $('<span/>').addClass('perforce perforce-error'),
        warning: $('<span/>').addClass('perforce perforce-warning')
    },
    clientStatusBarElement = $('<div/>')
        .addClass('git-branch inline-block')
        .append('<span class="icon icon-git-branch"></span>')
        .append('<span class="branch-label"></span>'),
    changeHunkDescriptorRegex = /^[\d,]+(\w)(\d+)(,)?(\d+)?$/,
    statusMessageTile,
    clientStatusBarTile;

function setStatusInfo(message, messageType, timeout) {
    var statusBar = document.querySelector('status-bar'),
        statusTileElement;

    if(statusBar && statusBar.addLeftTile) {
        statusTileElement = statusTileElements[messageType]
        .clone()
        .append('Perforce: ' + message);

        if(statusMessageTile && statusMessageTile.destroy) {
            statusMessageTile.destroy();
        }

        statusMessageTile = statusBar.addLeftTile({
            item: statusTileElement,
            priority: 100
        });

        if(timeout) {
            setTimeout(function() {
                if(statusMessageTile && statusMessageTile.destroy) {
                    statusMessageTile.destroy();
                }
            }, timeout);
        }
    }
    else {
        console.warn('status bar is not present');
    }
}

function setStatusClient(p4ClientName) {
    var statusBar = document.querySelector('status-bar'),
        statusElement;

    if(clientStatusBarTile && clientStatusBarTile.destroy) {
        clientStatusBarTile.destroy();
    }

    if(p4ClientName) {
        statusElement = clientStatusBarElement.clone();
        statusElement.find('.branch-label').text(p4ClientName);

        clientStatusBarTile = statusBar.addRightTile({
            item: statusElement,
            priority: 0
        });
    }
}

/**
 * build a list of change hunks, where each hunk is a descriptor, a list of additions, and a list of deletions
 * @param {string} p4DiffOutput the output of a p4 diff command
 * @return {array} list of hunk objects
 */
function processDiff(p4DiffOutput) {
    var changes = [],
        change, firstChar;

    p4DiffOutput.split('\n').forEach(function(line) {
        if(line.length > 1) {
            firstChar = line.substr(0, 1);
            if(!isNaN(parseInt(firstChar, 10))) {
                if(change) {
                    changes.push(change);
                }

                change = {
                    descriptor: line,
                    added: [],
                    removed: []
                };
            }
            else if(firstChar === '<') {
                change.removed.push(line);
            }
            else if(firstChar === '>') {
                change.added.push(line);
            }
        }
    });

    if(change) {
        changes.push(change);
    }

    return changes;
}

module.exports = {
    /**
     * p4 edit a file
     */
    edit: function edit() {
        var editor = atom.workspace.getActivePaneItem(),
            openedBufferFilePath,
            openedBufferFilename;

        if(editor && editor.getPath()) {
            openedBufferFilePath = path.dirname(editor.buffer.file.path);
            openedBufferFilename = editor.buffer.file.path.replace(openedBufferFilePath + path.sep, '');

            // call p4 info to make sure perforce is available
            Q.nfcall(p4.info, { cwd: openedBufferFilePath })
            .then(function(/*p4Info*/) {
                // open the file for edit
                return Q.nfcall(p4.edit, { cwd: openedBufferFilePath, files: [openedBufferFilename] })
                .then(function(result) {
                    setStatusInfo(result, 'info', 5000);
                    console.log(result, result);
                })
                .catch(function(err) {
                    setStatusInfo(err, 'error', 10000);
                    console.error(err);
                });
            })
            .then(function() {
                // reload the file, since perforce has likely chmod'd the file to be writable
                return editor.buffer.reload();
            })
            .catch(function(err) {
                setStatusInfo(err, 'error', 10000);
                console.err(err);
            });
        }
        else {
            console.warn('cannot edit an unsaved file');
            setStatusInfo('cannot edit an unsaved file', 'warning', 10000);<commit_msg>removed unnecessary reload after p4 editing a file<commit_after>'use strict';

var path = require('path'),
    p4 = require('node-perforce'),
    Q = require('q'),
    $ = require('jquery'),
    statusTileElements = {
        info: $('<span/>').addClass('perforce perforce-info'),
        error: $('<span/>').addClass('perforce perforce-error'),
        warning: $('<span/>').addClass('perforce perforce-warning')
    },
    clientStatusBarElement = $('<div/>')
        .addClass('git-branch inline-block')
        .append('<span class="icon icon-git-branch"></span>')
        .append('<span class="branch-label"></span>'),
    changeHunkDescriptorRegex = /^[\d,]+(\w)(\d+)(,)?(\d+)?$/,
    statusMessageTile,
    clientStatusBarTile;

function setStatusInfo(message, messageType, timeout) {
    var statusBar = document.querySelector('status-bar'),
        statusTileElement;

    if(statusBar && statusBar.addLeftTile) {
        statusTileElement = statusTileElements[messageType]
        .clone()
        .append('Perforce: ' + message);

        if(statusMessageTile && statusMessageTile.destroy) {
            statusMessageTile.destroy();
        }

        statusMessageTile = statusBar.addLeftTile({
            item: statusTileElement,
            priority: 100
        });

        if(timeout) {
            setTimeout(function() {
                if(statusMessageTile && statusMessageTile.destroy) {
                    statusMessageTile.destroy();
                }
            }, timeout);
        }
    }
    else {
        console.warn('status bar is not present');
    }
}

function setStatusClient(p4ClientName) {
    var statusBar = document.querySelector('status-bar'),
        statusElement;

    if(clientStatusBarTile && clientStatusBarTile.destroy) {
        clientStatusBarTile.destroy();
    }

    if(p4ClientName) {
        statusElement = clientStatusBarElement.clone();
        statusElement.find('.branch-label').text(p4ClientName);

        clientStatusBarTile = statusBar.addRightTile({
            item: statusElement,
            priority: 0
        });
    }
}

/**
 * build a list of change hunks, where each hunk is a descriptor, a list of additions, and a list of deletions
 * @param {string} p4DiffOutput the output of a p4 diff command
 * @return {array} list of hunk objects
 */
function processDiff(p4DiffOutput) {
    var changes = [],
        change, firstChar;

    p4DiffOutput.split('\n').forEach(function(line) {
        if(line.length > 1) {
            firstChar = line.substr(0, 1);
            if(!isNaN(parseInt(firstChar, 10))) {
                if(change) {
                    changes.push(change);
                }

                change = {
                    descriptor: line,
                    added: [],
                    removed: []
                };
            }
            else if(firstChar === '<') {
                change.removed.push(line);
            }
            else if(firstChar === '>') {
                change.added.push(line);
            }
        }
    });

    if(change) {
        changes.push(change);
    }

    return changes;
}

module.exports = {
    /**
     * p4 edit a file
     */
    edit: function edit() {
        var editor = atom.workspace.getActivePaneItem(),
            openedBufferFilePath,
            openedBufferFilename;

        if(editor && editor.getPath()) {
            openedBufferFilePath = path.dirname(editor.buffer.file.path);
            openedBufferFilename = editor.buffer.file.path.replace(openedBufferFilePath + path.sep, '');

            // call p4 info to make sure perforce is available
            Q.nfcall(p4.info, { cwd: openedBufferFilePath })
            .then(function(/*p4Info*/) {
                // open the file for edit
                return Q.nfcall(p4.edit, { cwd: openedBufferFilePath, files: [openedBufferFilename] })
                .then(function(result) {
                    setStatusInfo(result, 'info', 5000);
                    console.log(result, result);
                })
                .catch(function(err) {
                    setStatusInfo(err, 'error', 10000);
                    console.error(err);
                });
            })
            .catch(function(err) {
                setStatusInfo(err, 'error', 10000);
                console.err(err);
            });
        }
        else {
            console.warn('cannot edit an unsaved file');
            setStatusInfo('cannot edit an unsaved file', 'warning', 10000);
{'fraction_non_alphanumeric': 0.12907460074381974, 'fraction_numerical': 0.009078976154014439, 'mean_word_length': 1.841205717837166, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '5175991', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2392, 'n_words': 480}