public void ThrowsArgumentNullException_When_SemanticModelIsNull() { TypeParameterSyntax parameter = GetNode <TypeParameterSyntax>("class Test<[Test]T> { }"); SemanticModel? semanticModel = null; Assert.Throws <ArgumentNullException>(() => semanticModel !.GetAllAttributesOfType(parameter, AttributeSymbol)); }
public static string TypeParameter(TypeParameterSyntax prm, IMethodSymbol methodSymbol, MethodDeclarationSyntax methodSyntax) { var identifier = Utility.TypeConstraints(prm, methodSyntax.ConstraintClauses); return(identifier); }
private static string TypeParameter(TypeParameterSyntax type) { var ret = Utility.TypeConstraints(type, Context.Instance.Partials.SelectMany(z => z.Syntax.As <TypeDeclarationSyntax>().ConstraintClauses)); return(ret); }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { if (!Settings.IsCodeFixEnabled(CodeFixIdentifiers.RemoveTypeParameter)) { return; } SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); TypeParameterSyntax typeParameter = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <TypeParameterSyntax>(); Debug.Assert(typeParameter != null, $"{nameof(typeParameter)} is null"); if (typeParameter == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case CompilerDiagnosticIdentifiers.TypeParameterHasSameNameAsTypeParameterFromOuterType: { TypeParameterInfo info; if (TypeParameterInfo.TryCreate(typeParameter, out info)) { CodeAction codeAction = CodeAction.Create( $"Remove type parameter '{info.Name}'", cancellationToken => { SeparatedSyntaxList <TypeParameterSyntax> parameters = info.TypeParameterList.Parameters; TypeParameterListSyntax newTypeParameterList = (parameters.Count == 1) ? default(TypeParameterListSyntax) : info.TypeParameterList.WithParameters(parameters.Remove(typeParameter)); SyntaxNode newNode = GenericDeclarationHelper.WithTypeParameterList(info.Declaration, newTypeParameterList); TypeParameterConstraintClauseSyntax constraintClause = info.ConstraintClause; if (constraintClause != null) { newNode = GenericDeclarationHelper.WithConstraintClauses(newNode, info.ConstraintClauses.Remove(constraintClause)); } return(context.Document.ReplaceNodeAsync(info.Declaration, newNode, cancellationToken)); }, GetEquivalenceKey(diagnostic)); context.RegisterCodeFix(codeAction, diagnostic); } break; } } } }
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); TypeParameterSyntax typeParameter = root .FindNode(context.Span, getInnermostNodeForTie: true)? .FirstAncestorOrSelf <TypeParameterSyntax>(); Debug.Assert(typeParameter != null, $"{nameof(typeParameter)} is null"); if (typeParameter == null) { return; } foreach (Diagnostic diagnostic in context.Diagnostics) { switch (diagnostic.Id) { case DiagnosticIdentifiers.AddTypeParameterToDocumentationComment: { var refactoring = new AddTypeParameterToDocumentationCommentRefactoring(); CodeAction codeAction = CodeAction.Create( "Add type parameter to documentation comment", cancellationToken => refactoring.RefactorAsync(context.Document, typeParameter, cancellationToken), diagnostic.Id + EquivalenceKeySuffix); context.RegisterCodeFix(codeAction, diagnostic); break; } } } }
internal GenericParameterDeclarationSyntax( TypeParameterSyntax typeParameter, TypeParameterConstraintClauseSyntax constraintClause) { this.typeParameter = typeParameter; this.constraintClause = constraintClause; }
public GenericInfo RemoveTypeParameter(TypeParameterSyntax typeParameter) { switch (Kind) { case SyntaxKind.ClassDeclaration: return(new GenericInfo(((ClassDeclarationSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.DelegateDeclaration: return(new GenericInfo(((DelegateDeclarationSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.InterfaceDeclaration: return(new GenericInfo(((InterfaceDeclarationSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.LocalFunctionStatement: return(new GenericInfo(((LocalFunctionStatementSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.MethodDeclaration: return(new GenericInfo(((MethodDeclarationSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.StructDeclaration: return(new GenericInfo(((StructDeclarationSyntax)Declaration).WithTypeParameterList(RemoveTypeParameterHelper(typeParameter)))); case SyntaxKind.None: return(this); } Debug.Fail(Kind.ToString()); return(this); }
public static Task <Document> RefactorAsync( Document document, TypeParameterSyntax typeParameter, CancellationToken cancellationToken) { SyntaxNode node = typeParameter; var typeParameterList = (TypeParameterListSyntax)typeParameter.Parent; if (typeParameterList.Parameters.Count == 1) { node = typeParameterList; } SyntaxRemoveOptions options = SyntaxRemover.DefaultRemoveOptions; if (node.GetLeadingTrivia().All(f => f.IsWhitespaceTrivia())) { options &= ~SyntaxRemoveOptions.KeepLeadingTrivia; } if (node.GetTrailingTrivia().All(f => f.IsWhitespaceTrivia())) { options &= ~SyntaxRemoveOptions.KeepTrailingTrivia; } return(document.RemoveNodeAsync(node, options, cancellationToken)); }
private TypeParameterInfo( TypeParameterSyntax typeParameter, TypeParameterListSyntax typeParameterList) { TypeParameter = typeParameter; TypeParameterList = typeParameterList; }
private (TypeParameterSyntax syntax, SemanticModel semanticModel) GetTypeParameter(string src) { TypeParameterSyntax parameter = GetNode <TypeParameterSyntax>(src); SemanticModel semanticModel = Compilation.CurrentCompilation.GetSemanticModel(parameter.SyntaxTree, true); return(parameter, semanticModel); }
private Doc PrintTypeParameterSyntax(TypeParameterSyntax node) { return(Concat( this.PrintAttributeLists(node, node.AttributeLists), this.PrintSyntaxToken(node.VarianceKeyword, " "), this.PrintSyntaxToken(node.Identifier) )); }
public static Doc Print(TypeParameterSyntax node) { return(Doc.Concat( AttributeLists.Print(node, node.AttributeLists), Token.Print(node.VarianceKeyword, " "), Token.Print(node.Identifier) )); }
private TypeParameterListSyntax RemoveTypeParameterHelper(TypeParameterSyntax typeParameter) { SeparatedSyntaxList <TypeParameterSyntax> parameters = TypeParameters; return((parameters.Count == 1) ? default(TypeParameterListSyntax) : TypeParameterList.WithParameters(parameters.Remove(typeParameter))); }
private TypeParameterInfo(TypeParameterSyntax typeParameter, SyntaxNode declaration, TypeParameterListSyntax typeParameterList, SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses) : this() { TypeParameter = typeParameter; Name = typeParameter.Identifier.ValueText; Declaration = declaration; TypeParameterList = typeParameterList; ConstraintClauses = constraintClauses; }
public override void VisitTypeParameter(TypeParameterSyntax node) { if (node == null) { throw new InvalidOperationException("Type Parameter node is null!"); } ((m_currentParent as AbstractAddressableEntity)).AddTypeParameter(node.Identifier.Text); base.VisitTypeParameter(node); }
public override void VisitTypeParameter(TypeParameterSyntax node) { foreach (AttributeListSyntax listSyntax in node.AttributeLists) { listSyntax.Accept(this); } base.VisitTypeParameter(node); }
/// <summary> /// Initializes a new instance of the <see cref="TypeParameterData"/> struct. /// </summary> /// <param name="syntax">Target <see cref="TypeParameterSyntax"/>.</param> /// <param name="symbol"><see cref="ITypeParameterSymbol"/> represented by the target <paramref name="syntax"/>.</param> /// <param name="semanticModel"><see cref="Microsoft.CodeAnalysis.SemanticModel"/> of the target <paramref name="syntax"/>.</param> /// <param name="attribute">Valid <c>Durian.DefaultParamAttribute</c> defined on the target <paramref name="symbol"/>.</param> /// <param name="targetType">The <see cref="ITypeSymbol"/> that was specified using the <paramref name="attribute"/>. -or- <see langword="null"/> if <paramref name="attribute"/> is <see langword="null"/> or the type cannot be resolved because of error.</param> public TypeParameterData(TypeParameterSyntax syntax, ITypeParameterSymbol symbol, SemanticModel semanticModel, AttributeSyntax?attribute, ITypeSymbol?targetType) { Syntax = syntax; Symbol = symbol; Attribute = attribute; TargetType = targetType; Parent = (MemberDeclarationSyntax)syntax.Parent !.Parent !; SemanticModel = semanticModel; }
public static bool TryCreate(TypeParameterSyntax typeParameter, out TypeParameterInfo info) { if (typeParameter.IsParentKind(SyntaxKind.TypeParameterList)) { var typeParameterList = (TypeParameterListSyntax)typeParameter.Parent; SyntaxNode parent = typeParameterList.Parent; switch (parent?.Kind()) { case SyntaxKind.ClassDeclaration: { var classDeclaration = (ClassDeclarationSyntax)parent; info = new TypeParameterInfo(typeParameter, classDeclaration, typeParameterList, classDeclaration.ConstraintClauses); return(true); } case SyntaxKind.DelegateDeclaration: { var delegateDeclaration = (DelegateDeclarationSyntax)parent; info = new TypeParameterInfo(typeParameter, delegateDeclaration, typeParameterList, delegateDeclaration.ConstraintClauses); return(true); } case SyntaxKind.InterfaceDeclaration: { var interfaceDeclaration = (InterfaceDeclarationSyntax)parent; info = new TypeParameterInfo(typeParameter, interfaceDeclaration, typeParameterList, interfaceDeclaration.ConstraintClauses); return(true); } case SyntaxKind.LocalFunctionStatement: { var localFunctionStatement = (LocalFunctionStatementSyntax)parent; info = new TypeParameterInfo(typeParameter, localFunctionStatement, typeParameterList, localFunctionStatement.ConstraintClauses); return(true); } case SyntaxKind.MethodDeclaration: { var methodDeclaration = (MethodDeclarationSyntax)parent; info = new TypeParameterInfo(typeParameter, methodDeclaration, typeParameterList, methodDeclaration.ConstraintClauses); return(true); } case SyntaxKind.StructDeclaration: { var structDeclaration = (StructDeclarationSyntax)parent; info = new TypeParameterInfo(typeParameter, structDeclaration, typeParameterList, structDeclaration.ConstraintClauses); return(true); } } } info = default(TypeParameterInfo); return(false); }
public override Evaluation VisitTypeParameter(TypeParameterSyntax node) { foreach (AttributeListSyntax listSyntax in node.AttributeLists) { listSyntax.Accept <Evaluation>(this); } return(base.VisitTypeParameter(node)); }
public override void VisitTypeParameter(TypeParameterSyntax node) { var name = node.Identifier.Value.ToString(); if (!this.UsedVariablesNames.Contains(name)) { this.UsedVariablesNames.Add(name); } base.VisitTypeParameter(node); }
/// <summary> /// build up a string of summary text for a generic type parameter. /// </summary> /// <param name="typeParameter">the type parameter.</param> /// <returns>the text describing it.</returns> public string BuildSummaryTextForTypeParameter(TypeParameterSyntax typeParameter) { var name = typeParameter.Identifier.Text; var sentence = this.SplitCamelCaseWords(name); var articleFree = this.RemoveArticles(sentence); if (string.IsNullOrEmpty(articleFree)) articleFree = $"{{{typeParameter.Identifier.Text}}}"; return $"a type of {string.Join(" ", articleFree)}."; }
public static string TypeParameter(TypeParameterSyntax param) { if (!(param.Parent.Parent is MethodDeclarationSyntax)) return param.Identifier.Text; var typeConstraints = ((MethodDeclarationSyntax)param.Parent.Parent).ConstraintClauses; var constraints = typeConstraints .FirstOrDefault(constr => constr.Name.Identifier.Text == param.Identifier.Text).Constraints; return "<" + param.Identifier.Text + ": " + string.Join(", ", constraints) + ">"; //TODO: check if this is the right syntax for multiple constraints }
public static string TypeParameter(TypeParameterSyntax prm, IMethodSymbol methodSymbol, MethodDeclarationSyntax methodSyntax) { var identifier = Utility.TypeConstraints(prm, methodSyntax.ConstraintClauses); if (ClassTags.NeedsClassTag(methodSymbol, methodSyntax, prm.Identifier.ValueText)) { identifier += ":ClassTag"; } return(identifier); }
private TypeParameterInfo( TypeParameterSyntax typeParameter, SyntaxNode declaration, TypeParameterListSyntax typeParameterList, SyntaxList <TypeParameterConstraintClauseSyntax> constraintClauses) { TypeParameter = typeParameter; Declaration = declaration; TypeParameterList = typeParameterList; ConstraintClauses = constraintClauses; }
public static TypeParameterInfo Create(TypeParameterSyntax typeParameter) { TypeParameterInfo info; if (TryCreate(typeParameter, out info)) { return(info); } throw new ArgumentException("", nameof(typeParameter)); }
private static string TypeParameter(TypeParameterSyntax type) { var ret = Utility.TypeConstraints(type, TypeState.Instance.Partials.SelectMany(z => z.Syntax.As <TypeDeclarationSyntax>().ConstraintClauses)); if (ClassTags.NeedsClassTag(TypeState.Instance.Partials.First().Symbol, type.Identifier.ValueText)) { ret += " :ClassTag"; } return(ret); }
public static string TypeParameter(TypeParameterSyntax node) { var constraint = ""; if (node.Parent.Parent is MethodDeclarationSyntax) { var typeConstraints = ((MethodDeclarationSyntax)node.Parent.Parent).ConstraintClauses; var constraints = typeConstraints.FirstOrDefault(constr => SyntaxNode(constr.Name) == node.Identifier.Text).Constraints; constraint = ": " + string.Join(", ", constraints); //TODO: check if this is the right syntax for multiple constraints } return(node.Identifier.Text + constraint); }
public override SyntaxNode VisitTypeParameter(TypeParameterSyntax node) { var newNode = (TypeParameterSyntax)base.VisitTypeParameter(node); if (_replacementMap.TryGetValue(node, out object newValue)) { return(newNode.WithIdentifier(SyntaxFactory.Identifier(newValue.ToString()))); } else { return(newNode); } }
public static string TypeParameter(TypeParameterSyntax param) { if (!(param.Parent.Parent is MethodDeclarationSyntax)) { return(param.Identifier.Text); } var typeConstraints = ((MethodDeclarationSyntax)param.Parent.Parent).ConstraintClauses; var constraints = typeConstraints .FirstOrDefault(constr => constr.Name.Identifier.Text == param.Identifier.Text).Constraints; return("<" + param.Identifier.Text + ": " + string.Join(", ", constraints) + ">"); //TODO: check if this is the right syntax for multiple constraints }
public static string TypeParameter(TypeParameterSyntax prm, IEnumerable <TypeParameterConstraintClauseSyntax> constraints) { var identifier = prm.Identifier.ValueText; var constraint = constraints.SingleOrDefault(o => o.Name.Identifier.ValueText == identifier); if (constraint == null) { return(identifier); } return(identifier + ": (" + string.Join(", ", constraint.Constraints.OfType <TypeConstraintSyntax>().ToList().Select(o => TypeProcessor.ConvertType(o.Type))) + ")"); }
internal static string TypeConstraints(TypeParameterSyntax prm, IEnumerable <TypeParameterConstraintClauseSyntax> constraints) { var identifier = prm.Identifier.ValueText; var constraint = constraints.SingleOrDefault(o => o.Name.Identifier.ValueText == identifier); if (constraint != null) { return(identifier + string.Join("", constraint.Constraints.Select(TransformTypeConstraint))); } return(identifier); }
internal static TypeParameterInfo Create(TypeParameterSyntax typeParameter) { if (!(typeParameter.Parent is TypeParameterListSyntax typeParameterList)) { return(Default); } SyntaxNode parent = typeParameterList.Parent; switch (parent?.Kind()) { case SyntaxKind.ClassDeclaration: { var classDeclaration = (ClassDeclarationSyntax)parent; return(new TypeParameterInfo(typeParameter, classDeclaration, typeParameterList, classDeclaration.ConstraintClauses)); } case SyntaxKind.DelegateDeclaration: { var delegateDeclaration = (DelegateDeclarationSyntax)parent; return(new TypeParameterInfo(typeParameter, delegateDeclaration, typeParameterList, delegateDeclaration.ConstraintClauses)); } case SyntaxKind.InterfaceDeclaration: { var interfaceDeclaration = (InterfaceDeclarationSyntax)parent; return(new TypeParameterInfo(typeParameter, interfaceDeclaration, typeParameterList, interfaceDeclaration.ConstraintClauses)); } case SyntaxKind.LocalFunctionStatement: { var localFunctionStatement = (LocalFunctionStatementSyntax)parent; return(new TypeParameterInfo(typeParameter, localFunctionStatement, typeParameterList, localFunctionStatement.ConstraintClauses)); } case SyntaxKind.MethodDeclaration: { var methodDeclaration = (MethodDeclarationSyntax)parent; return(new TypeParameterInfo(typeParameter, methodDeclaration, typeParameterList, methodDeclaration.ConstraintClauses)); } case SyntaxKind.StructDeclaration: { var structDeclaration = (StructDeclarationSyntax)parent; return(new TypeParameterInfo(typeParameter, structDeclaration, typeParameterList, structDeclaration.ConstraintClauses)); } } return(Default); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitTypeParameter(TypeParameterSyntax node) { this.OnNodeVisited(node, this.type.IsInstanceOfType(node)); base.VisitTypeParameter(node); }
public TypeParameterTranslation(TypeParameterSyntax syntax, SyntaxTranslation parent) : base(syntax, parent) { }
/// <summary> /// Given a type parameter declaration (field or method), get the corresponding symbol /// </summary> /// <param name="typeParameter"></param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public override ITypeParameterSymbol GetDeclaredSymbol(TypeParameterSyntax typeParameter, CancellationToken cancellationToken = default(CancellationToken)) { using (Logger.LogBlock(FunctionId.CSharp_SemanticModel_GetDeclaredSymbol, message: this.SyntaxTree.FilePath, cancellationToken: cancellationToken)) { if (typeParameter == null) { throw new ArgumentNullException("typeParameter"); } if (!IsInTree(typeParameter)) { throw new ArgumentException("typeParameter not within tree"); } var typeParamList = typeParameter.Parent as TypeParameterListSyntax; if (typeParamList != null) { var memberDecl = typeParamList.Parent as MemberDeclarationSyntax; if (memberDecl != null) { var symbol = GetDeclaredSymbol(memberDecl, cancellationToken); if ((object)symbol != null) { var typeSymbol = symbol as NamedTypeSymbol; if ((object)typeSymbol != null) { return this.GetTypeParameterSymbol(typeSymbol.TypeParameters, typeParameter); } var methodSymbol = symbol as MethodSymbol; if ((object)methodSymbol != null) { return this.GetTypeParameterSymbol(methodSymbol.TypeParameters, typeParameter) ?? ((object)methodSymbol.PartialDefinitionPart == null ? null : this.GetTypeParameterSymbol(methodSymbol.PartialDefinitionPart.TypeParameters, typeParameter)); } } } } return null; } }
/// <summary> /// Given a type parameter declaration (field or method), get the corresponding symbol /// </summary> /// <param name="typeParameter"></param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public override ITypeParameterSymbol GetDeclaredSymbol(TypeParameterSyntax typeParameter, CancellationToken cancellationToken = default(CancellationToken)) { if (typeParameter == null) { throw new ArgumentNullException(nameof(typeParameter)); } if (!IsInTree(typeParameter)) { throw new ArgumentException("typeParameter not within tree"); } var typeParamList = typeParameter.Parent as TypeParameterListSyntax; if (typeParamList != null) { var memberDecl = typeParamList.Parent as MemberDeclarationSyntax; if (memberDecl != null) { var symbol = GetDeclaredSymbol(memberDecl, cancellationToken); if ((object)symbol != null) { var typeSymbol = symbol as NamedTypeSymbol; if ((object)typeSymbol != null) { return this.GetTypeParameterSymbol(typeSymbol.TypeParameters, typeParameter); } var methodSymbol = symbol as MethodSymbol; if ((object)methodSymbol != null) { return this.GetTypeParameterSymbol(methodSymbol.TypeParameters, typeParameter) ?? ((object)methodSymbol.PartialDefinitionPart == null ? null : this.GetTypeParameterSymbol(methodSymbol.PartialDefinitionPart.TypeParameters, typeParameter)); } } } } return null; }
public override void VisitTypeParameter(TypeParameterSyntax node) { this.Found |= node.Identifier.ValueText == this.name; base.VisitTypeParameter(node); }
public override ITypeParameterSymbol GetDeclaredSymbol(TypeParameterSyntax typeParameter, CancellationToken cancellationToken = default(CancellationToken)) { // Can't define alias inside member. return null; }
public void VisitTypeParameter(TypeParameterSyntax node) { if (node == null) throw new ArgumentNullException("node"); node.Validate(); WriteAttributes(node, true); switch (node.Variance) { case Variance.In: _writer.WriteKeyword(PrinterKeyword.In); _writer.WriteSpace(); break; case Variance.Out: _writer.WriteKeyword(PrinterKeyword.Out); _writer.WriteSpace(); break; } _writer.WriteIdentifier(node.Identifier); }
/// <summary> /// Given a type parameter declaration (field or method), get the corresponding symbol /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="typeParameter"></param> public abstract ITypeParameterSymbol GetDeclaredSymbol(TypeParameterSyntax typeParameter, CancellationToken cancellationToken = default(CancellationToken));
private TypeParameterSymbol GetTypeParameterSymbol(ImmutableArray<TypeParameterSymbol> parameters, TypeParameterSyntax parameter) { foreach (var symbol in parameters) { foreach (var location in symbol.Locations) { if (location.SourceTree == this.SyntaxTree && parameter.Span.Contains(location.SourceSpan)) { return symbol; } } } return null; }
public override void VisitTypeParameter(TypeParameterSyntax node) { if (!node.Identifier.Span.IsEmpty) { var symbol = _sm.GetDeclaredSymbol(node); _defined.Add(symbol); var def = Def.For(symbol: symbol, type: "typeparam", name: symbol.Name) .At(_path, node.Identifier.Span); def.Local = true; AddDef(def); } base.VisitTypeParameter(node); }
private void ClassifyUpdate(TypeParameterSyntax oldNode, TypeParameterSyntax newNode) { if (!SyntaxFactory.AreEquivalent(oldNode.Identifier, newNode.Identifier)) { ReportError(RudeEditKind.Renamed); return; } Debug.Assert(!SyntaxFactory.AreEquivalent(oldNode.VarianceKeyword, newNode.VarianceKeyword)); ReportError(RudeEditKind.VarianceUpdate); }
/// <summary> /// /// </summary> /// <param name="node"></param> public override sealed void VisitTypeParameter(TypeParameterSyntax node) { this.OnNodeVisited(node); if (!this.traverseRootOnly) base.VisitTypeParameter(node); }