Exemple #1
0
            private async Task <SyntaxNode> GetNewNodeAsync(SyntaxNode node, CancellationToken cancellationToken)
            {
                var syntaxFacts  = CSharpSyntaxFacts.Instance;
                var modifiers    = syntaxFacts.GetModifiers(node);
                var newModifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.NewKeyword));

                var options = await _document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

                var option = options.GetOption(CSharpCodeStyleOptions.PreferredModifierOrder);

                if (!CSharpOrderModifiersHelper.Instance.TryGetOrComputePreferredOrder(option.Value, out var preferredOrder) ||
                    !AbstractOrderModifiersHelpers.IsOrdered(preferredOrder, modifiers))
                {
                    return(syntaxFacts.WithModifiers(node, newModifiers));
                }

                var orderedModifiers = new SyntaxTokenList(
                    newModifiers.OrderBy(CompareModifiers));

                return(syntaxFacts.WithModifiers(node, orderedModifiers));

                int CompareModifiers(SyntaxToken left, SyntaxToken right)
                => GetOrder(left) - GetOrder(right);

                int GetOrder(SyntaxToken token)
                => preferredOrder.TryGetValue(token.RawKind, out var value) ? value : int.MaxValue;
            }
            private static SyntaxNode GetNewNode(SyntaxNode node, string preferredModifierOrder)
            {
                var syntaxFacts  = CSharpSyntaxFacts.Instance;
                var modifiers    = syntaxFacts.GetModifiers(node);
                var newModifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.NewKeyword));

                if (!CSharpOrderModifiersHelper.Instance.TryGetOrComputePreferredOrder(preferredModifierOrder, out var preferredOrder) ||
                    !AbstractOrderModifiersHelpers.IsOrdered(preferredOrder, modifiers))
                {
                    return(syntaxFacts.WithModifiers(node, newModifiers));
                }

                var orderedModifiers = new SyntaxTokenList(
                    newModifiers.OrderBy(CompareModifiers));

                return(syntaxFacts.WithModifiers(node, orderedModifiers));

                int CompareModifiers(SyntaxToken left, SyntaxToken right)
                => GetOrder(left) - GetOrder(right);

                int GetOrder(SyntaxToken token)
                => preferredOrder.TryGetValue(token.RawKind, out var value) ? value : int.MaxValue;
            }