#pragma warning disable CS0628 // New protected member declared in sealed class
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
#pragma warning restore CS0628 // New protected member declared in sealed class
        {
            // When initialized asynchronously, the current thread may be a background thread at this point.
            // Do any initialization that requires the UI thread after switching to the UI thread.
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            try
            {
                await SharedRapidXamlPackage.InitializeAsync(cancellationToken, this);

                SharedRapidXamlPackage.Logger.RecordNotice(StringRes.Info_LaunchVersionGeneration.WithParams(CoreDetails.GetVersion()));
                SharedRapidXamlPackage.Logger.RecordNotice(string.Empty);

                await CopyToClipboardCommand.InitializeAsync(this, SharedRapidXamlPackage.Logger);

                await SendToToolboxCommand.InitializeAsync(this, SharedRapidXamlPackage.Logger);

                await OpenOptionsCommand.InitializeAsync(this, SharedRapidXamlPackage.Logger);

                await RapidXamlDropHandlerProvider.InitializeAsync(this, SharedRapidXamlPackage.Logger);

                // Set the ServiceProvider of CodeParserBase as it's needed to get settings
                CodeParserBase.ServiceProvider = this;

                if (SharedRapidXamlPackage.Logger != null)
                {
                    SharedRapidXamlPackage.Logger.UseExtendedLogging = CodeParserBase.GetSettings().ExtendedOutputEnabled;
                }
            }
            catch (Exception exc)
            {
                SharedRapidXamlPackage.Logger?.RecordException(exc);
            }
        }
예제 #2
0
        private async void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            try
            {
                if (sender is OleMenuCommand menuCmd)
                {
                    bool showCommandButton = false;

                    var settings = CodeParserBase.GetSettings();

                    if (settings.IsActiveProfileSet)
                    {
                        var profile = settings.GetActiveProfile();
                        var dte     = await Instance.ServiceProvider.GetServiceAsync(typeof(DTE)) as DTE;

                        var logic = new SetDataContextCommandLogic(profile, this.Logger, new VisualStudioAbstraction(this.Logger, this.ServiceProvider, dte));

                        showCommandButton = logic.ShouldEnableCommand();
                    }

                    menuCmd.Visible = menuCmd.Enabled = showCommandButton;
                }
            }
            catch (Exception exc)
            {
                this.Logger.RecordException(exc);
                throw;  // Remove for launch. see issue #90
            }
        }
예제 #3
0
        public async Task <ParserOutput> GetXamlAsync(IAsyncServiceProvider serviceProvider)
        {
            ParserOutput result = null;

            if (CodeParserBase.GetSettings().Profiles.Any())
            {
                if (!(await serviceProvider.GetServiceAsync(typeof(EnvDTE.DTE)) is DTE dte))
                {
                    SharedRapidXamlPackage.Logger?.RecordError(StringRes.Error_FailedToGetDteInGetXamlAsync);
                }
                else
                {
                    var activeDocument = dte.ActiveDocument;

                    var textView = await GetTextViewAsync(serviceProvider);

                    var selection = textView.Selection;

                    bool isSelection = selection.Start.Position != selection.End.Position;

                    var caretPosition = textView.Caret.Position.BufferPosition;

                    var document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

                    var semanticModel = await document.GetSemanticModelAsync();

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

                    var proj = dte.Solution.GetProjectContainingFile(document.FilePath);

                    if (proj == null)
                    {
                        // Default to the "active project" if file is not part of a known project
                        proj = ((Array)dte.ActiveSolutionProjects).GetValue(0) as Project;
                    }

                    var projType = vs.GetProjectType(proj);

                    this.Logger?.RecordInfo(StringRes.Info_DetectedProjectType.WithParams(projType.GetDescription()));

                    IDocumentParser parser = null;

                    if (activeDocument.Language == "CSharp")
                    {
                        parser = new CSharpParser(this.Logger, projType, xamlIndent);
                    }
                    else if (activeDocument.Language == "Basic")
                    {
                        parser = new VisualBasicParser(this.Logger, projType, xamlIndent);
                    }

                    result = isSelection
                        ? parser?.GetSelectionOutput(await document.GetSyntaxRootAsync(), semanticModel, selection.Start.Position, selection.End.Position)
                        : parser?.GetSingleItemOutput(await document.GetSyntaxRootAsync(), semanticModel, caretPosition.Position);
                }
            }
예제 #4
0
        public void RecordInfo(string message)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (CodeParserBase.GetSettings().ExtendedOutputEnabled)
            {
                RxtOutputPane.Instance.Write(TimeStampMessage(message));
            }
        }
예제 #5
0
        public void RecordError(string message)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Activate the pane (bring to front) so errors are obvious
            if (CodeParserBase.GetSettings().ExtendedOutputEnabled)
            {
                RxtOutputPane.Instance.Write(TimeStampMessage(message));
                RxtOutputPane.Instance.Activate();
            }
            else
            {
                GeneralOutputPane.Instance.Write(TimeStampMessage(message));
                GeneralOutputPane.Instance.Activate();
            }
        }
예제 #6
0
        public async Task <ParserOutput> GetXamlAsync(IAsyncServiceProvider serviceProvider)
        {
            ParserOutput result = null;

            if (CodeParserBase.GetSettings().Profiles.Any())
            {
                if (!(await serviceProvider.GetServiceAsync(typeof(EnvDTE.DTE)) is DTE dte))
                {
                    RapidXamlPackage.Logger?.RecordError("Failed to get DTE in GetXamlFromCodeWindowBaseCommand.GetXamlAsync");
                }
                else
                {
                    var activeDocument = dte.ActiveDocument;

                    var textView = await GetTextViewAsync(serviceProvider);

                    var selection = textView.Selection;

                    bool isSelection = selection.Start.Position != selection.End.Position;

                    var caretPosition = textView.Caret.Position.BufferPosition;

                    var document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

                    var semanticModel = await document.GetSemanticModelAsync();

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

                    IDocumentParser parser = null;

                    if (activeDocument.Language == "CSharp")
                    {
                        parser = new CSharpParser(this.Logger, xamlIndent);
                    }
                    else if (activeDocument.Language == "Basic")
                    {
                        parser = new VisualBasicParser(this.Logger, xamlIndent);
                    }

                    result = isSelection
                        ? parser?.GetSelectionOutput(await document.GetSyntaxRootAsync(), semanticModel, selection.Start.Position, selection.End.Position)
                        : parser?.GetSingleItemOutput(await document.GetSyntaxRootAsync(), semanticModel, caretPosition.Position);
                }
            }
        private void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            try
            {
                if (sender is OleMenuCommand menuCmd)
                {
                    menuCmd.Visible = menuCmd.Enabled = false;

                    if (!CodeParserBase.GetSettings().IsActiveProfileSet)
                    {
                        menuCmd.Visible = menuCmd.Enabled = true;
                    }
                }
            }
            catch (Exception exc)
            {
                this.Logger.RecordException(exc);
                throw;  // Remove for launch. see issue #90
            }
        }
        private async void MenuItem_BeforeQueryStatus(object sender, EventArgs e)
        {
            try
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                if (sender is OleMenuCommand menuCmd)
                {
                    menuCmd.Visible = menuCmd.Enabled = false;

                    if (!this.IsSingleProjectItemSelection(out var hierarchy, out var itemid))
                    {
                        this.SelectedFileName = null;
                        return;
                    }

                    ((IVsProject)hierarchy).GetMkDocument(itemid, out var itemFullPath);
                    var transformFileInfo = new FileInfo(itemFullPath);

                    // Save the name of the selected file so we have it when the command is executed
                    this.SelectedFileName = transformFileInfo.FullName;

                    if (transformFileInfo.Name.EndsWith(".cs") || transformFileInfo.Name.EndsWith(".vb"))
                    {
                        if (CodeParserBase.GetSettings().IsActiveProfileSet)
                        {
                            menuCmd.Visible = menuCmd.Enabled = true;
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                this.Logger.RecordException(exc);
            }
        }
예제 #9
0
        private async void Execute(object sender, EventArgs e)
        {
            try
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

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

                var settings = CodeParserBase.GetSettings();
                var profile  = settings.GetActiveProfile();

                if (!(await Instance.ServiceProvider.GetServiceAsync(typeof(DTE)) is DTE dte))
                {
                    RapidXamlPackage.Logger?.RecordError("Failed to get DTE in SetDatacontextCommand.Execute");
                }
                else
                {
                    var vs    = new VisualStudioAbstraction(this.Logger, this.ServiceProvider, dte);
                    var logic = new SetDataContextCommandLogic(profile, this.Logger, vs);

                    var inXamlDoc = dte.ActiveDocument.Name.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase);

                    var(viewName, viewModelName, vmNamespace) = logic.InferViewModelNameFromFileName(dte.ActiveDocument.Name);

                    if (inXamlDoc)
                    {
                        if (profile.Datacontext.SetsXamlPageAttribute)
                        {
                            var(add, lineNo, content) = logic.GetPageAttributeToAdd(viewModelName, vmNamespace);

                            if (add)
                            {
                                if (dte.ActiveDocument.Object("TextDocument") is TextDocument objectDoc)
                                {
                                    objectDoc.Selection.GotoLine(lineNo);
                                    objectDoc.Selection.EndOfLine();
                                    objectDoc.Selection.Insert(content);
                                }
                            }
                        }

                        if (profile.Datacontext.SetsAnyCodeBehindContent)
                        {
                            if (profile.Datacontext.SetsCodeBehindPageContent)
                            {
                                // TODO: ISSUE#22 - set the DC in the CB file (C# or VB) may be open and unsaved
                            }

                            if (profile.Datacontext.SetsCodeBehindConstructorContent)
                            {
                                // TODO: ISSUE#22 - set the DC in the CB file (C# or VB) may be open and unsaved
                            }
                        }
                    }
                    else
                    {
                        if (profile.Datacontext.SetsXamlPageAttribute)
                        {
                            // TODO: ISSUE#22 - set the DC in the XAML file (C# or VB) may be open and unsaved
                        }

                        if (profile.Datacontext.SetsAnyCodeBehindContent)
                        {
                            if (dte.ActiveDocument.Object("TextDocument") is TextDocument objectDoc)
                            {
                                var textView = await GetTextViewAsync(Instance.ServiceProvider);

                                var caretPosition = textView.Caret.Position.BufferPosition;
                                var document      = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
                                var documentTree  = await document.GetSyntaxTreeAsync();

                                var documentRoot = documentTree.GetRoot();

                                var toAdd = logic.GetCodeBehindContentToAdd(viewName, viewModelName, vmNamespace, documentRoot);

                                foreach (var(anything, lineNo, contentToAdd) in toAdd)
                                {
                                    if (anything)
                                    {
                                        objectDoc.Selection.GotoLine(lineNo);
                                        objectDoc.Selection.EndOfLine();
                                        objectDoc.Selection.Insert(contentToAdd);
                                    }
                                }
                            }
                        }
                    }

                    this.SuppressAnyException(() => dte.FormatDocument(profile));
                }
            }