Skip to content

Commit

Permalink
Merge pull request #1686 from OmniSharp/feature/organize-imports-format
Browse files Browse the repository at this point in the history
Added support for organizing imports in formatting
  • Loading branch information
filipw authored Jan 24, 2020
2 parents 210a28d + 5887c8e commit f19bb41
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/OmniSharp.Roslyn.CSharp/Workers/Formatting/FormattingWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,33 +84,36 @@ public static SyntaxNode FindFormatTarget(SyntaxNode root, int position)

public static async Task<IEnumerable<LinePositionSpanTextChange>> 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);

var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory, TextSpan.FromBounds(start, end));
return await TextChanges.GetAsync(newDocument, document);
}

public static async Task<string> 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);
var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory);
var text = await newDocument.GetTextAsync();

return text.ToString();
}

public static async Task<IEnumerable<LinePositionSpanTextChange>> GetFormattedTextChanges(Document document, OmniSharpOptions omnisharpOptions, ILoggerFactory loggerFactory)
{
var newDocument = await FormatDocument(document, omnisharpOptions, loggerFactory);
return await TextChanges.GetAsync(newDocument, document);
}

private static async Task<Document> 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);

return await TextChanges.GetAsync(newDocument, document);
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 newDocument;
}
}
}
2 changes: 2 additions & 0 deletions src/OmniSharp.Shared/Options/FormattingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public FormattingOptions()
NewLineForClausesInQuery = true;
}

public bool OrganizeImports { get; set; }

public bool EnableEditorConfigSupport { get; set; }

public string NewLine { get; set; }
Expand Down
70 changes: 70 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
{
["FormattingOptions:OrganizeImports"] = "true"
}))
{
var requestHandler = host.GetRequestHandler<CodeFormatService>(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 DoesntOrganizeImportsWhenDisabled()
{
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<string, string>
{
["FormattingOptions:OrganizeImports"] = "false"
}))
{
var requestHandler = host.GetRequestHandler<CodeFormatService>(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);
Expand Down

0 comments on commit f19bb41

Please sign in to comment.