<commit_before>angular.module('cheatsheet')
    .service('csfAceEditor', [
        '$rootScope', 'csfUserSettings',
        function($rootScope, csfUserSettings) {
            var me = this,
                $aceEditorElement,
                aceEditor,
                deregisterRootScopeWatch,
                eventHandlers = {
                    release : null
                };

            function init() {
                $aceEditorElement = $('<div/>', {id: 'aceEditor'});
                $('body').append(
                    $('<div/>', {id:'aceEditorDock'})
                        .append( $('<div/>') )
                        .append( $aceEditorElement )
                );
                $('#aceEditorDock').hide();

                aceEditor = ace.edit("aceEditor");
                aceEditor.$blockScrolling = Infinity;
                aceEditor.setOptions({
                    enableBasicAutocompletion: true
                });

                aceEditor.commands.addCommand({
                    name: 'releaseEditor',
                    bindKey: {
                        win: 'esc',
                        mac: 'esc'
                    },
                    exec: function(env, args, request) {
                        if( !aceEditor ) {
                            return;
                        }

                        if(typeof me.onRelease === 'function') {
                            me.onRelease();
                        }

                        $aceEditorElement.prev().css('minHeight', '');
                        $aceEditorElement.appendTo( $('#aceEditorDock') );
                    }
                });

                csfUserSettings.UserSettingsPromise
                    .then(function(userSettings) {
                        deregisterRootScopeWatch = $rootScope.$watch(
                            function() {
                                return userSettings.instance.editor;
                            },
                            function(newValue, oldValue) {
                                if( newValue && newValue.theme ) {
                                    aceEditor.setTheme(newValue.theme);
                                }
                                if( newValue && newValue.fontSize ) {
                                    aceEditor.setFontSize( parseInt(newValue.fontSize, 10) )
                                }
                            },
                            true
                        );
                    });
            }

            /**
             * 
             * @param $container a jQuery container to which the editor will be appended
             * @returns {*} The Ace editor instance
             */
            this.acquire = function( $container ) {
                if( !aceEditor ) {
                    init();
                }

                aceEditor.setValue('', 0);
                aceEditor.getSession().getUndoManager().reset();
                $aceEditorElement.appendTo( $container );
                $aceEditorElement.prev().css('minHeight', '13em');
                aceEditor.resize();

                return aceEditor;
            };

            /**
             * Release the editor from its current position
             */
            this.release = function() {
                if( !aceEditor ) {
                    return;
                }
                
                aceEditor.execCommand('releaseEditor');
            };

            /**
             * Assign a callback to be executed before the editor is released from its current position.
             * 
             * @type {function}
             */
            this.onRelease = null;

            /**
             * Destroys the ace editor instance.<commit_msg>Fixed a small bug with the way ace editor is released<commit_after>angular.module('cheatsheet')
    .service('csfAceEditor', [
        '$rootScope', 'csfUserSettings',
        function($rootScope, csfUserSettings) {
            var me = this,
                $aceEditorElement,
                aceEditor,
                deregisterRootScopeWatch,
                eventHandlers = {
                    release : null
                };

            function init() {
                $aceEditorElement = $('<div/>', {id: 'aceEditor'});
                $('body').append(
                    $('<div/>', {id:'aceEditorDock'})
                        .append( $('<div/>') )
                        .append( $aceEditorElement )
                );
                $('#aceEditorDock').hide();

                aceEditor = ace.edit("aceEditor");
                aceEditor.$blockScrolling = Infinity;
                aceEditor.setOptions({
                    enableBasicAutocompletion: true
                });

                aceEditor.commands.addCommand({
                    name: 'releaseEditor',
                    bindKey: {
                        win: 'esc',
                        mac: 'esc'
                    },
                    exec: function(env, args, request) {
                        if( !aceEditor ) {
                            return;
                        }

                        if(typeof me.onRelease === 'function') {
                            me.onRelease();
                        }

                        $aceEditorElement.prev().css('minHeight', '');
                        $aceEditorElement.appendTo( $('#aceEditorDock') );
                    }
                });

                csfUserSettings.UserSettingsPromise
                    .then(function(userSettings) {
                        deregisterRootScopeWatch = $rootScope.$watch(
                            function() {
                                return userSettings.instance.editor;
                            },
                            function(newValue, oldValue) {
                                if( newValue && newValue.theme ) {
                                    aceEditor.setTheme(newValue.theme);
                                }
                                if( newValue && newValue.fontSize ) {
                                    aceEditor.setFontSize( parseInt(newValue.fontSize, 10) )
                                }
                            },
                            true
                        );
                    });
            }

            /**
             * 
             * @param $container a jQuery container to which the editor will be appended
             * @returns {*} The Ace editor instance
             */
            this.acquire = function( $container ) {
                if( !aceEditor ) {
                    init();
                } else {
                    me.release();
                }

                aceEditor.setValue('', 0);
                aceEditor.getSession().getUndoManager().reset();
                $aceEditorElement.appendTo( $container );
                $aceEditorElement.prev().css('minHeight', '13em');
                aceEditor.resize();

                return aceEditor;
            };

            /**
             * Release the editor from its current position
             */
            this.release = function() {
                if( !aceEditor ) {
                    return;
                }
                
                aceEditor.execCommand('releaseEditor');
            };

            /**
             * Assign a callback to be executed before the editor is released from its current position.
             * 
             * @type {function}
             */
            this.onRelease = null;

            /**
             * Destroys the ace editor instance.
{'fraction_non_alphanumeric': 0.09739494698258934, 'fraction_numerical': 0.0013090718680455558, 'mean_word_length': 0.8845584607794771, 'pattern_counts': {'":': 0, '<': 9, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5446504', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1564, 'n_words': 307}