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

Format range #1043

Merged
merged 9 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public async Task<FormatRangeResponse> Handle(FormatRangeRequest request)
var text = await document.GetTextAsync();
var start = text.Lines.GetPosition(new LinePosition(request.Line, request.Column));
var end = text.Lines.GetPosition(new LinePosition(request.EndLine, request.EndColumn));
var changes = await FormattingWorker.GetFormattingChanges(document, start, end);
var syntaxTree = await document.GetSyntaxRootAsync();
var tokenStart = syntaxTree.FindToken(start).FullSpan.Start;
var changes = await FormattingWorker.GetFormattingChanges(document, tokenStart, end);

return new FormatRangeResponse()
{
Expand Down
147 changes: 147 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/FormattingFacts.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Models;
using OmniSharp.Models.CodeFormat;
using OmniSharp.Models.Format;
Expand Down Expand Up @@ -128,6 +130,105 @@ await AssertTextChanges(string.Join("\r\n", source),
new LinePositionSpanTextChange { StartLine = 2, StartColumn = 30, EndLine = 3, EndColumn = 0, NewText = "\n " });
}

[Fact]
public async Task TextChangesOnStaringSpanBeforeFirstCharacterInLine()
{
var source =
@"class Program
{
public static void Main()
{
[| int foo = 1;|]
}
}";

var expected =
@"class Program
{
public static void Main()
{
int foo = 1;
}
}";

await AssertTextChanges(source, expected);
}

[Fact]
public async Task TextChangesOnStartingSpanAtFirstCharacterInLine()
{
var source =
@"class Program
{
public static void Main()
{
[|int foo = 1;|]
}
}";
var expected =
@"class Program
{
public static void Main()
{
int foo = 1;
}
}";

await AssertTextChanges(source, expected);
}

[Fact]
public async Task TextChangesOnStartingSpanAfterFirstCharacterInLine()
{
var source =
@"class Program
{
public static void Main()
{
i[|nt foo = 1;|]
}
}";

var expected =
@"class Program
{
public static void Main()
{
int foo = 1;
}
}";

await AssertTextChanges(source, expected);
}

[Fact]
public async Task TextChangesOnStartingSpanAfterFirstCharacterInLineWithMultipleLines()
{
var source =
@"class Program
{
public static void Main()
{
i[|nt foo = 1;
bool b = false;
Console.WriteLine(foo);|]
}
}";

var expected =
@"class Program
{
public static void Main()
{
int foo = 1;
bool b = false;
Console.WriteLine(foo);
}
}";

await AssertTextChanges(source, expected);
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blanks

[Fact]
public async Task FormatRespectsIndentationSize()
{
Expand Down Expand Up @@ -200,5 +301,51 @@ private async Task AssertTextChanges(string source, params LinePositionSpanTextC
}
}
}

private async Task AssertTextChanges(string source, string expected)
{
var testFile = new TestFile("dummy.cs", source);

using (var host = CreateOmniSharpHost(testFile))
{
var span = testFile.Content.GetSpans().Single();
var range = testFile.Content.GetRangeFromSpan(span);

var request = new FormatRangeRequest()
{
Buffer = testFile.Content.Code,
FileName = testFile.FileName,
Line = range.Start.Line,
Column = range.Start.Offset,
EndLine = range.End.Line,
EndColumn = range.End.Offset
};

var requestHandler = host.GetRequestHandler<FormatRangeService>(OmniSharpEndpoints.FormatRange);

var response = await requestHandler.Handle(request);
var actual = response.Changes.ToArray();

var oldText = testFile.Content.Text;
var textChanges = GetTextChanges(oldText, response.Changes);
var actualText = oldText.WithChanges(textChanges).ToString();
Assert.Equal(expected.Replace("\r\n","\n"), actualText.Replace("\r\n","\n"));
}
}

public static IEnumerable<TextChange> GetTextChanges(SourceText oldText, IEnumerable<LinePositionSpanTextChange> changes)
{
var textChanges = new List<TextChange>();
foreach (var change in changes)
{
var startPosition = new LinePosition(change.StartLine, change.StartColumn);
var endPosition = new LinePosition(change.EndLine, change.EndColumn);
var span = oldText.Lines.GetTextSpan(new LinePositionSpan(startPosition, endPosition));
var newText = change.NewText;
textChanges.Add(new TextChange(span, newText));
}

return textChanges.OrderBy(change => change.Span);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes returned by the service are arranged in descending order and the WithChanges function that we are using to get the new text for comparison requires the changes to be arranged in ascending order that is why this line has been added.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested that? I thought SourceText.WithChanges didn't care about the order.

Copy link

@rchande rchande Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
}