Example #1
0
 void IScriptGlobalsHelper.Write(ScriptGlobals globals, List <ColorAndText> list)
 {
     if (!IsCurrentScript(globals))
     {
         return;
     }
     ReplEditor.OutputPrint(list.Select(a => new ColorAndText(a.Color, a.Text)));
 }
Example #2
0
 void IScriptGlobalsHelper.Print(ScriptGlobals globals, object color, Exception ex)
 {
     if (!IsCurrentScript(globals))
     {
         return;
     }
     ReplEditor.OutputPrint(Format(ex), color);
 }
Example #3
0
 void IScriptGlobalsHelper.Print(ScriptGlobals globals, object color, string text)
 {
     if (!IsCurrentScript(globals))
     {
         return;
     }
     ReplEditor.OutputPrint(text, color);
 }
Example #4
0
        void ObjectOutput(object color, PrintOptionsImpl printOptions, object value, bool startOnNewLine = false)
        {
            var writable = GetOutputWritable(printOptions, value);

            if (writable != null)
            {
                writable.WriteTo(OutputWriter.Create(this, startOnNewLine));
            }
            else
            {
                ReplEditor.OutputPrint(Format(value, printOptions.RoslynPrintOptions), color, startOnNewLine);
            }
        }
Example #5
0
 public void ExecuteCommand(string input)
 {
     try {
         if (!ExecuteCommandInternal(input))
         {
             CommandExecuted();
         }
     }
     catch (Exception ex) {
         ReplEditor.OutputPrint(ex.ToString(), BoxedTextColor.Error, true);
         CommandExecuted();
     }
 }
Example #6
0
        bool ExecuteCommandInternal(string input)
        {
            Debug.Assert(execState != null && !execState.IsInitializing);
            if (execState == null || execState.IsInitializing)
            {
                return(true);
            }
            lock (lockObj) {
                Debug.Assert(execState.ExecTask == null && !execState.Executing);
                if (execState.ExecTask != null || execState.Executing)
                {
                    return(true);
                }
                execState.Executing = true;
            }

            try {
                var scState = ParseScriptCommand(input);
                if (scState != null)
                {
                    if (execState != null)
                    {
                        lock (lockObj)
                            execState.Executing = false;
                    }
                    scState.Command.Execute(this, scState.Arguments);
                    bool isReset = scState.Command is ResetCommand;
                    if (!isReset)
                    {
                        CommandExecuted();
                    }
                    return(true);
                }

                var oldState = execState;

                var taskSched = TaskScheduler.FromCurrentSynchronizationContext();
                Task.Run(() => {
                    oldState.CancellationToken.ThrowIfCancellationRequested();

                    var opts     = oldState.ScriptOptions.WithReferences(Array.Empty <MetadataReference>()).WithImports(Array.Empty <string>());
                    var execTask = oldState.ScriptState.ContinueWithAsync(input, opts, oldState.CancellationToken);
                    oldState.CancellationToken.ThrowIfCancellationRequested();
                    lock (lockObj) {
                        if (oldState == execState)
                        {
                            oldState.ExecTask = execTask;
                        }
                    }
                    execTask.ContinueWith(t => {
                        var ex = t.Exception;
                        bool isActive;
                        lock (lockObj) {
                            isActive = oldState == execState;
                            if (isActive)
                            {
                                oldState.ExecTask = null;
                            }
                        }
                        if (isActive)
                        {
                            try {
                                if (ex != null)
                                {
                                    ReplEditor.OutputPrint(Format(ex.InnerException), BoxedTextColor.Error, true);
                                }

                                if (!t.IsCanceled && !t.IsFaulted)
                                {
                                    oldState.ScriptState = t.Result;
                                    var val = t.Result.ReturnValue;
                                    if (val != null)
                                    {
                                        ObjectOutputLine(BoxedTextColor.ReplOutputText, oldState.Globals.PrintOptionsImpl, val, true);
                                    }
                                }
                            }
                            finally {
                                CommandExecuted();
                            }
                        }
                    }, CancellationToken.None, TaskContinuationOptions.None, taskSched);
                })
                .ContinueWith(t => {
                    if (execState != null)
                    {
                        lock (lockObj)
                            execState.Executing = false;
                    }
                    var innerEx = t.Exception?.InnerException;
                    if (innerEx is CompilationErrorException cee)
                    {
                        PrintDiagnostics(cee.Diagnostics);
                        CommandExecuted();
                    }
                    else if (innerEx is OperationCanceledException)
                    {
                        CommandExecuted();
                    }
                    else
                    {
                        var ex = t.Exception;
                        if (ex != null)
                        {
                            ReplEditor.OutputPrint(ex.ToString(), BoxedTextColor.Error, true);
                            CommandExecuted();
                        }
                    }
                }, CancellationToken.None, TaskContinuationOptions.None, taskSched);

                return(true);
            }
            catch (Exception ex) {
                if (execState != null)
                {
                    lock (lockObj)
                        execState.Executing = false;
                }
                ReplEditor.OutputPrintLine($"Error executing script:\n\n{ex}", BoxedTextColor.Error, true);
                return(false);
            }
        }