internal ReferenceLocation(Document document, IAliasSymbol alias, Location location, bool isImplicit, ValueUsageInfo valueUsageInfo, CandidateReason candidateReason) : this() { this.Document = document; this.Alias = alias; this.Location = location; this.IsImplicit = isImplicit; this.ValueUsageInfo = valueUsageInfo; this.CandidateReason = candidateReason; }
internal ReferenceLocation(Document document, IAliasSymbol alias, Location location, bool isImplicit, bool isWrittenTo, CandidateReason candidateReason) : this() { this.Document = document; this.Alias = alias; this.Location = location; this.IsImplicit = isImplicit; this.IsWrittenTo = isWrittenTo; this.CandidateReason = candidateReason; }
public TokenSemanticInfo( ISymbol declaredSymbol, IAliasSymbol aliasSymbol, ImmutableArray <ISymbol> referencedSymbols, ITypeSymbol type) { DeclaredSymbol = declaredSymbol; AliasSymbol = aliasSymbol; ReferencedSymbols = referencedSymbols; Type = type; }
public override void VisitAlias(IAliasSymbol symbol) { builder.Add(CreatePart(SymbolDisplayPartKind.AliasName, symbol, symbol.Name)); if (format.LocalOptions.IncludesOption(SymbolDisplayLocalOptions.IncludeType)) { // ??? AddPunctuation(SyntaxKind.EqualsToken); symbol.Target.Accept(this); } }
internal ReferenceLocation(Document document, IAliasSymbol alias, Location location, bool isImplicit, SymbolUsageInfo symbolUsageInfo, ImmutableArray <FindUsageProperty> additionalProperties, CandidateReason candidateReason) : this() { this.Document = document; this.Alias = alias; this.Location = location; this.IsImplicit = isImplicit; this.SymbolUsageInfo = symbolUsageInfo; this.FindUsagesProperties = additionalProperties.NullToEmpty(); this.CandidateReason = candidateReason; }
internal ReferenceLocation(Document document, IAliasSymbol alias, Location location, bool isImplicit, SymbolUsageInfo symbolUsageInfo, ImmutableDictionary <string, string> additionalProperties, CandidateReason candidateReason) : this() { this.Document = document; this.Alias = alias; this.Location = location; this.IsImplicit = isImplicit; this.SymbolUsageInfo = symbolUsageInfo; this.AdditionalProperties = additionalProperties ?? ImmutableDictionary <string, string> .Empty; this.CandidateReason = candidateReason; }
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node) { IAliasSymbol aliasSymbol = SemanticModel.GetAliasInfo(node, CancellationToken); if (SymbolEqualityComparer.Default.Equals(aliasSymbol, AliasSymbol)) { return(Replacement .WithTriviaFrom(node) .WithSimplifierAnnotation()); } return(base.VisitIdentifierName(node)); }
private static bool IsFixable(SyntaxNodeAnalysisContext context, TypeSyntax name) { var typeSymbol = context.SemanticModel.GetSymbol(name, context.CancellationToken) as ITypeSymbol; if (typeSymbol?.SupportsPredefinedType() == true) { IAliasSymbol aliasSymbol = context.SemanticModel.GetAliasInfo(name, context.CancellationToken); return(aliasSymbol == null); } return(false); }
public NameDeclarationInfo( ImmutableArray <SymbolKind> possibleSymbolKinds, Accessibility accessibility, DeclarationModifiers declarationModifiers, ITypeSymbol type, IAliasSymbol alias) { PossibleSymbolKinds = possibleSymbolKinds; DeclaredAccessibility = accessibility; Modifiers = declarationModifiers; Type = type; Alias = alias; }
private static void AnalyzeIdentifierName(SyntaxNodeAnalysisContext context) { var identifierName = (IdentifierNameSyntax)context.Node; if (identifierName.IsVar) { return; } if (identifierName.IsParentKind( SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.QualifiedName, SyntaxKind.UsingDirective)) { return; } if (!SupportsPredefinedType(identifierName)) { return; } if (identifierName.IsPartOfDocumentationComment()) { return; } if (IsArgumentExpressionOfNameOfExpression(context, identifierName)) { return; } if (!(context.SemanticModel.GetSymbol(identifierName, context.CancellationToken) is ITypeSymbol typeSymbol)) { return; } if (!CSharpFacts.IsPredefinedType(typeSymbol.SpecialType)) { return; } IAliasSymbol aliasSymbol = context.SemanticModel.GetAliasInfo(identifierName, context.CancellationToken); if (aliasSymbol != null) { return; } ReportDiagnostic(context, identifierName); }
public static IEnumerable <ReferencedSymbol> FilterToAliasMatches( this IEnumerable <ReferencedSymbol> result, IAliasSymbol aliasSymbolOpt) { if (aliasSymbolOpt == null) { return(result); } return(from r in result let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbolOpt)) where aliasLocations.Any() select new ReferencedSymbol(r.DefinitionAndProjectId, aliasLocations)); }
public static IEnumerable<ReferencedSymbol> FilterToAliasMatches( this IEnumerable<ReferencedSymbol> result, IAliasSymbol aliasSymbolOpt) { if (aliasSymbolOpt == null) { return result; } return from r in result let aliasLocations = r.Locations.Where(loc => SymbolEquivalenceComparer.Instance.Equals(loc.Alias, aliasSymbolOpt)) where aliasLocations.Any() select new ReferencedSymbol(r.Definition, aliasLocations); }
public TokenSemanticInfo( ISymbol declaredSymbol, IAliasSymbol aliasSymbol, ImmutableArray <ISymbol> referencedSymbols, ITypeSymbol type, ITypeSymbol convertedType, TextSpan span) { DeclaredSymbol = declaredSymbol; AliasSymbol = aliasSymbol; ReferencedSymbols = referencedSymbols; Type = type; ConvertedType = convertedType; Span = span; }
public static bool TryGetAliasSymbol(SemanticModel semanticModel, int namespaceId, INamespaceOrTypeSymbol targetSymbol, out IAliasSymbol aliasSymbol) { // TODO: given semantic model must be not speculative semantic model for now. // currently it can't be checked since it is not exposed to common layer yet. // once exposed, this method itself will make sure it use original semantic model aliasSymbol = null; if (!s_treeAliasMap.TryGetValue(semanticModel.Compilation, out var treeMap) || !treeMap.TryGetValue((semanticModel.SyntaxTree, namespaceId), out var symbolMap)) { return false; } symbolMap.TryGetValue(targetSymbol, out aliasSymbol); return true; }
internal static ImmutableArray <Words> GetBaseNames(IAliasSymbol alias) { var name = alias.Name; if (alias.Target.IsType && (((INamedTypeSymbol)alias.Target).IsInterfaceType() && CanRemoveInterfacePrefix(name))) { name = name.Substring(1); } var breaks = StringBreaker.GetWordParts(name); var result = GetInterleavedPatterns(breaks, name, pluralize: false); breaks.Free(); return(result); }
internal static ImmutableArray <Words> GetBaseNames(IAliasSymbol alias) { var name = alias.Name; if (alias.Target.IsType && ((INamedTypeSymbol)alias.Target).IsInterfaceType() && CanRemoveInterfacePrefix(name)) { name = name.Substring(1); } using var breaks = TemporaryArray <TextSpan> .Empty; StringBreaker.AddWordParts(name, ref breaks.AsRef()); var result = GetInterleavedPatterns(breaks, name, pluralize: false); return(result); }
public static async Task <Document> RefactorAsync( Document document, UsingDirectiveSyntax usingDirective, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); IAliasSymbol aliasSymbol = semanticModel.GetDeclaredSymbol(usingDirective, cancellationToken); SyntaxNode parent = usingDirective.Parent; Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), ""); SyntaxList <UsingDirectiveSyntax> usings = GetUsings(parent); int index = usings.IndexOf(usingDirective); List <IdentifierNameSyntax> names = CollectNames(parent, aliasSymbol, semanticModel, cancellationToken); NameSyntax name = usingDirective.Name; ISymbol symbol = semanticModel.GetSymbol(name, cancellationToken); SyntaxNode newNode = parent.ReplaceNodes(names, (f, _) => { if (symbol != null && semanticModel .GetSpeculativeSymbolInfo(f.SpanStart, name, SpeculativeBindingOption.BindAsTypeOrNamespace) .Symbol? .Equals(symbol) == true) { return(name.WithTriviaFrom(f)); } else { return(aliasSymbol.Target.ToMinimalTypeSyntax(semanticModel, f.SpanStart).WithTriviaFrom(f)); } }); newNode = RemoveUsingDirective(newNode, index); return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false)); }
private static TokenSemanticInfo GetSemanticInfo( SemanticModelBase semanticModel, ISemanticFactsService semanticFacts, ISyntaxFactsService syntaxFacts, ISyntaxToken token, CancellationToken cancellationToken) { //var aliasSymbol = semanticModel.GetAliasInfo(token.Parent, cancellationToken); IAliasSymbol aliasSymbol = null; var bindableParent = syntaxFacts.GetBindableParent(token); var type = semanticModel.GetTypeInfo(bindableParent).Type; var declaredSymbol = semanticFacts.GetDeclaredSymbol(semanticModel, token, cancellationToken); var allSymbols = semanticModel.GetSymbolInfo(bindableParent) .GetBestOrAllSymbols() .WhereAsArray(s => !s.Equals(declaredSymbol)) .SelectAsArray(s => s); return(new TokenSemanticInfo(declaredSymbol, aliasSymbol, allSymbols, type)); }
public static void Analyze(SyntaxNodeAnalysisContext context, IdentifierNameSyntax identifierName) { if (!identifierName.IsVar && !identifierName.IsParentKind( SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.QualifiedName, SyntaxKind.UsingDirective)) { var typeSymbol = context.SemanticModel.GetSymbol(identifierName, context.CancellationToken) as ITypeSymbol; if (typeSymbol?.SupportsPredefinedType() == true) { IAliasSymbol aliasSymbol = context.SemanticModel.GetAliasInfo(identifierName, context.CancellationToken); if (aliasSymbol == null) { ReportDiagnostic(context, identifierName); } } } }
internal ReferenceLocation( Document document, IAliasSymbol alias, Location location, bool isImplicit, SymbolUsageInfo symbolUsageInfo, ImmutableDictionary <string, string> additionalProperties, CandidateReason candidateReason ) : this( document, alias, location, isImplicit, symbolUsageInfo, additionalProperties, candidateReason, containingStringLocation : Location.None ) { }
protected static ITypeSymbol GetSymbolType(ISymbol symbol) { ILocalSymbol localSymbol = symbol as ILocalSymbol; if (localSymbol != null) { return(localSymbol.Type); } IFieldSymbol fieldSymbol = symbol as IFieldSymbol; if (fieldSymbol != null) { return(fieldSymbol.Type); } IPropertySymbol propertySymbol = symbol as IPropertySymbol; if (propertySymbol != null) { return(propertySymbol.Type); } IParameterSymbol parameterSymbol = symbol as IParameterSymbol; if (parameterSymbol != null) { return(parameterSymbol.Type); } IAliasSymbol aliasSymbol = symbol as IAliasSymbol; if (aliasSymbol != null) { return(aliasSymbol.Target as ITypeSymbol); } return(symbol as ITypeSymbol); }
public static async Task <Document> RefactorAsync( Document document, UsingDirectiveSyntax usingDirective, CancellationToken cancellationToken) { SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); IAliasSymbol aliasSymbol = semanticModel.GetDeclaredSymbol(usingDirective, cancellationToken); SyntaxNode parent = usingDirective.Parent; Debug.Assert(parent.IsKind(SyntaxKind.CompilationUnit, SyntaxKind.NamespaceDeclaration), ""); int index = SyntaxInfo.UsingDirectiveListInfo(parent).IndexOf(usingDirective); var rewriter = new Rewriter(aliasSymbol, aliasSymbol.Target.ToTypeSyntax(), semanticModel, cancellationToken); SyntaxNode newNode = rewriter.Visit(parent); newNode = RemoveUsingDirective(newNode, index); return(await document.ReplaceNodeAsync(parent, newNode, cancellationToken).ConfigureAwait(false)); }
private static List <IdentifierNameSyntax> CollectNames( SyntaxNode node, IAliasSymbol aliasSymbol, SemanticModel semanticModel, CancellationToken cancellationToken) { var names = new List <IdentifierNameSyntax>(); foreach (SyntaxNode descendant in node.DescendantNodes()) { if (descendant.Kind() == SyntaxKind.IdentifierName) { IAliasSymbol symbol = semanticModel.GetAliasInfo(descendant, cancellationToken); if (symbol?.Equals(aliasSymbol) == true) { names.Add((IdentifierNameSyntax)descendant); } } } return(names); }
public static void Analyze(SyntaxNodeAnalysisContext context, MemberAccessExpressionSyntax memberAccess) { if (!memberAccess.IsParentKind(SyntaxKind.SimpleMemberAccessExpression)) { ExpressionSyntax expression = memberAccess.Expression; if (expression?.IsKind( SyntaxKind.SimpleMemberAccessExpression, SyntaxKind.IdentifierName) == true) { var typeSymbol = context.SemanticModel.GetSymbol(expression, context.CancellationToken) as ITypeSymbol; if (typeSymbol?.SupportsPredefinedType() == true) { IAliasSymbol aliasSymbol = context.SemanticModel.GetAliasInfo(expression, context.CancellationToken); if (aliasSymbol == null) { ReportDiagnostic(context, expression); } } } } }
public override void VisitAlias(IAliasSymbol symbol) { // TODO(cyrusn): I don't think we need to inspect the target of an alias. }
public virtual TResult?VisitAlias(IAliasSymbol symbol) { return(DefaultVisit(symbol)); }
public RenameRewriter(RenameRewriterParameters parameters) : base(visitIntoStructuredTrivia: true) { _documentId = parameters.Document.Id; _renameRenamableSymbolDeclaration = parameters.RenamedSymbolDeclarationAnnotation; _solution = parameters.OriginalSolution; _replacementText = parameters.ReplacementText; _originalText = parameters.OriginalText; _possibleNameConflicts = parameters.PossibleNameConflicts; _renameLocations = parameters.RenameLocations; _conflictLocations = parameters.ConflictLocationSpans; _cancellationToken = parameters.CancellationToken; _semanticModel = parameters.SemanticModel; _renamedSymbol = parameters.RenameSymbol; _replacementTextValid = parameters.ReplacementTextValid; _renameSpansTracker = parameters.RenameSpansTracker; _isRenamingInStrings = parameters.OptionSet.GetOption(RenameOptions.RenameInStrings); _isRenamingInComments = parameters.OptionSet.GetOption(RenameOptions.RenameInComments); _stringAndCommentTextSpans = parameters.StringAndCommentTextSpans; _renameAnnotations = parameters.RenameAnnotations; _aliasSymbol = _renamedSymbol as IAliasSymbol; _renamableDeclarationLocation = _renamedSymbol.Locations.FirstOrDefault(loc => loc.IsInSource && loc.SourceTree == _semanticModel.SyntaxTree); _isVerbatim = _replacementText.StartsWith("@", StringComparison.Ordinal); _simplificationService = parameters.Document.Project.LanguageServices.GetService<ISimplificationService>(); _semanticFactsService = parameters.Document.Project.LanguageServices.GetService<ISemanticFactsService>(); }
public static void Create(IAliasSymbol symbol, SymbolKeyWriter visitor) { visitor.WriteString(symbol.Name); visitor.WriteSymbolKey(symbol.Target); visitor.WriteString(symbol.DeclaringSyntaxReferences.FirstOrDefault()?.SyntaxTree.FilePath ?? ""); }
public static SerializableSymbolAndProjectId Dehydrate( IAliasSymbol alias, Document document) { return alias == null ? null : Dehydrate(new SymbolAndProjectId(alias, document.Project.Id)); }
// We must verify that the alias actually binds back to the thing it's aliasing. // It's possible there's another symbol with the same name as the alias that binds // first private static bool ValidateAliasForTarget(IAliasSymbol aliasReplacement, SemanticModel semanticModel, ExpressionSyntax node, ISymbol symbol) { var aliasName = aliasReplacement.Name; var boundSymbols = semanticModel.LookupNamespacesAndTypes(node.SpanStart, name: aliasName); if (boundSymbols.Length == 1) { var boundAlias = boundSymbols[0] as IAliasSymbol; if (boundAlias != null && aliasReplacement.Target.Equals(symbol)) { return true; } } return false; }
public override Accessibility VisitAlias(IAliasSymbol symbol) { return(symbol.Target.Accept(this)); }
public sealed override void VisitAlias(IAliasSymbol symbol) { Visit(symbol.Target); }
public override void VisitAlias(IAliasSymbol symbol) { base.VisitAlias(symbol); }
public override TypeSyntax VisitAlias(IAliasSymbol symbol) { return(AddInformationTo(symbol.Name.ToIdentifierName(), symbol)); }
public sealed override void Create(IAliasSymbol symbol, SymbolKeyWriter visitor) { visitor.WriteString(symbol.Name); visitor.WriteSymbolKey(symbol.Target); visitor.WriteString(symbol.DeclaringSyntaxReferences.FirstOrDefault()?.SyntaxTree.FilePath ?? ""); }
public virtual void VisitAlias(IAliasSymbol symbol) { DefaultVisit(symbol); }
private static bool TryReplaceWithAlias(this ExpressionSyntax node, SemanticModel semanticModel, bool preferAliasToQualifiedName, CancellationToken cancellationToken, out IAliasSymbol aliasReplacement) { aliasReplacement = null; if (!node.IsAliasReplaceableExpression()) { return false; } var symbol = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol; // If the Symbol is a constructor get its containing type if (symbol.IsConstructor()) { symbol = symbol.ContainingType; } if (node is QualifiedNameSyntax || node is AliasQualifiedNameSyntax) { SyntaxAnnotation aliasAnnotationInfo = null; // The following condition checks if the user has used alias in the original code and // if so the expression is replaced with the Alias if (node is QualifiedNameSyntax) { var qualifiedNameNode = (QualifiedNameSyntax)node; if (qualifiedNameNode.Right.Identifier.HasAnnotations(AliasAnnotation.Kind)) { aliasAnnotationInfo = qualifiedNameNode.Right.Identifier.GetAnnotations(AliasAnnotation.Kind).Single(); } } if (node is AliasQualifiedNameSyntax) { var aliasQualifiedNameNode = (AliasQualifiedNameSyntax)node; if (aliasQualifiedNameNode.Name.Identifier.HasAnnotations(AliasAnnotation.Kind)) { aliasAnnotationInfo = aliasQualifiedNameNode.Name.Identifier.GetAnnotations(AliasAnnotation.Kind).Single(); } } if (aliasAnnotationInfo != null) { var aliasName = AliasAnnotation.GetAliasName(aliasAnnotationInfo); var aliasIdentifier = SyntaxFactory.IdentifierName(aliasName); var aliasTypeInfo = semanticModel.GetSpeculativeAliasInfo(node.SpanStart, aliasIdentifier, SpeculativeBindingOption.BindAsTypeOrNamespace); if (aliasTypeInfo != null) { aliasReplacement = aliasTypeInfo; return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol); } } } if (node.Kind() == SyntaxKind.IdentifierName && semanticModel.GetAliasInfo((IdentifierNameSyntax)node, cancellationToken) != null) { return false; } // an alias can only replace a type or namespace if (symbol == null || (symbol.Kind != SymbolKind.Namespace && symbol.Kind != SymbolKind.NamedType)) { return false; } if (node is QualifiedNameSyntax) { var qualifiedName = (QualifiedNameSyntax)node; if (!qualifiedName.Right.HasAnnotation(Simplifier.SpecialTypeAnnotation)) { var type = semanticModel.GetTypeInfo(node, cancellationToken).Type; if (type != null) { var keywordKind = GetPredefinedKeywordKind(type.SpecialType); if (keywordKind != SyntaxKind.None) { preferAliasToQualifiedName = false; } } } } if (node is AliasQualifiedNameSyntax) { var aliasQualifiedNameSyntax = (AliasQualifiedNameSyntax)node; if (!aliasQualifiedNameSyntax.Name.HasAnnotation(Simplifier.SpecialTypeAnnotation)) { var type = semanticModel.GetTypeInfo(node, cancellationToken).Type; if (type != null) { var keywordKind = GetPredefinedKeywordKind(type.SpecialType); if (keywordKind != SyntaxKind.None) { preferAliasToQualifiedName = false; } } } } aliasReplacement = GetAliasForSymbol((INamespaceOrTypeSymbol)symbol, node.GetFirstToken(), semanticModel, cancellationToken); if (aliasReplacement != null && preferAliasToQualifiedName) { return ValidateAliasForTarget(aliasReplacement, semanticModel, node, symbol); } return false; }
public override object VisitAlias(IAliasSymbol aliasSymbol) { WriteType(SymbolKeyType.Alias); AliasSymbolKey.Create(aliasSymbol, this); return(null); }
public override Accessibility VisitAlias(IAliasSymbol symbol) => symbol.Target.Accept(this);
public RenameRewriter(RenameRewriterParameters parameters) : base(visitIntoStructuredTrivia: true) { this.documentId = parameters.Document.Id; this.renameRenamableSymbolDeclaration = parameters.RenamedSymbolDeclarationAnnotation; this.solution = parameters.OriginalSolution; this.replacementText = parameters.ReplacementText; this.originalText = parameters.OriginalText; this.possibleNameConflicts = parameters.PossibleNameConflicts; this.renameLocations = parameters.RenameLocations; this.conflictLocations = parameters.ConflictLocationSpans; this.cancellationToken = parameters.CancellationToken; this.semanticModel = (SemanticModel)parameters.SemanticModel; this.renamedSymbol = parameters.RenameSymbol; this.replacementTextValid = parameters.ReplacementTextValid; this.renameSpansTracker = parameters.RenameSpansTracker; this.isRenamingInStrings = parameters.OptionSet.GetOption(RenameOptions.RenameInStrings); this.isRenamingInComments = parameters.OptionSet.GetOption(RenameOptions.RenameInComments); this.stringAndCommentTextSpans = parameters.StringAndCommentTextSpans; this.renameAnnotations = parameters.RenameAnnotations; this.aliasSymbol = this.renamedSymbol as IAliasSymbol; this.renamableDeclarationLocation = this.renamedSymbol.Locations.Where(loc => loc.IsInSource && loc.SourceTree == semanticModel.SyntaxTree).FirstOrDefault(); this.isVerbatim = this.replacementText.StartsWith("@"); this.simplificationService = LanguageService.GetService<ISimplificationService>(parameters.Document); }