<commit_before>/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * FSharp eval plugin
 * For creating and config evaluators that evaluate FSharp code and update code cell results.
 */
define(function (require, exports, bkSessionManager)
{
    'use strict';
    var PLUGIN_NAME = "FSharp";
    var COMMAND = "fsharp/fsharpPlugin";
    var serviceBase = null;
    var timer = null;
    var FSharp = {
        pluginName: PLUGIN_NAME,
        cmMode: "text/x-fsharp",
        background: "#30B9DB",
        bgColor: "#378BBA",
        fgColor: "#FFFFFF",
        borderColor: "",
        shortName: "F#",

        newShell: function (shellId, cb)
        {
            if (!shellId)
            {
                shellId = "";
            }
            bkHelper.httpPost(serviceBase + "/fsharp/getShell", { shellId: shellId })
                .success(cb)
                .error(function ()
                {
                    console.log("failed to create shell", arguments);
                });
        },

        evaluate: function (code, modelOutput)
        {
            var deferred = Q.defer();
            var self = this;
            var progressObj = {
                type: "BeakerDisplay",
                innertype: "Progress",
                object: {
                    message: "submitting ...",
                    startTime: new Date().getTime()
                }
            };

            modelOutput.result = progressObj;

            $.ajax({
                type: "POST",
                datatype: "json",
                url: serviceBase + "/fsharp/evaluate",
                data: { shellId: self.settings.shellID, code: code }
            }).done(function (ret)
            {
                if (ret.status === 0)
                {
                    if (ret.result.ContentType.indexOf("image/") === 0)
                    {
                        modelOutput.result =
                        {
                            type: "BeakerDisplay",
                            innertype: 'Html',
                            object: '<img src="data:' + ret.result.ContentType + ';base64,' + ret.result.Data + '" />'
                        };
                    }
                    else if (ret.result.ContentType === "table/grid")
                    {
                        modelOutput.result =
                        {
                            type: "TableDisplay",
                            tableDisplayModel: {
                                columnNames: ret.result.Data.Columns,
                                values: ret.result.Data.Rows
                            },
                            columnNames: ret.result.Data.Columns,
                            values: ret.result.Data.Rows
                        };
                    }
                    else if (ret.result.ContentType === "text/html")
                    {
                        modelOutput.result =
                        {
                            type: "BeakerDisplay",
                            innertype: 'Html',
                            object: ret.result.Data
                        };
                    }
                    else
                    {
                        modelOutput.result = ret.result.Data;
                    }
                }
                else
                {
                    modelOutput.result =
                    {
                        type: "BeakerDisplay",
                        innertype: "Error",
                        object: ret.result.Data
                    };
                }
                modelOutput.elapsedTime = new Date().getTime() - progressObj.object.startTime;
                bkHelper.refreshRootScope();
                deferred.resolve();
            });

            return deferred.promise;
        },

        autocomplete: function (code, cpos, cb)
        {
            var self = this;
            $.ajax({
                type: "POST",
                datatype: "json",
                url: serviceBase + "/fsharp/autocomplete",
                data: { shellId: self.settings.shellID, code: code, caretPosition: cpos }
            }).done(function (x)
            {
                cb(x.Declarations);
            });
        },

        exit: function (cb)
        {
            var self = this;
            $.ajax({
                type: "POST",
                datatype: "json",
                url: serviceBase + "/fsharp/exit",
                data: { shellId: self.settings.shellID }
            }).done(cb);
        },

        updateShell: function (cb)
        {
            bkHelper.httpPost(serviceBase + "/fsharp/setShellOptions", {
                shellId: this.settings.shellID
            }).success(cb);
        },

        spec: {
            useIntellisense: { type: "settableBoolean", action: "updateShell", name: "Use Intellisense" }
        }
    };

    var shellReadyDeferred = bkHelper.newDeferred();
    var init = function ()
    {
        function loadedScripts()
        {
            bkHelper.locatePluginService(PLUGIN_NAME, {
                command: COMMAND,
                startedIndicator: "Successfully started server",
                waitfor: "Successfully started server",
                recordOutput: "true"
            }).success(function (ret)
            {
                serviceBase = ret;

                var FSharpShell = function (settings, doneCB)
                {
                    var self = this;
                    var setShellIdCB = function (id)
                    {
                        settings.shellID = id;
                        self.settings = settings;
                        function cb()
                        {
                            if (bkHelper.hasSessionId())
                            {
                                var initCode = "let beaker = new NamespaceClient(\"" + bkHelper.getSessionId() + "\")";
                                self.evaluate(initCode, {}).then(function ()
                                {
                                    if (doneCB)
                                    {
                                        doneCB(self);
                                    }
                                });
                            }
                            else
                            {
                                if (doneCB)
                                {
                                    doneCB(self);
                                }
                            }
                        }
                        self.updateShell(cb);
                    };

                    if (!settings.shellID)
                    {
                        settings.shellID = "";
                    }
                    this.newShell(settings.shellID, setShellIdCB);
                    this.perform = function (what)
                    {
                        var action = this.spec[what].action;
                        this[action]();
                    };

                    function applyIntellisense()
                    {
                        $('.CodeMirror').each(function (idx, div)
                        {
                            var editor = div.CodeMirror;
                            if (editor.options.mode === 'text/x-fsharp' && !editor.intellisense)
                            {
                                var intellisense = new CodeMirrorIntellisense(editor);
                                editor.intellisense = intellisense;
                                intellisense.addDeclarationTrigger({ keyCode: 190, type: 'up' }); // `.`
                                intellisense.addDeclarationTrigger({ keyCode: 32, ctrlKey: true, preventDefault: true, type: 'down' }); // `ctrl+space`
                                intellisense.addDeclarationTrigger({ keyCode: 191 }); // `/`
                                intellisense.addDeclarationTrigger({ keyCode: 220 }); // `\`
                                intellisense.addDeclarationTrigger({ keyCode: 222 }); // `"`
                                intellisense.addDeclarationTrigger({ keyCode: 222, shiftKey: true }); // `"`
                                intellisense.addMethodsTrigger({ keyCode: 57, shiftKey: true }); // `(`
                                intellisense.addMethodsTrigger({ keyCode: 48, shiftKey: true });// `)`
                                intellisense.onMethod(function (item, position)
                                {

                                });
                                intellisense.onDeclaration(function (item, position)
                                {
                                    var cursor = editor.doc.getCursor();
                                    var line = editor.getLine(cursor.line);
                                    var isSlash = item.keyCode === 191 || item.keyCode === 220;
                                    var isQuote = item.keyCode === 222;

                                    var isLoadOrRef = line.indexOf('#load') === 0
                                        || line.indexOf('#r') === 0;

                                    var isStartLoadOrRef = line === '#load "'
                                        || line === '#r "'
                                        || line === '#load @"'
                                        || line === '#r @"';

                                    if (isSlash && !isLoadOrRef)
                                    {
                                        return;
                                    }
                                    if (isQuote && !isStartLoadOrRef)
                                    {
                                        return;
                                    }

                                    var self = this;
                                    $.ajax({
                                        type: "POST",
                                        datatype: "json",
                                        url: serviceBase + "/fsharp/intellisense",
                                        data: { shellId: settings.shellID, code: editor.getValue(), lineIndex: cursor.line, charIndex: cursor.ch }
                                    }).done(function (x)
                                    {
                                        if (x.declarations.length > 1)
                                        {
                                            var decls = intellisense.getDecls();
                                            intellisense.setDeclarations(x.declarations);<commit_msg>Fixed issue #4<commit_after>/*
 *  Copyright 2014 TWO SIGMA OPEN SOURCE, LLC
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * FSharp eval plugin
 * For creating and config evaluators that evaluate FSharp code and update code cell results.
 */
define(function (require, exports, bkSessionManager)
{
    'use strict';
    var PLUGIN_NAME = "FSharp";
    var COMMAND = "fsharp/fsharpPlugin";
    var serviceBase = null;
    var timer = null;
    var FSharp = {
        pluginName: PLUGIN_NAME,
        cmMode: "text/x-fsharp",
        background: "#30B9DB",
        bgColor: "#378BBA",
        fgColor: "#FFFFFF",
        borderColor: "",
        shortName: "F#",

        newShell: function (shellId, cb)
        {
            if (!shellId)
            {
                shellId = "";
            }
            bkHelper.httpPost(bkHelper.serverUrl(serviceBase + "/fsharp/getShell"), { shellId: shellId, sessionId: bkHelper.getSessionId() })
                .success(cb)
                .error(function ()
                {
                    console.log("failed to create shell", arguments);
                });
        },

        evaluate: function (code, modelOutput)
        {
            var deferred = Q.defer();
            var self = this;
            var progressObj = {
                type: "BeakerDisplay",
                innertype: "Progress",
                object: {
                    message: "submitting ...",
                    startTime: new Date().getTime()
                }
            };

            modelOutput.result = progressObj;

            $.ajax({
                type: "POST",
                datatype: "json",
                url: bkHelper.serverUrl(serviceBase + "/fsharp/evaluate"),
                data: { shellId: self.settings.shellID, code: code }
            }).done(function (ret)
            {
                if (ret.status === 0)
                {
                    if (ret.result.ContentType.indexOf("image/") === 0)
                    {
                        modelOutput.result =
                        {
                            type: "BeakerDisplay",
                            innertype: 'Html',
                            object: '<img src="data:' + ret.result.ContentType + ';base64,' + ret.result.Data + '" />'
                        };
                    }
                    else if (ret.result.ContentType === "table/grid")
                    {
                        modelOutput.result =
                        {
                            type: "TableDisplay",
                            tableDisplayModel: {
                                columnNames: ret.result.Data.Columns,
                                values: ret.result.Data.Rows
                            },
                            columnNames: ret.result.Data.Columns,
                            values: ret.result.Data.Rows
                        };
                    }
                    else if (ret.result.ContentType === "text/html")
                    {
                        modelOutput.result =
                        {
                            type: "BeakerDisplay",
                            innertype: 'Html',
                            object: ret.result.Data
                        };
                    }
                    else
                    {
                        modelOutput.result = ret.result.Data;
                    }
                }
                else
                {
                    modelOutput.result =
                    {
                        type: "BeakerDisplay",
                        innertype: "Error",
                        object: ret.result.Data
                    };
                }
                modelOutput.elapsedTime = new Date().getTime() - progressObj.object.startTime;
                bkHelper.refreshRootScope();
                deferred.resolve();
            });

            return deferred.promise;
        },

        autocomplete: function (code, cpos, cb)
        {
            var self = this;
            $.ajax({
                type: "POST",
                datatype: "json",
                url: bkHelper.serverUrl(serviceBase + "/fsharp/autocomplete"),
                data: { shellId: self.settings.shellID, code: code, caretPosition: cpos }
            }).done(function (x)
            {
                cb(x.Declarations);
            });
        },

        exit: function (cb)
        {
            var self = this;
            $.ajax({
                type: "POST",
                datatype: "json",
                url: bkHelper.serverUrl(serviceBase + "/fsharp/exit"),
                data: { shellId: self.settings.shellID }
            }).done(cb);
        },

        updateShell: function (cb)
        {
            bkHelper.httpPost(bkHelper.serverUrl(serviceBase + "/fsharp/setShellOptions"), {
                shellId: this.settings.shellID
            }).success(cb);
        },

        spec: {
            useIntellisense: { type: "settableBoolean", action: "updateShell", name: "Use Intellisense" }
        }
    };

    var shellReadyDeferred = bkHelper.newDeferred();
    var init = function ()
    {
        function loadedScripts()
        {
            bkHelper.locatePluginService(PLUGIN_NAME, {
                command: COMMAND,
                startedIndicator: "Successfully started server",
                waitfor: "Successfully started server",
                recordOutput: "true"
            }).success(function (ret)
            {
                serviceBase = ret;

                var FSharpShell = function (settings, doneCB)
                {
                    var self = this;
                    var setShellIdCB = function (id)
                    {
                        settings.shellID = id;
                        self.settings = settings;
                        function cb()
                        {
                            if (bkHelper.hasSessionId())
                            {
                                var initCode = "let beaker = new NamespaceClient(\"" + bkHelper.getSessionId() + "\")";
                                self.evaluate(initCode, {}).then(function ()
                                {
                                    if (doneCB)
                                    {
                                        doneCB(self);
                                    }
                                });
                            }
                            else
                            {
                                if (doneCB)
                                {
                                    doneCB(self);
                                }
                            }
                        }
                        self.updateShell(cb);
                    };

                    if (!settings.shellID)
                    {
                        settings.shellID = "";
                    }
                    this.newShell(settings.shellID, setShellIdCB);
                    this.perform = function (what)
                    {
                        var action = this.spec[what].action;
                        this[action]();
                    };

                    function applyIntellisense()
                    {
                        $('.CodeMirror').each(function (idx, div)
                        {
                            var editor = div.CodeMirror;
                            if (editor.options.mode === 'text/x-fsharp' && !editor.intellisense)
                            {
                                var intellisense = new CodeMirrorIntellisense(editor);
                                editor.intellisense = intellisense;
                                intellisense.addDeclarationTrigger({ keyCode: 190, type: 'up' }); // `.`
                                intellisense.addDeclarationTrigger({ keyCode: 32, ctrlKey: true, preventDefault: true, type: 'down' }); // `ctrl+space`
                                intellisense.addDeclarationTrigger({ keyCode: 191 }); // `/`
                                intellisense.addDeclarationTrigger({ keyCode: 220 }); // `\`
                                intellisense.addDeclarationTrigger({ keyCode: 222 }); // `"`
                                intellisense.addDeclarationTrigger({ keyCode: 222, shiftKey: true }); // `"`
                                intellisense.addMethodsTrigger({ keyCode: 57, shiftKey: true }); // `(`
                                intellisense.addMethodsTrigger({ keyCode: 48, shiftKey: true });// `)`
                                intellisense.onMethod(function (item, position)
                                {

                                });
                                intellisense.onDeclaration(function (item, position)
                                {
                                    var cursor = editor.doc.getCursor();
                                    var line = editor.getLine(cursor.line);
                                    var isSlash = item.keyCode === 191 || item.keyCode === 220;
                                    var isQuote = item.keyCode === 222;

                                    var isLoadOrRef = line.indexOf('#load') === 0
                                        || line.indexOf('#r') === 0;

                                    var isStartLoadOrRef = line === '#load "'
                                        || line === '#r "'
                                        || line === '#load @"'
                                        || line === '#r @"';

                                    if (isSlash && !isLoadOrRef)
                                    {
                                        return;
                                    }
                                    if (isQuote && !isStartLoadOrRef)
                                    {
                                        return;
                                    }

                                    var self = this;
                                    $.ajax({
                                        type: "POST",
                                        datatype: "json",
                                        url: bkHelper.serverUrl(serviceBase + "/fsharp/intellisense"),
                                        data: { shellId: settings.shellID, code: editor.getValue(), lineIndex: cursor.line, charIndex: cursor.ch }
                                    }).done(function (x)
                                    {
                                        if (x.declarations.length > 1)
                                        {
                                            var decls = intellisense.getDecls();
                                            intellisense.setDeclarations(x.declarations);
{'fraction_non_alphanumeric': 0.09347372162246213, 'fraction_numerical': 0.0045759473988182505, 'mean_word_length': 0.8594085577399636, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 96, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789122', 'n_tokens_mistral': 5234, 'n_tokens_neox': 4747, 'n_words': 1049}