Exemplo n.º 1
0
        private async Task SetRootModelAsync(REnvironment env)
        {
            await TaskUtilities.SwitchToBackgroundThread();

            var debugSession = await GetDebugSessionAsync();

            var frames = await debugSession.GetStackFramesAsync();

            var frame = frames.FirstOrDefault(f => f.Index == 0);

            if (frame != null)
            {
                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;
                DebugEvaluationResult result = await frame.EvaluateAsync(
                    GetExpression(env),
                    fields);

                var wrapper       = new EvaluationWrapper(result);
                var rootNodeModel = new VariableNode(_settings, wrapper);

                VsAppShell.Current.DispatchOnUIThread(
                    () => {
                    _rootNode.Model = rootNodeModel;
                });
            }
        }
Exemplo n.º 2
0
        private async Task UpdateList()
        {
            if (_updating)
            {
                return;
            }

            try {
                _updating = true;
                // May be null in tests
                var sessionProvider = EditorShell.Current.ExportProvider.GetExportedValueOrDefault <IRSessionProvider>();
                var session         = sessionProvider.GetOrCreate(GuidList.InteractiveWindowRSessionGuid, null);
                if (session.IsHostRunning)
                {
                    var debugSessionProvider = EditorShell.Current.ExportProvider.GetExportedValueOrDefault <IDebugSessionProvider>();
                    if (debugSessionProvider != null)
                    {
                        var debugSession = await debugSessionProvider.GetDebugSessionAsync(Session);

                        if (debugSession != null)
                        {
                            var stackFrames = await debugSession.GetStackFramesAsync();

                            var globalStackFrame = stackFrames.FirstOrDefault(s => s.IsGlobal);
                            if (globalStackFrame != null)
                            {
                                const DebugEvaluationResultFields fields =
                                    DebugEvaluationResultFields.Expression |
                                    DebugEvaluationResultFields.Kind |
                                    DebugEvaluationResultFields.ReprStr |
                                    DebugEvaluationResultFields.TypeName |
                                    DebugEvaluationResultFields.Classes |
                                    DebugEvaluationResultFields.Length |
                                    DebugEvaluationResultFields.SlotCount |
                                    DebugEvaluationResultFields.AttrCount |
                                    DebugEvaluationResultFields.Dim |
                                    DebugEvaluationResultFields.Flags;
                                DebugEvaluationResult evaluation = await globalStackFrame.EvaluateAsync("base::environment()", "Global Environment", fields);

                                var e = new RSessionDataObject(evaluation);  // root level doesn't truncate children and return every variables

                                _topLevelVariables.Clear();

                                var children = await e.GetChildrenAsync();

                                if (children != null)
                                {
                                    foreach (var x in children)
                                    {
                                        _topLevelVariables[x.Name] = x; // TODO: BUGBUG: this doesn't address removed variables
                                    }
                                }
                            }
                        }
                    }
                }
            } finally {
                _updating = false;
            }
        }
Exemplo n.º 3
0
        public AD7Property(AD7StackFrame stackFrame, DebugEvaluationResult result, bool isSynthetic = false, bool isFrameEnvironment = false) {
            StackFrame = stackFrame;
            EvaluationResult = result;
            IsSynthetic = isSynthetic;
            IsFrameEnvironment = isFrameEnvironment;

            _children = Lazy.Create(CreateChildren);
            _reprToString = Lazy.Create(CreateReprToString);
        }
Exemplo n.º 4
0
        public AD7Property(AD7StackFrame stackFrame, DebugEvaluationResult result, bool isSynthetic = false, bool isFrameEnvironment = false)
        {
            StackFrame         = stackFrame;
            EvaluationResult   = result;
            IsSynthetic        = isSynthetic;
            IsFrameEnvironment = isFrameEnvironment;

            _children     = Lazy.Create(CreateChildren);
            _reprToString = Lazy.Create(CreateReprToString);
        }
Exemplo n.º 5
0
        public void ShowDataGrid(DebugEvaluationResult evaluationResult)
        {
            var wrapper = new EvaluationWrapper(evaluationResult);

            if (!wrapper.CanShowDetail)
            {
                throw new InvalidOperationException("Cannot show data grid on evaluation result " + evaluationResult);
            }
            wrapper.ShowDetailCommand.Execute(null);
        }
Exemplo n.º 6
0
        public static async Task OpenDataCsvApp(DebugEvaluationResult result)
        {
            if (Interlocked.Exchange(ref _busy, 1) > 0)
            {
                return;
            }

            var workflowProvider = VsAppShell.Current.ExportProvider.GetExportedValue <IRInteractiveWorkflowProvider>();
            var workflow         = workflowProvider.GetOrCreate();
            var session          = workflow.RSession;

            var folder = GetTempCsvFilesFolder();

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            var variableName = result.Name ?? _variableNameReplacement;
            var csvFileName  = MakeCsvFileName(variableName);
            var file         = ProjectUtilities.GetUniqueFileName(folder, csvFileName, "csv", appendUnderscore: true);
            var rfile        = file.Replace('\\', '/');

            string currentStatusText = string.Empty;
            await VsAppShell.Current.DispatchOnMainThreadAsync(() => {
                var statusBar = VsAppShell.Current.GetGlobalService <IVsStatusbar>(typeof(SVsStatusbar));
                statusBar.GetText(out currentStatusText);
            });

            try {
                await SetStatusTextAsync(Resources.Status_WritingCSV);

                using (var e = await session.BeginEvaluationAsync()) {
                    await e.EvaluateAsync($"write.csv({result.Expression}, file='{rfile}')", REvaluationKind.Normal);
                }

                if (File.Exists(file))
                {
                    Task.Run(() => {
                        try {
                            Process.Start(file);
                        } catch (Win32Exception ex) {
                            ShowErrorMessage(ex.Message);
                        } catch (FileNotFoundException ex) {
                            ShowErrorMessage(ex.Message);
                        }
                    }).DoNotWait();
                }
            } finally {
                await SetStatusTextAsync(currentStatusText);
            }

            Interlocked.Exchange(ref _busy, 0);
        }
Exemplo n.º 7
0
        private void OnGlobalEnvironmentEvaluation(DebugEvaluationResult result)
        {
            var wrapper = new EvaluationWrapper(result);

            var rootNodeModel = new VariableNode(_settings, wrapper);

            VsAppShell.Current.DispatchOnUIThread(
                () => {
                EnvironmentName.Text = GlobalEnvironmentName;
                _rootNode.Model      = rootNodeModel;
            });
        }
Exemplo n.º 8
0
        protected int?GetMaxChildrenCount(DebugEvaluationResult parent)
        {
            var value = parent as DebugValueEvaluationResult;

            if (value != null)
            {
                if (value.Classes.Contains("data.frame"))
                {
                    return(null);
                }
            }
            return(DefaultMaxGrandChildren);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Create new instance of <see cref="DataEvaluation"/>
        /// </summary>
        /// <param name="evaluation">R session's evaluation result</param>
        public RSessionDataObject(DebugEvaluationResult evaluation, int?maxChildrenCount = null) : this()
        {
            DebugEvaluation = evaluation;

            Name = DebugEvaluation.Name?.TrimStart(NameTrimChars);

            if (DebugEvaluation is DebugValueEvaluationResult)
            {
                var valueEvaluation = (DebugValueEvaluationResult)DebugEvaluation;

                Value       = GetValue(valueEvaluation)?.Trim();
                ValueDetail = valueEvaluation.GetRepresentation().Deparse;
                TypeName    = valueEvaluation.TypeName;

                if (valueEvaluation.Classes != null)
                {
                    var escaped = valueEvaluation.Classes.Select((x) => x.IndexOf(' ') >= 0 ? "'" + x + "'" : x);
                    Class = string.Join(", ", escaped); // TODO: escape ',' in class names
                }

                HasChildren = valueEvaluation.HasChildren;

                Dimensions = valueEvaluation.Dim ?? new List <int>();
            }
            else if (DebugEvaluation is DebugPromiseEvaluationResult)
            {
                const string PromiseVaue = "<promise>";
                var          promise     = (DebugPromiseEvaluationResult)DebugEvaluation;

                Value    = promise.Code;
                TypeName = PromiseVaue;
                Class    = PromiseVaue;
            }
            else if (DebugEvaluation is DebugActiveBindingEvaluationResult)
            {
                const string ActiveBindingValue = "<active binding>";
                var          activeBinding      = (DebugActiveBindingEvaluationResult)DebugEvaluation;

                Value    = ActiveBindingValue;
                TypeName = ActiveBindingValue;
                Class    = ActiveBindingValue;
            }

            if (Dimensions == null)
            {
                Dimensions = new List <int>();
            }

            MaxChildrenCount = maxChildrenCount;
        }
Exemplo n.º 10
0
        private void SubscribeAction(DebugEvaluationResult evaluation)
        {
            VsAppShell.Current.DispatchOnUIThread(
                () => {
                if (evaluation is DebugErrorEvaluationResult)
                {
                    // evaluation error, this could happen if R object is removed
                    var error = (DebugErrorEvaluationResult)evaluation;
                    SetError(error.ErrorText);
                    return;
                }

                var wrapper = new EvaluationWrapper(evaluation);

                if (wrapper.TypeName == "NULL" && wrapper.Value == "NULL")
                {
                    // the variable should have been removed
                    SetError(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Package.Resources.VariableGrid_Missing,
                            evaluation.Expression));
                }
                else if (wrapper.Dimensions.Count != 2)
                {
                    // the same evaluation changed to non-matrix
                    SetError(
                        string.Format(
                            CultureInfo.InvariantCulture,
                            Package.Resources.VariableGrid_NotTwoDimension,
                            evaluation.Expression));
                }
                else if (wrapper.Dimensions[0] != _evaluation.Dimensions[0] ||
                         wrapper.Dimensions[1] != _evaluation.Dimensions[1])
                {
                    ClearError();

                    // matrix size changed. Reset the evaluation
                    SetEvaluation(wrapper);
                }
                else
                {
                    ClearError();

                    // size stays same. Refresh
                    VariableGrid.Refresh();
                }
            });
        }
Exemplo n.º 11
0
        /// <summary>
        /// Create new instance of <see cref="EvaluationWrapper"/>
        /// </summary>
        /// <param name="evaluation">R session's evaluation result</param>
        /// <param name="truncateChildren">true to truncate children returned by GetChildrenAsync</param>
        public EvaluationWrapper(DebugEvaluationResult evaluation, int index = -1, int?maxChildrenCount = null) :
            base(evaluation, maxChildrenCount)
        {
            Index = index;

            CanShowDetail = ComputeDetailAvailability(DebugEvaluation as DebugValueEvaluationResult);
            if (CanShowDetail)
            {
                ShowDetailCommand        = new DelegateCommand(ShowVariableGridWindowPane, (o) => CanShowDetail);
                ShowDetailCommandTooltip = Resources.ShowDetailCommandTooltip;

                OpenInExcelCommand        = new DelegateCommand(OpenInExcel, (o) => CanShowDetail);
                OpenInExcelCommandTooltip = Resources.OpenInExcelCommandTooltip;
            }
        }
Exemplo n.º 12
0
        private async Task PublishAsync(VariableSubscriptionToken token, IList <VariableSubscription> subscriptions)
        {
            if (subscriptions.Count == 0)
            {
                return;
            }

            Debug.Assert(_debugSession != null);

            var stackFrames = await _debugSession.GetStackFramesAsync();

            var stackFrame = stackFrames.FirstOrDefault(f => f.Index == token.FrameIndex);

            if (stackFrame != null)
            {
                const DebugEvaluationResultFields fields = DebugEvaluationResultFields.Classes
                                                           | DebugEvaluationResultFields.Expression
                                                           | DebugEvaluationResultFields.TypeName
                                                           | (DebugEvaluationResultFields.Repr | DebugEvaluationResultFields.ReprStr)
                                                           | DebugEvaluationResultFields.Dim
                                                           | DebugEvaluationResultFields.Length;

                DebugEvaluationResult evaluation = await stackFrame.EvaluateAsync(token.Expression, fields : fields);

                foreach (var sub in subscriptions)
                {
                    try {
                        var action = sub.GetExecuteAction();
                        if (action != null)
                        {
                            action(evaluation);
                        }
                    } catch (Exception e) {
                        Debug.Fail(e.ToString());
                        // swallow exception and continue
                    }
                }
            }
        }
Exemplo n.º 13
0
 public AD7Property(AD7Property parent, DebugEvaluationResult result, bool isSynthetic = false)
     : this(parent.StackFrame, result, isSynthetic, false)
 {
     Parent = parent;
 }
Exemplo n.º 14
0
 private void OnGlobalEnvironmentEvaluated(DebugEvaluationResult result)
 {
     _globalEnv = new EvaluationWrapper(result);
     _mre.Set();
 }
Exemplo n.º 15
0
 public AD7Property(AD7Property parent, DebugEvaluationResult result, bool isSynthetic = false)
     : this(parent.StackFrame, result, isSynthetic, false) {
     Parent = parent;
 }
Exemplo n.º 16
0
        public bool CanShowDataGrid(DebugEvaluationResult evaluationResult)
        {
            var wrapper = new EvaluationWrapper(evaluationResult);

            return(wrapper.CanShowDetail);
        }