|
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; |
@@ -155,7 +156,7 @@ private static void AddMethod(InterfaceBuilder codeGenerator, IMethodSymbol meth |
155 | 156 |
|
156 | 157 | var paramResult = new HashSet<string>(); |
157 | 158 | method |
158 | | - .Parameters.Select(x => x.ToDisplayString(FullyQualifiedDisplayFormat)) |
| 159 | + .Parameters.Select(p => GetParameterDisplayString(p, codeGenerator)) |
159 | 160 | .ToList() |
160 | 161 | .ForEach(x => paramResult.Add(x)); |
161 | 162 |
|
@@ -226,6 +227,35 @@ private static bool IsNullable(ITypeSymbol typeSymbol) |
226 | 227 | return false; |
227 | 228 | } |
228 | 229 |
|
| 230 | + private static string GetParameterDisplayString(IParameterSymbol param, InterfaceBuilder codeGenerator) |
| 231 | + { |
| 232 | + var paramParts = param.ToDisplayParts(FullyQualifiedDisplayFormat); |
| 233 | + var typeSb = new StringBuilder(); |
| 234 | + var restSb = new StringBuilder(); |
| 235 | + var isInsideType = true; |
| 236 | + // The part before the first space is the parameter type |
| 237 | + foreach (var part in paramParts) |
| 238 | + { |
| 239 | + if (isInsideType && part.Kind == SymbolDisplayPartKind.Space) |
| 240 | + { |
| 241 | + isInsideType = false; |
| 242 | + } |
| 243 | + if (isInsideType) |
| 244 | + { |
| 245 | + typeSb.Append(part.ToString()); |
| 246 | + } else |
| 247 | + { |
| 248 | + restSb.Append(part.ToString()); |
| 249 | + } |
| 250 | + } |
| 251 | + // 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 |
| 252 | + if (param.HasExplicitDefaultValue && param.ExplicitDefaultValue is null && param.NullableAnnotation != NullableAnnotation.Annotated && param.Type.IsReferenceType && codeGenerator.HasNullable) |
| 253 | + { |
| 254 | + typeSb.Append('?'); |
| 255 | + } |
| 256 | + return typeSb.Append(restSb).ToString(); |
| 257 | + } |
| 258 | + |
229 | 259 | private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator) |
230 | 260 | { |
231 | 261 | members |
|
0 commit comments