Exemple #1
0
        static void RenderResult(
            CodeCellState codeCellState,
            Evaluation result,
            bool isResultAnExpression)
        {
            if (codeCellState == null)
            {
                throw new ArgumentNullException(nameof(codeCellState));
            }

            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            var cultureInfo = CultureInfo.CurrentCulture;

            try {
                cultureInfo = CultureInfo.GetCultureInfo(result.CultureLCID);
            } catch (Exception e) when(
                e is CultureNotFoundException ||
                e is ArgumentOutOfRangeException)
            {
                Log.Error(TAG, $"Invalid CultureInfo LCID: {result.CultureLCID}");
            }

            codeCellState.View.EvaluationDuration = result.EvaluationDuration;

            if (result.Exception != null)
            {
                codeCellState.View.RenderResult(
                    cultureInfo,
                    EvaluationService.FilterException(result.Exception),
                    result.ResultHandling);
            }
            else if (!result.Interrupted && result.Result != null || isResultAnExpression)
            {
                codeCellState.View.RenderResult(
                    cultureInfo,
                    result.Result,
                    result.ResultHandling);
            }
        }
Exemple #2
0
        async Task <CodeCellEvaluationStatus> CoreEvaluateCodeCellAsync(
            CodeCellState codeCellState,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken = ClientSession.CancellationToken.LinkWith(cancellationToken);

            if (!ClientSession.Agent.IsConnected || ClientSession.CompilationWorkspace == null)
            {
                codeCellState.View.IsEvaluating        = false;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new Diagnostic(
                                                        DiagnosticSeverity.Error,
                                                        "Cannot evaluate: not connected to agent."));
                return(CodeCellEvaluationStatus.Disconnected);
            }

            CodeAnalysis.Compilation   compilation = null;
            IReadOnlyList <Diagnostic> diagnostics = null;
            ExceptionNode exception = null;
            bool          agentTerminatedWhileEvaluating = false;

            try {
                compilation = await ClientSession.CompilationWorkspace.EmitCellCompilationAsync(
                    codeCellState.CodeCellId,
                    new EvaluationEnvironment (ClientSession.WorkingDirectory),
                    cancellationToken);

                diagnostics = await ClientSession.CompilationWorkspace.GetCellDiagnosticsAsync(
                    codeCellState.CodeCellId,
                    cancellationToken);

                var integrationAssemblies = compilation
                                            .References
                                            .Where(ra => ra.HasIntegration)
                                            .ToArray();
                if (integrationAssemblies.Length > 0)
                {
                    await ClientSession.Agent.Api.LoadAssembliesAsync(
                        EvaluationContextId,
                        integrationAssemblies);
                }
            } catch (Exception e) {
                exception = ExceptionNode.Create(e);
            }

            var hasErrorDiagnostics = codeCellState.View.HasErrorDiagnostics = diagnostics
                                                                               .Any(d => d.Severity == DiagnosticSeverity.Error);

            foreach (var diagnostic in diagnostics)
            {
                codeCellState.View.RenderDiagnostic(diagnostic);
            }

            try {
                if (compilation != null)
                {
                    codeCellState.IsResultAnExpression = compilation.IsResultAnExpression;

                    await ClientSession.Agent.Api.EvaluateAsync(
                        compilation,
                        cancellationToken);
                }
            } catch (XipErrorMessageException e) {
                exception = e.XipErrorMessage.Exception;
            } catch (Exception e) {
                Log.Error(TAG, "marking agent as terminated", e);
                agentTerminatedWhileEvaluating         = true;
                codeCellState.View.HasErrorDiagnostics = true;
                codeCellState.View.RenderDiagnostic(new Diagnostic(
                                                        DiagnosticSeverity.Error,
                                                        Catalog.GetString(
                                                            "The application terminated during evaluation of this cell. " +
                                                            "Run this cell manually to try again.")));
            }

            codeCellState.View.IsEvaluating = false;

            CodeCellEvaluationStatus evaluationStatus;

            if (exception != null)
            {
                codeCellState.View.RenderResult(
                    CultureInfo.CurrentCulture,
                    EvaluationService.FilterException(exception),
                    EvaluationResultHandling.Replace);
                evaluationStatus = CodeCellEvaluationStatus.EvaluationException;
            }
            else if (hasErrorDiagnostics)
            {
                return(CodeCellEvaluationStatus.ErrorDiagnostic);
            }
            else if (agentTerminatedWhileEvaluating)
            {
                evaluationStatus = CodeCellEvaluationStatus.Disconnected;
            }
            else
            {
                evaluationStatus = CodeCellEvaluationStatus.Success;
            }

            if (ClientSession.SessionKind != ClientSessionKind.Workbook)
            {
                codeCellState.Freeze();
            }

            codeCellState.NotifyEvaluated(agentTerminatedWhileEvaluating);
            return(evaluationStatus);
        }