protected override void Run()
            {
                if (RunButton.Disabled || string.IsNullOrWhiteSpace(InputBar.Text))
                {
                    return;
                }

                RunButton.Disabled = true;

                var msg = _client._netManager.CreateNetMessage <MsgScriptEval>();

                msg.ScriptSession = _session;
                msg.Code          = _lastEnteredText = InputBar.Text;

                _client._netManager.ClientSendMessage(msg);

                InputBar.Clear();
            }
Пример #2
0
        protected override async void Run()
        {
            var code = InputBar.Text;

            InputBar.Clear();

            // Remove > or . at the end of the output panel.
            OutputPanel.RemoveEntry(^ 1);

            _inputBuffer.AppendLine(code);
            _linesEntered += 1;

            var tree = SyntaxFactory.ParseSyntaxTree(SourceText.From(_inputBuffer.ToString()), ScriptInstanceShared.ParseOptions);

            if (!SyntaxFactory.IsCompleteSubmission(tree))
            {
                if (_linesEntered == 1)
                {
                    OutputPanel.AddText($"> {code}");
                }
                else
                {
                    OutputPanel.AddText($". {code}");
                }
                OutputPanel.AddText(".");
                return;
            }

            code = _inputBuffer.ToString().Trim();

            // Remove echo of partial submission from the output panel.
            for (var i = 1; i < _linesEntered; i++)
            {
                OutputPanel.RemoveEntry(^ 1);
            }

            _inputBuffer.Clear();
            _linesEntered = 0;

            Script newScript;

            if (_state != null)
            {
                newScript = _state.Script.ContinueWith(code);
            }
            else
            {
                var options = ScriptInstanceShared.GetScriptOptions(_reflectionManager);
                newScript = CSharpScript.Create(code, options, typeof(ScriptGlobals));
            }

            // Compile ahead of time so that we can do syntax highlighting correctly for the echo.
            newScript.Compile();

            // Echo entered script.
            var echoMessage = new FormattedMessage();

            echoMessage.PushColor(Color.FromHex("#D4D4D4"));
            echoMessage.AddText("> ");
            ScriptInstanceShared.AddWithSyntaxHighlighting(newScript, echoMessage, code, _highlightWorkspace);

            OutputPanel.AddMessage(echoMessage);

            try
            {
                if (_state != null)
                {
                    _state = await newScript.RunFromAsync(_state, _ => true);
                }
                else
                {
                    _state = await newScript.RunAsync(_globals, _ => true);
                }
            }
            catch (CompilationErrorException e)
            {
                var msg = new FormattedMessage();

                msg.PushColor(Color.Crimson);

                foreach (var diagnostic in e.Diagnostics)
                {
                    msg.AddText(diagnostic.ToString());
                    msg.AddText("\n");
                }

                OutputPanel.AddMessage(msg);
                OutputPanel.AddText(">");
                return;
            }

            if (_state.Exception != null)
            {
                var msg = new FormattedMessage();
                msg.PushColor(Color.Crimson);
                msg.AddText(CSharpObjectFormatter.Instance.FormatException(_state.Exception));
                OutputPanel.AddMessage(msg);
            }
            else if (ScriptInstanceShared.HasReturnValue(newScript))
            {
                var msg = new FormattedMessage();
                msg.AddText(CSharpObjectFormatter.Instance.FormatObject(_state.ReturnValue));
                OutputPanel.AddMessage(msg);
            }

            OutputPanel.AddText(">");
        }