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

Add option to organize imports during document formatting. #4302

Merged
merged 2 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,11 @@
"default": false,
"description": "Enables support for showing unimported types and unimported extension methods in completion lists. When committed, the appropriate using directive will be added at the top of the current file. This option can have a negative impact on initial completion responsiveness, particularly for the first few completion sessions after opening a solution."
},
"omnisharp.organizeImportsOnFormat": {
"type": "boolean",
"default": false,
"description": "Specifies whether 'using' directives should be grouped and sorted during document formatting."
},
"razor.plugin.path": {
"type": [
"string",
Expand Down
3 changes: 2 additions & 1 deletion src/observers/OptionChangeObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const omniSharpOptions: ReadonlyArray<OptionsKey> = [
"loggingLevel",
"enableEditorConfigSupport",
"enableDecompilationSupport",
"enableImportCompletion"
"enableImportCompletion",
"organizeImportsOnFormat",
];

function OmniSharpOptionChangeObservable(optionObservable: Observable<Options>): Observable<Options> {
Expand Down
3 changes: 3 additions & 0 deletions src/omnisharp/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Options {
public maxProjectResults: number,
public useEditorFormattingSettings: boolean,
public useFormatting: boolean,
public organizeImportsOnFormat: boolean,
public showReferencesCodeLens: boolean,
public showTestsCodeLens: boolean,
public disableCodeActions: boolean,
Expand Down Expand Up @@ -74,6 +75,7 @@ export class Options {
const enableImportCompletion = omnisharpConfig.get<boolean>('enableImportCompletion', false);

const useFormatting = csharpConfig.get<boolean>('format.enable', true);
const organizeImportsOnFormat = omnisharpConfig.get<boolean>('organizeImportsOnFormat', false);

const showReferencesCodeLens = csharpConfig.get<boolean>('referencesCodeLens.enabled', true);
const showTestsCodeLens = csharpConfig.get<boolean>('testsCodeLens.enabled', true);
Expand Down Expand Up @@ -107,6 +109,7 @@ export class Options {
maxProjectResults,
useEditorFormattingSettings,
useFormatting,
organizeImportsOnFormat,
showReferencesCodeLens,
showTestsCodeLens,
disableCodeActions,
Expand Down
4 changes: 4 additions & 0 deletions src/omnisharp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ export class OmniSharpServer {
args.push('FormattingOptions:EnableEditorConfigSupport=true');
}

if (options.organizeImportsOnFormat === true) {
args.push('FormattingOptions:OrganizeImports=true');
}

if (this.decompilationAuthorized && options.enableDecompilationSupport === true) {
args.push('RoslynExtensionsOptions:EnableDecompilationSupport=true');
}
Expand Down
2 changes: 1 addition & 1 deletion test/unitTests/Fakes/FakeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
import { Options } from "../../../src/omnisharp/options";

export function getEmptyOptions(): Options {
return new Options("", "", false, "", false, 0, 0, false, false, false, false, false, false, 0, 0, false, false, false, false, false, false, false, undefined, "", "");
return new Options("", "", false, "", false, 0, 0, false, false, false, false, false, false, false, 0, 0, false, false, false, false, false, false, false, undefined, "", "");
}