int IOleCommandTarget.Exec(ref Guid commandGroupId, uint commandId, uint executeInformation, IntPtr inVar, IntPtr outVar) { if (commandGroupId == Guids.CommandSetId) { switch (commandId) { case CommandIDs.ImplementNotifyPropertyChangedCommandId: // TODO: Wrap in undo transaction IEnumerable <SyntaxAnnotation> propertyAnnotations; var newCompUnit = Implementer.Apply(GetText(), GetSelectionSpan(), out propertyAnnotations); // Store the top line number in view so we can reset it afterwards var topLineNumber = wpfTextView.TextViewLines.FirstVisibleLine.Start.GetContainingLine().LineNumber; var buffer = wpfTextView.TextBuffer; buffer.Replace(new Span(0, buffer.CurrentSnapshot.Length), newCompUnit.ToString()); var snapshot = buffer.CurrentSnapshot; var annotatedProperties = propertyAnnotations.Select(annotation => newCompUnit.GetAnnotatedNode <PropertyDeclarationSyntax>(annotation)); // Create tracking points from the start of the first property that we expanded to the end of the last property. var selectionStart = snapshot.CreateTrackingPoint(annotatedProperties.First().Span.Start, PointTrackingMode.Negative); var selectionEnd = snapshot.CreateTrackingPoint(annotatedProperties.Last().Span.End, PointTrackingMode.Positive); FormatDocument(); // After formatting, use our tracking points to select the expanded properties var formattedSnapshot = buffer.CurrentSnapshot; wpfTextView.Selection.Select( new VirtualSnapshotPoint(selectionStart.GetPoint(formattedSnapshot)), new VirtualSnapshotPoint(selectionEnd.GetPoint(formattedSnapshot))); // Reset the top line in view var topLine = formattedSnapshot.GetLineFromLineNumber(topLineNumber); wpfTextView.DisplayTextLineContainingBufferPosition(topLine.Start, 0.0, ViewRelativePosition.Top); return(VSConstants.S_OK); } } return(nextCommandTarget.Exec(ref commandGroupId, commandId, executeInformation, inVar, outVar)); }
int IOleCommandTarget.QueryStatus(ref Guid commandGroupId, uint commandCount, OLECMD[] commands, IntPtr commandText) { if (commandGroupId == Guids.CommandSetId) { switch (commands[0].cmdID) { case CommandIDs.ImplementNotifyPropertyChangedCommandId: if (Implementer.IsAvailable(GetText(), GetSelectionSpan())) { commands[0].cmdf = (uint)(OLECMDF.OLECMDF_ENABLED | OLECMDF.OLECMDF_SUPPORTED); } else { commands[0].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED); } return(VSConstants.S_OK); } } return(nextCommandTarget.QueryStatus(ref commandGroupId, commandCount, commands, commandText)); }