本文整理汇总了C#中SyntaxToken.GetLineSpan方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.GetLineSpan方法的具体用法?C# SyntaxToken.GetLineSpan怎么用?C# SyntaxToken.GetLineSpan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.GetLineSpan方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPreviousEmptyLines
private IEnumerable<SyntaxTrivia> GetPreviousEmptyLines(SyntaxToken openBrace)
{
var result = new List<SyntaxTrivia>();
var lineOfOpenBrace = openBrace.GetLineSpan().StartLinePosition.Line;
var lineToCheck = lineOfOpenBrace - 1;
while (lineToCheck > -1)
{
var trivias = openBrace.LeadingTrivia
.Where(t => t.GetLineSpan().StartLinePosition.Line == lineToCheck)
.ToList();
var endOfLineTrivia = trivias.Where(t => t.IsKind(SyntaxKind.EndOfLineTrivia)).ToList();
if (endOfLineTrivia.Any() && trivias.Except(endOfLineTrivia).All(t => t.IsKind(SyntaxKind.WhitespaceTrivia)))
{
lineToCheck--;
result.AddRange(trivias);
}
else
{
break;
}
}
return result;
}
示例2: CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame
private static void CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame(SyntaxNodeAnalysisContext context, SyntaxToken openToken, bool preserveLayout)
{
var previousToken = openToken.GetPreviousToken();
if (previousToken.IsMissing)
{
return;
}
var prevTokenLine = previousToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (prevTokenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != prevTokenLine.StartLinePosition.Line)
{
var properties = preserveLayout ? OpenCloseSpacingCodeFixProvider.RemovePrecedingPreserveLayout : OpenCloseSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openToken.GetLocation(), properties));
}
}
开发者ID:robinsedlaczek,项目名称:StyleCopAnalyzers,代码行数:18,代码来源:SA1110OpeningParenthesisMustBeOnDeclarationLine.cs
示例3: CheckIfLocationOfLastArgumentOrParameterAndCloseTokenAreTheSame
private static void CheckIfLocationOfLastArgumentOrParameterAndCloseTokenAreTheSame(
SyntaxNodeAnalysisContext context,
CSharpSyntaxNode parameterOrArgument,
SyntaxToken closeToken)
{
var lastParameterLine = parameterOrArgument.GetLineSpan();
var closeParenLine = closeToken.GetLineSpan();
if (lastParameterLine.IsValid &&
closeParenLine.IsValid &&
closeParenLine.StartLinePosition.Line != lastParameterLine.EndLinePosition.Line)
{
var properties = TokenSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties));
}
}
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:15,代码来源:SA1111ClosingParenthesisMustBeOnLineOfLastParameter.cs
示例4: IsAccessorWithSingleLineBlock
private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, SyntaxToken curlyBracketToken)
{
if (!curlyBracketToken.IsKind(SyntaxKind.OpenBraceToken))
{
return false;
}
switch (previousToken.Kind())
{
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
case SyntaxKind.AddKeyword:
case SyntaxKind.RemoveKeyword:
break;
default:
return false;
}
var token = curlyBracketToken;
var depth = 1;
while (depth > 0)
{
token = token.GetNextToken();
switch (token.Kind())
{
case SyntaxKind.CloseBraceToken:
depth--;
break;
case SyntaxKind.OpenBraceToken:
depth++;
break;
}
}
return curlyBracketToken.GetLineSpan().StartLinePosition.Line == token.GetLineSpan().StartLinePosition.Line;
}
示例5: CheckIfLocationOfOpenAndCloseTokensAreTheSame
private static void CheckIfLocationOfOpenAndCloseTokensAreTheSame(
SyntaxNodeAnalysisContext context,
SyntaxToken openToken,
SyntaxToken closeToken)
{
var closeParenLine = closeToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (closeParenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != closeParenLine.StartLinePosition.Line)
{
var properties = TokenSpacingCodeFixProvider.RemovePreceding;
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties));
}
}
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:15,代码来源:SA1112ClosingParenthesisMustBeOnLineOfOpeningParenthesis.cs
示例6: HandleOpenParenToken
private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (token.IsLastInLine())
{
// ignore open parenthesis when last on line.
return;
}
var prevToken = token.GetPreviousToken();
// Don't check leading spaces when preceded by a keyword that is already handled by SA1000
bool precededByKeyword;
switch (prevToken.Kind())
{
case SyntaxKind.AwaitKeyword:
case SyntaxKind.CaseKeyword:
case SyntaxKind.CatchKeyword:
case SyntaxKind.CheckedKeyword:
case SyntaxKind.DefaultKeyword:
case SyntaxKind.FixedKeyword:
case SyntaxKind.ForKeyword:
case SyntaxKind.ForEachKeyword:
case SyntaxKind.FromKeyword:
case SyntaxKind.GroupKeyword:
case SyntaxKind.IfKeyword:
case SyntaxKind.InKeyword:
case SyntaxKind.IntoKeyword:
case SyntaxKind.JoinKeyword:
case SyntaxKind.LetKeyword:
case SyntaxKind.LockKeyword:
case SyntaxKind.NameOfKeyword:
case SyntaxKind.NewKeyword:
case SyntaxKind.OrderByKeyword:
case SyntaxKind.ReturnKeyword:
case SyntaxKind.SelectKeyword:
case SyntaxKind.SizeOfKeyword:
case SyntaxKind.StackAllocKeyword:
case SyntaxKind.SwitchKeyword:
case SyntaxKind.ThrowKeyword:
case SyntaxKind.TypeOfKeyword:
case SyntaxKind.UncheckedKeyword:
case SyntaxKind.UsingKeyword:
case SyntaxKind.WhereKeyword:
case SyntaxKind.WhileKeyword:
case SyntaxKind.YieldKeyword:
precededByKeyword = true;
break;
default:
precededByKeyword = false;
break;
}
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);
var isFirstOnLine = false;
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
{
var done = false;
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)
{
switch (leadingTriviaList[i].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
isFirstOnLine = true;
done = true;
break;
default:
done = true;
break;
}
}
}
bool haveLeadingSpace;
bool partOfUnaryExpression;
bool startOfIndexer;
var prevTokenIsOpenParen = prevToken.IsKind(SyntaxKind.OpenParenToken);
switch (token.Parent.Kind())
{
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForEachStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.FixedStatement:
case SyntaxKind.LockStatement:
case SyntaxKind.UsingStatement:
//.........这里部分代码省略.........
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1008OpeningParenthesisMustBeSpacedCorrectly.cs
示例7: CheckCurlyBracketToken
private static void CheckCurlyBracketToken(SyntaxNodeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
int line = token.GetLineSpan().StartLinePosition.Line;
SyntaxToken previousToken = token.GetPreviousToken();
if (!previousToken.IsMissing)
{
if (previousToken.GetLineSpan().StartLinePosition.Line == line)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
// no need to report more than one instance for this token
return;
}
}
SyntaxToken nextToken = token.GetNextToken();
if (!nextToken.IsMissing)
{
switch (nextToken.Kind())
{
case SyntaxKind.CloseParenToken:
case SyntaxKind.CommaToken:
case SyntaxKind.SemicolonToken:
case SyntaxKind.DotToken:
// these are allowed to appear on the same line
return;
case SyntaxKind.None:
// last token of this file
return;
default:
break;
}
if (nextToken.GetLineSpan().StartLinePosition.Line == line)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation()));
}
}
}
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:47,代码来源:SA1500CurlyBracketsForMultiLineStatementsMustNotShareLine.cs
示例8: CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame
private static void CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame(SyntaxNodeAnalysisContext context, SyntaxToken openToken, bool preserveLayout)
{
var previousToken = openToken.GetPreviousToken();
if (previousToken.IsMissing)
{
return;
}
var prevTokenLine = previousToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (prevTokenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != prevTokenLine.StartLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = preserveLayout ? OpenCloseSpacingCodeFixProvider.LayoutPreserve : OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openToken.GetLocation(), properties.ToImmutableDictionary()));
}
}
开发者ID:iaingalloway,项目名称:StyleCopAnalyzers,代码行数:23,代码来源:SA1110OpeningParenthesisMustBeOnDeclarationLine.cs
示例9: CheckViolation
private static void CheckViolation(SyntaxNodeAnalysisContext context, SyntaxToken openBraceToken, SyntaxToken closeBraceToken)
{
var openingBraceLineSpan = openBraceToken.GetLineSpan();
var closingBraceLineSpan = closeBraceToken.GetLineSpan();
if (openingBraceLineSpan.EndLinePosition.Line == closingBraceLineSpan.StartLinePosition.Line)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openBraceToken.GetLocation()));
}
}
示例10: CheckIfLocationOfOpenAndCloseTokensAreTheSame
private static void CheckIfLocationOfOpenAndCloseTokensAreTheSame(SyntaxNodeAnalysisContext context,
SyntaxToken openToken, SyntaxToken closeToken)
{
var closeParenLine = closeToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (closeParenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != closeParenLine.StartLinePosition.Line)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation()));
}
}
开发者ID:nukefusion,项目名称:StyleCopAnalyzers,代码行数:12,代码来源:SA1112ClosingParenthesisMustBeOnLineOfOpeningParenthesis.cs
示例11: HandleOpenParenToken
private static void HandleOpenParenToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
if (token.IsMissing)
{
return;
}
if (token.IsLastInLine())
{
// ignore open parenthesis when last on line.
return;
}
var prevToken = token.GetPreviousToken();
var leadingTriviaList = TriviaHelper.MergeTriviaLists(prevToken.TrailingTrivia, token.LeadingTrivia);
var isFirstOnLine = false;
if (prevToken.GetLineSpan().EndLinePosition.Line < token.GetLineSpan().StartLinePosition.Line)
{
var done = false;
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--)
{
switch (leadingTriviaList[i].Kind())
{
case SyntaxKind.WhitespaceTrivia:
break;
case SyntaxKind.EndOfLineTrivia:
isFirstOnLine = true;
done = true;
break;
default:
done = true;
break;
}
}
}
bool haveLeadingSpace;
bool partOfUnaryExpression;
bool startOfIndexer;
var prevTokenIsOpenParen = prevToken.IsKind(SyntaxKind.OpenParenToken);
switch (token.Parent.Kind())
{
case SyntaxKind.IfStatement:
case SyntaxKind.DoStatement:
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
case SyntaxKind.ForEachStatement:
case SyntaxKind.SwitchStatement:
case SyntaxKind.FixedStatement:
case SyntaxKind.LockStatement:
case SyntaxKind.UsingStatement:
case SyntaxKind.CatchDeclaration:
case SyntaxKind.CatchFilterClause:
haveLeadingSpace = true;
break;
case SyntaxKind.ArgumentList:
case SyntaxKind.AttributeArgumentList:
case SyntaxKind.CheckedExpression:
case SyntaxKind.UncheckedExpression:
case SyntaxKind.ConstructorConstraint:
case SyntaxKind.DefaultExpression:
case SyntaxKind.SizeOfExpression:
case SyntaxKind.TypeOfExpression:
haveLeadingSpace = false;
break;
case SyntaxKind.ParenthesizedExpression:
partOfUnaryExpression = prevToken.Parent is PrefixUnaryExpressionSyntax;
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
var partOfCastExpression = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !partOfCastExpression;
break;
case SyntaxKind.CastExpression:
partOfUnaryExpression = prevToken.Parent is PrefixUnaryExpressionSyntax;
startOfIndexer = prevToken.IsKind(SyntaxKind.OpenBracketToken);
var consecutiveCast = prevToken.IsKind(SyntaxKind.CloseParenToken) && prevToken.Parent.IsKind(SyntaxKind.CastExpression);
var partOfInterpolation = prevToken.IsKind(SyntaxKind.OpenBraceToken) && prevToken.Parent.IsKind(SyntaxKind.Interpolation);
haveLeadingSpace = !partOfUnaryExpression && !startOfIndexer && !consecutiveCast && !partOfInterpolation;
break;
case SyntaxKind.ParameterList:
var partOfLambdaExpression = token.Parent.Parent.IsKind(SyntaxKind.ParenthesizedLambdaExpression);
haveLeadingSpace = partOfLambdaExpression;
break;
default:
haveLeadingSpace = false;
break;
}
// Ignore spacing before if another opening parenthesis is before this.
//.........这里部分代码省略.........
开发者ID:nvincent,项目名称:StyleCopAnalyzers,代码行数:101,代码来源:SA1008OpeningParenthesisMustBeSpacedCorrectly.cs
示例12: CheckIfLocationOfLastArgumentOrParameterAndCloseTokenAreTheSame
private static void CheckIfLocationOfLastArgumentOrParameterAndCloseTokenAreTheSame(
SyntaxNodeAnalysisContext context,
CSharpSyntaxNode parameterOrArgument, SyntaxToken closeToken)
{
var lastParameterLine = parameterOrArgument.GetLineSpan();
var closeParenLine = closeToken.GetLineSpan();
if (lastParameterLine.IsValid &&
closeParenLine.IsValid &&
closeParenLine.StartLinePosition.Line != lastParameterLine.EndLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties.ToImmutableDictionary()));
}
}
开发者ID:chrfin,项目名称:StyleCopAnalyzers,代码行数:19,代码来源:SA1111ClosingParenthesisMustBeOnLineOfLastParameter.cs
示例13: CheckIfLocationOfOpenAndCloseTokensAreTheSame
private static void CheckIfLocationOfOpenAndCloseTokensAreTheSame(
SyntaxNodeAnalysisContext context,
SyntaxToken openToken, SyntaxToken closeToken)
{
var closeParenLine = closeToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (closeParenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != closeParenLine.StartLinePosition.Line)
{
var properties = new Dictionary<string, string>
{
[OpenCloseSpacingCodeFixProvider.LocationKey] = OpenCloseSpacingCodeFixProvider.LocationPreceding,
[OpenCloseSpacingCodeFixProvider.ActionKey] = OpenCloseSpacingCodeFixProvider.ActionRemove,
[OpenCloseSpacingCodeFixProvider.LayoutKey] = OpenCloseSpacingCodeFixProvider.LayoutPack
};
context.ReportDiagnostic(Diagnostic.Create(Descriptor, closeToken.GetLocation(), properties.ToImmutableDictionary()));
}
}
开发者ID:iaingalloway,项目名称:StyleCopAnalyzers,代码行数:19,代码来源:SA1112ClosingParenthesisMustBeOnLineOfOpeningParenthesis.cs
示例14: CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame
private static void CheckIfLocationOfPreviousTokenAndOpenTokenAreTheSame(SyntaxNodeAnalysisContext context, SyntaxToken openToken)
{
var previousToken = openToken.GetPreviousToken();
if (previousToken.IsMissing)
{
return;
}
var prevTokenLine = previousToken.GetLineSpan();
var openParenLine = openToken.GetLineSpan();
if (prevTokenLine.IsValid &&
openParenLine.IsValid &&
openParenLine.StartLinePosition.Line != prevTokenLine.StartLinePosition.Line)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, openToken.GetLocation()));
}
}
开发者ID:hickford,项目名称:StyleCopAnalyzers,代码行数:17,代码来源:SA1110OpeningParenthesisMustBeOnDeclarationLine.cs
示例15: ReformatElement
private SyntaxNode ReformatElement(SyntaxNode syntaxRoot, SyntaxNode element, SyntaxToken openBraceToken, SyntaxToken closeBraceToken, IndentationOptions indentationOptions)
{
var tokenSubstitutions = new Dictionary<SyntaxToken, SyntaxToken>();
var parentLastToken = openBraceToken.GetPreviousToken();
var parentEndLine = parentLastToken.GetLineSpan().EndLinePosition.Line;
var blockStartLine = openBraceToken.GetLineSpan().StartLinePosition.Line;
// reformat parent if it is on the same line as the block.
if (parentEndLine == blockStartLine)
{
var newTrailingTrivia = parentLastToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
tokenSubstitutions.Add(parentLastToken, parentLastToken.WithTrailingTrivia(newTrailingTrivia));
}
var parentIndentationLevel = IndentationHelper.GetIndentationSteps(indentationOptions, element);
var indentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel);
var contentIndentationString = IndentationHelper.GenerateIndentationString(indentationOptions, parentIndentationLevel + 1);
// reformat opening brace
tokenSubstitutions.Add(openBraceToken, this.FormatBraceToken(openBraceToken, indentationString));
// reformat start of content
var startOfContentToken = openBraceToken.GetNextToken();
if (startOfContentToken != closeBraceToken)
{
var newStartOfContentTokenLeadingTrivia = startOfContentToken.LeadingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.Whitespace(contentIndentationString));
tokenSubstitutions.Add(startOfContentToken, startOfContentToken.WithLeadingTrivia(newStartOfContentTokenLeadingTrivia));
}
// reformat end of content
var endOfContentToken = closeBraceToken.GetPreviousToken();
if (endOfContentToken != openBraceToken)
{
var newEndOfContentTokenTrailingTrivia = endOfContentToken.TrailingTrivia
.WithoutTrailingWhitespace()
.Add(SyntaxFactory.CarriageReturnLineFeed);
// check if the token already exists (occurs when there is only one token in the block)
if (tokenSubstitutions.ContainsKey(endOfContentToken))
{
tokenSubstitutions[endOfContentToken] = tokenSubstitutions[endOfContentToken].WithTrailingTrivia(newEndOfContentTokenTrailingTrivia);
}
else
{
tokenSubstitutions.Add(endOfContentToken, endOfContentToken.WithTrailingTrivia(newEndOfContentTokenTrailingTrivia));
}
}
// reformat closing brace
tokenSubstitutions.Add(closeBraceToken, this.FormatBraceToken(closeBraceToken, indentationString));
var rewriter = new TokenRewriter(tokenSubstitutions);
var newSyntaxRoot = rewriter.Visit(syntaxRoot);
return newSyntaxRoot;
}