本文整理汇总了C#中SyntaxToken.WithTrailingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.WithTrailingTrivia方法的具体用法?C# SyntaxToken.WithTrailingTrivia怎么用?C# SyntaxToken.WithTrailingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.WithTrailingTrivia方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
if (_lastTokenEndedInWhitespace)
{
token = token.WithLeadingTrivia(Enumerable.Empty<SyntaxTrivia>());
}
else if (token.LeadingTrivia.Count > 0)
{
if (_useElasticTrivia)
{
token = token.WithLeadingTrivia(SyntaxFactory.ElasticSpace);
}
else
{
token = token.WithLeadingTrivia(SyntaxFactory.Space);
}
}
if (token.TrailingTrivia.Count > 0)
{
if (_useElasticTrivia)
{
token = token.WithTrailingTrivia(SyntaxFactory.ElasticSpace);
}
else
{
token = token.WithTrailingTrivia(SyntaxFactory.Space);
}
_lastTokenEndedInWhitespace = true;
}
else
{
_lastTokenEndedInWhitespace = false;
}
if (token.Kind() == SyntaxKind.StringLiteralToken ||
token.Kind() == SyntaxKind.InterpolatedStringTextToken)
{
if (s_newlinePattern.IsMatch(token.Text))
{
var newText = s_newlinePattern.Replace(token.Text, " ");
token = SyntaxFactory.Token(
token.LeadingTrivia,
token.Kind(),
newText, newText,
token.TrailingTrivia);
}
}
return token;
}
示例2: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
token = base.VisitToken(token);
if (token.IsMissing)
{
return token;
}
bool changed;
do
{
changed = false;
if ((token.HasLeadingTrivia && token.LeadingTrivia.Count >= 2) ||
(token.HasTrailingTrivia && token.TrailingTrivia.Count >= 2))
{
var newLeadingTrivia = RemoveBlankLines(token.LeadingTrivia, ref changed);
var newTrailingTrivia = RemoveBlankLines(token.TrailingTrivia, ref changed);
if (changed)
{
token = token.WithLeadingTrivia(Syntax.TriviaList(newLeadingTrivia));
token = token.WithTrailingTrivia(Syntax.TriviaList(newTrailingTrivia));
}
}
}
while (changed);
return token;
}
示例3: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
token = base.VisitToken(token);
if (token.IsMissing)
{
return token;
}
if (token.Kind != SyntaxKind.CloseBraceToken)
{
return token;
}
var nextToken = token.GetNextToken(includeSkipped: true);
var tokenLine = syntaxTree.GetText().GetLineNumberFromPosition(token.Span.Start);
var nextTokenLine = syntaxTree.GetText().GetLineNumberFromPosition(nextToken.Span.Start);
var nextTokenIsCloseBrace = nextToken.Kind == SyntaxKind.CloseBraceToken;
var expectedDiff = nextTokenIsCloseBrace ? 1 : 2;
if (nextTokenLine == tokenLine + expectedDiff)
{
return token;
}
var nonNewLineTrivia = token.TrailingTrivia.Where(t => t.Kind != SyntaxKind.EndOfLineTrivia);
var newTrivia = nonNewLineTrivia.Concat(Enumerable.Repeat(Syntax.EndOfLine("\r\n"), expectedDiff));
return token.WithTrailingTrivia(Syntax.TriviaList(newTrivia));
}
示例4: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
SyntaxToken corrected = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, SyntaxFactory.Space));
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
示例5: AddToken
// Process a token. and add to the list of triva/tokens we're accumulating.
public void AddToken(SyntaxToken token, bool isFirst, bool isLast)
{
bool isMissing = token.IsMissing;
if (token.HasLeadingTrivia && (isFirst || isMissing || token.GetLeadingTrivia().TriviaListContainsStructuredTrivia()))
{
FinishInProgressTokens();
AddTrivia(token.GetLeadingTrivia());
token = ((SyntaxToken)token.WithLeadingTrivia(null));
}
////if (!preserveExistingDiagnostics)
////{
//// token = token.WithoutDiagnostics();
////}
SyntaxNode trailingTrivia = null;
if (token.HasTrailingTrivia && (isLast || isMissing || token.GetTrailingTrivia().TriviaListContainsStructuredTrivia()))
{
trailingTrivia = token.GetTrailingTrivia();
token = ((SyntaxToken)token.WithTrailingTrivia(null));
}
if (isMissing)
{
// Don't add missing tokens to skipped tokens, but preserve their diagnostics.
////if (token.ContainsDiagnostics())
////{
//// // Move diagnostics on missing token to next token.
//// if (diagnosticsToAdd != null)
//// {
//// diagnosticsToAdd = diagnosticsToAdd.Concat(token.GetDiagnostics());
//// }
//// else
//// {
//// diagnosticsToAdd = token.GetDiagnostics();
//// }
//// addDiagnosticsToFirstTokenOnly = true;
////}
}
else
{
skippedTokensBuilder.Add(token);
}
if (trailingTrivia != null)
{
FinishInProgressTokens();
AddTrivia(trailingTrivia);
}
if (isFirst && addDiagnosticsToFirstTokenOnly)
{
FinishInProgressTokens(); // implicitly adds the diagnostics.
}
}
示例6: VisitToken
public override SyntaxToken VisitToken(SyntaxToken token)
{
if (_lastTokenEndedInWhitespace)
{
token = token.WithLeadingTrivia(Enumerable.Empty<SyntaxTrivia>());
}
else if (token.LeadingTrivia.Count > 0)
{
if (useElasticTrivia)
{
token = token.WithLeadingTrivia(SyntaxFactory.ElasticSpace);
}
else
{
token = token.WithLeadingTrivia(SyntaxFactory.Space);
}
}
if (token.TrailingTrivia.Count > 0)
{
if (useElasticTrivia)
{
token = token.WithTrailingTrivia(SyntaxFactory.ElasticSpace);
}
else
{
token = token.WithTrailingTrivia(SyntaxFactory.Space);
}
_lastTokenEndedInWhitespace = true;
}
else
{
_lastTokenEndedInWhitespace = false;
}
return token;
}
示例7: RenameWithinToken
private SyntaxToken RenameWithinToken(SyntaxToken oldToken, SyntaxToken newToken)
{
if (_isProcessingComplexifiedSpans ||
(_isProcessingTrivia == 0 &&
!_stringAndCommentTextSpans.Contains(oldToken.Span)))
{
return newToken;
}
if (_isRenamingInStrings)
{
if (newToken.IsKind(SyntaxKind.StringLiteralToken))
{
newToken = RenameInStringLiteral(oldToken, newToken, SyntaxFactory.Literal);
}
else if (newToken.IsKind(SyntaxKind.InterpolatedStringTextToken))
{
newToken = RenameInStringLiteral(oldToken, newToken, (leadingTrivia, text, value, trailingTrivia) =>
SyntaxFactory.Token(newToken.LeadingTrivia, SyntaxKind.InterpolatedStringTextToken, text, value, newToken.TrailingTrivia));
}
}
if (_isRenamingInComments)
{
if (newToken.IsKind(SyntaxKind.XmlTextLiteralToken))
{
newToken = RenameInStringLiteral(oldToken, newToken, SyntaxFactory.XmlTextLiteral);
}
else if (newToken.IsKind(SyntaxKind.IdentifierToken) && newToken.Parent.IsKind(SyntaxKind.XmlName) && newToken.ValueText == _originalText)
{
var newIdentifierToken = SyntaxFactory.Identifier(newToken.LeadingTrivia, _replacementText, newToken.TrailingTrivia);
newToken = newToken.CopyAnnotationsTo(_renameAnnotations.WithAdditionalAnnotations(newIdentifierToken, new RenameTokenSimplificationAnnotation() { OriginalTextSpan = oldToken.Span }));
AddModifiedSpan(oldToken.Span, newToken.Span);
}
if (newToken.HasLeadingTrivia)
{
var updatedToken = RenameInTrivia(oldToken, oldToken.LeadingTrivia);
if (updatedToken != oldToken)
{
newToken = newToken.WithLeadingTrivia(updatedToken.LeadingTrivia);
}
}
if (newToken.HasTrailingTrivia)
{
var updatedToken = RenameInTrivia(oldToken, oldToken.TrailingTrivia);
if (updatedToken != oldToken)
{
newToken = newToken.WithTrailingTrivia(updatedToken.TrailingTrivia);
}
}
}
return newToken;
}
示例8: GetNewEndTokenWithAddedPragmaAsync
internal static async Task<SyntaxToken> GetNewEndTokenWithAddedPragmaAsync(
SyntaxToken endToken,
TextSpan currentDiagnosticSpan,
Diagnostic diagnostic,
AbstractSuppressionCodeFixProvider fixer,
Func<SyntaxNode, Task<SyntaxNode>> formatNode,
bool isRemoveSuppression = false)
{
ImmutableArray<SyntaxTrivia> trivia;
var isEOF = fixer.IsEndOfFileToken(endToken);
if (isEOF)
{
trivia = endToken.LeadingTrivia.ToImmutableArray();
}
else
{
trivia = endToken.TrailingTrivia.ToImmutableArray();
}
SyntaxTrivia insertBeforeTrivia;
var index = GetPositionForPragmaInsertion(trivia, currentDiagnosticSpan, fixer, isStartToken: false, triviaAtIndex: out insertBeforeTrivia);
bool needsTrailingEOL;
if (index < trivia.Length)
{
needsTrailingEOL = !IsEndOfLineOrHasLeadingEndOfLine(insertBeforeTrivia, fixer);
}
else if (isEOF)
{
needsTrailingEOL = false;
}
else
{
needsTrailingEOL = true;
}
var pragmaTrivia = !isRemoveSuppression
? await fixer.CreatePragmaRestoreDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false)
: await fixer.CreatePragmaDisableDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false);
if (isEOF)
{
return endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia));
}
else
{
return endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaTrivia));
};
}
示例9: EnsureAndGetBraceTokens
private static void EnsureAndGetBraceTokens(
BaseTypeDeclarationSyntax typeDeclaration,
bool hasMembers,
out SyntaxToken openBrace,
out SyntaxToken closeBrace)
{
openBrace = EnsureToken(typeDeclaration.OpenBraceToken);
closeBrace = EnsureToken(typeDeclaration.CloseBraceToken, appendNewLineIfMissing: true);
if (!hasMembers)
{
// Bug 539673: If there are no members, take any trivia that
// belongs to the end brace and attach it to the opening brace.
int index = -1;
var leadingTrivia = closeBrace.LeadingTrivia;
for (int i = leadingTrivia.Count - 1; i >= 0; i--)
{
if (!leadingTrivia[i].IsWhitespaceOrEndOfLine())
{
index = i;
break;
}
}
if (index != -1)
{
openBrace = openBrace.WithTrailingTrivia(
openBrace.TrailingTrivia.Concat(closeBrace.LeadingTrivia.Take(index + 1)));
closeBrace = closeBrace.WithLeadingTrivia(
closeBrace.LeadingTrivia.Skip(index + 1));
}
}
}
示例10: UpdateTriviaList
private static SyntaxToken UpdateTriviaList(SyntaxToken token, bool isStartToken, SyntaxTriviaList triviaList, AbstractSuppressionCodeFixProvider fixer)
{
return isStartToken || fixer.IsEndOfFileToken(token)
? token.WithLeadingTrivia(triviaList)
: token.WithTrailingTrivia(triviaList);
}
开发者ID:RoryVL,项目名称:roslyn,代码行数:6,代码来源:AbstractSuppressionCodeFixProvider.RemoveSuppressionCodeAction_Pragma.cs
示例11: GetNewEndToken
private static SyntaxToken GetNewEndToken(SyntaxToken endToken, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer)
{
ImmutableArray<SyntaxTrivia> trivia;
var isEOF = fixer.IsEndOfFileToken(endToken);
if (isEOF)
{
trivia = endToken.LeadingTrivia.ToImmutableArray();
}
else
{
trivia = endToken.TrailingTrivia.ToImmutableArray();
}
SyntaxTrivia lastNonEOLTrivia = trivia.LastOrDefault(t => !fixer.IsEndOfLine(t));
// Insert the #pragma restore directive after the last trailing trivia that is not a new line trivia.
int index;
if (lastNonEOLTrivia == default(SyntaxTrivia))
{
index = 0;
}
else
{
index = trivia.IndexOf(lastNonEOLTrivia) + 1;
}
bool needsTrailingEOL;
if (index < trivia.Length)
{
needsTrailingEOL = !fixer.IsEndOfLine(trivia[index]);
}
else if (isEOF)
{
needsTrailingEOL = false;
}
else
{
needsTrailingEOL = true;
}
var pragmaRestoreTrivia = fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, needsTrailingEOL);
if (isEOF)
{
return endToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia));
}
else
{
return endToken.WithTrailingTrivia(trivia.InsertRange(index, pragmaRestoreTrivia));
}
}
开发者ID:noahfalk,项目名称:roslyn,代码行数:51,代码来源:AbstractSuppressionCodeFixProvider.PragmaWarningCodeAction.cs
示例12: UpdateReplaceMap
private static void UpdateReplaceMap(Dictionary<SyntaxToken, SyntaxToken> replaceMap, SyntaxToken token, Diagnostic diagnostic)
{
string location;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.LocationKey, out location))
{
return;
}
string action;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.ActionKey, out action))
{
return;
}
string layout;
if (!diagnostic.Properties.TryGetValue(TokenSpacingProperties.LayoutKey, out layout))
{
layout = TokenSpacingProperties.LayoutPack;
}
SyntaxTriviaList triviaList;
switch (location)
{
case TokenSpacingProperties.LocationPreceding:
var prevToken = token.GetPreviousToken();
switch (action)
{
case TokenSpacingProperties.ActionInsert:
if (!replaceMap.ContainsKey(prevToken))
{
replaceMap[token] = token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space));
}
break;
case TokenSpacingProperties.ActionRemove:
bool tokenIsFirstInLine = token.IsFirstInLine();
bool preserveLayout = layout == TokenSpacingProperties.LayoutPreserve;
triviaList = prevToken.TrailingTrivia.AddRange(token.LeadingTrivia);
if (triviaList.Any(t => t.IsDirective))
{
break;
}
replaceMap[prevToken] = prevToken.WithTrailingTrivia();
if ((!preserveLayout || !tokenIsFirstInLine)
&& triviaList.All(i => i.IsKind(SyntaxKind.WhitespaceTrivia) || i.IsKind(SyntaxKind.EndOfLineTrivia)))
{
replaceMap[token] = token.WithLeadingTrivia();
}
else if (tokenIsFirstInLine && token.IsLastInLine())
{
/* This block covers the case where `token` is the only non-trivia token on its line. However,
* the line may still contain non-whitespace trivia which we want the removal process to
* preserve. This code fix only removes the whitespace surrounding `token` if it is the only
* non-whitespace token on the line.
*/
int lastNewLineLeading = token.LeadingTrivia.LastIndexOf(SyntaxKind.EndOfLineTrivia);
int firstNewLineFollowing = token.TrailingTrivia.IndexOf(SyntaxKind.EndOfLineTrivia);
bool onlyWhitespace = true;
for (int i = lastNewLineLeading + 1; i < token.LeadingTrivia.Count; i++)
{
onlyWhitespace &= token.LeadingTrivia[i].IsKind(SyntaxKind.WhitespaceTrivia);
}
firstNewLineFollowing = firstNewLineFollowing == -1 ? token.TrailingTrivia.Count : firstNewLineFollowing;
for (int i = 0; i < firstNewLineFollowing; i++)
{
onlyWhitespace &= token.TrailingTrivia[i].IsKind(SyntaxKind.WhitespaceTrivia);
}
if (onlyWhitespace)
{
// Move the token, and remove the other tokens from its line. Keep all other surrounding
// trivia. Keep the last newline that precedes token, but not the first that follows it.
SyntaxTriviaList trailingTrivia = prevToken.TrailingTrivia;
if (lastNewLineLeading >= 0)
{
trailingTrivia = trailingTrivia.AddRange(token.LeadingTrivia.Take(lastNewLineLeading + 1));
}
// firstNewLineFollowing was adjusted above to account for the missing case.
trailingTrivia = trailingTrivia.AddRange(token.TrailingTrivia.Take(firstNewLineFollowing));
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
else
{
// Just move the token and keep all surrounding trivia.
SyntaxTriviaList trailingTrivia = triviaList.AddRange(token.TrailingTrivia);
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
}
else
{
SyntaxTriviaList trailingTrivia = triviaList.AddRange(token.TrailingTrivia.WithoutLeadingWhitespace(endOfLineIsWhitespace: false));
replaceMap[token] = token.WithLeadingTrivia().WithTrailingTrivia(trailingTrivia);
}
break;
//.........这里部分代码省略.........
示例13: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken token)
{
bool isAddingSpace = true;
switch (token.Kind())
{
case SyntaxKind.NewKeyword:
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.OpenBracketToken) || nextToken.IsKind(SyntaxKind.OpenParenToken))
{
isAddingSpace = false;
}
}
break;
case SyntaxKind.ReturnKeyword:
case SyntaxKind.ThrowKeyword:
{
SyntaxToken nextToken = token.GetNextToken();
if (nextToken.IsKind(SyntaxKind.SemicolonToken))
{
isAddingSpace = false;
}
}
break;
case SyntaxKind.CheckedKeyword:
case SyntaxKind.UncheckedKeyword:
isAddingSpace = token.GetNextToken().IsKind(SyntaxKind.OpenBraceToken);
break;
case SyntaxKind.DefaultKeyword:
case SyntaxKind.NameOfKeyword:
case SyntaxKind.SizeOfKeyword:
case SyntaxKind.TypeOfKeyword:
isAddingSpace = false;
break;
case SyntaxKind.IdentifierToken:
if (token.Text == "nameof")
{
// SA1000 would only have been reported for a nameof expression. No need to verify.
goto case SyntaxKind.NameOfKeyword;
}
return Task.FromResult(document);
default:
break;
}
if (isAddingSpace == token.HasTrailingTrivia)
{
return Task.FromResult(document);
}
if (isAddingSpace)
{
SyntaxTrivia whitespace = SyntaxFactory.Space;
SyntaxToken corrected = token.WithTrailingTrivia(token.TrailingTrivia.Insert(0, whitespace));
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
else
{
SyntaxToken corrected = token.WithoutTrailingWhitespace().WithoutFormatting();
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, corrected));
return Task.FromResult(updatedDocument);
}
}