Ejemplo n.º 1
0
        private async Task ProcessBrowsePromptWorker(IRSessionInteraction inter)
        {
            var frames = await Session.TracebackAsync();

            // If there's .doTrace(rtvs:::breakpoint) anywhere on the stack, we're inside the internal machinery
            // that triggered Browse> prompt when hitting a breakpoint. We need to step out of it until we're
            // back at the frame where the breakpoint was actually set, so that those internal frames do not show
            // on the call stack, and further stepping does not try to step through them.
            // Since browserSetDebug-based step out is not reliable in the presence of loops, we'll just keep
            // stepping over with "n" until we're all the way out. Every step will trigger a new prompt, and
            // we will come back to this method again.
            var doTraceFrame = frames.FirstOrDefault(frame => IsDoTrace(frame));

            if (doTraceFrame != null)
            {
                await inter.RespondAsync(Invariant($"n\n"));

                return;
            }

            IReadOnlyCollection <RBreakpoint> breakpointsHit = null;
            var lastFrame = frames.LastOrDefault();

            if (lastFrame != null)
            {
                // Report breakpoints first, so that by the time step completion is reported, all actions associated
                // with breakpoints (e.g. printing messages for tracepoints) have already been completed.
                if (lastFrame.FileName != null && lastFrame.LineNumber != null)
                {
                    var         location = new RSourceLocation(lastFrame.FileName, lastFrame.LineNumber.Value);
                    RBreakpoint bp;
                    if (_breakpoints.TryGetValue(location, out bp))
                    {
                        bp.RaiseBreakpointHit();
                        breakpointsHit = Enumerable.Repeat(bp, bp.UseCount).ToArray();
                    }
                }
            }

            bool isStepCompleted = false;

            if (_stepTcs != null)
            {
                var stepTcs = _stepTcs;
                _stepTcs = null;
                stepTcs.TrySetResult(breakpointsHit == null || breakpointsHit.Count == 0);
                isStepCompleted = true;
            }

            EventHandler <RBrowseEventArgs> browse;

            lock (_browseLock) {
                browse = _browse;
            }

            var eventArgs = new RBrowseEventArgs(inter, isStepCompleted, breakpointsHit);

            _currentBrowseEventArgs = eventArgs;
            browse?.Invoke(this, eventArgs);
        }
Ejemplo n.º 2
0
 public Task CancelAllAsync(CancellationToken сancellationToken = default(CancellationToken)) {
     if (_inter != null) {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(_inter.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         _inter = null;
     }
     return Task.CompletedTask;
 }
Ejemplo n.º 3
0
        public RCodeBlockTest()
        {
            _shell   = TestCoreShell.CreateSubstitute();
            _session = _shell.SetupSessionSubstitute();

            _interaction = Substitute.For <IRSessionInteraction>();
            _session.BeginInteractionAsync(Arg.Any <bool>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(_interaction));
        }
Ejemplo n.º 4
0
 public Task CancelAllAsync(CancellationToken сancellationToken = default(CancellationToken))
 {
     if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(_inter.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Ejemplo n.º 5
0
 public Task CancelAllAsync() {
     if (_eval != null) {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_eval.Contexts, Prompt, 4096, true));
         _eval = null;
     }
     else if (_inter != null) {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_inter.Contexts, Prompt, 4096, true));
         _inter = null;
     }
     return Task.CompletedTask;
 }
Ejemplo n.º 6
0
 public Task CancelAllAsync()
 {
     if (Evaluation != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(Evaluation.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         Evaluation = null;
     }
     else if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RAfterRequestEventArgs(_inter.Contexts, Prompt, string.Empty, addToHistory: true, isVisible: true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Ejemplo n.º 7
0
 public Task CancelAllAsync()
 {
     if (_eval != null)
     {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_eval.Contexts, Prompt, 4096, true));
         _eval = null;
     }
     else if (_inter != null)
     {
         AfterRequest?.Invoke(this, new RRequestEventArgs(_inter.Contexts, Prompt, 4096, true));
         _inter = null;
     }
     return(Task.CompletedTask);
 }
Ejemplo n.º 8
0
        private async Task ShowHelpOnCurrentAsync(string prefix, string item)
        {
            try {
                using (IRSessionEvaluation evaluation = await _workflow.RSession.BeginEvaluationAsync()) {
                    REvaluationResult result = await evaluation.EvaluateAsync(prefix + item, REvaluationKind.Normal);

                    if (result.ParseStatus == RParseStatus.OK &&
                        string.IsNullOrEmpty(result.Error))
                    {
                        if (string.IsNullOrEmpty(result.StringResult) ||
                            result.StringResult == "NA")
                        {
                            prefix = "??";
                        }
                    }
                    else
                    {
                        // Parsing or other errors, bail out
                        Debug.Assert(false,
                                     string.Format(CultureInfo.InvariantCulture,
                                                   "Evaluation of help expression failed. Error: {0}, Status: {1}", result.Error, result.ParseStatus));
                    }
                }
            } catch (RException) {
            } catch (OperationCanceledException) {
            }

            // Now actually request the help. First call may throw since 'starting help server...'
            // message in REPL is actually an error (comes in red) so we'll get RException.
            int retries = 0;

            while (retries < 3)
            {
                using (IRSessionInteraction interaction = await _workflow.RSession.BeginInteractionAsync(isVisible: false)) {
                    try {
                        await interaction.RespondAsync(prefix + item + Environment.NewLine);
                    } catch (RException ex) {
                        if ((uint)ex.HResult == 0x80131500)
                        {
                            // Typically 'starting help server...' so try again
                            retries++;
                            continue;
                        }
                    } catch (OperationCanceledException) { }
                }

                break;
            }
        }
Ejemplo n.º 9
0
        public static Task PreviousPlot(this IRSessionInteraction evaluation)
        {
            var script = "rtvs:::graphics.ide.previousplot()\n";

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 10
0
        public static Task ResizePlot(this IRSessionInteraction evaluation, int width, int height)
        {
            var script = string.Format("rtvs:::graphics.ide.resize({0}, {1})\n", width, height);

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 11
0
        public static Task InstallPackageAsync(this IRSessionInteraction interaction, string name)
        {
            var script = Invariant($"install.packages({name.ToRStringLiteral()})\n");

            return(interaction.RespondAsync(script));
        }
Ejemplo n.º 12
0
        public static Task UnloadPackageAsync(this IRSessionInteraction interaction, string name)
        {
            var script = Invariant($"unloadNamespace({name.ToRStringLiteral()})\n");

            return(interaction.RespondAsync(script));
        }
Ejemplo n.º 13
0
        public static Task LoadPackageAsync(this IRSessionInteraction interaction, string name, string libraryPath)
        {
            var script = Invariant($"library({name.ToRStringLiteral()}, lib.loc={libraryPath.ToRPath().ToRStringLiteral()})\n");

            return(interaction.RespondAsync(script));
        }
Ejemplo n.º 14
0
        public static Task ResizePlotAsync(this IRSessionInteraction evaluation, int width, int height, int resolution)
        {
            var script = Invariant($"rtvs:::graphics.ide.resize({width}, {height}, {resolution})\n");

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 15
0
 public Task<IRSessionInteraction> BeginInteractionAsync(bool isVisible = true, CancellationToken cancellationToken = default (CancellationToken)) {
     _inter = new RSessionInteractionMock();
     BeforeRequest?.Invoke(this, new RRequestEventArgs(_inter.Contexts, Prompt, 4096, true));
     return Task.FromResult(_inter);
 }
Ejemplo n.º 16
0
 public static Task LoadPackageAsync(this IRSessionInteraction interaction, string name) {
     var script = Invariant($"library({name.ToRStringLiteral()})\n");
     return interaction.RespondAsync(script);
 }
Ejemplo n.º 17
0
 public Task <IRSessionInteraction> BeginInteractionAsync(bool isVisible = true, CancellationToken cancellationToken = default(CancellationToken))
 {
     _inter = new RSessionInteractionMock();
     BeforeRequest?.Invoke(this, new RRequestEventArgs(_inter.Contexts, Prompt, 4096, true));
     return(Task.FromResult(_inter));
 }
Ejemplo n.º 18
0
 public async Task Source(IRSession session, bool debug = true)
 {
     using (IRSessionInteraction eval = await session.BeginInteractionAsync()) {
         await eval.RespondAsync($"{(debug ? "rtvs::debug_source" : "source")}({FilePath.ToRStringLiteral()})" + Environment.NewLine);
     }
 }
Ejemplo n.º 19
0
        public static Task NextPlotAsync(this IRSessionInteraction evaluation)
        {
            var script = "rtvs:::graphics.ide.nextplot()\n";

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 20
0
        public static Task ClearPlotHistory(this IRSessionInteraction evaluation)
        {
            var script = "rtvs:::graphics.ide.clearplots()\n";

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 21
0
        public static Task RemoveCurrentPlot(this IRSessionInteraction evaluation)
        {
            var script = "rtvs:::graphics.ide.removeplot()\n";

            return(evaluation.RespondAsync(script));
        }
Ejemplo n.º 22
0
 public static Task Quit(this IRSessionInteraction interaction)
 {
     return(interaction.RespondAsync("q()\n"));
 }
Ejemplo n.º 23
0
        public static Task UninstallPackageAsync(this IRSessionInteraction interaction, string name, string libraryPath)
        {
            var script = Invariant($"remove.packages({name.ToRStringLiteral()}, lib={libraryPath.ToRPath().ToRStringLiteral()})\n");

            return(interaction.RespondAsync(script));
        }
Ejemplo n.º 24
0
        private async Task ProcessBrowsePromptWorker(IRSessionInteraction inter) {
            var frames = await Session.TracebackAsync();

            // If there's .doTrace(rtvs:::breakpoint) anywhere on the stack, we're inside the internal machinery
            // that triggered Browse> prompt when hitting a breakpoint. We need to step out of it until we're
            // back at the frame where the breakpoint was actually set, so that those internal frames do not show
            // on the call stack, and further stepping does not try to step through them. 
            // Since browserSetDebug-based step out is not reliable in the presence of loops, we'll just keep
            // stepping over with "n" until we're all the way out. Every step will trigger a new prompt, and
            // we will come back to this method again.
            var doTraceFrame = frames.FirstOrDefault(frame => IsDoTrace(frame));
            if (doTraceFrame != null) {
                await inter.RespondAsync(Invariant($"n\n"));
                return;
            }

            IReadOnlyCollection<RBreakpoint> breakpointsHit = null;
            var lastFrame = frames.LastOrDefault();
            if (lastFrame != null) {
                // Report breakpoints first, so that by the time step completion is reported, all actions associated
                // with breakpoints (e.g. printing messages for tracepoints) have already been completed.
                if (lastFrame.FileName != null && lastFrame.LineNumber != null) {
                    var location = new RSourceLocation(lastFrame.FileName, lastFrame.LineNumber.Value);
                    RBreakpoint bp;
                    if (_breakpoints.TryGetValue(location, out bp)) {
                        bp.RaiseBreakpointHit();
                        breakpointsHit = Enumerable.Repeat(bp, bp.UseCount).ToArray();
                    }
                }
            }

            bool isStepCompleted = false;
            if (_stepTcs != null) {
                var stepTcs = _stepTcs;
                _stepTcs = null;
                stepTcs.TrySetResult(breakpointsHit == null || breakpointsHit.Count == 0);
                isStepCompleted = true;
            }

            EventHandler<RBrowseEventArgs> browse;
            lock (_browseLock) {
                browse = _browse;
           }

            var eventArgs = new RBrowseEventArgs(inter, isStepCompleted, breakpointsHit);
            _currentBrowseEventArgs = eventArgs;
            browse?.Invoke(this, eventArgs);
        }