@@ -198,8 +198,8 @@ private static bool IsPropertyDeclaration(SyntaxToken token, SemanticModel seman
198
198
return result . Type != null ;
199
199
}
200
200
201
- private static bool IsMethodDeclaration ( SyntaxToken token , SemanticModel semanticModel ,
202
- CancellationToken cancellationToken , out NameDeclarationInfo result )
201
+ private static bool IsMethodDeclaration (
202
+ SyntaxToken token , SemanticModel semanticModel , CancellationToken cancellationToken , out NameDeclarationInfo result )
203
203
{
204
204
result = IsLastTokenOfType < MethodDeclarationSyntax > (
205
205
token ,
@@ -220,45 +220,30 @@ private static NameDeclarationInfo IsFollowingTypeOrComma<TSyntaxNode>(
220
220
Func < DeclarationModifiers , ImmutableArray < SymbolKindOrTypeKind > > possibleDeclarationComputer ,
221
221
CancellationToken cancellationToken ) where TSyntaxNode : SyntaxNode
222
222
{
223
- if ( ! IsPossibleTypeToken ( token ) && ! token . IsKind ( SyntaxKind . CommaToken ) )
224
- {
223
+ var afterComma = token . IsKind ( SyntaxKind . CommaToken ) ;
224
+ if ( ! IsPossibleTypeToken ( token ) && ! afterComma )
225
225
return default ;
226
- }
227
226
228
227
var target = token . GetAncestor < TSyntaxNode > ( ) ;
229
228
if ( target == null )
230
- {
231
229
return default ;
232
- }
233
230
234
- if ( token . IsKind ( SyntaxKind . CommaToken ) && token . Parent != target )
235
- {
231
+ if ( afterComma && token . Parent != target )
236
232
return default ;
237
- }
238
233
239
234
var typeSyntax = typeSyntaxGetter ( target ) ;
240
235
if ( typeSyntax == null )
241
- {
242
236
return default ;
243
- }
244
237
245
- if ( ! token . IsKind ( SyntaxKind . CommaToken ) && token != typeSyntax . GetLastToken ( ) )
246
- {
238
+ if ( ! afterComma && token != typeSyntax . GetLastToken ( ) )
247
239
return default ;
248
- }
249
240
250
241
var modifiers = modifierGetter ( target ) ;
251
242
if ( modifiers == null )
252
- {
253
243
return default ;
254
- }
255
244
256
- return new NameDeclarationInfo (
257
- possibleDeclarationComputer ( GetDeclarationModifiers ( modifiers . Value ) ) ,
258
- GetAccessibility ( modifiers . Value ) ,
259
- GetDeclarationModifiers ( modifiers . Value ) ,
260
- semanticModel . GetTypeInfo ( typeSyntax , cancellationToken ) . Type ,
261
- semanticModel . GetAliasInfo ( typeSyntax , cancellationToken ) ) ;
245
+ return ComputeInfo (
246
+ semanticModel , possibleDeclarationComputer , typeSyntax , modifiers . Value , tryInferAsync : ! afterComma , cancellationToken ) ;
262
247
}
263
248
264
249
private static NameDeclarationInfo IsLastTokenOfType < TSyntaxNode > (
@@ -295,11 +280,36 @@ private static NameDeclarationInfo IsLastTokenOfType<TSyntaxNode>(
295
280
296
281
var modifiers = modifierGetter ( target ) ;
297
282
283
+ return ComputeInfo ( semanticModel , possibleDeclarationComputer , typeSyntax , modifiers , tryInferAsync : true , cancellationToken ) ;
284
+ }
285
+
286
+ private static NameDeclarationInfo ComputeInfo (
287
+ SemanticModel semanticModel ,
288
+ Func < DeclarationModifiers , ImmutableArray < SymbolKindOrTypeKind > > possibleDeclarationComputer ,
289
+ SyntaxNode typeSyntax ,
290
+ SyntaxTokenList modifiers ,
291
+ bool tryInferAsync ,
292
+ CancellationToken cancellationToken )
293
+ {
294
+ var declarationModifiers = GetDeclarationModifiers ( modifiers ) ;
295
+ var possibleDeclarations = possibleDeclarationComputer ( declarationModifiers ) ;
296
+
297
+ // Treat a declaration as async if if it is explicitly marked as 'async' or if it is a method that returns a
298
+ // Task-like type. The latter ensures that even if the user didn't have an explicit 'async' modifier, we still
299
+ // name the member in an appropriate fashion (e.g. `GetCustomerAsync` for `public Task<Customer> $$`).
300
+ var type = semanticModel . GetTypeInfo ( typeSyntax , cancellationToken ) . Type ;
301
+ if ( ! declarationModifiers . IsAsync && tryInferAsync )
302
+ {
303
+ var knownTaskTypes = new KnownTaskTypes ( semanticModel . Compilation ) ;
304
+ if ( knownTaskTypes . IsTaskLike ( type ) )
305
+ declarationModifiers = declarationModifiers . WithAsync ( true ) ;
306
+ }
307
+
298
308
return new NameDeclarationInfo (
299
- possibleDeclarationComputer ( GetDeclarationModifiers ( modifiers ) ) ,
309
+ possibleDeclarations ,
300
310
GetAccessibility ( modifiers ) ,
301
- GetDeclarationModifiers ( modifiers ) ,
302
- semanticModel . GetTypeInfo ( typeSyntax , cancellationToken ) . Type ,
311
+ declarationModifiers ,
312
+ type ,
303
313
semanticModel . GetAliasInfo ( typeSyntax , cancellationToken ) ) ;
304
314
}
305
315
0 commit comments