本文整理汇总了C#中ExpressionSyntax.GetLeadingTrivia方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.GetLeadingTrivia方法的具体用法?C# ExpressionSyntax.GetLeadingTrivia怎么用?C# ExpressionSyntax.GetLeadingTrivia使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.GetLeadingTrivia方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TrySimplifyMemberAccessOrQualifiedName
// Note: The caller needs to verify that replacement doesn't change semantics of the original expression.
private static bool TrySimplifyMemberAccessOrQualifiedName(
ExpressionSyntax left,
ExpressionSyntax right,
SemanticModel semanticModel,
OptionSet optionSet,
out ExpressionSyntax replacementNode,
out TextSpan issueSpan)
{
replacementNode = null;
issueSpan = default(TextSpan);
if (left != null && right != null)
{
var leftSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, left);
if (leftSymbol != null && (leftSymbol.Kind == SymbolKind.NamedType))
{
var rightSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, right);
if (rightSymbol != null && (rightSymbol.IsStatic || rightSymbol.Kind == SymbolKind.NamedType))
{
// Static member access or nested type member access.
INamedTypeSymbol containingType = rightSymbol.ContainingType;
var enclosingSymbol = semanticModel.GetEnclosingSymbol(left.SpanStart);
List<ISymbol> enclosingTypeParametersInsideOut = new List<ISymbol>();
while (enclosingSymbol != null)
{
if (enclosingSymbol is IMethodSymbol)
{
var methodSymbol = (IMethodSymbol)enclosingSymbol;
if (methodSymbol.TypeArguments.Length != 0)
{
enclosingTypeParametersInsideOut.AddRange(methodSymbol.TypeArguments);
}
}
if (enclosingSymbol is INamedTypeSymbol)
{
var namedTypeSymbol = (INamedTypeSymbol)enclosingSymbol;
if (namedTypeSymbol.TypeArguments.Length != 0)
{
enclosingTypeParametersInsideOut.AddRange(namedTypeSymbol.TypeArguments);
}
}
enclosingSymbol = enclosingSymbol.ContainingSymbol;
}
if (containingType != null && !containingType.Equals(leftSymbol))
{
var namedType = leftSymbol as INamedTypeSymbol;
if (namedType != null)
{
if ((namedType.GetBaseTypes().Contains(containingType) &&
!optionSet.GetOption(SimplificationOptions.AllowSimplificationToBaseType)) ||
(!optionSet.GetOption(SimplificationOptions.AllowSimplificationToGenericType) &&
containingType.TypeArguments.Length != 0))
{
return false;
}
}
// We have a static member access or a nested type member access using a more derived type.
// Simplify syntax so as to use accessed member's most immediate containing type instead of the derived type.
replacementNode = containingType.GenerateTypeSyntax()
.WithLeadingTrivia(left.GetLeadingTrivia())
.WithTrailingTrivia(left.GetTrailingTrivia());
issueSpan = left.Span;
return true;
}
}
}
}
return false;
}
示例2: CreatePredefinedTypeSyntax
private static TypeSyntax CreatePredefinedTypeSyntax(ExpressionSyntax expression, SyntaxKind keywordKind)
{
return SyntaxFactory.PredefinedType(SyntaxFactory.Token(expression.GetLeadingTrivia(), keywordKind, expression.GetTrailingTrivia()));
}
示例3: FullyQualifyIdentifierName
private ExpressionSyntax FullyQualifyIdentifierName(
ISymbol symbol,
ExpressionSyntax rewrittenNode,
ExpressionSyntax originalNode,
bool replaceNode,
bool isInsideCref,
bool omitLeftHandSide)
{
Debug.Assert(!replaceNode || rewrittenNode.Kind() == SyntaxKind.IdentifierName);
//// TODO: use and expand Generate*Syntax(isymbol) to not depend on symbol display any more.
//// See GenerateExpressionSyntax();
var result = rewrittenNode;
// only if this symbol has a containing type or namespace there is work for us to do.
if (replaceNode || symbol.ContainingType != null || symbol.ContainingNamespace != null)
{
ImmutableArray<SymbolDisplayPart> displayParts;
ExpressionSyntax left = null;
// we either need to create an AliasQualifiedName if the symbol is directly contained in the global namespace,
// otherwise it a QualifiedName.
if (!replaceNode && symbol.ContainingType == null && symbol.ContainingNamespace.IsGlobalNamespace)
{
return rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.AliasQualifiedName(
SyntaxFactory.IdentifierName(SyntaxFactory.Token(SyntaxKind.GlobalKeyword)),
(SimpleNameSyntax)rewrittenNode.WithLeadingTrivia(null))
.WithLeadingTrivia(rewrittenNode.GetLeadingTrivia()));
}
displayParts = replaceNode
? symbol.ToDisplayParts(s_typeNameFormatWithGenerics)
: (symbol.ContainingType ?? (ISymbol)symbol.ContainingNamespace).ToDisplayParts(s_typeNameFormatWithGenerics);
rewrittenNode = TryAddTypeArgumentToIdentifierName(rewrittenNode, symbol);
// Replaces the '<' token with the '{' token since we are inside crefs
rewrittenNode = TryReplaceAngleBracesWithCurlyBraces(rewrittenNode, isInsideCref);
result = rewrittenNode;
if (!omitLeftHandSide)
{
left = SyntaxFactory.ParseTypeName(displayParts.ToDisplayString());
// Replaces the '<' token with the '{' token since we are inside crefs
left = TryReplaceAngleBracesWithCurlyBraces(left, isInsideCref);
if (replaceNode)
{
return left
.WithLeadingTrivia(rewrittenNode.GetLeadingTrivia())
.WithTrailingTrivia(rewrittenNode.GetTrailingTrivia());
}
// now create syntax for the combination of left and right syntax, or a simple replacement in case of an identifier
var parent = originalNode.Parent;
var leadingTrivia = rewrittenNode.GetLeadingTrivia();
rewrittenNode = rewrittenNode.WithLeadingTrivia(null);
switch (parent.Kind())
{
case SyntaxKind.QualifiedName:
var qualifiedParent = (QualifiedNameSyntax)parent;
result = rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.QualifiedName(
(NameSyntax)left,
(SimpleNameSyntax)rewrittenNode));
break;
case SyntaxKind.SimpleMemberAccessExpression:
var memberAccessParent = (MemberAccessExpressionSyntax)parent;
result = rewrittenNode.CopyAnnotationsTo(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
left,
(SimpleNameSyntax)rewrittenNode));
break;
default:
Debug.Assert(rewrittenNode is SimpleNameSyntax);
if (SyntaxFacts.IsInNamespaceOrTypeContext(originalNode))
{
var right = (SimpleNameSyntax)rewrittenNode;
result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right.WithAdditionalAnnotations(Simplifier.SpecialTypeAnnotation)));
}
else if (originalNode.Parent is CrefSyntax)
{
var right = (SimpleNameSyntax)rewrittenNode;
result = rewrittenNode.CopyAnnotationsTo(SyntaxFactory.QualifiedName((NameSyntax)left, right));
}
else
{
//.........这里部分代码省略.........
示例4: TryAddTypeArgumentToIdentifierName
private ExpressionSyntax TryAddTypeArgumentToIdentifierName(ExpressionSyntax newNode, ISymbol symbol)
{
if (newNode.Kind() == SyntaxKind.IdentifierName && symbol.Kind == SymbolKind.Method)
{
if (((IMethodSymbol)symbol).TypeArguments.Length != 0)
{
var typeArguments = ((IMethodSymbol)symbol).TypeArguments;
if (!typeArguments.Any(t => t.ContainsAnonymousType()))
{
var genericName = SyntaxFactory.GenericName(
((IdentifierNameSyntax)newNode).Identifier,
SyntaxFactory.TypeArgumentList(
SyntaxFactory.SeparatedList(
typeArguments.Select(p => SyntaxFactory.ParseTypeName(p.ToDisplayParts(s_typeNameFormatWithGenerics).ToDisplayString())))))
.WithLeadingTrivia(newNode.GetLeadingTrivia())
.WithTrailingTrivia(newNode.GetTrailingTrivia())
.WithAdditionalAnnotations(Simplifier.Annotation);
genericName = newNode.CopyAnnotationsTo(genericName);
return genericName;
}
}
}
return newNode;
}
示例5: ConvertToAwaitExpression
private static SyntaxNode ConvertToAwaitExpression(ExpressionSyntax expression)
{
if ((expression is BinaryExpressionSyntax || expression is ConditionalExpressionSyntax) && expression.HasTrailingTrivia)
{
var expWithTrailing = expression.WithoutLeadingTrivia();
var span = expWithTrailing.GetLocation().GetLineSpan().Span;
if (span.Start.Line == span.End.Line && !expWithTrailing.DescendantTrivia().Any(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia)))
{
return SyntaxFactory.AwaitExpression(SyntaxFactory.ParenthesizedExpression(expWithTrailing))
.WithLeadingTrivia(expression.GetLeadingTrivia())
.WithAdditionalAnnotations(Formatter.Annotation);
}
}
return SyntaxFactory.AwaitExpression(expression.WithoutTrivia().Parenthesize())
.WithTriviaFrom(expression)
.WithAdditionalAnnotations(Simplifier.Annotation, Formatter.Annotation);
}
示例6: RewriteRoleExpression
private ExpressionSyntax RewriteRoleExpression(ExpressionSyntax expression)
{
var isRole = IsRole(expression);
if (isRole)
{
var fieldName = prefix + GetRoleName(expression);
expression = (SimpleNameSyntax)Syntax.IdentifierName(fieldName).WithLeadingTrivia(expression.GetLeadingTrivia());
}
return expression;
}