public override void Execute(CancellationToken cancellationToken) { var resPath = this.GetResourceFilePath(); if (resPath == null) { return; } var vs = new VisualStudioTextManipulation(ProjectHelpers.Dte); vs.StartSingleUndoOperation(StringRes.Info_UndoContextMoveStringToResourceFile); try { // If the resource file is open with unsaved changes VS will prompt about data being lost. this.AddResource(resPath, $"{this.Tag.UidValue}.{this.Tag.AttributeName}", this.Tag.Value); if (this.Tag.AttributeType == AttributeType.Inline) { var currentAttribute = $"{this.Tag.AttributeName}=\"{this.Tag.Value}\""; if (this.Tag.UidExists) { vs.RemoveInActiveDocOnLine(currentAttribute, this.Tag.GetDesignerLineNumber()); } else { var uidTag = $"x:Uid=\"{this.Tag.UidValue}\""; vs.ReplaceInActiveDocOnLine(currentAttribute, uidTag, this.Tag.GetDesignerLineNumber()); } } else if (this.Tag.AttributeType == AttributeType.Element) { var currentAttribute = $"<{this.Tag.ElementName}.{this.Tag.AttributeName}>{this.Tag.Value}</{this.Tag.ElementName}.{this.Tag.AttributeName}>"; vs.RemoveInActiveDocOnLine(currentAttribute, this.Tag.GetDesignerLineNumber()); if (!this.Tag.UidExists) { var uidTag = $"<{this.Tag.ElementName} x:Uid=\"{this.Tag.UidValue}\""; vs.ReplaceInActiveDocOnLineOrAbove($"<{this.Tag.ElementName}", uidTag, this.Tag.GetDesignerLineNumber()); } } else if (this.Tag.AttributeType == AttributeType.DefaultValue) { var current = $">{this.Tag.Value}</{this.Tag.ElementName}>"; var replaceWith = this.Tag.UidExists ? " />" : $" x:Uid=\"{this.Tag.UidValue}\" />"; vs.ReplaceInActiveDocOnLine(current, replaceWith, this.Tag.GetDesignerLineNumber()); } RapidXamlDocumentCache.TryUpdate(this.File); } finally { vs.EndSingleUndoOperation(); } }
public override void Execute(CancellationToken cancellationToken) { var vs = new VisualStudioTextManipulation(ProjectHelpers.Dte); vs.StartSingleUndoOperation(this.UndoOperationName); try { var lineNumber = this.Tag.Snapshot.GetLineNumberFromPosition(this.Tag.InsertPosition) + 1; string toInsert; if (this.Tag.GridNeedsExpanding) { vs.RemoveInActiveDocOnLine(" />", lineNumber); toInsert = $">{Environment.NewLine}{this.InjectedXaml}"; toInsert = toInsert.Replace("\n", "\n" + this.Tag.LeftPad); string shortPad = this.Tag.LeftPad.EndsWith("\t") ? this.Tag.LeftPad.Substring(0, this.Tag.LeftPad.Length - 1) : this.Tag.LeftPad.Substring(0, this.Tag.LeftPad.Length - 4); toInsert += $"{Environment.NewLine}{shortPad}</Grid>"; } else { toInsert = Environment.NewLine + this.InjectedXaml; toInsert = toInsert.Replace("\n", "\n" + this.Tag.LeftPad); } vs.InsertAtEndOfLine(lineNumber, toInsert); RapidXamlDocumentCache.TryUpdate(this.File); } finally { vs.EndSingleUndoOperation(); } }
#pragma warning disable IDE0060 // Remove unused parameter - cancellationToken private void InnerExecute(VisualStudioTextManipulation vs, CustomAnalysisTag tag, CancellationToken cancellationToken) #pragma warning restore IDE0060 // Remove unused parameter { switch (tag.Action) { case RapidXaml.ActionType.AddAttribute: var lineNumber = tag.Snapshot.GetLineNumberFromPosition(tag.InsertPosition) + 1; // Can't rely on the original element name as this may be supplemental after it's been renamed if (XamlElementProcessor.IsSelfClosing(tag.AnalyzedElement.OriginalString.AsSpan())) { var before = $"/>"; var after = $"{tag.Name}=\"{tag.Value}\" />"; vs.ReplaceInActiveDocOnLine(before, after, lineNumber); } else { var before = $">"; var after = $"{tag.Name}=\"{tag.Value}\" /"; vs.ReplaceInActiveDocOnLine(before, after, lineNumber); } break; case RapidXaml.ActionType.AddChild: var origXaml = tag.AnalyzedElement.OriginalString; // Allow for self-closing elements if (origXaml.EndsWith("/>")) { var replacementXaml = $">{Environment.NewLine}{tag.Content}{Environment.NewLine}</{tag.ElementName}>"; var insertLine = tag.Snapshot.GetLineNumberFromPosition(tag.InsertPosition) + 1; vs.ReplaceInActiveDocOnLine("/>", replacementXaml, insertLine); } else { // Allows for opening and closing tags on same or different lines var insertLine = tag.Snapshot.GetLineNumberFromPosition(tag.InsertPosition) + 1; vs.InsertIntoActiveDocOnLineAfterClosingTag(insertLine, tag.Content); } break; case RapidXaml.ActionType.HighlightWithoutAction: // As the name implies, do nothing. break; case RapidXaml.ActionType.RemoveAttribute: if (tag.IsInlineAttribute ?? false) { var currentAttribute = $" {tag.Name}=\"{tag.Value}\""; vs.RemoveInActiveDocOnLine(currentAttribute, tag.GetDesignerLineNumber()); } else { var attrs = tag.AnalyzedElement.GetAttributes(tag.Name).ToList(); if (attrs.Count() == 1) { var attr = attrs.First(); var toRemove = tag.AnalyzedElement.OriginalString.Substring( attr.Location.Start - tag.InsertPosition, attr.Location.Length); vs.RemoveInActiveDocOnLine(toRemove, tag.GetDesignerLineNumber()); } } break; case RapidXaml.ActionType.RemoveChild: vs.RemoveInActiveDocOnLine(tag.Element.OriginalString, tag.GetDesignerLineNumber()); break; case RapidXaml.ActionType.ReplaceElement: vs.ReplaceInActiveDocOnLine( tag.AnalyzedElement.OriginalString, tag.Content, tag.Snapshot.GetLineNumberFromPosition(tag.AnalyzedElement.Location.Start)); break; case RapidXaml.ActionType.RenameElement: // Just change opening tags as Visual Studio will change closing tags automatically var renameLineNumber = tag.Snapshot.GetLineNumberFromPosition(tag.InsertPosition); vs.ReplaceInActiveDocOnLine(tag.ElementName, tag.Name, renameLineNumber); foreach (var childAttr in tag.AnalyzedElement.ChildAttributes) { renameLineNumber = tag.Snapshot.GetLineNumberFromPosition(childAttr.Location.Start); vs.ReplaceInActiveDocOnLine($"{tag.ElementName}.{childAttr.Name}", $"{tag.Name}.{childAttr.Name}", renameLineNumber); } break; } }