-
Notifications
You must be signed in to change notification settings - Fork 420
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
Format range #1043
Changes from 7 commits
ba6e969
4e16b20
41ffcc7
e5383ee
40098ee
18955cf
35117b1
c54de26
a204624
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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() | ||
{ | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra blanks