|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
| 4 | +using System.Text; |
4 | 5 | using Microsoft.CodeAnalysis; |
5 | 6 | using Microsoft.CodeAnalysis.CSharp; |
6 | 7 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
@@ -115,7 +116,7 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth |
115 | 116 |
|
116 | 117 | var paramResult = new HashSet<string>(); |
117 | 118 | method |
118 | | - .Parameters.Select(x => x.ToDisplayString(FullyQualifiedDisplayFormat)) |
| 119 | + .Parameters.Select(p => GetParameterDisplayString(p, codeGenerator.HasNullable)) |
119 | 120 | .ToList() |
120 | 121 | .ForEach(x => paramResult.Add(x)); |
121 | 122 |
|
@@ -186,6 +187,45 @@ private static bool IsNullable(ITypeSymbol typeSymbol) |
186 | 187 | return false; |
187 | 188 | } |
188 | 189 |
|
| 190 | + private static string GetParameterDisplayString( |
| 191 | + IParameterSymbol param, |
| 192 | + bool nullableContextEnabled |
| 193 | + ) |
| 194 | + { |
| 195 | + var paramParts = param.ToDisplayParts(FullyQualifiedDisplayFormat); |
| 196 | + var typeSb = new StringBuilder(); |
| 197 | + var restSb = new StringBuilder(); |
| 198 | + var isInsideType = true; |
| 199 | + // The part before the first space is the parameter type |
| 200 | + foreach (var part in paramParts) |
| 201 | + { |
| 202 | + if (isInsideType && part.Kind == SymbolDisplayPartKind.Space) |
| 203 | + { |
| 204 | + isInsideType = false; |
| 205 | + } |
| 206 | + if (isInsideType) |
| 207 | + { |
| 208 | + typeSb.Append(part.ToString()); |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + restSb.Append(part.ToString()); |
| 213 | + } |
| 214 | + } |
| 215 | + // 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 |
| 216 | + if ( |
| 217 | + param.HasExplicitDefaultValue |
| 218 | + && param.ExplicitDefaultValue is null |
| 219 | + && param.NullableAnnotation != NullableAnnotation.Annotated |
| 220 | + && param.Type.IsReferenceType |
| 221 | + && nullableContextEnabled |
| 222 | + ) |
| 223 | + { |
| 224 | + typeSb.Append('?'); |
| 225 | + } |
| 226 | + return typeSb.Append(restSb).ToString(); |
| 227 | + } |
| 228 | + |
189 | 229 | private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator) |
190 | 230 | { |
191 | 231 | members |
|
0 commit comments