@@ -249,59 +249,58 @@ private AsyncCompletionData.CommitResult Commit(
249
249
return new AsyncCompletionData . CommitResult ( isHandled : true , AsyncCompletionData . CommitBehavior . None ) ;
250
250
}
251
251
252
- ITextSnapshot updatedCurrentSnapshot ;
253
252
using ( var edit = subjectBuffer . CreateEdit ( EditOptions . DefaultMinimalChange , reiteratedVersionNumber : null , editTag : null ) )
254
253
{
255
254
edit . Replace ( mappedSpan . Span , change . TextChange . NewText ) ;
256
255
257
256
// edit.Apply() may trigger changes made by extensions.
258
257
// updatedCurrentSnapshot will contain changes made by Roslyn but not by other extensions.
259
- updatedCurrentSnapshot = edit . Apply ( ) ;
260
- }
258
+ var updatedCurrentSnapshot = edit . Apply ( ) ;
261
259
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 ) )
260
+ if ( change . NewPosition . HasValue )
280
261
{
281
- view . TryMoveCaretToAndEnsureVisible ( new SnapshotPoint ( subjectBuffer . CurrentSnapshot , mappedSpan . Start . Position + textChange . NewText ? . Length ?? 0 ) ) ;
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 ) ) ;
282
265
}
283
266
else
284
267
{
285
- view . Caret . EnsureVisible ( ) ;
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
+ }
286
285
}
287
- }
288
286
289
- includesCommitCharacter = change . IncludesCommitCharacter ;
290
-
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 > ( ) ;
287
+ includesCommitCharacter = change . IncludesCommitCharacter ;
297
288
298
- if ( currentDocument != null && formattingService != null )
289
+ if ( roslynItem . Rules . FormatOnCommit )
299
290
{
300
- var spanToFormat = triggerSnapshotSpan . TranslateTo ( subjectBuffer . CurrentSnapshot , SpanTrackingMode . EdgeInclusive ) ;
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 > ( ) ;
301
295
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 ) ;
296
+ if ( currentDocument != null && formattingService != null )
297
+ {
298
+ var spanToFormat = triggerSnapshotSpan . TranslateTo ( subjectBuffer . CurrentSnapshot , SpanTrackingMode . EdgeInclusive ) ;
299
+
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
+ }
305
304
}
306
305
}
307
306
0 commit comments