Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace AutomaticInterface
{
internal sealed class AddNullableAnnotationSyntaxRewriter : CSharpSyntaxRewriter
{
public override SyntaxNode? VisitParameter(ParameterSyntax node)
{
var newNode = node;
if (node.Type != null)
{
newNode = node.WithType(
SyntaxFactory.ParseTypeName(
node.Type.GetLeadingTrivia()
+ node.Type.ToString()
+ "?"
+ node.Type.GetTrailingTrivia()
)
);
}
return base.VisitParameter(newNode);
}
}
}
27 changes: 5 additions & 22 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,26 +192,6 @@ private static string GetParameterDisplayString(
bool nullableContextEnabled
)
{
var paramParts = param.ToDisplayParts(FullyQualifiedDisplayFormat);
var typeSb = new StringBuilder();
var restSb = new StringBuilder();
var isInsideType = true;
// The part before the first space is the parameter type
foreach (var part in paramParts)
{
if (isInsideType && part.Kind == SymbolDisplayPartKind.Space)
{
isInsideType = false;
}
if (isInsideType)
{
typeSb.Append(part.ToString());
}
else
{
restSb.Append(part.ToString());
}
}
// If this parameter has default value null and we're enabling the nullable context, we need to force the nullable annotation if there isn't one already
if (
param.HasExplicitDefaultValue
Expand All @@ -221,9 +201,12 @@ bool nullableContextEnabled
&& nullableContextEnabled
)
{
typeSb.Append('?');
var addNullable = new AddNullableAnnotationSyntaxRewriter();
return addNullable
.Visit(param.DeclaringSyntaxReferences.First().GetSyntax())
.ToFullString();
}
return typeSb.Append(restSb).ToString();
return param.ToDisplayString(FullyQualifiedDisplayFormat);
}

private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace AutomaticInterfaceExample
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(int?, int, string)" />
bool TryStartTransaction(int? param, int param2 = 0, string? data = null);
/// <inheritdoc cref="AutomaticInterfaceExample.DemoClass.TryStartTransaction(int?, int, string, Func{int, int})" />
bool TryStartTransaction(int? param, int param2 = 0, string? data = null, Func<int, int>? func = null);

}
}
Expand Down
8 changes: 4 additions & 4 deletions AutomaticInterface/Tests/Methods/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
public class DemoClass
{
public bool TryStartTransaction(int? param, int param2 = 0, string data = null)
{
return true;
}
public bool TryStartTransaction(int? param, int param2 = 0, string data = null, Func<int, int> func = null)
{
return true;
}
}


Expand Down
Loading