Exemple #1
0
        private static UsingDirectiveSyntax ConsolidateUsingDirective(UsingDirectiveSyntax usingDirective)
        {
            var leadingTrivia = usingDirective.GetLeadingTrivia()
                                .Where(trivia => !trivia.IsKind(SyntaxKind.EndOfLineTrivia))
                                .ToList();

            return(usingDirective.WithLeadingTrivia(new SyntaxTriviaList(leadingTrivia)));
        }
Exemple #2
0
        private UsingDirectiveSyntax WithLeadingNewlineCount(UsingDirectiveSyntax node, int newlineCount)
        {
            // Count current leading newlines
            int currentNewlineCount = 0;

            foreach (SyntaxTrivia trivia in node.GetLeadingTrivia())
            {
                if (trivia.Kind() == SyntaxKind.EndOfLineTrivia)
                {
                    currentNewlineCount++;
                }
                break;
            }

            if (currentNewlineCount == newlineCount)
            {
                // If count is already correct, leave alone
                return(node);
            }
            else if (currentNewlineCount > newlineCount)
            {
                // If too many, exclude the excess ones
                return(node.WithLeadingTrivia(node.GetLeadingTrivia().Skip(currentNewlineCount - newlineCount)));
            }
            else
            {
                // If too few, insert new leading ones
                SyntaxTriviaList newLeadingTrivia = node.GetLeadingTrivia();
                for (int i = currentNewlineCount; i < newlineCount; ++i)
                {
                    newLeadingTrivia = newLeadingTrivia.Insert(0, SyntaxFactory.CarriageReturnLineFeed);
                }

                return(node.WithLeadingTrivia(newLeadingTrivia));
            }
        }
Exemple #3
0
        private static UsingDirectiveSyntax RemoveLeadingAndTrailingCompilerDirectives(UsingDirectiveSyntax usingSyntax)
        {
            UsingDirectiveSyntax usingDirectiveToUse = usingSyntax;

            if (usingDirectiveToUse.HasLeadingTrivia)
            {
                if (usingDirectiveToUse.HasLeadingTrivia)
                {
                    var newLeadingTrivia = RemoveCompilerDirectives(usingDirectiveToUse.GetLeadingTrivia());
                    usingDirectiveToUse = usingDirectiveToUse.WithLeadingTrivia(newLeadingTrivia);
                }
                if (usingDirectiveToUse.HasTrailingTrivia)
                {
                    var newTrailingTrivia = RemoveCompilerDirectives(usingDirectiveToUse.GetTrailingTrivia());
                    usingDirectiveToUse = usingDirectiveToUse.WithTrailingTrivia(newTrailingTrivia);
                }
            }

            return(usingDirectiveToUse);
        }
        private static Task <Document> AddBlankLineBeforeUsingDirectiveAsync(
            Document document,
            UsingDirectiveSyntax usingDirective,
            CancellationToken cancellationToken)
        {
            SyntaxTriviaList leadingTrivia = usingDirective.GetLeadingTrivia();

            int index = leadingTrivia.Count;

            if (index > 0 &&
                leadingTrivia.Last().IsWhitespaceTrivia())
            {
                index--;
            }

            SyntaxTriviaList newLeadingTrivia = leadingTrivia.Insert(index, SyntaxTriviaAnalysis.DetermineEndOfLine(usingDirective));

            UsingDirectiveSyntax newUsingDirective = usingDirective.WithLeadingTrivia(newLeadingTrivia);

            return(document.ReplaceNodeAsync(usingDirective, newUsingDirective, cancellationToken));
        }
		private static NamespaceDeclarationSyntax AddUsing(NamespaceDeclarationSyntax nspaceSyntax, UsingDirectiveSyntax newUsing)
		{
			if (nspaceSyntax.Usings.Count > 0)
			{
				var lastUsing = nspaceSyntax.Usings.Last();
				newUsing = newUsing
					.WithLeadingTrivia(lastUsing.GetLeadingTrivia())
					.WithTrailingTrivia(lastUsing.GetTrailingTrivia());

				return nspaceSyntax.WithUsings(nspaceSyntax.Usings.Add(newUsing));
			}

			return nspaceSyntax.WithUsings(nspaceSyntax.Usings.Add(newUsing.NormalizeWhitespace()));
		}