|
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,45 @@ private static bool IsNullable(ITypeSymbol typeSymbol) |
226 | 227 | return false; |
227 | 228 | } |
228 | 229 |
|
| 230 | + private static string GetParameterDisplayString( |
| 231 | + IParameterSymbol param, |
| 232 | + InterfaceBuilder codeGenerator |
| 233 | + ) |
| 234 | + { |
| 235 | + var paramParts = param.ToDisplayParts(FullyQualifiedDisplayFormat); |
| 236 | + var typeSb = new StringBuilder(); |
| 237 | + var restSb = new StringBuilder(); |
| 238 | + var isInsideType = true; |
| 239 | + // The part before the first space is the parameter type |
| 240 | + foreach (var part in paramParts) |
| 241 | + { |
| 242 | + if (isInsideType && part.Kind == SymbolDisplayPartKind.Space) |
| 243 | + { |
| 244 | + isInsideType = false; |
| 245 | + } |
| 246 | + if (isInsideType) |
| 247 | + { |
| 248 | + typeSb.Append(part.ToString()); |
| 249 | + } |
| 250 | + else |
| 251 | + { |
| 252 | + restSb.Append(part.ToString()); |
| 253 | + } |
| 254 | + } |
| 255 | + // 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 |
| 256 | + if ( |
| 257 | + param.HasExplicitDefaultValue |
| 258 | + && param.ExplicitDefaultValue is null |
| 259 | + && param.NullableAnnotation != NullableAnnotation.Annotated |
| 260 | + && param.Type.IsReferenceType |
| 261 | + && codeGenerator.HasNullable |
| 262 | + ) |
| 263 | + { |
| 264 | + typeSb.Append('?'); |
| 265 | + } |
| 266 | + return typeSb.Append(restSb).ToString(); |
| 267 | + } |
| 268 | + |
229 | 269 | private static void AddEventsToInterface(List<ISymbol> members, InterfaceBuilder codeGenerator) |
230 | 270 | { |
231 | 271 | members |
|
0 commit comments