@@ -166,9 +166,9 @@ public static Task<Document> RefactorAsync(
166
166
if ( CanRefactor ( member ) )
167
167
{
168
168
AnalyzerConfigOptions configOptions = document . GetConfigOptions ( selectedMembers . Parent . SyntaxTree ) ;
169
- bool wrapLineBeforeArrowToken = DiagnosticRules . PutExpressionBodyOnItsOwnLine . IsEffective ( member . SyntaxTree , document . Project . CompilationOptions ) ;
169
+ NewLinePosition newLinePosition = GetNewLinePosition ( document , member , configOptions , cancellationToken ) ;
170
170
171
- var newMember = ( MemberDeclarationSyntax ) Refactor ( member , configOptions , wrapLineBeforeArrowToken ) ;
171
+ var newMember = ( MemberDeclarationSyntax ) Refactor ( member , configOptions , newLinePosition ) ;
172
172
173
173
return newMember
174
174
. WithTrailingTrivia ( member . GetTrailingTrivia ( ) )
@@ -181,20 +181,43 @@ public static Task<Document> RefactorAsync(
181
181
return document . ReplaceMembersAsync ( SyntaxInfo . MemberDeclarationListInfo ( selectedMembers . Parent ) , newMembers , cancellationToken ) ;
182
182
}
183
183
184
- public static Task < Document > RefactorAsync (
184
+ public static async Task < Document > RefactorAsync (
185
185
Document document ,
186
186
SyntaxNode node ,
187
187
CancellationToken cancellationToken = default )
188
188
{
189
189
AnalyzerConfigOptions configOptions = document . GetConfigOptions ( node . SyntaxTree ) ;
190
- bool wrapLineBeforeArrowToken = DiagnosticRules . PutExpressionBodyOnItsOwnLine . IsEffective ( node . SyntaxTree , document . Project . CompilationOptions , cancellationToken ) ;
190
+ NewLinePosition newLinePosition = GetNewLinePosition ( document , node , configOptions , cancellationToken ) ;
191
+
192
+ SyntaxNode newNode = Refactor ( node , configOptions , newLinePosition ) . WithFormatterAnnotation ( ) ;
191
193
192
- SyntaxNode newNode = Refactor ( node , configOptions , wrapLineBeforeArrowToken ) . WithFormatterAnnotation ( ) ;
194
+ if ( newLinePosition == NewLinePosition . After )
195
+ {
196
+ var arrowToken = CSharpUtility . GetExpressionBody ( newNode ) . ArrowToken ;
197
+ var annotation = new SyntaxAnnotation ( ) ;
198
+ newNode = newNode . ReplaceToken ( arrowToken , arrowToken . WithAdditionalAnnotations ( annotation ) ) ;
199
+ document = await document . ReplaceNodeAsync ( node , newNode , cancellationToken ) . ConfigureAwait ( false ) ;
200
+ SyntaxNode root = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
201
+ arrowToken = root . GetAnnotatedTokens ( annotation ) . Single ( ) ;
202
+ var textChange = new TextChange ( TextSpan . FromBounds ( arrowToken . GetPreviousToken ( ) . Span . End , arrowToken . SpanStart ) , " " ) ;
203
+ return await document . WithTextChangeAsync ( textChange , cancellationToken ) . ConfigureAwait ( false ) ;
204
+ }
193
205
194
- return document . ReplaceNodeAsync ( node , newNode , cancellationToken ) ;
206
+ return await document . ReplaceNodeAsync ( node , newNode , cancellationToken ) . ConfigureAwait ( false ) ;
195
207
}
196
208
197
- private static SyntaxNode Refactor ( SyntaxNode node , AnalyzerConfigOptions configOptions , bool wrapLineBeforeArrowToken )
209
+ private static NewLinePosition GetNewLinePosition ( Document document , SyntaxNode node , AnalyzerConfigOptions configOptions , CancellationToken cancellationToken )
210
+ {
211
+ if ( DiagnosticRules . PutExpressionBodyOnItsOwnLine . IsEffective ( node . SyntaxTree , document . Project . CompilationOptions , cancellationToken )
212
+ && ConvertExpressionBodyAnalysis . AllowPutExpressionBodyOnItsOwnLine ( node . Kind ( ) ) )
213
+ {
214
+ return configOptions . GetArrowTokenNewLinePosition ( ) ;
215
+ }
216
+
217
+ return NewLinePosition . None ;
218
+ }
219
+
220
+ private static SyntaxNode Refactor ( SyntaxNode node , AnalyzerConfigOptions configOptions , NewLinePosition newLinePosition )
198
221
{
199
222
switch ( node . Kind ( ) )
200
223
{
@@ -204,7 +227,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
204
227
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( methodDeclaration . Body ) ;
205
228
206
229
return methodDeclaration
207
- . WithExpressionBody ( CreateExpressionBody ( analysis , methodDeclaration , configOptions , wrapLineBeforeArrowToken ) )
230
+ . WithExpressionBody ( CreateExpressionBody ( analysis , methodDeclaration , configOptions , newLinePosition ) )
208
231
. WithSemicolonToken ( CreateSemicolonToken ( methodDeclaration . Body , analysis ) )
209
232
. WithBody ( null ) ;
210
233
}
@@ -214,7 +237,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
214
237
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( constructorDeclaration . Body ) ;
215
238
216
239
return constructorDeclaration
217
- . WithExpressionBody ( CreateExpressionBody ( analysis , constructorDeclaration , configOptions , wrapLineBeforeArrowToken ) )
240
+ . WithExpressionBody ( CreateExpressionBody ( analysis , constructorDeclaration , configOptions , newLinePosition ) )
218
241
. WithSemicolonToken ( CreateSemicolonToken ( constructorDeclaration . Body , analysis ) )
219
242
. WithBody ( null ) ;
220
243
}
@@ -224,7 +247,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
224
247
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( destructorDeclaration . Body ) ;
225
248
226
249
return destructorDeclaration
227
- . WithExpressionBody ( CreateExpressionBody ( analysis , destructorDeclaration , configOptions , wrapLineBeforeArrowToken ) )
250
+ . WithExpressionBody ( CreateExpressionBody ( analysis , destructorDeclaration , configOptions , newLinePosition ) )
228
251
. WithSemicolonToken ( CreateSemicolonToken ( destructorDeclaration . Body , analysis ) )
229
252
. WithBody ( null ) ;
230
253
}
@@ -234,7 +257,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
234
257
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( localFunction . Body ) ;
235
258
236
259
return localFunction
237
- . WithExpressionBody ( CreateExpressionBody ( analysis , localFunction , configOptions , wrapLineBeforeArrowToken ) )
260
+ . WithExpressionBody ( CreateExpressionBody ( analysis , localFunction , configOptions , newLinePosition ) )
238
261
. WithSemicolonToken ( CreateSemicolonToken ( localFunction . Body , analysis ) )
239
262
. WithBody ( null ) ;
240
263
}
@@ -244,7 +267,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
244
267
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( operatorDeclaration . Body ) ;
245
268
246
269
return operatorDeclaration
247
- . WithExpressionBody ( CreateExpressionBody ( analysis , operatorDeclaration , configOptions , wrapLineBeforeArrowToken ) )
270
+ . WithExpressionBody ( CreateExpressionBody ( analysis , operatorDeclaration , configOptions , newLinePosition ) )
248
271
. WithSemicolonToken ( CreateSemicolonToken ( operatorDeclaration . Body , analysis ) )
249
272
. WithBody ( null ) ;
250
273
}
@@ -254,7 +277,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
254
277
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( operatorDeclaration . Body ) ;
255
278
256
279
return operatorDeclaration
257
- . WithExpressionBody ( CreateExpressionBody ( analysis , operatorDeclaration , configOptions , wrapLineBeforeArrowToken ) )
280
+ . WithExpressionBody ( CreateExpressionBody ( analysis , operatorDeclaration , configOptions , newLinePosition ) )
258
281
. WithSemicolonToken ( CreateSemicolonToken ( operatorDeclaration . Body , analysis ) )
259
282
. WithBody ( null ) ;
260
283
}
@@ -264,7 +287,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
264
287
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( propertyDeclaration . AccessorList ) ;
265
288
266
289
return propertyDeclaration
267
- . WithExpressionBody ( CreateExpressionBody ( analysis , propertyDeclaration , configOptions , wrapLineBeforeArrowToken ) )
290
+ . WithExpressionBody ( CreateExpressionBody ( analysis , propertyDeclaration , configOptions , newLinePosition ) )
268
291
. WithSemicolonToken ( CreateSemicolonToken ( analysis . Block , analysis ) )
269
292
. WithAccessorList ( null ) ;
270
293
}
@@ -274,7 +297,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
274
297
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( indexerDeclaration . AccessorList ) ;
275
298
276
299
return indexerDeclaration
277
- . WithExpressionBody ( CreateExpressionBody ( analysis , indexerDeclaration , configOptions , wrapLineBeforeArrowToken ) )
300
+ . WithExpressionBody ( CreateExpressionBody ( analysis , indexerDeclaration , configOptions , newLinePosition ) )
278
301
. WithSemicolonToken ( CreateSemicolonToken ( analysis . Block , analysis ) )
279
302
. WithAccessorList ( null ) ;
280
303
}
@@ -288,7 +311,7 @@ private static SyntaxNode Refactor(SyntaxNode node, AnalyzerConfigOptions config
288
311
BlockExpressionAnalysis analysis = BlockExpressionAnalysis . Create ( accessor ) ;
289
312
290
313
return accessor
291
- . WithExpressionBody ( CreateExpressionBody ( analysis , accessor , configOptions , wrapLineBeforeArrowToken ) )
314
+ . WithExpressionBody ( CreateExpressionBody ( analysis , accessor , configOptions , newLinePosition ) )
292
315
. WithSemicolonToken ( CreateSemicolonToken ( analysis . Block , analysis ) )
293
316
. WithBody ( null ) ;
294
317
}
@@ -304,7 +327,7 @@ private static ArrowExpressionClauseSyntax CreateExpressionBody(
304
327
BlockExpressionAnalysis analysis ,
305
328
SyntaxNode declaration ,
306
329
AnalyzerConfigOptions configOptions ,
307
- bool wrapLineBeforeArrowToken )
330
+ NewLinePosition newLinePosition )
308
331
{
309
332
SyntaxToken arrowToken = Token ( SyntaxKind . EqualsGreaterThanToken ) ;
310
333
@@ -335,42 +358,21 @@ private static ArrowExpressionClauseSyntax CreateExpressionBody(
335
358
}
336
359
}
337
360
338
- expression = SyntaxTriviaAnalysis . SetIndentation ( expression , declaration , configOptions ) ;
339
-
340
- if ( wrapLineBeforeArrowToken )
341
- {
342
- NewLinePosition newLinePosition = GetArrowNewLinePosition ( declaration . Kind ( ) , configOptions ) ;
343
- return CreateArrayExpression ( arrowToken , expression , newLinePosition ) ;
344
- }
345
- else
346
- {
347
- return ArrowExpressionClause ( arrowToken , expression ) ;
348
- }
349
- }
350
-
351
- private static NewLinePosition GetArrowNewLinePosition ( SyntaxKind syntaxKind , AnalyzerConfigOptions configOptions )
352
- {
353
- if ( ConvertExpressionBodyAnalysis . AllowPutExpressionBodyOnItsOwnLine ( syntaxKind ) )
354
- {
355
- return configOptions . GetArrowTokenNewLinePosition ( ) ;
356
- }
357
- else
358
- {
359
- return NewLinePosition . None ;
360
- }
361
- }
362
-
363
- private static ArrowExpressionClauseSyntax CreateArrayExpression ( SyntaxToken arrowToken , ExpressionSyntax expression , NewLinePosition newLinePosition )
364
- {
365
361
switch ( newLinePosition )
366
362
{
367
363
case NewLinePosition . After :
368
364
arrowToken = arrowToken . AppendToTrailingTrivia ( CSharpFactory . NewLine ( ) ) ;
365
+ expression = SyntaxTriviaAnalysis . SetIndentation ( expression , declaration , configOptions ) ;
369
366
break ;
370
367
case NewLinePosition . Before :
371
- arrowToken = arrowToken . PrependToLeadingTrivia ( CSharpFactory . NewLine ( ) ) ;
368
+ SyntaxTrivia trivia = SyntaxTriviaAnalysis . GetIncreasedIndentationTrivia ( declaration , configOptions , CancellationToken . None ) ;
369
+ arrowToken = arrowToken . WithLeadingTrivia ( trivia ) ;
370
+ break ;
371
+ default :
372
+ expression = SyntaxTriviaAnalysis . SetIndentation ( expression , declaration , configOptions ) ;
372
373
break ;
373
374
}
375
+
374
376
return ArrowExpressionClause ( arrowToken , expression ) ;
375
377
}
376
378
0 commit comments