src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/DocumentExtensions.cs
/// <summary>
/// this will return either regular semantic model or speculative semantic based on context. 
/// any feature that is involved in typing or run on UI thread should use this to take advantage of speculative semantic model 
/// whenever possible automatically.
/// 
/// when using this API, semantic model should only be used to ask node inside of the given span. 
/// otherwise, it might throw if semantic model returned by this API is a speculative semantic model.
/// 
/// also, symbols from the semantic model returned by this API might have out of date location information. 
/// if exact location (not relative location) is needed from symbol, regular GetSemanticModel should be used.
/// </summary>
this will return either regular semantic model or speculative semantic based on context.
any feature that is involved in typing or run on UI thread should use this to take advantage of speculative semantic model
whenever possible automatically.

when using this API, semantic model should only be used to ask node inside of the given span.
otherwise, it might throw if semantic model returned by this API is a speculative semantic model.

also, symbols from the semantic model returned by this API might have out of date location information.
if exact location (not relative location) is needed from symbol, regular GetSemanticModel should be used.
this will return either regular semantic model or speculative semantic based on context . any feature that is involved in typing or run on UI thread should use this to take advantage of speculative semantic model whenever possible automatically . when using this API semantic model should only be used to ask node inside of the given span . otherwise it might throw if semantic model returned by this API is a speculative semantic model . also symbols from the semantic model returned by this API might have out of date location information . if exact location ( not relative location ) is needed from symbol regular GetSemanticModel should be used .
public static async Task<SemanticModel> GetSemanticModelForSpanAsync(this Document document, TextSpan span, CancellationToken cancellationToken)
        {
            Contract.ThrowIfFalse(document.SupportsSemanticModel);
            var syntaxFactService = document.GetLanguageService<ISyntaxFactsService>();
            var semanticModelService = document.Project.Solution.Workspace.Services.GetService<ISemanticModelService>();
            if (semanticModelService == null || syntaxFactService == null)
            {
                return (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false))!;
            }
            var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
            Contract.ThrowIfNull(root, "We shouldn't have a null root if the document supports semantic models");
            var token = root.FindToken(span.Start);
            if (token.Parent == null)
            {
                return (await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false))!;
            }
            var node = token.Parent.AncestorsAndSelf().First(a => a.FullSpan.Contains(span));
            return await GetSemanticModelForNodeAsync(semanticModelService, syntaxFactService, document, node, span, cancellationToken).ConfigureAwait(false);
        }
public static async Task < SemanticModel > GetSemanticModelForSpanAsync ( this Document document , TextSpan span , CancellationToken cancellationToken ) { Contract . ThrowIfFalse ( document . SupportsSemanticModel ) ; var syntaxFactService = document . GetLanguageService < ISyntaxFactsService > ( ) ; var semanticModelService = document . Project . Solution . Workspace . Services . GetService < ISemanticModelService > ( ) ; if ( semanticModelService == null || syntaxFactService == null ) { return ( await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ) ! ; } var root = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ; Contract . ThrowIfNull ( root , " " ) ; var token = root . FindToken ( span . Start ) ; if ( token . Parent == null ) { return ( await document . GetSemanticModelAsync ( cancellationToken ) . ConfigureAwait ( false ) ) ! ; } var node = token . Parent . AncestorsAndSelf ( ) . First ( a => a . FullSpan . Contains ( span ) ) ; return await GetSemanticModelForNodeAsync ( semanticModelService , syntaxFactService , document , node , span , cancellationToken ) . ConfigureAwait ( false ) ; }
this will return either regular semantic model or speculative semantic based on context.
this will return either regular semantic model or speculative semantic based on context .
Document
this
span
TextSpan
cancellationToken
CancellationToken