public static (ITypeSymbol typeSymbol, TypeInferredFrom inferredFrom) GetTypeSymbol(this SemanticModel semanticModel, SyntaxToken currentToken) { ITypeSymbol typeSymbol = null; TypeInferredFrom inferredFrom = TypeInferredFrom.None; SyntaxNode currentSyntaxNode = currentToken.Parent; // If new keyword is already present, we need to work with parent node if (currentSyntaxNode is ObjectCreationExpressionSyntax // happens with named arguments - we need to get ArgumentSyntax || currentSyntaxNode is NameColonSyntax) { currentSyntaxNode = currentSyntaxNode.Parent; } if (currentSyntaxNode?.Parent?.Parent is VariableDeclarationSyntax varDeclarationSyntax && !varDeclarationSyntax.Type.IsVar) { var typeInfo = semanticModel.GetTypeInfo(varDeclarationSyntax.Type); typeSymbol = typeInfo.Type; inferredFrom = TypeInferredFrom.VariableDeclaration; }
public SyntaxContext(Document document, SemanticModel semanticModel, SyntaxTree syntaxTree, int position, IEnumerable <string> importedNamespaces, bool isTypeContext = false, bool isAttributeContext = false, bool isMemberAccessContext = false, ITypeSymbol accessedSymbolType = null, ISymbol accessedSymbol = null, ITypeSymbol inferredType = null, TypeInferredFrom inferredFrom = TypeInferredFrom.None, SyntaxToken currentToken = default(SyntaxToken), CancellationToken token = default(CancellationToken)) { Document = document; SemanticModel = semanticModel; SyntaxTree = syntaxTree; Position = position; ImportedNamespaces = importedNamespaces.ToImmutableHashSet(); IsTypeContext = isTypeContext; IsAttributeContext = isAttributeContext; IsMemberAccessContext = isMemberAccessContext; AccessedSymbolType = accessedSymbolType; AccessedSymbol = accessedSymbol; InferredType = inferredType; TypeInferredFrom = inferredFrom; CurrentToken = currentToken; CancellationToken = token; }