本文整理汇总了C#中SyntaxToken.IsFirstTokenOnLine方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.IsFirstTokenOnLine方法的具体用法?C# SyntaxToken.IsFirstTokenOnLine怎么用?C# SyntaxToken.IsFirstTokenOnLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.IsFirstTokenOnLine方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatTokenAsync
public Task<IList<TextChange>> FormatTokenAsync(Workspace workspace, SyntaxToken token, CancellationToken cancellationToken)
{
Contract.ThrowIfTrue(token.Kind() == SyntaxKind.None || token.Kind() == SyntaxKind.EndOfFileToken);
// get previous token
var previousToken = token.GetPreviousToken(includeZeroWidth: true);
if (previousToken.Kind() == SyntaxKind.None)
{
// no previous token. nothing to format
return Task.FromResult(SpecializedCollections.EmptyList<TextChange>());
}
// This is a heuristic to prevent brace completion from breaking user expectation/muscle memory in common scenarios (see Devdiv:823958).
// Formatter uses FindToken on the position, which returns token to left, if there is nothing to the right and returns token to the right
// if there exists one. If the shape is "{|}", we're including '}' in the formatting range. Avoid doing that to improve verbatim typing
// in the following special scenarios.
int adjustedEndPosition = token.Span.End;
if (token.IsKind(SyntaxKind.OpenBraceToken) &&
(token.Parent.IsInitializerForArrayOrCollectionCreationExpression() ||
token.Parent is AnonymousObjectCreationExpressionSyntax))
{
var nextToken = token.GetNextToken(includeZeroWidth: true);
if (nextToken.IsKind(SyntaxKind.CloseBraceToken))
{
// Format upto '{' and exclude '}'
adjustedEndPosition = token.SpanStart;
}
}
var smartTokenformattingRules = (new SmartTokenFormattingRule()).Concat(_formattingRules);
var adjustedStartPosition = previousToken.SpanStart;
var indentStyle = workspace.Options.GetOption(FormattingOptions.SmartIndent, LanguageNames.CSharp);
if (token.IsKind(SyntaxKind.OpenBraceToken) && token.IsFirstTokenOnLine(token.SyntaxTree.GetText()) && indentStyle != FormattingOptions.IndentStyle.Smart)
{
adjustedStartPosition = token.SpanStart;
}
return Formatter.GetFormattedTextChangesAsync(_root, new TextSpan[] { TextSpan.FromBounds(adjustedStartPosition, adjustedEndPosition) }, workspace, _optionSet, smartTokenformattingRules, cancellationToken);
}
示例2: TokenShouldNotFormatOnTypeCharAsync
private static async Task<bool> TokenShouldNotFormatOnTypeCharAsync(
SyntaxToken token, CancellationToken cancellationToken)
{
// If the token is a ) we only want to format if it's the close paren
// of a using statement. That way if we have nested usings, the inner
// using will align with the outer one when the user types the close paren.
if (token.IsKind(SyntaxKind.CloseParenToken) && !token.Parent.IsKind(SyntaxKind.UsingStatement))
{
return true;
}
// If the token is a : we only want to format if it's a labeled statement
// or case. When the colon is typed we'll want ot immediately have those
// statements snap to their appropriate indentation level.
if (token.IsKind(SyntaxKind.ColonToken) && !(token.Parent.IsKind(SyntaxKind.LabeledStatement) || token.Parent is SwitchLabelSyntax))
{
return true;
}
// Only format an { if it is the first token on a line. We don't want to
// mess with it if it's inside a line.
if (token.IsKind(SyntaxKind.OpenBraceToken))
{
var text = await token.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
if (!token.IsFirstTokenOnLine(text))
{
return true;
}
}
return false;
}