<commit_before>/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */

/** Extension that starts/stops Recode and saves when appropriate */
define(function (require, exports, module) {
    "use strict";

    var CommandManager = brackets.getModule('command/CommandManager'),
        Menus = brackets.getModule('command/Menus'),
        MainViewManager = brackets.getModule('view/MainViewManager'),
        DocumentManager = brackets.getModule('document/DocumentManager'),
        FileSystem = brackets.getModule('filesystem/FileSystem'),
        ProjectManager = brackets.getModule('project/ProjectManager'),
        Dialogs = brackets.getModule('widgets/Dialogs'),
        DefaultDialogs = brackets.getModule('widgets/DefaultDialogs'),
        ProjectModel = brackets.getModule('project/ProjectModel');

    var Recoder = function() {
        var self = this;

        this.currentDocument = null;
        this.recording = false;
        this.saveDir = null;
        this.startTime = null;
        this.lastTime = null;
        this.trackedFiles = [];
        this.trackedFileObjects = [];
        this.actions = [];

        this.onTextChangeProxy = function() {
            Recoder.prototype.onTextChange.apply(self, arguments);
        };

        this.onFileChangeProxy = function() {
            Recoder.prototype.onFileChange.apply(self, arguments);
        }
    };

    Recoder.prototype.addEvent = function(e) {
        var now = Date.now();
        var difference = now - this.lastTime;

        if (e.distance == null) {
            e.distance = difference;
        }

        this.lastTime = now;

        this.actions.push(e);
    };

    Recoder.prototype.onFileChange = function(e, newFile, newPaneId, oldFile, oldPaneId) {
        this.removeDocument();
        this.addDocument();
    };

    // This function *tries* to handle multiple selections
    // Proper tests need to be built to make sure that actually works
    Recoder.prototype.onTextChange = function(e, document, changelist) {
        var first = true;

        // Make sure the changes are in order
        // So we can adjust for changes in position
        // As all changes are in in pre-change coordinates
        if (changelist.length > 1) {
            changelist = changelist.slice().sort(function(a, b) {
                if ((a.to.line > b.from.line) || ((a.to.line == b.from.line) && (a.to.ch > b.from.ch))) {
                    return 1;
                } else if ((a.to.line == b.from.line) && (a.to.ch == b.from.ch)) {
                    return 0;
                } else {
                    return -1;
                }
            });
        }

        var offsets = { },
            rowOffset = 0;

        for (var i in changelist) {
            var change = changelist[i];

            var offset = 0;

            if (!first) {
                offset = offsets[String(change.from.line)] || 0;
            }

            var event = {
                data: change.text,
                position: {
                    row: change.from.line,
                    col: change.from.ch + offset
                },
                length: {
                    row: change.to.line - change.from.line,
                    col: (change.to.ch + ((change.from.line == change.to.line) ? offset : 0)) - (change.from.ch + offset)
                },
                mode: 0
            };

            var addOffset = change.text[change.text.length - 1].length - ((change.from.line == change.to.line)
                ? change.to.ch - change.from.ch
                : change.to.ch);

            offsets[String(change.to.line)] = (offsets[String(change.to.line)] || 0) + addOffset;

            // It appears that line values are in a post-change coordinate system
            //rowOffset += change.text.length - (change.to.line - change.from.line);

            if (!first) {
                event.distance = 0;
            }

            this.addEvent(event);
            console.log(event);

            first = false;
        }
        console.log(changelist);
    };

    Recoder.prototype.addDocument = function() {
        this.currentDocument = DocumentManager.getCurrentDocument();

        if (this.currentDocument) {
            var displayPath = ProjectManager.makeProjectRelativeIfPossible(this.currentDocument.file.fullPath);
            var name = displayPath.replace(/\//g, '--');<commit_msg>Add selection listen event (but don't bind it yet)<commit_after>/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global define, $, brackets, window */

/** Extension that starts/stops Recode and saves when appropriate */
define(function (require, exports, module) {
    "use strict";

    var CommandManager = brackets.getModule('command/CommandManager'),
        Menus = brackets.getModule('command/Menus'),
        MainViewManager = brackets.getModule('view/MainViewManager'),
        DocumentManager = brackets.getModule('document/DocumentManager'),
        FileSystem = brackets.getModule('filesystem/FileSystem'),
        ProjectManager = brackets.getModule('project/ProjectManager'),
        Dialogs = brackets.getModule('widgets/Dialogs'),
        DefaultDialogs = brackets.getModule('widgets/DefaultDialogs'),
        ProjectModel = brackets.getModule('project/ProjectModel');

    var Recoder = function() {
        var self = this;

        this.currentDocument = null;
        this.recording = false;
        this.saveDir = null;
        this.startTime = null;
        this.lastTime = null;
        this.trackedFiles = [];
        this.trackedFileObjects = [];
        this.actions = [];

        this.onTextChangeProxy = Recoder.prototype.onTextChange.bind(this);
        this.onFileChangeProxy = Recoder.prototype.onFileChange.bind(this);
        this.onSelectionChangeProxy = Recoder.prototype.onSelectionChange.bind(this);
    };

    Recoder.prototype.addEvent = function(e) {
        var now = Date.now();
        var difference = now - this.lastTime;

        if (e.distance == null) {
            e.distance = difference;
        }

        this.lastTime = now;

        this.actions.push(e);
    };

    Recoder.prototype.onFileChange = function(e, newFile, newPaneId, oldFile, oldPaneId) {
        this.removeDocument();
        this.addDocument();
    };

    // This function *tries* to handle multiple selections
    // Proper tests need to be built to make sure that actually works
    Recoder.prototype.onTextChange = function(e, document, changelist) {
        var first = true;

        // Make sure the changes are in order
        // So we can adjust for changes in position
        // As all changes are in in pre-change coordinates
        if (changelist.length > 1) {
            changelist = changelist.slice().sort(function(a, b) {
                if ((a.to.line > b.from.line) || ((a.to.line == b.from.line) && (a.to.ch > b.from.ch))) {
                    return 1;
                } else if ((a.to.line == b.from.line) && (a.to.ch == b.from.ch)) {
                    return 0;
                } else {
                    return -1;
                }
            });
        }

        var offsets = { },
            rowOffset = 0;

        for (var i in changelist) {
            var change = changelist[i];

            var offset = 0;

            if (!first) {
                offset = offsets[String(change.from.line)] || 0;
            }

            var event = {
                data: change.text,
                position: {
                    row: change.from.line,
                    col: change.from.ch + offset
                },
                length: {
                    row: change.to.line - change.from.line,
                    col: (change.to.ch + ((change.from.line == change.to.line) ? offset : 0)) - (change.from.ch + offset)
                },
                mode: 0
            };

            var addOffset = change.text[change.text.length - 1].length - ((change.from.line == change.to.line)
                ? change.to.ch - change.from.ch
                : change.to.ch);

            offsets[String(change.to.line)] = (offsets[String(change.to.line)] || 0) + addOffset;

            // It appears that line values are in a post-change coordinate system
            //rowOffset += change.text.length - (change.to.line - change.from.line);

            if (!first) {
                event.distance = 0;
            }

            this.addEvent(event);
            console.log(event);

            first = false;
        }
        console.log(changelist);
    };

    Recoder.prototype.onSelectionChange = function(e) {
        console.log(arguments);
    }

    Recoder.prototype.addDocument = function() {
        this.currentDocument = DocumentManager.getCurrentDocument();

        if (this.currentDocument) {
            var displayPath = ProjectManager.makeProjectRelativeIfPossible(this.currentDocument.file.fullPath);
            var name = displayPath.replace(/\//g, '--');
{'fraction_non_alphanumeric': 0.12244224422442244, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 1.8136799752398638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '427404', 'n_tokens_mistral': 2606, 'n_tokens_neox': 2425, 'n_words': 553}