@@ -249,58 +249,59 @@ private AsyncCompletionData.CommitResult Commit(
249
249
return new AsyncCompletionData . CommitResult ( isHandled : true , AsyncCompletionData . CommitBehavior . None ) ;
250
250
}
251
251
252
+ ITextSnapshot updatedCurrentSnapshot ;
252
253
using ( var edit = subjectBuffer . CreateEdit ( EditOptions . DefaultMinimalChange , reiteratedVersionNumber : null , editTag : null ) )
253
254
{
254
255
edit . Replace ( mappedSpan . Span , change . TextChange . NewText ) ;
255
256
256
257
// edit.Apply() may trigger changes made by extensions.
257
258
// updatedCurrentSnapshot will contain changes made by Roslyn but not by other extensions.
258
- var updatedCurrentSnapshot = edit . Apply ( ) ;
259
+ updatedCurrentSnapshot = edit . Apply ( ) ;
260
+ }
259
261
260
- if ( change . NewPosition . HasValue )
262
+ if ( change . NewPosition . HasValue )
263
+ {
264
+ // Roslyn knows how to position the caret in the snapshot we just created.
265
+ // If there were more edits made by extensions, TryMoveCaretToAndEnsureVisible maps the snapshot point to the most recent one.
266
+ view . TryMoveCaretToAndEnsureVisible ( new SnapshotPoint ( updatedCurrentSnapshot , change . NewPosition . Value ) ) ;
267
+ }
268
+ else
269
+ {
270
+ // Or, If we're doing a minimal change, then the edit that we make to the
271
+ // buffer may not make the total text change that places the caret where we
272
+ // would expect it to go based on the requested change. In this case,
273
+ // determine where the item should go and set the care manually.
274
+
275
+ // Note: we only want to move the caret if the caret would have been moved
276
+ // by the edit. i.e. if the caret was actually in the mapped span that
277
+ // we're replacing.
278
+ var caretPositionInBuffer = view . GetCaretPoint ( subjectBuffer ) ;
279
+ if ( caretPositionInBuffer . HasValue && mappedSpan . IntersectsWith ( caretPositionInBuffer . Value ) )
261
280
{
262
- // Roslyn knows how to position the caret in the snapshot we just created.
263
- // If there were more edits made by extensions, TryMoveCaretToAndEnsureVisible maps the snapshot point to the most recent one.
264
- view . TryMoveCaretToAndEnsureVisible ( new SnapshotPoint ( updatedCurrentSnapshot , change . NewPosition . Value ) ) ;
281
+ view . TryMoveCaretToAndEnsureVisible ( new SnapshotPoint ( subjectBuffer . CurrentSnapshot , mappedSpan . Start . Position + textChange . NewText ? . Length ?? 0 ) ) ;
265
282
}
266
283
else
267
284
{
268
- // Or, If we're doing a minimal change, then the edit that we make to the
269
- // buffer may not make the total text change that places the caret where we
270
- // would expect it to go based on the requested change. In this case,
271
- // determine where the item should go and set the care manually.
272
-
273
- // Note: we only want to move the caret if the caret would have been moved
274
- // by the edit. i.e. if the caret was actually in the mapped span that
275
- // we're replacing.
276
- var caretPositionInBuffer = view . GetCaretPoint ( subjectBuffer ) ;
277
- if ( caretPositionInBuffer . HasValue && mappedSpan . IntersectsWith ( caretPositionInBuffer . Value ) )
278
- {
279
- view . TryMoveCaretToAndEnsureVisible ( new SnapshotPoint ( subjectBuffer . CurrentSnapshot , mappedSpan . Start . Position + textChange . NewText ? . Length ?? 0 ) ) ;
280
- }
281
- else
282
- {
283
- view . Caret . EnsureVisible ( ) ;
284
- }
285
+ view . Caret . EnsureVisible ( ) ;
285
286
}
287
+ }
286
288
287
- includesCommitCharacter = change . IncludesCommitCharacter ;
289
+ includesCommitCharacter = change . IncludesCommitCharacter ;
288
290
289
- if ( roslynItem . Rules . FormatOnCommit )
290
- {
291
- // The edit updates the snapshot however other extensions may make changes there.
292
- // Therefore, it is required to use subjectBuffer.CurrentSnapshot for further calculations rather than the updated current snapshot defined above.
293
- var currentDocument = subjectBuffer . CurrentSnapshot . GetOpenDocumentInCurrentContextWithChanges ( ) ;
294
- var formattingService = currentDocument ? . GetRequiredLanguageService < IFormattingInteractionService > ( ) ;
291
+ if ( roslynItem . Rules . FormatOnCommit )
292
+ {
293
+ // The edit updates the snapshot however other extensions may make changes there.
294
+ // Therefore, it is required to use subjectBuffer.CurrentSnapshot for further calculations rather than the updated current snapshot defined above.
295
+ var currentDocument = subjectBuffer . CurrentSnapshot . GetOpenDocumentInCurrentContextWithChanges ( ) ;
296
+ var formattingService = currentDocument ? . GetRequiredLanguageService < IFormattingInteractionService > ( ) ;
295
297
296
- if ( currentDocument != null && formattingService != null )
297
- {
298
- var spanToFormat = triggerSnapshotSpan . TranslateTo ( subjectBuffer . CurrentSnapshot , SpanTrackingMode . EdgeInclusive ) ;
298
+ if ( currentDocument != null && formattingService != null )
299
+ {
300
+ var spanToFormat = triggerSnapshotSpan . TranslateTo ( subjectBuffer . CurrentSnapshot , SpanTrackingMode . EdgeInclusive ) ;
299
301
300
- // Note: C# always completes synchronously, TypeScript is async
301
- var changes = formattingService . GetFormattingChangesAsync ( currentDocument , subjectBuffer , spanToFormat . Span . ToTextSpan ( ) , cancellationToken ) . WaitAndGetResult ( cancellationToken ) ;
302
- currentDocument . Project . Solution . Workspace . ApplyTextChanges ( currentDocument . Id , changes , cancellationToken ) ;
303
- }
302
+ // Note: C# always completes synchronously, TypeScript is async
303
+ var changes = formattingService . GetFormattingChangesAsync ( currentDocument , subjectBuffer , spanToFormat . Span . ToTextSpan ( ) , cancellationToken ) . WaitAndGetResult ( cancellationToken ) ;
304
+ subjectBuffer . ApplyChanges ( changes ) ;
304
305
}
305
306
}
306
307
0 commit comments