本文整理汇总了C#中Document.GetSemanticModelForSpanAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Document.GetSemanticModelForSpanAsync方法的具体用法?C# Document.GetSemanticModelForSpanAsync怎么用?C# Document.GetSemanticModelForSpanAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Document
的用法示例。
在下文中一共展示了Document.GetSemanticModelForSpanAsync方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateContext
protected override async Task<AbstractSyntaxContext> CreateContext(Document document, int position, CancellationToken cancellationToken)
{
var workspace = document.Project.Solution.Workspace;
var span = new TextSpan(position, 0);
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
return CSharpSyntaxContext.CreateContext(workspace, semanticModel, position, cancellationToken);
}
示例2: GetDocumentHighlightsAsync
public async Task<ImmutableArray<DocumentHighlights>> GetDocumentHighlightsAsync(
Document document, int position, IImmutableSet<Document> documentsToSearch, CancellationToken cancellationToken)
{
// use speculative semantic model to see whether we are on a symbol we can do HR
var span = new TextSpan(position, 0);
var solution = document.Project.Solution;
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var symbol = await SymbolFinder.FindSymbolAtPositionAsync(
semanticModel, position, solution.Workspace, cancellationToken).ConfigureAwait(false);
if (symbol == null)
{
return ImmutableArray<DocumentHighlights>.Empty;
}
symbol = await GetSymbolToSearchAsync(document, position, semanticModel, symbol, cancellationToken).ConfigureAwait(false);
if (symbol == null)
{
return ImmutableArray<DocumentHighlights>.Empty;
}
// Get unique tags for referenced symbols
return await GetTagsForReferencedSymbolAsync(
new SymbolAndProjectId(symbol, document.Project.Id), documentsToSearch,
solution, cancellationToken).ConfigureAwait(false);
}
示例3: GetClassifiedSpansAsync
public static async Task<IEnumerable<ClassifiedSpan>> GetClassifiedSpansAsync(
Document document,
TextSpan textSpan,
CancellationToken cancellationToken = default(CancellationToken))
{
var semanticModel = await document.GetSemanticModelForSpanAsync(textSpan, cancellationToken).ConfigureAwait(false);
return GetClassifiedSpans(semanticModel, textSpan, document.Project.Solution.Workspace, cancellationToken);
}
示例4: GetSnippetsForDocumentAsync
private async Task<IEnumerable<CompletionItem>> GetSnippetsForDocumentAsync(
Document document, int position, Workspace workspace, CancellationToken cancellationToken)
{
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var semanticFacts = document.GetLanguageService<ISemanticFactsService>();
if (syntaxFacts.IsInNonUserCode(syntaxTree, position, cancellationToken) ||
syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken) ||
syntaxFacts.GetContainingTypeDeclaration(await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false), position) is EnumDeclarationSyntax)
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
var span = new TextSpan(position, 0);
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var isPossibleTupleContext = syntaxFacts.IsPossibleTupleContext(syntaxTree, position, cancellationToken);
if (semanticFacts.IsPreProcessorDirectiveContext(semanticModel, position, cancellationToken))
{
var directive = syntaxTree.GetRoot(cancellationToken).FindTokenOnLeftOfPosition(position, includeDirectives: true).GetAncestor<DirectiveTriviaSyntax>();
if (directive.DirectiveNameToken.IsKind(
SyntaxKind.IfKeyword,
SyntaxKind.RegionKeyword,
SyntaxKind.ElseKeyword,
SyntaxKind.ElifKeyword,
SyntaxKind.ErrorKeyword,
SyntaxKind.LineKeyword,
SyntaxKind.PragmaKeyword,
SyntaxKind.EndIfKeyword,
SyntaxKind.UndefKeyword,
SyntaxKind.EndRegionKeyword,
SyntaxKind.WarningKeyword))
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
return await GetSnippetCompletionItemsAsync(workspace, semanticModel, isPreProcessorContext: true,
isTupleContext: isPossibleTupleContext, cancellationToken: cancellationToken).ConfigureAwait(false);
}
if (semanticFacts.IsGlobalStatementContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsExpressionContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsStatementContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsTypeContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsTypeDeclarationContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsNamespaceContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsNamespaceDeclarationNameContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsMemberDeclarationContext(semanticModel, position, cancellationToken) ||
semanticFacts.IsLabelContext(semanticModel, position, cancellationToken))
{
return await GetSnippetCompletionItemsAsync(workspace, semanticModel, isPreProcessorContext: false,
isTupleContext: isPossibleTupleContext, cancellationToken: cancellationToken).ConfigureAwait(false);
}
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
示例5: AddSemanticClassificationsAsync
public async Task AddSemanticClassificationsAsync(Document document, TextSpan textSpan, Func<SyntaxNode, List<ISyntaxClassifier>> getNodeClassifiers, Func<SyntaxToken, List<ISyntaxClassifier>> getTokenClassifiers, List<ClassifiedSpan> result, CancellationToken cancellationToken)
{
try
{
var semanticModel = await document.GetSemanticModelForSpanAsync(textSpan, cancellationToken).ConfigureAwait(false);
AddSemanticClassifications(semanticModel, textSpan, document.Project.Solution.Workspace, getNodeClassifiers, getTokenClassifiers, result, cancellationToken);
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
}
示例6: CaseCorrectAsync
public async Task<Document> CaseCorrectAsync(Document document, IEnumerable<TextSpan> spans, CancellationToken cancellationToken)
{
if (!spans.Any())
{
return document;
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetSemanticModelForSpanAsync(spans.Collapse(), cancellationToken).ConfigureAwait(false);
var newRoot = CaseCorrect(semanticModel, root, spans, document.Project.Solution.Workspace, cancellationToken);
return (root == newRoot) ? document : document.WithSyntaxRoot(newRoot);
}
示例7: CleanupAsync
public async Task<Document> CleanupAsync(Document document, IEnumerable<TextSpan> spans, IEnumerable<ICodeCleanupProvider> providers, CancellationToken cancellationToken)
{
using (Logger.LogBlock(FunctionId.CodeCleanup_CleanupAsync, cancellationToken))
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
// If there is no span to format...
if (!spans.Any())
{
// ... then return the Document unchanged
return document;
}
var codeCleaners = providers ?? GetDefaultProviders();
var normalizedSpan = spans.ToNormalizedSpans();
if (CleanupWholeNode(root.FullSpan, normalizedSpan))
{
// We are cleaning up the whole document, so there is no need to do expansive span tracking between cleaners.
return await IterateAllCodeCleanupProvidersAsync(document, document, r => SpecializedCollections.SingletonEnumerable(r.FullSpan), codeCleaners, cancellationToken).ConfigureAwait(false);
}
var syntaxFactsService = document.Project.LanguageServices.GetService<ISyntaxFactsService>();
Contract.Requires(syntaxFactsService != null);
// We need to track spans between cleaners. Annotate the tree with the provided spans.
var newNodeAndAnnotations = AnnotateNodeForTextSpans(syntaxFactsService, root, normalizedSpan, cancellationToken);
// If it urns out we don't need to annotate anything since all spans are merged to one span that covers the whole node...
if (newNodeAndAnnotations.newNode == null)
{
// ... then we are cleaning up the whole document, so there is no need to do expansive span tracking between cleaners.
return await IterateAllCodeCleanupProvidersAsync(document, document, n => SpecializedCollections.SingletonEnumerable(n.FullSpan), codeCleaners, cancellationToken).ConfigureAwait(false);
}
var model = await document.GetSemanticModelForSpanAsync(spans.Collapse(), cancellationToken).ConfigureAwait(false);
// Replace the initial node and document with the annotated node.
var annotatedRoot = newNodeAndAnnotations.newNode;
var annotatedDocument = document.WithSyntaxRoot(annotatedRoot);
// Run the actual cleanup.
return await IterateAllCodeCleanupProvidersAsync(
document, annotatedDocument,
r => GetTextSpansFromAnnotation(r, newNodeAndAnnotations.annotations, cancellationToken),
codeCleaners, cancellationToken).ConfigureAwait(false);
}
}
示例8: GetDocumentHighlightsAsync
public async Task<IEnumerable<DocumentHighlights>> GetDocumentHighlightsAsync(Document document, int position, IEnumerable<Document> documentsToSearch, CancellationToken cancellationToken)
{
// use speculative semantic model to see whether we are on a symbol we can do HR
var span = new TextSpan(position, 0);
var solution = document.Project.Solution;
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var symbol = SymbolFinder.FindSymbolAtPosition(semanticModel, position, solution.Workspace, cancellationToken: cancellationToken);
if (symbol == null)
{
return SpecializedCollections.EmptyEnumerable<DocumentHighlights>();
}
symbol = await GetSymbolToSearchAsync(document, position, semanticModel, symbol, cancellationToken).ConfigureAwait(false);
// Get unique tags for referenced symbols
return await GetTagsForReferencedSymbolAsync(symbol, ImmutableHashSet.CreateRange(documentsToSearch), solution, cancellationToken).ConfigureAwait(false);
}
示例9: GetItemsWorkerAsync
protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(Document document, int position, CompletionTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var workspace = document.Project.Solution.Workspace;
var semanticModel = await document.GetSemanticModelForSpanAsync(new TextSpan(position, 0), cancellationToken).ConfigureAwait(false);
var typeAndLocation = GetInitializedType(document, semanticModel, position, cancellationToken);
if (typeAndLocation == null)
{
return null;
}
var initializedType = typeAndLocation.Item1 as INamedTypeSymbol;
var initializerLocation = typeAndLocation.Item2;
if (initializedType == null)
{
return null;
}
// Find the members that can be initialized. If we have a NamedTypeSymbol, also get the overridden members.
IEnumerable<ISymbol> members = semanticModel.LookupSymbols(position, initializedType);
members = members.Where(m => IsInitializable(m, initializedType) &&
m.CanBeReferencedByName &&
IsLegalFieldOrProperty(m) &&
!m.IsImplicitlyDeclared);
// Filter out those members that have already been typed
var alreadyTypedMembers = GetInitializedMembers(semanticModel.SyntaxTree, position, cancellationToken);
var uninitializedMembers = members.Where(m => !alreadyTypedMembers.Contains(m.Name));
uninitializedMembers = uninitializedMembers.Where(m => m.IsEditorBrowsable(document.ShouldHideAdvancedMembers(), semanticModel.Compilation));
var text = await semanticModel.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
var changes = GetTextChangeSpan(text, position);
// Return the members
return uninitializedMembers.Select(
m => CreateItem(workspace, m.Name, changes,
CommonCompletionUtilities.CreateDescriptionFactory(workspace, semanticModel, initializerLocation.SourceSpan.Start, m),
m.GetGlyph()));
}
示例10: GetItemsWorkerAsync
protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(
Document document,
int position,
CompletionTriggerInfo triggerInfo,
CancellationToken cancellationToken)
{
var span = new TextSpan(position, 0);
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var syntaxTree = semanticModel.SyntaxTree;
var syntaxFacts = document.GetLanguageService<ISyntaxFactsService>();
var semanticFacts = document.GetLanguageService<ISemanticFactsService>();
if (syntaxFacts.IsInNonUserCode(syntaxTree, position, cancellationToken) ||
semanticFacts.IsPreProcessorDirectiveContext(semanticModel, position, cancellationToken))
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
if (!syntaxTree.IsRightOfDotOrArrowOrColonColon(position, cancellationToken))
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
var node = syntaxTree.FindTokenOnLeftOfPosition(position, cancellationToken)
.GetPreviousTokenIfTouchingWord(position)
.Parent;
if (node.Kind() == SyntaxKind.ExplicitInterfaceSpecifier)
{
return await GetCompletionsOffOfExplicitInterfaceAsync(
document, semanticModel, position, ((ExplicitInterfaceSpecifierSyntax)node).Name, cancellationToken).ConfigureAwait(false);
}
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
示例11: GetItemsAsync
public async Task<SignatureHelpItems> GetItemsAsync(
Document document, int position, SignatureHelpTriggerInfo triggerInfo, CancellationToken cancellationToken)
{
var itemsForCurrentDocument = await GetItemsWorkerAsync(document, position, triggerInfo, cancellationToken).ConfigureAwait(false);
if (itemsForCurrentDocument == null)
{
return itemsForCurrentDocument;
}
var relatedDocuments = document.GetLinkedDocumentIds();
if (!relatedDocuments.Any())
{
return itemsForCurrentDocument;
}
var relatedDocumentsAndItems = await GetItemsForRelatedDocuments(document, relatedDocuments, position, triggerInfo, cancellationToken).ConfigureAwait(false);
var candidateLinkedProjectsAndSymbolSets = await ExtractSymbolsFromRelatedItems(position, relatedDocumentsAndItems, cancellationToken).ConfigureAwait(false);
var totalProjects = candidateLinkedProjectsAndSymbolSets.Select(c => c.Item1).Concat(document.Project.Id);
var semanticModel = await document.GetSemanticModelForSpanAsync(new TextSpan(position, 0), cancellationToken).ConfigureAwait(false);
var compilation = semanticModel.Compilation;
var finalItems = new List<SignatureHelpItem>();
foreach (var item in itemsForCurrentDocument.Items)
{
var symbolKey = ((SymbolKeySignatureHelpItem)item).SymbolKey;
if (symbolKey == null)
{
finalItems.Add(item);
continue;
}
var expectedSymbol = symbolKey.Resolve(compilation, ignoreAssemblyKey: true, cancellationToken: cancellationToken).Symbol;
if (expectedSymbol == null)
{
finalItems.Add(item);
continue;
}
var invalidProjectsForCurrentSymbol = candidateLinkedProjectsAndSymbolSets.Where(c => !c.Item2.Contains(expectedSymbol, LinkedFilesSymbolEquivalenceComparer.IgnoreAssembliesInstance))
.Select(c => c.Item1)
.ToList();
var platformData = new SupportedPlatformData(invalidProjectsForCurrentSymbol, totalProjects, document.Project.Solution.Workspace);
finalItems.Add(UpdateItem(item, platformData, expectedSymbol));
}
return new SignatureHelpItems(
finalItems, itemsForCurrentDocument.ApplicableSpan,
itemsForCurrentDocument.ArgumentIndex,
itemsForCurrentDocument.ArgumentCount,
itemsForCurrentDocument.ArgumentName,
itemsForCurrentDocument.SelectedItemIndex);
}
示例12: AddSemanticClassificationsAsync
public async Task AddSemanticClassificationsAsync(Document document, TextSpan textSpan, Func<SyntaxNode, List<ISyntaxClassifier>> getNodeClassifiers, Func<SyntaxToken, List<ISyntaxClassifier>> getTokenClassifiers, List<ClassifiedSpan> result, CancellationToken cancellationToken)
{
var semanticModel = await document.GetSemanticModelForSpanAsync(textSpan, cancellationToken).ConfigureAwait(false);
AddSemanticClassifications(semanticModel, textSpan, document.Project.Solution.Workspace, getNodeClassifiers, getTokenClassifiers, result, cancellationToken);
}
示例13: GetItemsWorkerAsync
protected override async Task<IEnumerable<CompletionItem>> GetItemsWorkerAsync(
Document document, int position, CompletionTriggerInfo triggerInfo,
CancellationToken cancellationToken)
{
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
if (tree.IsInNonUserCode(position, cancellationToken))
{
return null;
}
var token = tree.FindTokenOnLeftOfPosition(position, cancellationToken);
if (token.IsMandatoryNamedParameterPosition())
{
return null;
}
var typeInferenceService = document.GetLanguageService<ITypeInferenceService>();
var span = new TextSpan(position, 0);
var semanticModel = await document.GetSemanticModelForSpanAsync(span, cancellationToken).ConfigureAwait(false);
var type = typeInferenceService.InferType(semanticModel, position,
objectAsDefault: true,
cancellationToken: cancellationToken);
// If we have a Nullable<T>, unwrap it.
if (type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
{
type = type.GetTypeArguments().FirstOrDefault();
if (type == null)
{
return null;
}
}
if (type.TypeKind != TypeKind.Enum)
{
type = GetCompletionListType(type, semanticModel.GetEnclosingNamedType(position, cancellationToken), semanticModel.Compilation);
if (type == null)
{
return null;
}
}
if (!type.IsEditorBrowsable(document.ShouldHideAdvancedMembers(), semanticModel.Compilation))
{
return null;
}
// Does type have any aliases?
ISymbol alias = await type.FindApplicableAlias(position, semanticModel, cancellationToken).ConfigureAwait(false);
var displayService = document.GetLanguageService<ISymbolDisplayService>();
var displayText = alias != null
? alias.Name
: displayService.ToMinimalDisplayString(semanticModel, position, type);
var workspace = document.Project.Solution.Workspace;
var text = await semanticModel.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
var textChangeSpan = CompletionUtilities.GetTextChangeSpan(text, position);
var item = new CSharpCompletionItem(
workspace,
this,
displayText: displayText,
filterSpan: textChangeSpan,
descriptionFactory: CommonCompletionUtilities.CreateDescriptionFactory(workspace, semanticModel, position, alias ?? type),
glyph: (alias ?? type).GetGlyph(),
preselect: true);
return SpecializedCollections.SingletonEnumerable(item);
}