本文整理汇总了C#中SyntaxGenerator.IdentifierName方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxGenerator.IdentifierName方法的具体用法?C# SyntaxGenerator.IdentifierName怎么用?C# SyntaxGenerator.IdentifierName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxGenerator
的用法示例。
在下文中一共展示了SyntaxGenerator.IdentifierName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetInvertedStatement
private SyntaxNode GetInvertedStatement(
SyntaxGenerator generator, IMethodSymbol containingOperator, Compilation compilation)
{
if (containingOperator.Name == WellKnownMemberNames.EqualityOperatorName)
{
return generator.ReturnStatement(
generator.LogicalNotExpression(
generator.ValueEqualsExpression(
generator.IdentifierName(containingOperator.Parameters[0].Name),
generator.IdentifierName(containingOperator.Parameters[1].Name))));
}
else if (containingOperator.Name == WellKnownMemberNames.InequalityOperatorName)
{
return generator.ReturnStatement(
generator.LogicalNotExpression(
generator.ValueNotEqualsExpression(
generator.IdentifierName(containingOperator.Parameters[0].Name),
generator.IdentifierName(containingOperator.Parameters[1].Name))));
}
else
{
// If it's a < > <= or >= operator then we can't simply invert a call
// to the existing operator. i.e. the body of the "<" method should *not* be:
// return !(a > b);
// Just provide a throwing impl for now.
return generator.DefaultMethodStatement(compilation);
}
}
示例2: CreateOrdinalMemberAccess
internal SyntaxNode CreateOrdinalMemberAccess(SyntaxGenerator syntaxFactoryService, SemanticModel model)
{
var stringComparisonType = WellKnownTypes.StringComparison(model.Compilation);
return syntaxFactoryService.MemberAccessExpression(
syntaxFactoryService.TypeExpression(stringComparisonType),
syntaxFactoryService.IdentifierName(CA1309DiagnosticAnalyzer.OrdinalText));
}
示例3: FixArgument
protected override Task<Document> FixArgument(Document document, SyntaxGenerator generator, SyntaxNode root, SyntaxNode argument)
{
var memberAccess = ((ArgumentSyntax)argument)?.Expression as MemberAccessExpressionSyntax;
if (memberAccess != null)
{
// preserve the "IgnoreCase" suffix if present
bool isIgnoreCase = memberAccess.Name.GetText().ToString().EndsWith(UseOrdinalStringComparisonAnalyzer.IgnoreCaseText, StringComparison.Ordinal);
string newOrdinalText = isIgnoreCase ? UseOrdinalStringComparisonAnalyzer.OrdinalIgnoreCaseText : UseOrdinalStringComparisonAnalyzer.OrdinalText;
SyntaxNode newIdentifier = generator.IdentifierName(newOrdinalText);
MemberAccessExpressionSyntax newMemberAccess = memberAccess.WithName((SimpleNameSyntax)newIdentifier).WithAdditionalAnnotations(Formatter.Annotation);
SyntaxNode newRoot = root.ReplaceNode(memberAccess, newMemberAccess);
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}
return Task.FromResult(document);
}
示例4: CreateEqualsExpression
internal SyntaxNode CreateEqualsExpression(SyntaxGenerator syntaxFactoryService, SemanticModel model, SyntaxNode operand1, SyntaxNode operand2, bool isEquals)
{
var stringType = model.Compilation.GetSpecialType(SpecialType.System_String);
var memberAccess = syntaxFactoryService.MemberAccessExpression(
syntaxFactoryService.TypeExpression(stringType),
syntaxFactoryService.IdentifierName(UseOrdinalStringComparisonAnalyzer.EqualsMethodName));
var ordinal = CreateOrdinalMemberAccess(syntaxFactoryService, model);
var invocation = syntaxFactoryService.InvocationExpression(
memberAccess,
operand1,
operand2.WithoutTrailingTrivia(),
ordinal)
.WithAdditionalAnnotations(Formatter.Annotation);
if (!isEquals)
{
invocation = syntaxFactoryService.LogicalNotExpression(invocation);
}
invocation = invocation.WithTrailingTrivia(operand2.GetTrailingTrivia());
return invocation;
}
示例5: TriviaVarMissingHelper
internal static SyntaxNode TriviaVarMissingHelper(SyntaxGenerator generator, IfStatementSyntax declaration)
{
var methodDecl = declaration.Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>().First();
var methodBlock = methodDecl.Body as BlockSyntax;
var secondStatement = methodBlock.Statements[1] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(secondStatement.Declaration.Variables[0].Identifier.ValueText);
var ifTrailing = generator.MemberAccessExpression(variableName, "TrailingTrivia");
var fullVariable = generator.MemberAccessExpression(ifTrailing, "First");
var parameters = new SyntaxList<SyntaxNode>();
var variableExpression = generator.InvocationExpression(fullVariable, parameters);
var localDeclaration = generator.LocalDeclarationStatement("trailingTrivia", variableExpression);
return localDeclaration;
}
示例6: KeywordHelper
internal static SyntaxNode KeywordHelper(SyntaxGenerator generator, BlockSyntax methodBlock)
{
var firstStatement = methodBlock.Statements[0] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(firstStatement.Declaration.Variables[0].Identifier.ValueText);
var initializer = generator.MemberAccessExpression(variableName, "IfKeyword");
var ifKeyword = generator.LocalDeclarationStatement("ifKeyword", initializer);
return ifKeyword;
}
示例7: TriviaCheckHelper
internal static SyntaxNode TriviaCheckHelper(SyntaxGenerator generator, BlockSyntax methodBlock, SyntaxList<StatementSyntax> ifBlockStatements)
{
var secondStatement = methodBlock.Statements[1] as LocalDeclarationStatementSyntax;
var variableName = generator.IdentifierName(secondStatement.Declaration.Variables[0].Identifier.ValueText);
var conditional = generator.MemberAccessExpression(variableName, "HasTrailingTrivia");
var ifStatement = generator.IfStatement(conditional, ifBlockStatements);
return ifStatement;
}
示例8: CreateDiagnostic
internal static SyntaxNode CreateDiagnostic(SyntaxGenerator generator, string locationName, string ruleName)
{
var identifier = generator.IdentifierName("Diagnostic");
var expression = generator.MemberAccessExpression(identifier, "Create");
SyntaxList<SyntaxNode> arguments = new SyntaxList<SyntaxNode>();
var ruleExpression = generator.IdentifierName(ruleName);
var ruleArg = generator.Argument(ruleExpression);
var locationExpression = generator.IdentifierName(locationName);
var locationArg = generator.Argument(locationExpression);
arguments = arguments.Add(ruleArg);
arguments = arguments.Add(locationArg);
string name = "diagnostic";
var initializer = generator.InvocationExpression(expression, arguments);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例9: KeywordHelper
// creates the if-keyword statement
protected internal static SyntaxNode KeywordHelper(SyntaxGenerator generator, BlockSyntax methodBlock)
{
string variableName = GetIfStatementName(methodBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
SyntaxNode initializer = generator.MemberAccessExpression(identifierName, "IfKeyword");
SyntaxNode ifKeyword = generator.LocalDeclarationStatement("ifKeyword", initializer);
return ifKeyword;
}
示例10: CreateLocation
//creates the diagnostic location statement
internal static SyntaxNode CreateLocation(SyntaxGenerator generator, string ifStatementIdentifier, string spanIdentifier)
{
string name = "diagnosticLocation";
SyntaxNode memberIdentifier = generator.IdentifierName("Location");
SyntaxNode memberName = generator.IdentifierName("Create");
SyntaxNode expression = generator.MemberAccessExpression(memberIdentifier, memberName);
SyntaxList<SyntaxNode> arguments = new SyntaxList<SyntaxNode>();
var treeIdentifier = generator.IdentifierName(ifStatementIdentifier);
var treeArgExpression = generator.MemberAccessExpression(treeIdentifier, "SyntaxTree");
var treeArg = generator.Argument(treeArgExpression);
var spanArgIdentifier = generator.IdentifierName(spanIdentifier);
var spanArg = generator.Argument(spanArgIdentifier);
arguments = arguments.Add(treeArg);
arguments = arguments.Add(spanArg);
SyntaxNode initializer = generator.InvocationExpression(expression, arguments);
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(name, initializer);
return localDeclaration;
}
示例11: CreateEndOrStartSpan
//creates an end or start span statement
internal static SyntaxNode CreateEndOrStartSpan(SyntaxGenerator generator, string identifierString, string variableName)
{
SyntaxNode identifier = generator.IdentifierName(identifierString);
SyntaxNode initializer = generator.MemberAccessExpression(identifier, "SpanStart");
SyntaxNode localDeclaration = generator.LocalDeclarationStatement(variableName, initializer);
return localDeclaration;
}
示例12: CreateRegister
// creates a correct register statement
protected internal static SyntaxNode CreateRegister(SyntaxGenerator generator, MethodDeclarationSyntax declaration, string methodName)
{
var argument1 = generator.Argument(generator.IdentifierName(methodName)) as ArgumentSyntax;
var argument2 = generator.Argument(generator.MemberAccessExpression(generator.IdentifierName("SyntaxKind"), "IfStatement")) as ArgumentSyntax;
SeparatedSyntaxList<ArgumentSyntax> arguments = new SeparatedSyntaxList<ArgumentSyntax>();
arguments = arguments.Add(argument1);
arguments = arguments.Add(argument2);
ArgumentListSyntax argumentList = SyntaxFactory.ArgumentList(arguments);
string parameterName = GetFirstParameterName(declaration);
SyntaxNode invocationExpr = BuildRegister(generator, parameterName, "RegisterSyntaxNodeAction", argumentList);
return invocationExpr;
}
示例13: Fix
private static async Task<Document> Fix(CodeFixContext context, SyntaxNode root, SyntaxGenerator generator, SemanticModel semanticModel, CancellationToken cancellationToken)
{
SyntaxNode node = root.FindNode(context.Span);
Diagnostic diagnostic = context.Diagnostics.First();
switch (diagnostic.Properties[OperatorOverloadsHaveNamedAlternatesAnalyzer.DiagnosticKindText])
{
case OperatorOverloadsHaveNamedAlternatesAnalyzer.AddAlternateText:
SyntaxNode methodDeclaration = generator.GetDeclaration(node, DeclarationKind.Operator) ?? generator.GetDeclaration(node, DeclarationKind.ConversionOperator);
var operatorOverloadSymbol = (IMethodSymbol)semanticModel.GetDeclaredSymbol(methodDeclaration, cancellationToken);
INamedTypeSymbol typeSymbol = operatorOverloadSymbol.ContainingType;
// For C# the following `typeDeclarationSyntax` and `typeDeclaration` nodes are identical, but for VB they're different so in
// an effort to keep this as language-agnostic as possible, the heavy-handed approach is used.
SyntaxNode typeDeclarationSyntax = typeSymbol.DeclaringSyntaxReferences.First().GetSyntax(cancellationToken);
SyntaxNode typeDeclaration = generator.GetDeclaration(typeDeclarationSyntax,
typeSymbol.TypeKind == TypeKind.Struct ? DeclarationKind.Struct : DeclarationKind.Class);
SyntaxNode addedMember;
IEnumerable<SyntaxNode> bodyStatements = generator.DefaultMethodBody(semanticModel.Compilation);
if (OperatorOverloadsHaveNamedAlternatesAnalyzer.IsPropertyExpected(operatorOverloadSymbol.Name))
{
// add a property
addedMember = generator.PropertyDeclaration(
name: OperatorOverloadsHaveNamedAlternatesAnalyzer.IsTrueText,
type: generator.TypeExpression(SpecialType.System_Boolean),
accessibility: Accessibility.Public,
modifiers: DeclarationModifiers.ReadOnly,
getAccessorStatements: bodyStatements);
}
else
{
// add a method
ExpectedMethodSignature expectedSignature = GetExpectedMethodSignature(operatorOverloadSymbol, semanticModel.Compilation);
if (expectedSignature.Name == "CompareTo" && operatorOverloadSymbol.ContainingType.TypeKind == TypeKind.Class)
{
var nullCheck = generator.IfStatement(
generator.InvocationExpression(
generator.IdentifierName("ReferenceEquals"),
generator.IdentifierName(expectedSignature.Parameters.First().Item1),
generator.NullLiteralExpression()),
new[]
{
generator.ReturnStatement(generator.LiteralExpression(1))
});
bodyStatements = new[] {nullCheck}.Concat(bodyStatements);
}
addedMember = generator.MethodDeclaration(
name: expectedSignature.Name,
parameters: expectedSignature.Parameters.Select(p => generator.ParameterDeclaration(p.Item1, generator.TypeExpression(p.Item2))),
returnType: generator.TypeExpression(expectedSignature.ReturnType),
accessibility: Accessibility.Public,
modifiers: expectedSignature.IsStatic ? DeclarationModifiers.Static : DeclarationModifiers.None,
statements: bodyStatements);
}
SyntaxNode newTypeDeclaration = generator.AddMembers(typeDeclaration, addedMember);
return context.Document.WithSyntaxRoot(root.ReplaceNode(typeDeclaration, newTypeDeclaration));
case OperatorOverloadsHaveNamedAlternatesAnalyzer.FixVisibilityText:
SyntaxNode badVisibilityNode = generator.GetDeclaration(node, DeclarationKind.Method) ?? generator.GetDeclaration(node, DeclarationKind.Property);
ISymbol badVisibilitySymbol = semanticModel.GetDeclaredSymbol(badVisibilityNode, cancellationToken);
SymbolEditor symbolEditor = SymbolEditor.Create(context.Document);
ISymbol newSymbol = await symbolEditor.EditOneDeclarationAsync(badVisibilitySymbol,
(documentEditor, syntaxNode) => documentEditor.SetAccessibility(badVisibilityNode, Accessibility.Public), cancellationToken).ConfigureAwait(false);
Document newDocument = symbolEditor.GetChangedDocuments().Single();
SyntaxNode newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return context.Document.WithSyntaxRoot(newRoot);
default:
return context.Document;
}
}
示例14: WhitespaceCheckHelper
// creates the whitespace check
protected internal static SyntaxNode WhitespaceCheckHelper(SyntaxGenerator generator, IfStatementSyntax ifStatement, SyntaxList<SyntaxNode> ifBlockStatements)
{
var ifBlock = ifStatement.Parent as BlockSyntax;
string variableName = GetTrailingTriviaName(ifBlock);
SyntaxNode identifierName = generator.IdentifierName(variableName);
var arguments = new SyntaxList<SyntaxNode>();
SyntaxNode trailingTriviaToString = generator.InvocationExpression(generator.MemberAccessExpression(identifierName, "ToString"), arguments);
SyntaxNode rightSide = generator.LiteralExpression(" ");
SyntaxNode equalsExpression = generator.ValueEqualsExpression(trailingTriviaToString, rightSide);
SyntaxNode newIfStatement = generator.IfStatement(equalsExpression, ifBlockStatements);
return newIfStatement;
}
示例15: CreateEmptyRule
// creates a variable holding a DiagnosticDescriptor
// uses SyntaxFactory for formatting
protected internal static FieldDeclarationSyntax CreateEmptyRule(SyntaxGenerator generator, string idName="", string titleDefault="Enter a title for this diagnostic", string messageDefault="Enter a message to be displayed with this diagnostic",
string categoryDefault="Enter a category for this diagnostic (e.g. Formatting)", ExpressionSyntax severityDefault=null, ExpressionSyntax enabledDefault=null)
{
if (severityDefault == null)
{
severityDefault = generator.DefaultExpression(SyntaxFactory.ParseTypeName("DiagnosticSeverity")) as ExpressionSyntax;
}
if (enabledDefault == null)
{
enabledDefault = generator.DefaultExpression(generator.TypeExpression(SpecialType.System_Boolean)) as ExpressionSyntax;
}
var type = SyntaxFactory.ParseTypeName("DiagnosticDescriptor");
var arguments = new ArgumentSyntax[6];
string whitespace = " ";
SyntaxNode id = null;
if (idName != "")
{
id = generator.LiteralExpression(idName);
}
else
{
id = generator.IdentifierName("").WithTrailingTrivia(SyntaxFactory.ParseTrailingTrivia("/* The ID here should be the public constant declared above */"));
}
var idArg = generator.Argument("id", RefKind.None, id).WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[0] = idArg;
SyntaxNode title = generator.LiteralExpression(titleDefault);
var titleArg = generator.Argument("title", RefKind.None, title).WithLeadingTrivia(SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[1] = titleArg;
SyntaxNode message = generator.LiteralExpression(messageDefault);
var messageArg = generator.Argument("messageFormat", RefKind.None, message).WithLeadingTrivia(SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[2] = messageArg;
SyntaxNode category = generator.LiteralExpression(categoryDefault);
var categoryArg = generator.Argument("category", RefKind.None, category).WithLeadingTrivia(SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[3] = categoryArg;
var defaultSeverityArg = generator.Argument("defaultSeverity", RefKind.None, severityDefault).WithLeadingTrivia(SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[4] = defaultSeverityArg;
var enabledArg = generator.Argument("isEnabledByDefault", RefKind.None, enabledDefault).WithLeadingTrivia(SyntaxFactory.Whitespace(whitespace)) as ArgumentSyntax;
arguments[5] = enabledArg;
SyntaxToken identifier = SyntaxFactory.ParseToken("spacingRule");
var separators = new List<SyntaxToken>();
SyntaxToken separator = SyntaxFactory.ParseToken(",").WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
separators.Add(separator);
separators.Add(separator);
separators.Add(separator);
separators.Add(separator);
separators.Add(separator);
SeparatedSyntaxList<ArgumentSyntax> argumentsNewLines = SyntaxFactory.SeparatedList(arguments, separators);
ArgumentListSyntax argumentList = SyntaxFactory.ArgumentList(argumentsNewLines);
ObjectCreationExpressionSyntax value = SyntaxFactory.ObjectCreationExpression(type, argumentList, null);
EqualsValueClauseSyntax initializer = SyntaxFactory.EqualsValueClause(value);
var variables = new SeparatedSyntaxList<VariableDeclaratorSyntax>();
VariableDeclaratorSyntax variable = SyntaxFactory.VariableDeclarator(identifier, null, initializer);
variables = variables.Add(variable);
VariableDeclarationSyntax declaration = SyntaxFactory.VariableDeclaration(type.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), variables);
SyntaxTokenList modifiers = SyntaxFactory.TokenList(SyntaxFactory.ParseToken("internal").WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), SyntaxFactory.ParseToken("static").WithTrailingTrivia(SyntaxFactory.Whitespace(" ")));
FieldDeclarationSyntax rule = SyntaxFactory.FieldDeclaration(new SyntaxList<AttributeListSyntax>(), modifiers, declaration);
return rule;
}