dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.06k stars 4.04k forks source link

OrganizeDocument: ArgumentException: The specified SnapshotPoint is on a different ITextSnapshot than this SnapshotPoint. #74594

Open KirillOsenkov opened 3 months ago

KirillOsenkov commented 3 months ago
  1. Have any C# project stored in Git with no pending changes
  2. Open any .cs file, add an unused using and any other line of code in the class below (needs to be two adds, one for using and one other)
  3. In Git Changes, right-click on the changed file, Compare with Unmodified
  4. Right-click the editor diff and select Remove and Sort usings

image

ArgumentException: The specified SnapshotPoint is on a different ITextSnapshot than this SnapshotPoint.

image

https://github.com/dotnet/roslyn/blob/52e29db514078c9f1bcf6d4c01a106d69f95d63b/src/EditorFeatures/Core/Organizing/OrganizeDocumentCommandHandler.cs#L128

This is because this code grabs the caretPoint from the subjectBuffer, which is C#, but then calls textView.GetTextElementSpan(caretPoint.Value) which expects a point in the top (projection) buffer.

It needs to also grab a caretPoint from the top buffer, and use that for the indicatorFactory, because that expects a span on the top (projection) buffer, not the subject buffer.

    Microsoft.VisualStudio.Platform.VSEditor    WpfTextView.ValidateBufferPosition Line 4829
    Microsoft.VisualStudio.Platform.VSEditor    WpfTextView.GetTextElementSpan Line 687
    Microsoft.CodeAnalysis.EditorFeatures   OrganizeDocumentCommandHandler.ExecuteAsync
    Microsoft.CodeAnalysis.EditorFeatures   OrganizeDocumentCommandHandler.ExecuteCommand
    Microsoft.CodeAnalysis.EditorFeatures   OrganizeDocumentCommandHandler.ExecuteCommand
    Microsoft.VisualStudio.Platform.VSEditor    EditorCommandHandlerService.ExecuteStartingAtIndex Line 218
    Microsoft.VisualStudio.Platform.VSEditor    GuardedOperations.CallExtensionPoint Line 549
    Microsoft.VisualStudio.Platform.VSEditor    EditorCommandHandlerService.ExecuteStartingAtIndex Line 232
    Microsoft.VisualStudio.Platform.VSEditor    EditorCommandHandlerService.Execute Line 154
    Microsoft.VisualStudio.Platform.VSEditor    EditorCommandHandlerService.ExecuteCommandHandlerChain Line 318
    Microsoft.VisualStudio.Platform.VSEditor    EditorCommandHandlerService.Execute Line 159
KirillOsenkov commented 3 months ago

@CyrusNajmabadi should be an easy fix

CyrusNajmabadi commented 3 months ago

@KirillOsenkov def feel free to contribute :D

KirillOsenkov commented 3 months ago

@CyrusNajmabadi here you go: https://github.com/dotnet/roslyn/pull/74645

KirillOsenkov commented 3 months ago

Oops, this was closed by accident. The PR is still outstanding.