From dc91d943cf5337b691a5c6b5ddb9b550eececcde Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Thu, 17 Dec 2020 10:46:48 -0800 Subject: [PATCH] Add options to organize imports during document formatting. --- package.json | 5 +++++ src/observers/OptionChangeObserver.ts | 3 ++- src/omnisharp/options.ts | 3 +++ src/omnisharp/server.ts | 4 ++++ test/unitTests/Fakes/FakeOptions.ts | 2 +- 5 files changed, 15 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2349353b5..b1ac5087b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/observers/OptionChangeObserver.ts b/src/observers/OptionChangeObserver.ts index e3c445c98..2021ab609 100644 --- a/src/observers/OptionChangeObserver.ts +++ b/src/observers/OptionChangeObserver.ts @@ -20,7 +20,8 @@ const omniSharpOptions: ReadonlyArray = [ "loggingLevel", "enableEditorConfigSupport", "enableDecompilationSupport", - "enableImportCompletion" + "enableImportCompletion", + "organizeImportsOnFormat", ]; function OmniSharpOptionChangeObservable(optionObservable: Observable): Observable { diff --git a/src/omnisharp/options.ts b/src/omnisharp/options.ts index 2d856ed5c..da928a45c 100644 --- a/src/omnisharp/options.ts +++ b/src/omnisharp/options.ts @@ -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, @@ -74,6 +75,7 @@ export class Options { const enableImportCompletion = omnisharpConfig.get('enableImportCompletion', false); const useFormatting = csharpConfig.get('format.enable', true); + const organizeImportsOnFormat = omnisharpConfig.get('organizeImportsOnFormat', false); const showReferencesCodeLens = csharpConfig.get('referencesCodeLens.enabled', true); const showTestsCodeLens = csharpConfig.get('testsCodeLens.enabled', true); @@ -107,6 +109,7 @@ export class Options { maxProjectResults, useEditorFormattingSettings, useFormatting, + organizeImportsOnFormat, showReferencesCodeLens, showTestsCodeLens, disableCodeActions, diff --git a/src/omnisharp/server.ts b/src/omnisharp/server.ts index 9549189cd..b67774f10 100644 --- a/src/omnisharp/server.ts +++ b/src/omnisharp/server.ts @@ -354,6 +354,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'); } diff --git a/test/unitTests/Fakes/FakeOptions.ts b/test/unitTests/Fakes/FakeOptions.ts index 07885d295..72dd48d32 100644 --- a/test/unitTests/Fakes/FakeOptions.ts +++ b/test/unitTests/Fakes/FakeOptions.ts @@ -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, "", ""); }