private async void Execute(object sender, EventArgs e)
        {
            try
            {
                ThreadHelper.ThrowIfNotOnUIThread();

                this.Logger?.RecordFeatureUsage(nameof(InsertGridRowDefinitionCommand));

                var dte = await Instance.ServiceProvider.GetServiceAsync(typeof(DTE)) as DTE;

                var vs = new VisualStudioAbstraction(this.Logger, this.ServiceProvider, dte);

                var activeDocText = vs.GetActiveDocumentText();

                if (activeDocText.IsValidXml())
                {
                    var logic = new InsertGridRowDefinitionCommandLogic(this.Logger, vs);

                    var replacements = logic.GetReplacements();
                    var(start, end, exclusions)   = logic.GetGridBoundary();
                    var(newDefinition, newDefPos) = logic.GetDefinitionAtCursor();

                    vs.StartSingleUndoOperation(StringRes.Info_UndoContextIndertRowDef);
                    try
                    {
                        vs.ReplaceInActiveDoc(replacements, start, end, exclusions);
                        vs.InsertIntoActiveDocumentOnNextLine(newDefinition, newDefPos);
                    }
                    finally
                    {
                        vs.EndSingleUndoOperation();
                    }
                }
                else
                {
                    this.Logger.RecordInfo(StringRes.Info_UnableToInsertRowDefinitionInvalidXaml);
                }
            }
            catch (Exception exc)
            {
                this.Logger?.RecordException(exc);
                throw;
            }
        }
示例#2
0
#pragma warning disable VSTHRD100 // Avoid async void methods - Allowed as called from event handler.
        private async void Execute(object sender, EventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
        {
            System.Windows.Forms.Cursor previousCursor = System.Windows.Forms.Cursor.Current;
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            try
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                this.Logger?.RecordFeatureUsage(nameof(MoveAllHardCodedStringsToResourceFileCommand));

                var dte = await Instance.AsyncPackage.GetServiceAsync <DTE, DTE>();

                var vs = new VisualStudioAbstraction(this.Logger, this.AsyncPackage, dte);

                var filePath = vs.GetActiveDocumentFilePath();

                // Ensure that the open document has been saved or modifications may go wrong
                var rdt = new RunningDocumentTable(Package);
                rdt.SaveFileIfDirty(filePath);

                // Do this as don't have direct snapshot access and want any changes to be processed before making changes
                RapidXamlDocumentCache.TryUpdate(filePath);

                var tags = RapidXamlDocumentCache.ErrorListTags(filePath)
                           .OfType <HardCodedStringTag>()
                           .OrderByDescending(t => t.Span.Start);

                var referenceUids = new Dictionary <Guid, string>();

                vs.StartSingleUndoOperation(StringRes.UI_UndoContextMoveStringsToResourceFile);

                try
                {
                    foreach (var tag in tags)
                    {
                        if (!tag.UidExists && referenceUids.ContainsKey(tag.ElementGuid))
                        {
                            tag.UidExists = true;
                            tag.UidValue  = referenceUids[tag.ElementGuid];
                        }

                        // pass in vs so have the same reference for tracking undo actions
                        var action = new HardCodedStringAction(filePath, null, tag, vs);
                        action.Execute(new CancellationTokenSource().Token);

                        if (tag.UidExists == false && tag.ElementGuid != Guid.Empty && !referenceUids.ContainsKey(tag.ElementGuid))
                        {
                            referenceUids.Add(tag.ElementGuid, tag.UidValue);
                        }
                    }
                }
                finally
                {
                    vs.EndSingleUndoOperation();
                }

                // Update again to force reflecting the changes that were just made.
                RapidXamlDocumentCache.TryUpdate(filePath);
            }
            catch (Exception exc)
            {
                this.Logger?.RecordException(exc);
                throw;
            }
            finally
            {
                System.Windows.Forms.Cursor.Current = previousCursor;
            }
        }