dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.87k stars 676 forks source link

[BUG] C# Base Language Support - Expand selection (alt + shift + right arrow) of methods and other members with body does not work correctly #7067

Open dibarbet opened 6 months ago

dibarbet commented 6 months ago

Describe the Issue

Expand selection (alt + shift + right arrow) does not work right when the cursor is currently on the method name. Pressing (alt + shift + right arrow) will expand the selection to the entire method signature (correct). Pressing it further will expand the selection to other same-level members of the type instead of expanding to the method body first.

Steps To Reproduce

  1. Using Windows 11 and VS Code Ensure that "C# Base language support" and "C# DevKit" are installed and enabled.
  2. In VS Code open any C# class with methods and other members.
  3. In VS Code editor put the cursor on any method name and expand the selection by pressing alt + shift + arrow right. Repeat pressing this key until the entire method signature is selected (varies depending on the method name's casing). image
  4. Press alt + shift + arrow right one more time.
  5. Selection is expanded thru the entire class (incorrect). image

This also happens on methods with multiple params. When current cursor is in any of the parameters pressing alt + shift + right arrow expands selection to ALL method params instead of the current selected parameter first (i.e. current param type and param name should be selected first before selecting all params). This is the expected behavior on any other language extension (test this on python) or even in any modern IDE including Visual Studio.

Expected Behavior

Selection should expand to the method body first before other class members. image

Similarly, selection should expand to the currently selected method param's type and name before expanding to other params. image

Environment Information

-OS: Windows 11 -VS Code version 1.88, installed with C# (v2.23.15) and C# Dev Kit (1.4.29) extensions from MS.

alcjamgit commented 2 months ago

@dibarbet any update on this bug? Can you point me to editor the source implementation, so I can make suggestions or perhaps open a PR myself?

dibarbet commented 2 months ago

@alcjamgit I'm not 100% sure which extension component is driving this feature in VSCode. I suspect that the Roslyn server needs to implement the LSP textDocument/selectionRange request. We do not implement it today, so it is most likely using some VSCode default behavior or textmate.

We're happy to take contributions - the implementation would go in a new handler in here - https://github.com/dotnet/roslyn/tree/main/src/LanguageServer/Protocol/Handler and set the corresponding capability here https://github.com/dotnet/roslyn/blob/4619ed7bd37e93cccc0fbe8c0832929f309a1eeb/src/LanguageServer/Protocol/DefaultCapabilitiesProvider.cs#L46

We should already have the type definitions for the protocol message, e.g. https://github.com/dotnet/roslyn/blob/4619ed7bd37e93cccc0fbe8c0832929f309a1eeb/src/LanguageServer/Protocol/Protocol/SelectionRange.cs#L16

In Visual Studio, I believe the behavior of expand selection is controlled by this code, which might be a helpful reference (I haven't dug deep into it).