Skip to content

Commit

Permalink
Added support for type parameter and added support for more compiler …
Browse files Browse the repository at this point in the history
…diagnostics cases.
  • Loading branch information
MaStr11 committed Oct 5, 2017
1 parent 3d943fe commit 6dfd11e
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 76 deletions.
401 changes: 378 additions & 23 deletions src/EditorFeatures/CSharpTest/AddParameter/AddParameterTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@ internal class CSharpAddParameterCodeFixProvider : AbstractAddParameterCodeFixPr
ArgumentListSyntax,
AttributeArgumentListSyntax,
InvocationExpressionSyntax,
ObjectCreationExpressionSyntax>
ObjectCreationExpressionSyntax,
TypeSyntax>
{
private static readonly ImmutableArray<string> AddParameterFixableDiagnosticIds =
GenerateConstructorDiagnosticIds.AllDiagnosticIds.Union(
GenerateMethodDiagnosticIds.FixableDiagnosticIds).Union(
Enumerable.Repeat("CS1593", 1)). // C# Delegate 'Action' does not take 1 arguments
ToImmutableArray();

public override ImmutableArray<string> FixableDiagnosticIds => AddParameterFixableDiagnosticIds;
public override ImmutableArray<string> FixableDiagnosticIds
=> AddParameterFixableDiagnosticIds;

protected override ImmutableArray<string> TooManyArgumentsDiagnosticIds { get; } =
GenerateConstructorDiagnosticIds.TooManyArgumentsDiagnosticIds;
protected override ImmutableArray<string> TooManyArgumentsDiagnosticIds
=> GenerateConstructorDiagnosticIds.TooManyArgumentsDiagnosticIds;

protected override ImmutableArray<string> CannotConvertDiagnosticIds
=> GenerateConstructorDiagnosticIds.CannotConvertDiagnosticIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ internal static class GenerateConstructorDiagnosticIds
public const string CS1729 = nameof(CS1729); // CS1729: 'C' does not contain a constructor that takes n arguments
public const string CS1739 = nameof(CS1739); // CS1739: The best overload for 'Program' does not have a parameter named 'v'
public const string CS1503 = nameof(CS1503); // CS1503: Argument 1: cannot convert from 'T1' to 'T2'
public const string CS1660 = nameof(CS1660); // CS1660: Cannot convert lambda expression to type 'string[]' because it is not a delegate type
public const string CS7036 = nameof(CS7036); // CS7036: There is no argument given that corresponds to the required formal parameter 'v' of 'C.C(int)'

public static readonly ImmutableArray<string> AllDiagnosticIds =
ImmutableArray.Create(CS0122, CS1729, CS1739, CS1503, CS7036);

public static readonly ImmutableArray<string> TooManyArgumentsDiagnosticIds =
ImmutableArray.Create(CS1729);

public static readonly ImmutableArray<string> CannotConvertDiagnosticIds =
ImmutableArray.Create(CS1503, CS1660);
}

/// <summary>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.AddParameter
ArgumentListSyntax,
ArgumentListSyntax,
InvocationExpressionSyntax,
ObjectCreationExpressionSyntax)
ObjectCreationExpressionSyntax,
TypeSyntax)

Public Overrides ReadOnly Property FixableDiagnosticIds As ImmutableArray(Of String) =
GenerateConstructorDiagnosticIds.AllDiagnosticIds

Protected Overrides ReadOnly Property TooManyArgumentsDiagnosticIds As ImmutableArray(Of String) =
GenerateConstructorDiagnosticIds.TooManyArgumentsDiagnosticIds

Protected Overrides ReadOnly Property CannotConvertDiagnosticIds As ImmutableArray(Of String) =
GenerateConstructorDiagnosticIds.CannotConvertDiagnosticIds

End Class
End Namespace
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.GenerateConstructor

Friend Shared ReadOnly AllDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057, BC30272, BC30274, BC30389, BC30455, BC32006, BC30512, BC30387)
Friend Shared ReadOnly TooManyArgumentsDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30057)
Friend Shared ReadOnly CannotConvertDiagnosticIds As ImmutableArray(Of String) = ImmutableArray.Create(BC30512, BC32006)
End Class

<ExportCodeFixProvider(LanguageNames.VisualBasic, Name:=PredefinedCodeFixProviderNames.GenerateConstructor), [Shared]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SyntaxTrivia ElasticCarriageReturnLineFeed
protected override IDocumentationCommentService DocumentationCommentService
=> CSharpDocumentationCommentService.Instance;

public bool SupportsIndexingInitializer(ParseOptions options)
public bool SupportsIndexingInitializer(ParseOptions options)
=> ((CSharpParseOptions)options).LanguageVersion >= LanguageVersion.CSharp6;

public bool SupportsThrowExpression(ParseOptions options)
Expand Down Expand Up @@ -259,10 +259,10 @@ public bool IsReturnStatement(SyntaxNode node)

public bool IsStatement(SyntaxNode node)
=> node is StatementSyntax;

public bool IsParameter(SyntaxNode node)
=> node is ParameterSyntax;

public bool IsVariableDeclarator(SyntaxNode node)
=> node is VariableDeclaratorSyntax;

Expand Down Expand Up @@ -1343,6 +1343,25 @@ public SeparatedSyntaxList<SyntaxNode> GetArgumentsOfObjectCreationExpression(Sy
public SeparatedSyntaxList<SyntaxNode> GetArgumentsOfArgumentList(SyntaxNode argumentList)
=> (argumentList as ArgumentListSyntax)?.Arguments ?? default(SeparatedSyntaxList<SyntaxNode>);

public SeparatedSyntaxList<SyntaxNode> GetTypeArgumentsOfInvocationExpression(SyntaxNode invocationExpression)
{
if (invocationExpression is InvocationExpressionSyntax invocationExpressionSyntax)
{
switch (invocationExpressionSyntax.Expression)
{
case GenericNameSyntax genericName:
return genericName.TypeArgumentList.Arguments;
case MemberAccessExpressionSyntax memberAccessExpression when memberAccessExpression.Name is GenericNameSyntax genericName:
return genericName.TypeArgumentList.Arguments;
}
}

return default(SeparatedSyntaxList<SyntaxNode>);
}

public SeparatedSyntaxList<SyntaxNode> GetTypeArgumentsOfObjectCreationExpression(SyntaxNode objectCreationExpression)
=> ((objectCreationExpression as ObjectCreationExpressionSyntax)?.Type as GenericNameSyntax)?.TypeArgumentList?.Arguments ?? default(SeparatedSyntaxList<SyntaxNode>);

public bool IsRegularComment(SyntaxTrivia trivia)
=> trivia.IsRegularComment();

Expand Down Expand Up @@ -1422,7 +1441,7 @@ public bool IsDeclaration(SyntaxNode node)
private static readonly SyntaxAnnotation s_annotation = new SyntaxAnnotation();

public void AddFirstMissingCloseBrace(
SyntaxNode root, SyntaxNode contextNode,
SyntaxNode root, SyntaxNode contextNode,
out SyntaxNode newRoot, out SyntaxNode newContextNode)
{
// First, annotate the context node in the tree so that we can find it again
Expand Down Expand Up @@ -1587,9 +1606,9 @@ public override bool IsShebangDirectiveTrivia(SyntaxTrivia trivia)
public override bool IsPreprocessorDirective(SyntaxTrivia trivia)
=> SyntaxFacts.IsPreprocessorDirective(trivia.Kind());

private class AddFirstMissingCloseBaceRewriter: CSharpSyntaxRewriter
private class AddFirstMissingCloseBaceRewriter : CSharpSyntaxRewriter
{
private readonly SyntaxNode _contextNode;
private readonly SyntaxNode _contextNode;
private bool _seenContextNode = false;
private bool _addedFirstCloseCurly = false;

Expand Down Expand Up @@ -1626,7 +1645,7 @@ public override SyntaxNode Visit(SyntaxNode node)
// then still ask to format its close curly to make sure all the
// curlies up the stack are properly formatted.
var braces = rewritten.GetBraces();
if (braces.openBrace.Kind() == SyntaxKind.None &&
if (braces.openBrace.Kind() == SyntaxKind.None &&
braces.closeBrace.Kind() == SyntaxKind.None)
{
// Not an item with braces. Just pass it up.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ internal interface ISyntaxFactsService : ILanguageService
SeparatedSyntaxList<SyntaxNode> GetArgumentsOfInvocationExpression(SyntaxNode node);
SeparatedSyntaxList<SyntaxNode> GetArgumentsOfObjectCreationExpression(SyntaxNode node);
SeparatedSyntaxList<SyntaxNode> GetArgumentsOfArgumentList(SyntaxNode node);
SeparatedSyntaxList<SyntaxNode> GetTypeArgumentsOfInvocationExpression(SyntaxNode node);
SeparatedSyntaxList<SyntaxNode> GetTypeArgumentsOfObjectCreationExpression(SyntaxNode node);

bool IsUsingDirectiveName(SyntaxNode node);
bool IsIdentifierName(SyntaxNode node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return If(arguments.HasValue, arguments.Value, Nothing)
End Function

Public Function GetTypeArgumentsOfInvocationExpression(invocationExpression As SyntaxNode) As SeparatedSyntaxList(Of SyntaxNode) Implements ISyntaxFactsService.GetTypeArgumentsOfInvocationExpression
Dim arguments = TryCast(TryCast(invocationExpression, InvocationExpressionSyntax)?.Expression, GenericNameSyntax)?.TypeArgumentList?.Arguments
Return If(arguments.HasValue, arguments.Value, Nothing)
End Function

Public Function GetTypeArgumentsOfObjectCreationExpression(objectCreationExpression As SyntaxNode) As SeparatedSyntaxList(Of SyntaxNode) Implements ISyntaxFactsService.GetTypeArgumentsOfObjectCreationExpression
Dim arguments = TryCast(TryCast(objectCreationExpression, ObjectCreationExpressionSyntax)?.Type, GenericNameSyntax)?.TypeArgumentList?.Arguments
Return If(arguments.HasValue, arguments.Value, Nothing)
End Function

Public Function ConvertToSingleLine(node As SyntaxNode, Optional useElasticTrivia As Boolean = False) As SyntaxNode Implements ISyntaxFactsService.ConvertToSingleLine
Return node.ConvertToSingleLine(useElasticTrivia)
End Function
Expand Down

0 comments on commit 6dfd11e

Please sign in to comment.