本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.NameSyntax类的典型用法代码示例。如果您正苦于以下问题:C# NameSyntax类的具体用法?C# NameSyntax怎么用?C# NameSyntax使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NameSyntax类属于Microsoft.CodeAnalysis.CSharp.Syntax命名空间,在下文中一共展示了NameSyntax类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: TryClassifySymbol
private bool TryClassifySymbol(
NameSyntax name,
SymbolInfo symbolInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken,
out IEnumerable<ClassifiedSpan> result)
{
if (symbolInfo.CandidateReason == CandidateReason.Ambiguous)
{
return TryClassifyAmbiguousSymbol(name, symbolInfo, semanticModel, cancellationToken, out result);
}
// Only classify if we get one good symbol back, or if it bound to a constructor symbol with
// overload resolution/accessibility errors, or bound to type/constructor and type wasn't creatable.
var symbol = TryGetSymbol(name, symbolInfo, semanticModel);
ClassifiedSpan classifiedSpan;
if (TryClassifySymbol(name, symbol, semanticModel, cancellationToken, out classifiedSpan))
{
result = SpecializedCollections.SingletonEnumerable(classifiedSpan);
return true;
}
result = null;
return false;
}
开发者ID:jkotas,项目名称:roslyn,代码行数:26,代码来源:NameSyntaxClassifier.cs
示例2: AppendName
private static void AppendName(StringBuilder builder, NameSyntax name)
{
if (name.Kind() == SyntaxKind.QualifiedName)
{
AppendName(builder, ((QualifiedNameSyntax)name).Left);
}
switch (name.Kind())
{
case SyntaxKind.IdentifierName:
AppendDotIfNeeded(builder);
builder.Append(((IdentifierNameSyntax)name).Identifier.ValueText);
break;
case SyntaxKind.GenericName:
var genericName = (GenericNameSyntax)name;
AppendDotIfNeeded(builder);
builder.Append(genericName.Identifier.ValueText);
AppendArity(builder, genericName.Arity);
break;
case SyntaxKind.AliasQualifiedName:
var aliasQualifiedName = (AliasQualifiedNameSyntax)name;
AppendName(builder, aliasQualifiedName.Alias);
builder.Append("::");
AppendName(builder, aliasQualifiedName.Name);
break;
case SyntaxKind.QualifiedName:
AppendName(builder, ((QualifiedNameSyntax)name).Right);
break;
}
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:33,代码来源:CSharpCodeModelService.NodeNameGenerator.cs
示例3: GetCompletionsOffOfExplicitInterfaceAsync
private async Task<IEnumerable<CompletionItem>> GetCompletionsOffOfExplicitInterfaceAsync(
Document document, SemanticModel semanticModel, int position, NameSyntax name, CancellationToken cancellationToken)
{
// Bind the interface name which is to the left of the dot
var syntaxTree = semanticModel.SyntaxTree;
var nameBinding = semanticModel.GetSymbolInfo(name, cancellationToken);
var context = CSharpSyntaxContext.CreateContext(document.Project.Solution.Workspace, semanticModel, position, cancellationToken);
var symbol = nameBinding.Symbol as ITypeSymbol;
if (symbol == null || symbol.TypeKind != TypeKind.Interface)
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
var members = semanticModel.LookupSymbols(
position: name.SpanStart,
container: symbol)
.Where(s => !s.IsStatic)
.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation);
// We're going to create a entry for each one, including the signature
var completions = new List<CompletionItem>();
var signatureDisplayFormat =
new SymbolDisplayFormat(
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
memberOptions:
SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions:
SymbolDisplayParameterOptions.IncludeName |
SymbolDisplayParameterOptions.IncludeType |
SymbolDisplayParameterOptions.IncludeParamsRefOut,
miscellaneousOptions:
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes);
var namePosition = name.SpanStart;
var text = await context.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
var textChangeSpan = CompletionUtilities.GetTextChangeSpan(text, context.Position);
foreach (var member in members)
{
var displayString = member.ToMinimalDisplayString(semanticModel, namePosition, signatureDisplayFormat);
var memberCopied = member;
var insertionText = displayString;
completions.Add(new SymbolCompletionItem(
this,
displayString,
insertionText: insertionText,
filterSpan: textChangeSpan,
position: position,
symbols: new List<ISymbol> { member },
context: context,
rules: ItemRules.Instance));
}
return completions;
}
开发者ID:noahstein,项目名称:roslyn,代码行数:60,代码来源:ExplicitInterfaceCompletionProvider.cs
示例4: BuildName
private static void BuildName(NameSyntax nameSyntax, StringBuilder builder, bool includeAlias)
{
if (nameSyntax.IsKind(SyntaxKind.IdentifierName))
{
var identifierNameSyntax = (IdentifierNameSyntax)nameSyntax;
builder.Append(identifierNameSyntax.Identifier.ValueText);
}
else if (nameSyntax.IsKind(SyntaxKind.QualifiedName))
{
var qualifiedNameSyntax = (QualifiedNameSyntax)nameSyntax;
BuildName(qualifiedNameSyntax.Left, builder, includeAlias);
builder.Append(DotChar);
BuildName(qualifiedNameSyntax.Right, builder, includeAlias);
}
else if (nameSyntax.IsKind(SyntaxKind.GenericName))
{
var genericNameSyntax = (GenericNameSyntax)nameSyntax;
builder.AppendFormat("{0}{1}", genericNameSyntax.Identifier.ValueText, genericNameSyntax.TypeArgumentList.ToString());
}
else if (nameSyntax.IsKind(SyntaxKind.AliasQualifiedName))
{
var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)nameSyntax;
if (includeAlias)
{
builder.Append(aliasQualifiedNameSyntax.Alias.Identifier.ValueText);
builder.Append("::");
}
builder.Append(aliasQualifiedNameSyntax.Name.Identifier.ValueText);
}
}
开发者ID:JaRau,项目名称:StyleCopAnalyzers,代码行数:31,代码来源:NameSyntaxHelpers.cs
示例5: IsNamespaceName
private static bool IsNamespaceName(NameSyntax name)
{
while (name.Parent is NameSyntax)
{
name = (NameSyntax)name.Parent;
}
return name.IsParentKind(SyntaxKind.NamespaceDeclaration);
}
开发者ID:jkotas,项目名称:roslyn,代码行数:9,代码来源:NameSyntaxClassifier.cs
示例6: IntroduceQueryLocalForSingleOccurrence
private Document IntroduceQueryLocalForSingleOccurrence(
SemanticDocument document,
ExpressionSyntax expression,
NameSyntax newLocalName,
LetClauseSyntax letClause,
bool allOccurrences,
CancellationToken cancellationToken)
{
var oldClause = expression.GetAncestors<SyntaxNode>().First(IsAnyQueryClause);
var newClause = Rewrite(
document, expression, newLocalName, document, oldClause, allOccurrences, cancellationToken);
var oldQuery = (QueryBodySyntax)oldClause.Parent;
var newQuery = GetNewQuery(oldQuery, oldClause, newClause, letClause);
var newRoot = document.Root.ReplaceNode(oldQuery, newQuery);
return document.Document.WithSyntaxRoot(newRoot);
}
开发者ID:XieShuquan,项目名称:roslyn,代码行数:18,代码来源:CSharpIntroduceVariableService_IntroduceQueryLocal.cs
示例7: TryGetSymbol
private static ISymbol TryGetSymbol(NameSyntax name, SymbolInfo symbolInfo, SemanticModel semanticModel)
{
if (symbolInfo.Symbol == null && symbolInfo.CandidateSymbols.Length > 0)
{
var firstSymbol = symbolInfo.CandidateSymbols[0];
switch (symbolInfo.CandidateReason)
{
case CandidateReason.NotAValue:
return firstSymbol;
case CandidateReason.NotCreatable:
// We want to color types even if they can't be constructed.
if (firstSymbol.IsConstructor() || firstSymbol is ITypeSymbol)
{
return firstSymbol;
}
break;
case CandidateReason.OverloadResolutionFailure:
// If we couldn't bind to a constructor, still classify the type.
if (firstSymbol.IsConstructor())
{
return firstSymbol;
}
break;
case CandidateReason.Inaccessible:
// If a constructor wasn't accessible, still classify the type if it's accessible.
if (firstSymbol.IsConstructor() && semanticModel.IsAccessible(name.SpanStart, firstSymbol.ContainingType))
{
return firstSymbol;
}
break;
}
}
return symbolInfo.Symbol;
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:42,代码来源:NameSyntaxClassifier.cs
示例8: ClassifyTypeSyntax
private IEnumerable<ClassifiedSpan> ClassifyTypeSyntax(
NameSyntax name,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
if (!IsNamespaceName(name))
{
var symbolInfo = semanticModel.GetSymbolInfo(name, cancellationToken);
IEnumerable<ClassifiedSpan> result;
if (TryClassifySymbol(name, symbolInfo, semanticModel, cancellationToken, out result) ||
TryClassifyFromIdentifier(name, symbolInfo, out result) ||
TryClassifyValueIdentifier(name, symbolInfo, out result))
{
return result;
}
}
return null;
}
开发者ID:EkardNT,项目名称:Roslyn,代码行数:20,代码来源:NameSyntaxClassifier.cs
示例9: CheckNameSyntax
private static void CheckNameSyntax(SyntaxNodeAnalysisContext context, NameSyntax nameSyntax)
{
if (nameSyntax == null || nameSyntax.IsMissing)
{
return;
}
QualifiedNameSyntax qualifiedNameSyntax = nameSyntax as QualifiedNameSyntax;
if (qualifiedNameSyntax != null)
{
CheckNameSyntax(context, qualifiedNameSyntax.Left);
CheckNameSyntax(context, qualifiedNameSyntax.Right);
return;
}
SimpleNameSyntax simpleNameSyntax = nameSyntax as SimpleNameSyntax;
if (simpleNameSyntax != null)
{
CheckElementNameToken(context, simpleNameSyntax.Identifier);
return;
}
// TODO: any other cases?
}
开发者ID:endjin,项目名称:StyleCopAnalyzers,代码行数:24,代码来源:SA1300ElementMustBeginWithUpperCaseLetter.cs
示例10: GetSymbolsOffOfName
private static ImmutableArray<ISymbol> GetSymbolsOffOfName(
CSharpSyntaxContext context,
NameSyntax name,
CancellationToken cancellationToken)
{
// Check if we're in an interesting situation like this:
//
// int i = 5;
// i. // <-- here
// List<string> ml = new List<string>();
// The problem is that "i.List<string>" gets parsed as a type. In this case we need to
// try binding again as if "i" is an expression and not a type. In order to do that, we
// need to speculate as to what 'i' meant if it wasn't part of a local declaration's
// type.
if (name.IsFoundUnder<LocalDeclarationStatementSyntax>(d => d.Declaration.Type))
{
var speculativeBinding = context.SemanticModel.GetSpeculativeSymbolInfo(name.SpanStart, name, SpeculativeBindingOption.BindAsExpression);
var container = context.SemanticModel.GetSpeculativeTypeInfo(name.SpanStart, name, SpeculativeBindingOption.BindAsExpression).Type;
return GetSymbolsOffOfBoundExpression(context, name, name, speculativeBinding, container, cancellationToken);
}
// We're in a name-only context, since if we were an expression we'd be a
// MemberAccessExpressionSyntax. Thus, let's do other namespaces and types.
var nameBinding = context.SemanticModel.GetSymbolInfo(name, cancellationToken);
var symbol = nameBinding.Symbol as INamespaceOrTypeSymbol;
if (symbol != null)
{
if (context.IsNameOfContext)
{
return context.SemanticModel.LookupSymbols(position: name.SpanStart, container: symbol);
}
var symbols = context.SemanticModel.LookupNamespacesAndTypes(
position: name.SpanStart,
container: symbol);
if (context.IsNamespaceDeclarationNameContext)
{
var declarationSyntax = name.GetAncestorOrThis<NamespaceDeclarationSyntax>();
return symbols.WhereAsArray(s => IsNonIntersectingNamespace(s, declarationSyntax));
}
// Filter the types when in a using directive, but not an alias.
//
// Cases:
// using | -- Show namespaces
// using A.| -- Show namespaces
// using static | -- Show namespace and types
// using A = B.| -- Show namespace and types
var usingDirective = name.GetAncestorOrThis<UsingDirectiveSyntax>();
if (usingDirective != null && usingDirective.Alias == null)
{
if (usingDirective.StaticKeyword.IsKind(SyntaxKind.StaticKeyword))
{
return symbols.WhereAsArray(s => !s.IsDelegateType() && !s.IsInterfaceType());
}
else
{
symbols = symbols.WhereAsArray(s => s.IsNamespace());
}
}
if (symbols.Any())
{
return symbols;
}
}
return ImmutableArray<ISymbol>.Empty;
}
开发者ID:orthoxerox,项目名称:roslyn,代码行数:73,代码来源:CSharpRecommendationService.cs
示例11: GetDeclaredMember
private Symbol GetDeclaredMember(NamespaceOrTypeSymbol container, TextSpan declarationSpan, NameSyntax name)
{
switch (name.Kind())
{
case SyntaxKind.GenericName:
case SyntaxKind.IdentifierName:
return GetDeclaredMember(container, declarationSpan, ((SimpleNameSyntax)name).Identifier.ValueText);
case SyntaxKind.QualifiedName:
var qn = (QualifiedNameSyntax)name;
var left = GetDeclaredMember(container, declarationSpan, qn.Left) as NamespaceOrTypeSymbol;
Debug.Assert((object)left != null);
return GetDeclaredMember(left, declarationSpan, qn.Right);
case SyntaxKind.AliasQualifiedName:
// this is not supposed to happen, but we allow for errors don't we!
var an = (AliasQualifiedNameSyntax)name;
return GetDeclaredMember(container, declarationSpan, an.Name);
default:
throw ExceptionUtilities.UnexpectedValue(name.Kind());
}
}
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:23,代码来源:SyntaxTreeSemanticModel.cs
示例12: IsRewritableMethod
bool IsRewritableMethod(NameSyntax methodNameSyntax)
{
var symbol = model.GetSymbolInfo(methodNameSyntax).Symbol as IMethodSymbol;
return symbol != null && symbol.HasAttribute<MetaRewriteTypeArgsAttribute>(model.Compilation);
}
开发者ID:VitalyTVA,项目名称:MetaSharp,代码行数:5,代码来源:Rewriter.cs
示例13: PreferPredefinedTypeKeywordInDeclarations
private static bool PreferPredefinedTypeKeywordInDeclarations(NameSyntax name, OptionSet optionSet, SemanticModel semanticModel)
{
return !IsInMemberAccessContext(name) &&
!InsideCrefReference(name) &&
!InsideNameOfExpression(name, semanticModel) &&
SimplificationHelpers.PreferPredefinedTypeKeywordInDeclarations(optionSet, semanticModel.Language);
}
开发者ID:GuilhermeSa,项目名称:roslyn,代码行数:7,代码来源:ExpressionSyntaxExtensions.cs
示例14: TryClassifyFromIdentifier
private bool TryClassifyFromIdentifier(
NameSyntax name,
SymbolInfo symbolInfo,
out IEnumerable<ClassifiedSpan> result)
{
// Okay - it wasn't a type. If the syntax matches "var q = from" or "q = from", and from
// doesn't bind to anything then optimistically color from as a keyword.
var identifierName = name as IdentifierNameSyntax;
if (identifierName != null &&
identifierName.Identifier.HasMatchingText(SyntaxKind.FromKeyword) &&
symbolInfo.Symbol == null)
{
var token = identifierName.Identifier;
if (identifierName.IsRightSideOfAnyAssignExpression() || identifierName.IsVariableDeclaratorValue())
{
result = SpecializedCollections.SingletonEnumerable(
new ClassifiedSpan(token.Span, ClassificationTypeNames.Keyword));
return true;
}
}
result = null;
return false;
}
开发者ID:jkotas,项目名称:roslyn,代码行数:24,代码来源:NameSyntaxClassifier.cs
示例15: PreferPredefinedTypeKeywordInDeclarations
private static bool PreferPredefinedTypeKeywordInDeclarations(NameSyntax name, OptionSet optionSet, SemanticModel semanticModel)
{
return (name.Parent != null) && !(name.Parent is MemberAccessExpressionSyntax) &&
!InsideCrefReference(name) && !InsideNameOfExpression(name, semanticModel) &&
optionSet.GetOption(SimplificationOptions.PreferIntrinsicPredefinedTypeKeywordInDeclaration, LanguageNames.CSharp);
}
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:6,代码来源:ExpressionSyntaxExtensions.cs
示例16: TryReduceAttributeSuffix
private static bool TryReduceAttributeSuffix(
NameSyntax name,
SyntaxToken identifierToken,
SemanticModel semanticModel,
out TypeSyntax replacementNode,
out TextSpan issueSpan,
CancellationToken cancellationToken)
{
issueSpan = default(TextSpan);
replacementNode = default(TypeSyntax);
// we can try to remove the Attribute suffix if this is the attribute name
if (SyntaxFacts.IsAttributeName(name))
{
if (name.Parent.Kind() == SyntaxKind.Attribute || name.IsRightSideOfDotOrColonColon())
{
const string AttributeName = "Attribute";
// an attribute that should keep it (unnecessary "Attribute" suffix should be annotated with a DontSimplifyAnnotation
if (identifierToken.ValueText != AttributeName && identifierToken.ValueText.EndsWith(AttributeName, StringComparison.Ordinal) && !identifierToken.HasAnnotation(SimplificationHelpers.DontSimplifyAnnotation))
{
// weird. the semantic model is able to bind attribute syntax like "[as()]" although it's not valid code.
// so we need another check for keywords manually.
var newAttributeName = identifierToken.ValueText.Substring(0, identifierToken.ValueText.Length - 9);
if (SyntaxFacts.GetKeywordKind(newAttributeName) != SyntaxKind.None)
{
return false;
}
// if this attribute name in source contained unicode escaping, we will loose it now
// because there is no easy way to determine the substring from identifier->ToString()
// which would be needed to pass to SyntaxFactory.Identifier
// The result is an unescaped unicode character in source.
// once we remove the Attribute suffix, we can't use an escaped identifier
var newIdentifierToken = identifierToken.CopyAnnotationsTo(
SyntaxFactory.Identifier(
identifierToken.LeadingTrivia,
newAttributeName,
identifierToken.TrailingTrivia));
replacementNode = SyntaxFactory.IdentifierName(newIdentifierToken)
.WithLeadingTrivia(name.GetLeadingTrivia());
issueSpan = new TextSpan(identifierToken.Span.End - 9, 9);
return true;
}
}
}
return false;
}
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:52,代码来源:ExpressionSyntaxExtensions.cs
示例17: TryClassifyAmbiguousSymbol
private bool TryClassifyAmbiguousSymbol(
NameSyntax name,
SymbolInfo symbolInfo,
SemanticModel semanticModel,
CancellationToken cancellationToken,
out IEnumerable<ClassifiedSpan> result)
{
// If everything classifies the same way, then just pick that classification.
var set = new HashSet<ClassifiedSpan>();
foreach (var symbol in symbolInfo.CandidateSymbols)
{
ClassifiedSpan classifiedSpan;
if (TryClassifySymbol(name, symbol, semanticModel, cancellationToken, out classifiedSpan))
{
set.Add(classifiedSpan);
}
}
if (set.Count == 1)
{
result = SpecializedCollections.SingletonEnumerable(set.First());
return true;
}
result = null;
return false;
}
开发者ID:jkotas,项目名称:roslyn,代码行数:27,代码来源:NameSyntaxClassifier.cs
示例18: TryClassifyNameOfIdentifier
private bool TryClassifyNameOfIdentifier(NameSyntax name, SymbolInfo symbolInfo, out IEnumerable<ClassifiedSpan> result)
{
var identifierName = name as IdentifierNameSyntax;
if (identifierName != null &&
identifierName.Identifier.IsKindOrHasMatchingText(SyntaxKind.NameOfKeyword) &&
symbolInfo.Symbol == null &&
!symbolInfo.CandidateSymbols.Any())
{
result = SpecializedCollections.SingletonEnumerable(new ClassifiedSpan(identifierName.Identifier.Span, ClassificationTypeNames.Keyword));
return true;
}
result = null;
return false;
}
开发者ID:jkotas,项目名称:roslyn,代码行数:15,代码来源:NameSyntaxClassifier.cs
示例19: TryClassifyValueIdentifier
private bool TryClassifyValueIdentifier(
NameSyntax name,
SymbolInfo symbolInfo,
out IEnumerable<ClassifiedSpan> result)
{
var identifierName = name as IdentifierNameSyntax;
if (symbolInfo.Symbol.IsImplicitValueParameter())
{
result = SpecializedCollections.SingletonEnumerable(
new ClassifiedSpan(identifierName.Identifier.Span, ClassificationTypeNames.Keyword));
return true;
}
result = null;
return false;
}
开发者ID:jkotas,项目名称:roslyn,代码行数:16,代码来源:NameSyntaxClassifier.cs
示例20: CanSimplifyNullable
private static bool CanSimplifyNullable(INamedTypeSymbol type, NameSyntax name, SemanticModel semanticModel)
{
if (!type.IsNullable())
{
return false;
}
if (type.IsUnboundGenericType)
{
// Don't simplify unbound generic type "Nullable<>".
return false;
}
if (InsideNameOfExpression(name, semanticModel))
{
// Nullable<T> can't be simplified to T? in nameof expressions.
return false;
}
if (!InsideCrefReference(name))
{
// Nullable<T> can always be simplified to T? outside crefs.
return true;
}
// Inside crefs, if the T in this Nullable{T} is being declared right here
// then this Nullable{T} is not a constructed generic type and we should
// not offer to simplify this to T?.
//
// For example, we should not offer the simplification in the following cases where
// T does not bind to an existing type / type parameter in the user's code.
// - <see cref="Nullable{T}"/>
// - <see cref="System.Nullable{T}.Value"/>
//
// And we should offer the simplification in the following cases where SomeType and
// SomeMethod bind to a type and method declared elsewhere in the users code.
// - <see cref="SomeType.SomeMethod(Nullable{SomeType})"/>
var argument = type.TypeArguments.SingleOrDefault();
if (argument == null || argument.IsErrorType())
{
return false;
}
var argumentDecl = argument.DeclaringSyntaxReferences.FirstOrDefault();
if (argumentDecl == null)
{
// The type argument is a type from metadata - so this is a constructed generic nullable type that can be simplified (e.g. Nullable(Of Integer)).
return true;
}
return !name.Span.Contains(argumentDecl.Span);
}
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:53,代码来源:ExpressionSyntaxExtensions.cs
注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.NameSyntax类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论