private void Unbind_OnMouseLeftButtonUp( object sender, MouseButtonEventArgs e ) { ThreadHelper.ThrowIfNotOnUIThread(); var tag = (sender as FrameworkElement)?.Tag as DpdtBindingTargetViewModel; if (tag is null) { return; } if (!TryGetBinding(tag.BindingUniqueUnstableIdentifier, out var containerAndScanner, out var binding)) { return; } if (binding !.IsConventional) { return; } var dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2; if (dte == null) { return; } var componentModel = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel; if (componentModel == null) { return; } var textManager = Package.GetGlobalService(typeof(SVsTextManager)) as IVsTextManager; if (textManager == null) { return; } ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out var currentActiveView)); var currentDocumentFilePath = System.IO.Path.Combine(dte.ActiveDocument.Path, dte.ActiveDocument.Name); currentActiveView.GetCaretPos(out var currentLine, out var currentColumn); //switch to modified document var modifiedDocumentHelper = new VisualStudioDocumentHelper( binding !.Position.FilePath ); modifiedDocumentHelper.OpenAndNavigate( binding !.Position.StartLine, binding !.Position.StartColumn, binding !.Position.EndLine, binding !.Position.EndColumn ); try { dte.UndoContext.Open($"Remove bind {binding.TargetRepresentation}"); ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out var activeView)); var editorAdapter = componentModel.GetService <IVsEditorAdaptersFactoryService>(); var textView = editorAdapter.GetWpfTextView(activeView); if (textView != null) { textView.TextBuffer.Delete( new Span( binding.Position.SpanStart, binding.Position.SpanLength ) ); } } finally { dte.UndoContext.Close(); } //get back to our document var sourceDocumentHelper = new VisualStudioDocumentHelper( currentDocumentFilePath ); sourceDocumentHelper.OpenAndNavigate( currentLine, currentColumn, currentLine, currentColumn ); }
public async Task DoSurgeryAsync( NewBindingInfo newBindingInfo ) { if (newBindingInfo is null) { throw new ArgumentNullException(nameof(newBindingInfo)); } if (string.IsNullOrEmpty(_targetMethod.MethodDeclaration.Position.FilePath)) { return; } await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var dte = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2; if (dte == null) { return; } var componentModel = Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel; if (componentModel == null) { return; } var textManager = Package.GetGlobalService(typeof(SVsTextManager)) as IVsTextManager; if (textManager == null) { return; } var workspace = (Workspace)componentModel.GetService <VisualStudioWorkspace>(); if (workspace == null) { return; } ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out var currentActiveView)); var currentDocumentFilePath = System.IO.Path.Combine(dte.ActiveDocument.Path, dte.ActiveDocument.Name); currentActiveView.GetCaretPos(out var currentLine, out var currentColumn); #region open modified document var modifiedDocumentHelper = new VisualStudioDocumentHelper( _targetMethod.MethodDeclaration.Position.FilePath ); modifiedDocumentHelper.OpenAndNavigate( _targetMethod.MethodDeclaration.Position.StartLine, _targetMethod.MethodDeclaration.Position.StartColumn, _targetMethod.MethodDeclaration.Position.EndLine, _targetMethod.MethodDeclaration.Position.EndColumn ); #endregion #region switch back to source document if needed if (newBindingInfo.IsBindingComplete) { var sourceDocumentHelper = new VisualStudioDocumentHelper( currentDocumentFilePath ); sourceDocumentHelper.OpenAndNavigate( currentLine, currentColumn, currentLine, currentColumn ); } #endregion var document = workspace.GetDocument( _targetMethod.MethodDeclaration.Position.FilePath ); if (document == null) { return; } var surgeon = new SyntaxSurgeon( _targetMethod ); var(surgedDocument, addedBinding) = await surgeon.SurgeAsync( document, newBindingInfo ); if (surgedDocument == null) { return; } if (!workspace.TryApplyChanges(surgedDocument.Project.Solution)) { DpdtPackage.ShowMessageBox( "Error", "Error happened while additing a new binding clause. Please try again." ); return; } if (!newBindingInfo.IsBindingComplete && addedBinding != null) { var addedBindingLineSpan = addedBinding.GetLocation().GetLineSpan(); modifiedDocumentHelper.OpenAndNavigate( addedBindingLineSpan.StartLinePosition.Line, addedBindingLineSpan.StartLinePosition.Character, addedBindingLineSpan.EndLinePosition.Line, addedBindingLineSpan.EndLinePosition.Character ); } }