<commit_before>module('lively.ide.codeeditor.Keyboard').requires('lively.ide.codeeditor.JumpChar').toRun(function() {

module("lively.ide.CodeEditor");

// -=-=-=-=-=-=-=-=-=
// keyboard shortcuts
// -=-=-=-=-=-=-=-=-=

Object.subclass('lively.ide.CodeEditor.KeyboardShortcuts',
'initialization', {
    attach: function(codeEditor) {
        var self = this;
        codeEditor.withAceDo(function(ed) {
            var kbd = ed.getKeyboardHandler();
            if (kbd.hasLivelyKeys) return;
            // so that mutli key shortcuts can be transfered from the global
            // key handler:
            ed.keyBinding.$data.keyChain = "";
            self.setupEvalBindings(kbd);
            self.setupTextManipulationBindings(kbd);
            self.setupSelectionAndNavigationBindings(kbd);
            self.setupSearchBindings(kbd);
            self.setupMultiSelectBindings(kbd);
            self.setupEditorConfigBindings(kbd);
            self.setupInputLineBindings(kbd);
            self.setupSnippetBindings(kbd);
            self.setupASTNavigation(kbd);
            self.setupKeyboardMacroBindings(kbd);
            self.setupToolSpecificBindings(kbd);
            self.setupUsefulHelperBindings(kbd);
            self.setupJumpChar(kbd);
            self.setupUserKeyBindings(kbd, codeEditor);
            kbd.hasLivelyKeys = true;
        });
    },

    addCommands: function(kbd, commands) {
        var platform = kbd.platform; // mac or win

        function lookupCommand(keySpec) {
            return keySpec.split('|').detect(function(keys) {
                var binding = kbd.parseKeys(keys),
                    command = kbd.findKeyCommand(binding.hashId, binding.key);
                return command && command.name;
            });
        }

        // first remove a keybinding if one already exists
        commands.forEach(function(cmd) {
            var keys = cmd.bindKey && (cmd.bindKey[platform] || cmd.bindKey),
                existing = keys && typeof keys === "string" && lookupCommand(keys);
            if (existing) kbd.removeCommand(existing);
        });
        kbd.addCommands(commands);
    },

    morphBinding: function(cmdName, args) {
        return function(ed) { return ed.$morph[cmdName].apply(ed.$morph, args); }
    }

},
"accessing", {
    allCommandsOf: function(codeEditor) {
        return codeEditor.withAceDo(function(ed) {
            return Object.merge(ed.keyBinding.$handlers.pluck('commands')); });
    },
    lookupCommand: function(codeEditor, keySpec) {
        return codeEditor.withAceDo(function(ed) {
            var handler = ed.commands,
                binding = handler.parseKeys(keySpec),
                command = handler.findKeyCommand(binding.hashId, binding.key);
            if (!command) return null;
            if (!command.hasOwnProperty('toString')) {
                command.toString = function() { return '[cmd:' + command.name + ']' }
            }
            return command;
        });
    }
},
'shortcuts', {
    setupEvalBindings: function(kbd) {

        this.addCommands(kbd, [{
                name: 'evalAll',
                exec: function(ed, args) {
                    if (args && args.confirm) {
                        $world.alertOK('Evaluating complete text...');
                    }
                    ed.$morph.saveExcursion(function(whenDone) {
                        ed.$morph.selectAll();
                        maybeUseModeFunction(ed, "doEval", "doit", [false]);
                        whenDone();
                    });
                },
                handlesCount: true,
                readOnly: true
            }, {
                name: 'doit',
                bindKey: {win: 'Ctrl-D',  mac: 'Command-D'},
                exec: function(ed) { maybeUseModeFunction(ed, "doEval", "doit", [false]); },
                multiSelectAction: "forEach",
                readOnly: true // false if this command should not apply in readOnly mode
            }, {
                name: 'debugit',
                // bindKey: {win: 'Ctrl-Shift-D',  mac: 'Command-Shift-D'},
                exec: function(ed) { ed.$morph.doDebugit(); },
                multiSelectAction: "forEach",
                readOnly: true
            }, {
                name: 'printit',
                bindKey: {win: 'Ctrl-P',  mac: 'Command-P'},
                exec: function(ed) { maybeUseModeFunction(ed, "doEval", "doit", [true]);; },
                multiSelectAction: "forEach",
                readOnly: false
            }, {
                name: 'list protocol',
                bindKey: {win: 'Ctrl-Shift-P',  mac: 'Command-Shift-P'},
                exec: this.morphBinding("doListProtocol"),
                multiSelectAction: "single",
                readOnly: false
            }, {
                name: 'doSave',
                bindKey: {win: 'Ctrl-S',  mac: 'Command-S'},
                exec: this.morphBinding("doSave"),
                multiSelectAction: "single",
                readOnly: false
            }, {
                name: 'printInspect',
                bindKey: {win: 'Ctrl-I',  mac: 'Command-I'},
                exec: function(ed, args) {
                    maybeUseModeFunction(ed, "printInspect", "printInspect", [{depth: args && args.count}]);
                },
                multiSelectAction: "forEach",
                handlesCount: true,
                readOnly: true
            }, {
                name: 'doInspect',
                bindKey: {win: 'Ctrl-Shift-I',  mac: 'Command-Shift-I'},
                exec: this.morphBinding("doInspect"),
                multiSelectAction: "forEach",
                readOnly: true
            }, { // shell eval
                name: 'runShellCommand',
                exec: function(ed, args) {
                    lively.ide.commands.exec('lively.ide.execShellCommand', ed.$morph, args);
                },
                handlesCount: true
            }, {
                name: 'runShellCommandOnRegion',
                exec: function(ed, args) {
                    var input = ed.$morph.getSelectionOrLineString();
                    if (!input || input.length === 0) {
                        show('Nothing to input into command, aborting...'); return; }
                    $world.prompt('Enter shell command to run on region.', function(cmdString) {
                        if (!cmdString) { show('No command entered, aborting...!'); return; }
                        lively.shell.run(cmdString, {stdin: input}, function(cmd) {
                            ed.session.replace(ed.selection.getRange(), cmd.resultString(true));
                        });
                    }, {historyId: 'lively.ide.execShellCommand'});
                },
                multiSelectAction: 'forEach',
                handlesCount: true
            }]);
            // FIXME for some reason this does not work with bindKeys?!
            kbd.bindKey("»", 'runShellCommandOnRegion');

        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

        function maybeUseModeFunction(ed, featureName, morphMethodName, args) {
            var mode = ed.session.getMode();
            var morph = ed.$morph;
            if (!mode[featureName]) morph.doit.apply(morph, args);<commit_msg>codeeditor: fixing doit/printit/inspection key commands<commit_after>module('lively.ide.codeeditor.Keyboard').requires('lively.ide.codeeditor.JumpChar').toRun(function() {

module("lively.ide.CodeEditor");

// -=-=-=-=-=-=-=-=-=
// keyboard shortcuts
// -=-=-=-=-=-=-=-=-=

Object.subclass('lively.ide.CodeEditor.KeyboardShortcuts',
'initialization', {
    attach: function(codeEditor) {
        var self = this;
        codeEditor.withAceDo(function(ed) {
            var kbd = ed.getKeyboardHandler();
            if (kbd.hasLivelyKeys) return;
            // so that mutli key shortcuts can be transfered from the global
            // key handler:
            ed.keyBinding.$data.keyChain = "";
            self.setupEvalBindings(kbd);
            self.setupTextManipulationBindings(kbd);
            self.setupSelectionAndNavigationBindings(kbd);
            self.setupSearchBindings(kbd);
            self.setupMultiSelectBindings(kbd);
            self.setupEditorConfigBindings(kbd);
            self.setupInputLineBindings(kbd);
            self.setupSnippetBindings(kbd);
            self.setupASTNavigation(kbd);
            self.setupKeyboardMacroBindings(kbd);
            self.setupToolSpecificBindings(kbd);
            self.setupUsefulHelperBindings(kbd);
            self.setupJumpChar(kbd);
            self.setupUserKeyBindings(kbd, codeEditor);
            kbd.hasLivelyKeys = true;
        });
    },

    addCommands: function(kbd, commands) {
        var platform = kbd.platform; // mac or win

        function lookupCommand(keySpec) {
            return keySpec.split('|').detect(function(keys) {
                var binding = kbd.parseKeys(keys),
                    command = kbd.findKeyCommand(binding.hashId, binding.key);
                return command && command.name;
            });
        }

        // first remove a keybinding if one already exists
        commands.forEach(function(cmd) {
            var keys = cmd.bindKey && (cmd.bindKey[platform] || cmd.bindKey),
                existing = keys && typeof keys === "string" && lookupCommand(keys);
            if (existing) kbd.removeCommand(existing);
        });
        kbd.addCommands(commands);
    },

    morphBinding: function(cmdName, args) {
        return function(ed) { return ed.$morph[cmdName].apply(ed.$morph, args); }
    }

},
"accessing", {
    allCommandsOf: function(codeEditor) {
        return codeEditor.withAceDo(function(ed) {
            return Object.merge(ed.keyBinding.$handlers.pluck('commands')); });
    },
    lookupCommand: function(codeEditor, keySpec) {
        return codeEditor.withAceDo(function(ed) {
            var handler = ed.commands,
                binding = handler.parseKeys(keySpec),
                command = handler.findKeyCommand(binding.hashId, binding.key);
            if (!command) return null;
            if (!command.hasOwnProperty('toString')) {
                command.toString = function() { return '[cmd:' + command.name + ']' }
            }
            return command;
        });
    }
},
'shortcuts', {
    setupEvalBindings: function(kbd) {

        this.addCommands(kbd, [{
                name: 'evalAll',
                exec: function(ed, args) {
                    if (args && args.confirm) {
                        $world.alertOK('Evaluating complete text...');
                    }
                    ed.$morph.saveExcursion(function(whenDone) {
                        ed.$morph.selectAll();
                        maybeUseModeFunction(ed, "doEval", "doit", [false]);
                        whenDone();
                    });
                },
                handlesCount: true,
                readOnly: true
            }, {
                name: 'doit',
                bindKey: {win: 'Ctrl-D',  mac: 'Command-D'},
                exec: function(ed) { maybeUseModeFunction(ed, "doEval", "doit", [false]); },
                multiSelectAction: "forEach",
                readOnly: true // false if this command should not apply in readOnly mode
            }, {
                name: 'debugit',
                // bindKey: {win: 'Ctrl-Shift-D',  mac: 'Command-Shift-D'},
                exec: function(ed) { ed.$morph.doDebugit(); },
                multiSelectAction: "forEach",
                readOnly: true
            }, {
                name: 'printit',
                bindKey: {win: 'Ctrl-P',  mac: 'Command-P'},
                exec: function(ed) { maybeUseModeFunction(ed, "doEval", "doit", [true]);; },
                multiSelectAction: "forEach",
                readOnly: false
            }, {
                name: 'list protocol',
                bindKey: {win: 'Ctrl-Shift-P',  mac: 'Command-Shift-P'},
                exec: this.morphBinding("doListProtocol"),
                multiSelectAction: "single",
                readOnly: false
            }, {
                name: 'doSave',
                bindKey: {win: 'Ctrl-S',  mac: 'Command-S'},
                exec: this.morphBinding("doSave"),
                multiSelectAction: "single",
                readOnly: false
            }, {
                name: 'printInspect',
                bindKey: {win: 'Ctrl-I',  mac: 'Command-I'},
                exec: function(ed, args) {
                    maybeUseModeFunction(ed, "printInspect", "printInspect", [{depth: args && args.count}]);
                },
                multiSelectAction: "forEach",
                handlesCount: true,
                readOnly: true
            }, {
                name: 'doInspect',
                bindKey: {win: 'Ctrl-Shift-I',  mac: 'Command-Shift-I'},
                exec: this.morphBinding("doInspect"),
                multiSelectAction: "forEach",
                readOnly: true
            }, { // shell eval
                name: 'runShellCommand',
                exec: function(ed, args) {
                    lively.ide.commands.exec('lively.ide.execShellCommand', ed.$morph, args);
                },
                handlesCount: true
            }, {
                name: 'runShellCommandOnRegion',
                exec: function(ed, args) {
                    var input = ed.$morph.getSelectionOrLineString();
                    if (!input || input.length === 0) {
                        show('Nothing to input into command, aborting...'); return; }
                    $world.prompt('Enter shell command to run on region.', function(cmdString) {
                        if (!cmdString) { show('No command entered, aborting...!'); return; }
                        lively.shell.run(cmdString, {stdin: input}, function(cmd) {
                            ed.session.replace(ed.selection.getRange(), cmd.resultString(true));
                        });
                    }, {historyId: 'lively.ide.execShellCommand'});
                },
                multiSelectAction: 'forEach',
                handlesCount: true
            }]);
            // FIXME for some reason this does not work with bindKeys?!
            kbd.bindKey("»", 'runShellCommandOnRegion');

        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

        function maybeUseModeFunction(ed, featureName, morphMethodName, args) {
            var mode = ed.session.getMode();
            var morph = ed.$morph;
            if (!mode[featureName]) morph[morphMethodName].apply(morph, args);
{'fraction_non_alphanumeric': 0.13599671637706937, 'fraction_numerical': 0.00013681762210972773, 'mean_word_length': 1.6198924731182796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1995147', 'n_tokens_mistral': 4011, 'n_tokens_neox': 3814, 'n_words': 739}