Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer generate constructor in more places #7682

Open
DanteMarshal opened this issue Oct 22, 2024 · 4 comments
Open

Offer generate constructor in more places #7682

DanteMarshal opened this issue Oct 22, 2024 · 4 comments

Comments

@DanteMarshal
Copy link

DanteMarshal commented Oct 22, 2024

Type: Bug

Issue Description

There used to be a quick action on structs and classes to automatically generate a constructor injecting all defined fields. I don't recall exactly since which version, but I can't find that option anymore since the past month or so.

Steps to Reproduce

Open quick actions menu on a class or a struct with defined fields and no constructor.

Expected Behavior

A Generate Constructor 'ClassName( field1, field2, ... )' should be available.

Actual Behavior

On classes, only Generate Constructor ClassName() is available, and on structs no option to generate any kind of constructor is available.

Logs

Nothing out of ordinary happens, no errors or warnings are reported. It's as if this feature was totally removed.

Environment information

VSCode version: 1.90.2
C# Extension: 2.50.27
Using OmniSharp: false

Dotnet Information .NET SDK: Version: 8.0.403 Commit: c64aa40a71 Workload version: 8.0.400-manifests.e99c892e MSBuild version: 17.11.9+a69bbaaf5

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.403\

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
Version: 8.0.10
Architecture: x64
Commit: 81cabf2857

.NET SDKs installed:
8.0.403 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.35 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Visual Studio Code Extensions
Extension Author Version Folder Name
csharp ms-dotnettools 2.50.27 ms-dotnettools.csharp-2.50.27-win32-x64
git-graph mhutchie 1.30.0 mhutchie.git-graph-1.30.0
material-product-icons PKief 1.7.1 pkief.material-product-icons-1.7.1
nugetpackagemanagergui aliasadidev 2.1.1 aliasadidev.nugetpackagemanagergui-2.1.1
remote-explorer ms-vscode 0.4.3 ms-vscode.remote-explorer-0.4.3
remote-ssh ms-vscode-remote 0.113.1 ms-vscode-remote.remote-ssh-0.113.1
remote-ssh-edit ms-vscode-remote 0.87.0 ms-vscode-remote.remote-ssh-edit-0.87.0
vsc-material-theme-icons equinusocio 3.8.8 equinusocio.vsc-material-theme-icons-3.8.8
vscode-dotnet-runtime ms-dotnettools 2.2.0 ms-dotnettools.vscode-dotnet-runtime-2.2.0
vscode-open sandcastle 0.3.0 sandcastle.vscode-open-0.3.0
vscode-todo-plus fabiospampinato 4.19.1 fabiospampinato.vscode-todo-plus-4.19.1
vscord LeonardSSH 5.2.13 leonardssh.vscord-5.2.13

Extension version: 2.50.27
VS Code version: Code 1.90.2 (5437499feb04f7a586f677b155b039bc2b3669eb, 2024-06-18T22:34:26.404Z)
OS version: Windows_NT x64 10.0.22631
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz (8 x 1992)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) undefined
Memory (System) 31.88GB (22.09GB free)
Process Argv --crash-reporter-id 4994e761-2f9a-48c0-9071-98dd85d0b400
Screen Reader no
VM 0%
@dibarbet
Copy link
Member

@DanteMarshal I'm able to see the refactorings for generating a constructor. For example by selecting the fields:
Image

The selection can be a bit finicky. Putting your cursor on a field variable should show an action to generate a ctor with that field. Selecting multiple fields should show an option to generate a ctor with all.

@DanteMarshal
Copy link
Author

I see, so I guess this is more of a UX issue than a bug.
I remember having the option to do that when my cursor was on the class name, I think that brings convenience and we need to bring that feature back. But as a user, let's see how current UX goes :

My first hunch after reading your reply was to try multi-cursor, so I did that first but it doesn't work with multiple selections. Only the main selection is used :
Image

Then I tried selecting mutiple fields (with a single selection) and it worked :
Image

But there's a lot of issues with that.
First of all, the fields are not always close to each other. This same code I'm screenshotting now is a good example.
Let's say I want to build a constructor for all fields in this class :
Image

I can't use multi-cursor to select only fields (and not properties), so I have to select them like this :
Image

Which gives me an option to generate a constructor for all selected stuff, including properties (not just fields). But since those are simply backing backing fields for the properties, the generated constructor will include many dublicate parameters (including properties that I didn't want to include). It will look like this :
Image

My suggestion is to keep this feature, but also bring back the old option on constructor which is very useful in cases like mine (I used that option a lot); And also add multi-cursor support for this new per-field generation feature.

p.s. I'm not a contributor, but if there's anything I can help with to do this faster, let me know.

@dibarbet dibarbet changed the title Option to generate constructor with fields no longer found Offer generate constructor in more places Oct 26, 2024
@dibarbet
Copy link
Member

dibarbet commented Oct 26, 2024

I'm not 100% sure we ever offered to generate all on the class definition (definitely possible, but I can't remember off the top of my head). However I agree that it currently isn't very ergonomic to use. I've updated the title of this issue - we'll use it to track improvements in how and when the code action is offered.

Unfortunately I'm not sure if all of these will be extremely easy to offer. For example, LSP doesn't seem to provide multiple selection ranges to server at all - so that may require base LSP updates (which tend to take a while).

@DanteMarshal
Copy link
Author

I just played with the options more and I realized, it wasn't an older version of C# extension that provided the option on class definition, it was Omnisharp. When I switched to useOmnisharp = true I got this good old menu that I was used to work with :
Image
that generates it like this :
Image

I used to work with omnisharp way after the big update that turned it off by default, so when I stopped using it after a few months I must've thought that it was an update to the extension that removed the option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants