From c9a8563c36acc6dfba6b28e1491d1267593c0732 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 21 Jan 2020 16:25:39 +0100 Subject: [PATCH 1/4] organize imports support in formatting --- .../Workers/Formatting/FormattingWorker.cs | 16 ++++++++++++++++ .../Options/FormattingOptions.cs | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs b/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs index 6e7e5c4193..5178d6d9e8 100644 --- a/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs +++ b/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs @@ -89,6 +89,11 @@ public static async Task> GetFormattingC : document.Project.Solution.Workspace.Options; var newDocument = await Formatter.FormatAsync(document, TextSpan.FromBounds(start, end), optionSet); + if (omnisharpOptions.FormattingOptions.OrganizeImports) + { + newDocument = await Formatter.OrganizeImportsAsync(document); + } + return await TextChanges.GetAsync(newDocument, document); } @@ -98,6 +103,12 @@ public static async Task GetFormattedText(Document document, OmniSharpOp ? await document.Project.Solution.Workspace.Options.WithEditorConfigOptions(document.FilePath, loggerFactory) : document.Project.Solution.Workspace.Options; var newDocument = await Formatter.FormatAsync(document, optionSet); + + if (omnisharpOptions.FormattingOptions.OrganizeImports) + { + newDocument = await Formatter.OrganizeImportsAsync(document); + } + var text = await newDocument.GetTextAsync(); return text.ToString(); @@ -110,6 +121,11 @@ public static async Task> GetFormattedTe : document.Project.Solution.Workspace.Options; var newDocument = await Formatter.FormatAsync(document, optionSet); + if (omnisharpOptions.FormattingOptions.OrganizeImports) + { + newDocument = await Formatter.OrganizeImportsAsync(document); + } + return await TextChanges.GetAsync(newDocument, document); } } diff --git a/src/OmniSharp.Shared/Options/FormattingOptions.cs b/src/OmniSharp.Shared/Options/FormattingOptions.cs index 98690912a0..c95ccd0f32 100644 --- a/src/OmniSharp.Shared/Options/FormattingOptions.cs +++ b/src/OmniSharp.Shared/Options/FormattingOptions.cs @@ -59,6 +59,8 @@ public FormattingOptions() NewLineForClausesInQuery = true; } + public bool OrganizeImports { get; set; } + public bool EnableEditorConfigSupport { get; set; } public string NewLine { get; set; } From 0c6113749f3a0188ffcd7ea4c1936ee9c14ad8a7 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 21 Jan 2020 16:26:01 +0100 Subject: [PATCH 2/4] tests for OrganizesImports in formatting --- .../FormattingFacts.cs | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs index 9729048f7f..90e65ad7f6 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs @@ -254,6 +254,76 @@ public async Task FormatRespectsIndentationSize() } } + [Fact] + public async Task OrganizesImports() + { + var testFile = new TestFile("dummy.cs", @" +using System.IO; +using Dummy; +using System; + +namespace Bar +{ + class Foo {} +}"); + + using (var host = CreateOmniSharpHost(new[] { testFile }, configurationData: new Dictionary + { + ["FormattingOptions:OrganizeImports"] = "true" + })) + { + var requestHandler = host.GetRequestHandler(OmniSharpEndpoints.CodeFormat); + + var request = new CodeFormatRequest { FileName = testFile.FileName }; + var response = await requestHandler.Handle(request); + + Assert.Equal(@" +using System; +using System.IO; +using Dummy; + +namespace Bar +{ + class Foo {} +}", response.Buffer); + } + } + + [Fact] + public async Task DoersntOrganizeImportsWhenDisabled() + { + var testFile = new TestFile("dummy.cs", @" +using System.IO; +using Dummy; +using System; + +namespace Bar +{ + class Foo {} +}"); + + using (var host = CreateOmniSharpHost(new[] { testFile }, configurationData: new Dictionary + { + ["FormattingOptions:OrganizeImports"] = "false" + })) + { + var requestHandler = host.GetRequestHandler(OmniSharpEndpoints.CodeFormat); + + var request = new CodeFormatRequest { FileName = testFile.FileName }; + var response = await requestHandler.Handle(request); + + Assert.Equal(@" +using System.IO; +using Dummy; +using System; + +namespace Bar +{ + class Foo {} +}", response.Buffer); + } + } + private static void AssertFormatTargetKind(SyntaxKind kind, string input) { var content = TestContent.Parse(input); From 419a1bc9483423430836d73a0e636549248c39d1 Mon Sep 17 00:00:00 2001 From: filipw Date: Tue, 21 Jan 2020 16:28:06 +0100 Subject: [PATCH 3/4] fixed typo and test --- tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs b/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs index 90e65ad7f6..017927f2f8 100644 --- a/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs +++ b/tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs @@ -264,7 +264,7 @@ public async Task OrganizesImports() namespace Bar { - class Foo {} + class Foo { } }"); using (var host = CreateOmniSharpHost(new[] { testFile }, configurationData: new Dictionary @@ -284,13 +284,13 @@ class Foo {} namespace Bar { - class Foo {} + class Foo { } }", response.Buffer); } } [Fact] - public async Task DoersntOrganizeImportsWhenDisabled() + public async Task DoesntOrganizeImportsWhenDisabled() { var testFile = new TestFile("dummy.cs", @" using System.IO; @@ -299,7 +299,7 @@ public async Task DoersntOrganizeImportsWhenDisabled() namespace Bar { - class Foo {} + class Foo { } }"); using (var host = CreateOmniSharpHost(new[] { testFile }, configurationData: new Dictionary @@ -319,7 +319,7 @@ class Foo {} namespace Bar { - class Foo {} + class Foo { } }", response.Buffer); } } From 5887c8e5534638733c49ada9fe107c6f5aceaac6 Mon Sep 17 00:00:00 2001 From: filipw Date: Wed, 22 Jan 2020 09:56:53 +0100 Subject: [PATCH 4/4] reduced code duplication --- .../Workers/Formatting/FormattingWorker.cs | 33 ++++++------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs b/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs index 5178d6d9e8..2cbec07d22 100644 --- a/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs +++ b/src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs @@ -84,49 +84,36 @@ public static SyntaxNode FindFormatTarget(SyntaxNode root, int position) public static async Task> GetFormattingChanges(Document document, int start, int end, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory) { - var optionSet = omnisharpOptions.FormattingOptions.EnableEditorConfigSupport - ? await document.Project.Solution.Workspace.Options.WithEditorConfigOptions(document.FilePath, loggerFactory) - : document.Project.Solution.Workspace.Options; - var newDocument = await Formatter.FormatAsync(document, TextSpan.FromBounds(start, end), optionSet); - - if (omnisharpOptions.FormattingOptions.OrganizeImports) - { - newDocument = await Formatter.OrganizeImportsAsync(document); - } - + var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory, TextSpan.FromBounds(start, end)); return await TextChanges.GetAsync(newDocument, document); } public static async Task GetFormattedText(Document document, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory) { - var optionSet = omnisharpOptions.FormattingOptions.EnableEditorConfigSupport - ? await document.Project.Solution.Workspace.Options.WithEditorConfigOptions(document.FilePath, loggerFactory) - : document.Project.Solution.Workspace.Options; - var newDocument = await Formatter.FormatAsync(document, optionSet); - - if (omnisharpOptions.FormattingOptions.OrganizeImports) - { - newDocument = await Formatter.OrganizeImportsAsync(document); - } - + var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory); var text = await newDocument.GetTextAsync(); - return text.ToString(); } public static async Task> GetFormattedTextChanges(Document document, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory) + { + var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory); + return await TextChanges.GetAsync(newDocument, document); + } + + private static async Task FormatDocument(Document document, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory, TextSpan? textSpan = null) { var optionSet = omnisharpOptions.FormattingOptions.EnableEditorConfigSupport ? await document.Project.Solution.Workspace.Options.WithEditorConfigOptions(document.FilePath, loggerFactory) : document.Project.Solution.Workspace.Options; - var newDocument = await Formatter.FormatAsync(document, optionSet); + var newDocument = textSpan != null ? await Formatter.FormatAsync(document, textSpan.Value, optionSet) : await Formatter.FormatAsync(document, optionSet); if (omnisharpOptions.FormattingOptions.OrganizeImports) { newDocument = await Formatter.OrganizeImportsAsync(document); } - return await TextChanges.GetAsync(newDocument, document); + return newDocument; } } }