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)); } }
private void Execute(object sender, EventArgs e) { try { ThreadHelper.ThrowIfNotOnUIThread(); this.Logger?.RecordFeatureUsage(nameof(SetDatacontextCommand)); var settings = AnalyzerBase.GetSettings(); var profile = settings.GetActiveProfile(); var dte = (DTE)Instance.ServiceProvider.GetServiceAsync(typeof(DTE)).Result; var logic = new SetDataContextCommandLogic(profile, this.Logger, new VisualStudioAbstraction(dte)); 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); 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 = GetTextView(Instance.ServiceProvider); var caretPosition = textView.Caret.Position.BufferPosition; var document = caretPosition.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); var documentRoot = document.GetSyntaxTreeAsync().Result.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); } } } } } } catch (Exception exc) { this.Logger?.RecordException(exc); throw; } }