Format document does not support sorting using directives
From vscode-dotnettools created by PAHeartBeat: microsoft/vscode-dotnettools#369
Type: Bug
Auto Format and Sorting Using/import statement is not working properly when C# (OmniSharp) and C# dev kit is enabled.
I have mentioned below that the namespace is added to the file after saving it's not auto-sorting.
using System;
using UnityEngine;
using Amazon;
using Amazon.CognitoIdentity;
using Amazon.MobileAnalytics.MobileAnalyticsManager;
using System.Collections;
using System.Net;
using System.Net.Sockets;
My User level Code setting I have added below in a few snippet.
from A Plugin which is disabled now, as sorting and format are part of c# Extension.
"csharpfixformat.sort.usings.order": "System Microsoft Unity",
"csharpfixformat.sort.usings.splitGroups": true,
For OmniSharp.
"omnisharp.organizeImportsOnFormat": true,
"omnisharp.useEditorFormattingSettings": true,
"omnisharp.useModernNet": true,
Default format setting
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
},
"editor.defaultFormatter": "ms-dotnettools.csharp",
Editor Format setting
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
Extension version: 0.3.21 VS Code version: Code 1.81.0 (Universal) (6445d93c81ebe42c4cbd7a60712e0b17d9463e97, 2023-08-02T12:40:02.782Z) OS version: Darwin arm64 22.5.0 Modes:
System Info
| Item | Value |
|---|---|
| CPUs | Apple M1 (8 x 24) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled metal: disabled_off multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok video_decode: enabled video_encode: enabled vulkan: disabled_off webgl: enabled webgl2: enabled webgpu: enabled |
| Load (avg) | 2, 3, 3 |
| Memory (System) | 8.00GB (0.06GB free) |
| Screen Reader | no |
| VM | 0% |
A/B Experiments
vsliv368:30146709
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263:30335439
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
pythonvs932:30410667
vscaac:30438847
vsclangdc:30486549
c4g48928:30535728
dsvsc012:30540252
pynewext54:30695312
azure-dev_surveyone:30548225
vsccc:30803844
2e4cg342:30602488
f6dab269:30613381
a9j8j154:30646983
showlangstatbar:30737416
vsctsb:30748421
a2ce3375:30757347
pythonfmttext:30731395
pythoncmv:30756943
fixshowwlkth:30771522
showindicator:30805244
pythongtdpath:30769146
i26e3531:30792625
gsofb:30804716
pythonnosmt12:30797651
pythonidxptcf:30805731
pythonnoceb:30805159
e537b577:30795824
dsvsc013:30795093
dsvsc014:30804076
Duped another from CDK where editor.formatOnType is not working.
... after saving it's not auto-sorting.
This would be the expected behavior for Format Document. This command only adjusts whitespace. There is a separate Sort Usings command internally which handles the sorting of using directives.
Could be they are expecting the omnisharp setting omnisharp.organizeImportsOnFormat": true to work with the Roslyn LSP.
Omnisharp has always handled the sort of usings. Why is this removed with the new extension?
@JoeRobich @dibarbet I'm looking at the FormattingOptions structure in LSP, and there is an extension mechanism defined for "further properties". Do we have a way to include omnisharp.organizeImportsOnFormat in this request?
There is a separate Sort Usings command internally which handles the sorting of using directives
That command doesn't exist in vscode. We could add it, but it would be a custom client side command to implement. It is definitely an option though.
@JoeRobich @dibarbet I'm looking at the FormattingOptions structure in LSP, and there is an extension mechanism defined for "further properties". Do we have a way to include
omnisharp.organizeImportsOnFormatin this request?
I think we can augment that - but there might be a better approach we can already use if we want to allow format document to sort usings.
- [ ] Change the option name to be more general (and not reference O#), e.g.
dotnet.formatting.organizeImportsor similar. For example I renamed the codelens options names here - https://github.com/dotnet/vscode-csharp/blob/main/package.json#L1198C1-L1199C1 - [ ] Add a migration from the O# option name to the dotnet option name - https://github.com/dotnet/vscode-csharp/blob/main/src/shared/migrateOptions.ts#L62
- [ ] Update this to read both the new and old option name - https://github.com/dotnet/vscode-csharp/blob/main/src/shared/options.ts#L165
- [x] Now these dotnet configurations will get automatically sent to the server when the option changes. On the server side we would introduce a global option for it - https://github.com/dotnet/roslyn/blob/main/src/Features/LanguageServer/Protocol/Handler/Configuration/DidChangeConfigurationNotificationHandler_OptionList.cs#L61 (this is how the name is mapped to the server - https://github.com/dotnet/vscode-csharp/blob/main/src/lsptoolshost/configurationMiddleware.ts#L11 ) (https://github.com/dotnet/roslyn/pull/70538)
- [x] In formatting, read the option value and sort imports (or not). (https://github.com/dotnet/roslyn/pull/70538)
Still waiting for this feature before C# Dev Kit is usable for me. CBA sorting usings myself and people comment on that stuff on my PRs where I work.
In formatting, read the option value and sort imports (or not).
I'm uncomfortable adding this to the core formatter (it is not a pure whitespace change), but we could perhaps add it to the LSP handler which invokes the formatter (run the sort operation after running the format operation).