本文整理汇总了C#中Microsoft.CodeAnalysis.Document.GetCSharpSemanticModelAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetCSharpSemanticModelAsync方法的具体用法?C# Document.GetCSharpSemanticModelAsync怎么用?C# Document.GetCSharpSemanticModelAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.Document
的用法示例。
在下文中一共展示了Document.GetCSharpSemanticModelAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
ConstructorInitializerSyntax constructorInitializer;
if (!TryGetConstructorInitializer(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out constructorInitializer))
{
return null;
}
var semanticModel = await document.GetCSharpSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var within = semanticModel.GetEnclosingNamedType(position, cancellationToken);
if (within == null)
{
return null;
}
if (within.TypeKind != TypeKind.Struct && within.TypeKind != TypeKind.Class)
{
return null;
}
var type = constructorInitializer.Kind() == SyntaxKind.BaseConstructorInitializer
? within.BaseType
: within;
if (type == null)
{
return null;
}
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
var accessibleConstructors = type.InstanceConstructors
.Where(c => c.IsAccessibleWithin(within))
.Where(c => c.IsEditorBrowsable(document.ShouldHideAdvancedMembers(), semanticModel.Compilation))
.Sort(symbolDisplayService, semanticModel, constructorInitializer.SpanStart);
if (!accessibleConstructors.Any())
{
return null;
}
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormattingService = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = SignatureHelpUtilities.GetSignatureHelpSpan(constructorInitializer.ArgumentList);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
return CreateSignatureHelpItems(accessibleConstructors.Select(c =>
Convert(c, constructorInitializer.ArgumentList.OpenParenToken, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, cancellationToken)),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}
示例2: GetItemsWorkerAsync
protected override async Task<SignatureHelpItems> GetItemsWorkerAsync(Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var root = await document.GetCSharpSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
ExpressionSyntax expression;
SyntaxToken openBrace;
if (!TryGetElementAccessExpression(root, position, document.GetLanguageService<ISyntaxFactsService>(), triggerInfo.TriggerReason, cancellationToken, out expression, out openBrace))
{
return null;
}
var semanticModel = await document.GetCSharpSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var expressionSymbol = semanticModel.GetSymbolInfo(expression, cancellationToken).GetAnySymbol();
if (expressionSymbol is INamedTypeSymbol)
{
// foo?[$$]
var namedType = (INamedTypeSymbol)expressionSymbol;
if (namedType.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T &&
expression.IsKind(SyntaxKind.NullableType) &&
expression.IsChildNode<ArrayTypeSyntax>(a => a.ElementType))
{
// Speculatively bind the type part of the nullable as an expression
var nullableTypeSyntax = (NullableTypeSyntax)expression;
var speculativeBinding = semanticModel.GetSpeculativeSymbolInfo(position, nullableTypeSyntax.ElementType, SpeculativeBindingOption.BindAsExpression);
expressionSymbol = speculativeBinding.GetAnySymbol();
expression = nullableTypeSyntax.ElementType;
}
}
if (expressionSymbol != null && expressionSymbol is INamedTypeSymbol)
{
return null;
}
IEnumerable<IPropertySymbol> indexers;
ITypeSymbol expressionType;
if (!TryGetIndexers(position, semanticModel, expression, cancellationToken, out indexers, out expressionType) &&
!TryGetComIndexers(semanticModel, expression, cancellationToken, out indexers, out expressionType))
{
return null;
}
var within = semanticModel.GetEnclosingNamedTypeOrAssembly(position, cancellationToken);
if (within == null)
{
return null;
}
var accessibleIndexers = indexers.Where(m => m.IsAccessibleWithin(within, throughTypeOpt: expressionType));
if (!accessibleIndexers.Any())
{
return null;
}
var symbolDisplayService = document.Project.LanguageServices.GetService<ISymbolDisplayService>();
accessibleIndexers = accessibleIndexers.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation)
.Sort(symbolDisplayService, semanticModel, expression.SpanStart);
var anonymousTypeDisplayService = document.Project.LanguageServices.GetService<IAnonymousTypeDisplayService>();
var documentationCommentFormattingService = document.Project.LanguageServices.GetService<IDocumentationCommentFormattingService>();
var textSpan = GetTextSpan(expression, openBrace);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
return CreateSignatureHelpItems(accessibleIndexers.Select(p =>
Convert(p, openBrace, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, cancellationToken)),
textSpan, GetCurrentArgumentState(root, position, syntaxFacts, textSpan, cancellationToken));
}