Skip to content

Commit

Permalink
Feedback and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Sep 24, 2020
1 parent a61047f commit e059ff1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,19 @@ public CSharpFormatter(
_server = languageServer;
_filePathNormalizer = filePathNormalizer;

var type = typeof(CSharpFormattingOptions).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Indentation.CSharpIndentationService", throwOnError: true);
_indentationService = Activator.CreateInstance(type);
var indentationService = type.GetInterface("IIndentationService");
_getIndentationMethod = indentationService.GetMethod("GetIndentation");
try
{
var type = typeof(CSharpFormattingOptions).Assembly.GetType("Microsoft.CodeAnalysis.CSharp.Indentation.CSharpIndentationService", throwOnError: true);
_indentationService = Activator.CreateInstance(type);
var indentationService = type.GetInterface("IIndentationService");
_getIndentationMethod = indentationService.GetMethod("GetIndentation");
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Error occured when creating an instance of Roslyn's IIndentationService. Roslyn may have changed in an unexpected way.",
ex);
}
}

public async Task<TextEdit[]> FormatAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private List<TextChange> AdjustIndentation(FormattingContext context, Cancellati
// Couldn't remap. This is probably a non-C# location.
// Use SourceMapping indentations to locate the C# scope of this line.
// E.g,
//
//
// @if (true) {
// <div>
// |</div>
Expand All @@ -175,16 +175,16 @@ private List<TextChange> AdjustIndentation(FormattingContext context, Cancellati
// We can't find a direct mapping at |, but we can infer its base indentation from the
// indentation of the latest source mapping prior to this line.
// We use binary search to find that spot.

var index = Array.BinarySearch(sourceMappingIndentationScopes, lineStart);
if (index < 0)
{
// Couldn't find the exact value. Find the index of the element to the right of the searched value.
index = ~index;
// Couldn't find the exact value. Find the index of the element to the left of the searched value.
index = (~index) - 1;
}

// This will now be set to the same value as the end of the closest source mapping.
csharpDesiredIndentation = index == 0 ? 0 : sourceMappingIndentations[sourceMappingIndentationScopes[index - 1]];
csharpDesiredIndentation = index < 0 ? 0 : sourceMappingIndentations[sourceMappingIndentationScopes[index]];
}

// Now let's use that information to figure out the effective C# indentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public class HelloWorld
}
@functions{
public class Bar {}
}
|",
Expand Down Expand Up @@ -323,7 +323,7 @@ public class Foo { }
");
}

[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/25475")]
[Fact]
public async Task IndentsCodeBlockDirectiveStart()
{
await RunFormattingTestAsync(
Expand All @@ -340,7 +340,7 @@ public class Foo { }
");
}

[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/25475")]
[Fact]
public async Task IndentsCodeBlockDirectiveEnd()
{
await RunFormattingTestAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private RazorDocumentRangeFormattingResponse Format(RazorDocumentRangeFormatting
var workspace = new AdhocWorkspace();
var cSharpOptions = workspace.Options
.WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, (int)options.TabSize)
.WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, (int)options.TabSize)
.WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, !options.InsertSpaces);

var codeDocument = _documents[@params.HostDocumentFilePath];
Expand All @@ -99,8 +100,7 @@ private RazorDocumentRangeFormattingResponse Format(RazorDocumentRangeFormatting
response.Edits = Array.Empty<TextEdit>();

var codeDocument = _documents[@params.HostDocumentFilePath];
var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml;
var inputText = SourceText.From(generatedHtml);
var generatedHtml = codeDocument.GetHtmlDocument().GeneratedHtml.Replace("\r", "").Replace("\n", "\r\n");

// Get formatted baseline file
var baselineInputFileName = Path.ChangeExtension(_baselineFileName, ".input.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected async Task RunFormattingTestAsync(string input, string expected, int t
#if GENERATE_BASELINES
Assert.False(true, "GENERATE_BASELINES is set to true.");
#else
Assert.Equal(expected, actual);
Assert.Equal(expected, actual, ignoreLineEndingDifferences: true);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

~~~~~~~~~~ ~
~~~~~~ ~~~~~ ~~~~~
~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
~~~~~~~~~~ ~
~~~~~~ ~~~~~ ~~~~~
~
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Hello World
~~~~~~~~~~ ~~~~~~~ ~~~~~ ~~~~~
~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hello World
~~~~~~~~~~ ~~~~~~~ ~~~~~ ~~~~~
~

0 comments on commit e059ff1

Please sign in to comment.