Inheritance: IInteractiveEvaluator
Esempio n. 1
0
        public async Task <IInteractiveWindowVisualComponent> GetOrCreateVisualComponent(IInteractiveWindowComponentContainerFactory componentContainerFactory, int instanceId = 0)
        {
            Shell.AssertIsOnMainThread();

            if (ActiveWindow != null)
            {
                // Right now only one instance of interactive window is allowed
                if (instanceId != 0)
                {
                    throw new InvalidOperationException("Right now only one instance of interactive window is allowed");
                }

                return(ActiveWindow);
            }

            var evaluator = new RInteractiveEvaluator(RSession, History, Connections, Shell, _settings);

            ActiveWindow = componentContainerFactory.Create(instanceId, evaluator);
            var interactiveWindow = ActiveWindow.InteractiveWindow;

            interactiveWindow.TextView.Closed += (_, __) => evaluator.Dispose();
            _operations.InteractiveWindow      = interactiveWindow;
            await interactiveWindow.InitializeAsync();

            ActiveWindow.Container.UpdateCommandStatus(true);
            return(ActiveWindow);
        }
        public async Task EvaluatorTest01() {
            using (new VsRHostScript()) {
                var session = _sessionProvider.GetInteractiveWindowRSession();
                using (var eval = new RInteractiveEvaluator(session, RHistoryStubFactory.CreateDefault(), VsAppShell.Current, RToolsSettings.Current)) {
                    var tb = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
                    var tv = new WpfTextViewMock(tb);

                    var iwm = new InteractiveWindowMock(tv);
                    eval.CurrentWindow = iwm;

                    var result = await eval.InitializeAsync();
                    result.Should().Be(ExecutionResult.Success);
                    session.IsHostRunning.Should().BeTrue();

                    eval.CanExecuteCode("x <-").Should().BeFalse();
                    eval.CanExecuteCode("(()").Should().BeFalse();
                    eval.CanExecuteCode("a *(b+c)").Should().BeTrue();

                    VsRHostScript.DoIdle(300);

                    result = await eval.ExecuteCodeAsync(new string(new char[10000])+"\r\n");
                    result.Should().Be(ExecutionResult.Failure);
                    string text = tb.CurrentSnapshot.GetText();
                    text.Should().Contain(string.Format(Microsoft.R.Components.Resources.InputIsTooLong, 4096));
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("z <- '電話帳 全米のお'\n");
                    result.Should().Be(ExecutionResult.Success);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("z" + Environment.NewLine);
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"電話帳 全米のお\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("Encoding(z)\n");
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"UTF-8\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("x <- c(1:10)\n");
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    await eval.ResetAsync(initialize: false);
                    text = tb.CurrentSnapshot.GetText();
                    text.Should().StartWith(Microsoft.R.Components.Resources.MicrosoftRHostStopping);
                }
            }
        }
        private async Task CreateVisualComponentAsync(int instanceId)
        {
            var factory   = Services.GetService <IInteractiveWindowComponentContainerFactory>();
            var evaluator = new RInteractiveEvaluator(RSessions, RSession, History, Connections, Services, _settings, Console);

            var window            = factory.Create(instanceId, evaluator, RSessions);
            var interactiveWindow = window.InteractiveWindow;

            interactiveWindow.TextView.Closed += (_, __) => evaluator.Dispose();
            _operations.InteractiveWindow      = interactiveWindow;

            if (!RSessions.HasBroker)
            {
                var connectedToBroker = await Connections.TryConnectToPreviouslyUsedAsync();

                if (!connectedToBroker)
                {
                    var showConnectionsWindow = Connections.RecentConnections.Any();
                    if (!showConnectionsWindow)
                    {
                        var message = Resources.NoLocalR.FormatInvariant(Environment.NewLine + Environment.NewLine,
                                                                         Environment.NewLine);
                        var ui = Services.UI();
                        showConnectionsWindow = ui.ShowMessage(message, MessageButtons.YesNo) == MessageButtons.No;
                    }

                    if (!showConnectionsWindow)
                    {
                        var installer = Services.GetService <IMicrosoftRClientInstaller>();
                        installer.LaunchRClientSetup(Services);
                    }
                    else
                    {
                        var toolWindows = Services.GetService <IRInteractiveWorkflowToolWindowService>();
                        toolWindows.Connections().Show(focus: false, immediate: false);
                    }
                }
            }

            await interactiveWindow.InitializeAsync();

            RSession.RestartOnBrokerSwitch = true;

            ActiveWindow = window;
            ActiveWindow.Container.UpdateCommandStatus(true);
            _visualComponentTcs.SetResult(ActiveWindow);
            ActiveWindowChanged?.Invoke(this, new ActiveWindowChangedEventArgs(window));
        }
        public async Task EvaluatorTest02() {
            using (new VsRHostScript()) {
                var session = _sessionProvider.GetInteractiveWindowRSession();
                using (var eval = new RInteractiveEvaluator(session, RHistoryStubFactory.CreateDefault(), VsAppShell.Current, RToolsSettings.Current)) {
                    var tb = new TextBufferMock(string.Empty, RContentTypeDefinition.ContentType);
                    var tv = new WpfTextViewMock(tb);

                    var iwm = new InteractiveWindowMock(tv);
                    eval.CurrentWindow = iwm;

                    var result = await eval.InitializeAsync();
                    result.Should().Be(ExecutionResult.Success);
                    session.IsHostRunning.Should().BeTrue();

                    VsRHostScript.DoIdle(1000);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("w <- dQuote('text')" + Environment.NewLine);
                    result.Should().Be(ExecutionResult.Success);
                    var text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("w" + Environment.NewLine);
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"“text”\"");
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("e <- dQuote('абвг')" + Environment.NewLine);
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.Should().Be(string.Empty);
                    tb.Clear();

                    result = await eval.ExecuteCodeAsync("e" + Environment.NewLine);
                    result.Should().Be(ExecutionResult.Success);
                    text = tb.CurrentSnapshot.GetText();
                    text.TrimEnd().Should().Be("[1] \"“абвг”\"");
                    tb.Clear();
                }
            }
        }
Esempio n. 5
0
        private async Task CreateVisualComponentAsync(int instanceId) {
            var factory = Shell.ExportProvider.GetExportedValue<IInteractiveWindowComponentContainerFactory>();
            var evaluator = new RInteractiveEvaluator(RSessions, RSession, History, Connections, Shell, _settings, new InteractiveWindowConsole(this));

            var window = factory.Create(instanceId, evaluator, RSessions);
            var interactiveWindow = window.InteractiveWindow;
            interactiveWindow.TextView.Closed += (_, __) => evaluator.Dispose();
            _operations.InteractiveWindow = interactiveWindow;

            if (!RSessions.HasBroker) {
                var connectedToBroker = await Connections.TryConnectToPreviouslyUsedAsync();
                if (!connectedToBroker) {
                    var showConnectionsWindow = Connections.RecentConnections.Any();
                    if (!showConnectionsWindow){
                        var message = Resources.NoLocalR.FormatInvariant(Environment.NewLine + Environment.NewLine, Environment.NewLine);
                        showConnectionsWindow = Shell.ShowMessage(message, MessageButtons.YesNo) == MessageButtons.Yes;
                    }

                    if (!showConnectionsWindow) {
                        var installer = Shell.ExportProvider.GetExportedValue<IMicrosoftRClientInstaller>();
                        installer.LaunchRClientSetup(Shell);
                    } else {
                        Connections.GetOrCreateVisualComponent().Container.Show(focus: false, immediate: false);
                    }
                }
            }

            await interactiveWindow.InitializeAsync();

            ActiveWindow = window;
            ActiveWindow.Container.UpdateCommandStatus(true);
            _visualComponentTcs.SetResult(ActiveWindow);
            ActiveWindowChanged?.Invoke(this, new ActiveWindowChangedEventArgs(window));
        }