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

Added support for organizing imports in formatting #1686

Merged
merged 4 commits into from
Jan 24, 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
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