protected SyntaxTriviaList CleanUpList(SyntaxTriviaList newList) { var lineBreaksAtBeginning = newList.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { newList = newList.Skip(lineBreaksAtBeginning - 1).ToSyntaxTriviaList(); } return(newList); }
public override SyntaxTriviaList VisitList(SyntaxTriviaList list) { list = base.VisitList(list); var lineBreaksAtBeginning = list.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { list = SyntaxFactory.TriviaList(list.Skip(lineBreaksAtBeginning - 1)); } return list; }
public override SyntaxTriviaList VisitList(SyntaxTriviaList list) { list = base.VisitList(list); var lineBreaksAtBeginning = list.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { list = SyntaxFactory.TriviaList(list.Skip(lineBreaksAtBeginning - 1)); } return(list); }
SyntaxTriviaList CleanUpList(SyntaxTriviaList newList, CleanupTypes?option = null) { if (option.HasValue && CheckOption((int)option.Value) == false) { return(newList); } var lineBreaksAtBeginning = newList.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > 1) { newList = newList.Skip(lineBreaksAtBeginning - 1).ToSyntaxTriviaList(); } return(newList); }
private static SyntaxTriviaList CleanTrivias(SyntaxTriviaList old, TextSpan span) { if (old.Span.Start > span.End || old.Span.End < span.Start) { return(old); } var before = old .TakeWhile(x => x.Span.End < span.Start); var buffer = old .SkipWhile(x => x.Span.Start < span.End) .SkipWhile(x => x.IsKind(SyntaxKind.WhitespaceTrivia)) .ToList(); var after = buffer.Any() && buffer.First().IsKind(SyntaxKind.EndOfLineTrivia) ? buffer.Skip(1) : buffer; var list = SyntaxTriviaList.Empty .AddRange(before) .AddRange(after); return(list); }
protected SyntaxTriviaList CleanUpList(SyntaxTriviaList syntaxTrivias, int exactNumberOfBlanks) { var lineBreaksAtBeginning = syntaxTrivias.TakeWhile(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).Count(); if (lineBreaksAtBeginning > exactNumberOfBlanks) { syntaxTrivias = syntaxTrivias.Skip(lineBreaksAtBeginning - exactNumberOfBlanks) .ToSyntaxTriviaList(); } else if (lineBreaksAtBeginning < exactNumberOfBlanks) { var newList = syntaxTrivias.ToList(); for (var i = lineBreaksAtBeginning; i < exactNumberOfBlanks; i++) { newList.Insert(0, _endOfLineTrivia); } syntaxTrivias = new SyntaxTriviaList().AddRange(newList); } return(syntaxTrivias); }
public override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); if (!TryFindFirstAncestorOrSelf(root, context.Span, out CompilationUnitSyntax compilationUnit)) { return; } Document document = context.Document; Diagnostic diagnostic = context.Diagnostics[0]; switch (diagnostic.Id) { case DiagnosticIdentifiers.NormalizeWhitespaceAtBeginningOfFile: { SyntaxToken token = compilationUnit.EndOfFileToken; if (token.FullSpan.Start > 0) { token = compilationUnit.GetFirstToken(); } SyntaxTriviaList leading = token.LeadingTrivia; string title; if (leading[0].IsWhitespaceTrivia() && (leading.Count == 1 || leading[1].IsEndOfLineTrivia())) { title = "Remove whitespace"; } else { title = CodeFixTitles.RemoveNewLine; } CodeAction codeAction = CodeAction.Create( title, ct => { SyntaxToken token = compilationUnit.EndOfFileToken; if (token.FullSpan.Start > 0) { token = compilationUnit.GetFirstToken(); } SyntaxTriviaList leading = token.LeadingTrivia; int count = leading.TakeWhile(f => f.IsWhitespaceOrEndOfLineTrivia()).Count(); SyntaxToken newToken = token.WithLeadingTrivia(leading.RemoveRange(0, count)); return(document.ReplaceTokenAsync(token, newToken, ct)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); break; } } }