@@ -33,7 +33,8 @@ internal static class UnsafeModifierCodeFixHelpers
3333 internal static async Task RegisterAddUnsafeCodeFixAsync (
3434 CodeFixContext context ,
3535 LocalizableString codeFixTitle ,
36- Func < SyntaxNode , bool > isSupportedDeclaration )
36+ Func < SyntaxNode , bool > isSupportedDeclaration ,
37+ SyntaxKind insertAfterModifier = SyntaxKind . None )
3738 {
3839 var root = await context . Document . GetSyntaxRootAsync ( context . CancellationToken ) . ConfigureAwait ( false ) ;
3940 if ( root is null )
@@ -68,7 +69,12 @@ internal static async Task RegisterAddUnsafeCodeFixAsync(
6869 context . RegisterCodeFix (
6970 CodeAction . Create (
7071 displayTitle ,
71- cancellationToken => AddModifierAsync ( context . Document , declaration , modifier , cancellationToken ) ,
72+ cancellationToken => AddModifierAsync (
73+ context . Document ,
74+ declaration ,
75+ modifier ,
76+ cancellationToken ,
77+ insertAfterModifier ) ,
7278 title ) ,
7379 context . Diagnostics [ 0 ] ) ;
7480 }
@@ -102,10 +108,11 @@ internal static async Task<Document> AddModifierAsync(
102108 Document document ,
103109 SyntaxNode declaration ,
104110 SyntaxKind modifier ,
105- CancellationToken cancellationToken )
111+ CancellationToken cancellationToken ,
112+ SyntaxKind insertAfterModifier = SyntaxKind . None )
106113 {
107114 var editor = await DocumentEditor . CreateAsync ( document , cancellationToken ) . ConfigureAwait ( false ) ;
108- editor . ReplaceNode ( declaration , AddModifier ( declaration , modifier ) ) ;
115+ editor . ReplaceNode ( declaration , AddModifier ( declaration , modifier , insertAfterModifier ) ) ;
109116 return editor . GetChangedDocument ( ) ;
110117 }
111118
@@ -138,14 +145,17 @@ internal static async Task<Document> ReplaceUnsafeWithSafeAsync(
138145 /// <summary>
139146 /// Returns <paramref name="declaration"/> with a safety modifier added.
140147 /// </summary>
141- internal static SyntaxNode AddModifier ( SyntaxNode declaration , SyntaxKind modifier )
148+ internal static SyntaxNode AddModifier (
149+ SyntaxNode declaration ,
150+ SyntaxKind modifier ,
151+ SyntaxKind insertAfterModifier = SyntaxKind . None )
142152 {
143153 if ( declaration is AccessorDeclarationSyntax accessor )
144154 return AddModifier ( accessor , modifier ) ;
145155
146156 SyntaxTokenList modifiers = UnsafeMigrationSyntaxHelpers . GetModifiers ( declaration ) ;
147157 if ( modifiers . Count > 0 )
148- return WithModifiers ( declaration , AddModifier ( modifiers , modifier ) ) ;
158+ return WithModifiers ( declaration , AddModifier ( modifiers , modifier , insertAfterModifier ) ) ;
149159
150160 // With no existing modifiers the declaration's leading trivia belongs to the token the new modifier
151161 // displaces, so it has to move with it. Attribute lists keep their own leading trivia.
@@ -206,7 +216,7 @@ private static AccessorDeclarationSyntax AddModifier(AccessorDeclarationSyntax a
206216 {
207217 // SyntaxGenerator does not yet model unsafe property accessors, so edit their tokens directly.
208218 if ( accessor . Modifiers . Count > 0 )
209- return accessor . WithModifiers ( AddModifier ( accessor . Modifiers , modifier ) ) ;
219+ return accessor . WithModifiers ( AddModifier ( accessor . Modifiers , modifier , SyntaxKind . None ) ) ;
210220
211221 SyntaxToken modifierToken = SyntaxFactory . Token ( modifier )
212222 . WithLeadingTrivia ( accessor . Keyword . LeadingTrivia )
@@ -243,12 +253,26 @@ private static AccessorDeclarationSyntax RemoveUnsafeModifier(AccessorDeclaratio
243253 return accessor . WithModifiers ( modifiers ) ;
244254 }
245255
246- private static SyntaxTokenList AddModifier ( SyntaxTokenList modifiers , SyntaxKind modifier )
256+ private static SyntaxTokenList AddModifier (
257+ SyntaxTokenList modifiers ,
258+ SyntaxKind modifier ,
259+ SyntaxKind insertAfterModifier )
247260 {
248- // 'extern' and 'partial' conventionally sit closest to the return type, so the safety modifier goes
249- // before them. Otherwise it is appended, which matches the repo's preferred modifier order where
250- // 'unsafe' follows 'virtual', 'abstract', 'sealed' and 'override'.
251- int insertionIndex = GetFirstModifierIndex ( modifiers , SyntaxKind . ExternKeyword , SyntaxKind . PartialKeyword ) ;
261+ int insertionIndex = insertAfterModifier == SyntaxKind . None
262+ ? - 1
263+ : modifiers . IndexOf ( insertAfterModifier ) ;
264+ if ( insertionIndex >= 0 )
265+ {
266+ insertionIndex ++ ;
267+ }
268+ else
269+ {
270+ // 'extern' and 'partial' conventionally sit closest to the return type, so the safety modifier goes
271+ // before them. Otherwise it is appended, which matches the repo's preferred modifier order where
272+ // 'unsafe' follows 'virtual', 'abstract', 'sealed' and 'override'.
273+ insertionIndex = GetFirstModifierIndex ( modifiers , SyntaxKind . ExternKeyword , SyntaxKind . PartialKeyword ) ;
274+ }
275+
252276 if ( insertionIndex < 0 )
253277 insertionIndex = modifiers . Count ;
254278
0 commit comments