private static async Task <Document> MoveDefaultLabelToLastPositionAsync( Document document, SwitchSectionSyntax switchSection, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels; SwitchLabelSyntax defaultLabel = labels.First(f => f.IsKind(SyntaxKind.DefaultSwitchLabel)); int index = labels.IndexOf(defaultLabel); SwitchLabelSyntax lastLabel = labels.Last(); labels = labels.Replace(lastLabel, defaultLabel.WithTriviaFrom(lastLabel)); labels = labels.Replace(labels[index], lastLabel.WithTriviaFrom(defaultLabel)); SwitchSectionSyntax newSwitchSection = switchSection.WithLabels(labels); SyntaxNode newRoot = root.ReplaceNode(switchSection, newSwitchSection); return(document.WithSyntaxRoot(newRoot)); }
public static async Task <Document> RefactorAsync( Document document, SwitchSectionSyntax switchSection, CancellationToken cancellationToken = default(CancellationToken)) { SyntaxList <SwitchLabelSyntax> labels = switchSection.Labels; SwitchLabelSyntax defaultLabel = labels.First(f => f.IsKind(SyntaxKind.DefaultSwitchLabel)); int index = labels.IndexOf(defaultLabel); SwitchLabelSyntax lastLabel = labels.Last(); labels = labels.Replace(lastLabel, defaultLabel.WithTriviaFrom(lastLabel)); labels = labels.Replace(labels[index], lastLabel.WithTriviaFrom(defaultLabel)); SwitchSectionSyntax newSwitchSection = switchSection.WithLabels(labels); return(await document.ReplaceNodeAsync(switchSection, newSwitchSection, cancellationToken).ConfigureAwait(false)); }