Skip to content

Commit

Permalink
Merge pull request #1043 from akshita31/format_range
Browse files Browse the repository at this point in the history
Format range
  • Loading branch information
david-driscoll authored Dec 11, 2017
2 parents d687e44 + a204624 commit c563075
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 1 deletion.
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);
}

[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);
}
}
}

0 comments on commit c563075

Please sign in to comment.