Skip to content

Commit

Permalink
Feedback and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Sep 24, 2020
1 parent b296c4f commit a61047f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Text;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
Expand Down Expand Up @@ -108,19 +107,35 @@ public int GetCSharpIndentation(FormattingContext context, int projectedDocument
// Get the line number at the position after the marker
var line = changedText.Lines.GetLinePosition(projectedDocumentIndex + marker.Length).Line;

var result = _getIndentationMethod.Invoke(
_indentationService,
new object[] { changedDocument, line, CodeAnalysis.Formatting.FormattingOptions.IndentStyle.Smart, cancellationToken });
try
{
var result = _getIndentationMethod.Invoke(
_indentationService,
new object[] { changedDocument, line, CodeAnalysis.Formatting.FormattingOptions.IndentStyle.Smart, cancellationToken });

var baseProperty = result.GetType().GetProperty("BasePosition");
var basePosition = (int)baseProperty.GetValue(result);
var offsetProperty = result.GetType().GetProperty("Offset");
var offset = (int)offsetProperty.GetValue(result);

var baseProperty = result.GetType().GetProperty("BasePosition");
var basePosition = (int)baseProperty.GetValue(result);
var offsetProperty = result.GetType().GetProperty("Offset");
var offset = (int)offsetProperty.GetValue(result);
var resultLine = changedText.Lines.GetLinePosition(basePosition);
var indentation = resultLine.Character + offset;

var resultLine = changedText.Lines.GetLinePosition(basePosition);
var indentation = resultLine.Character + offset;
// IIndentationService always returns offset as the number of spaces.
// So if the client uses tabs instead of spaces, we need to convert accordingly.
if (!context.Options.InsertSpaces)
{
indentation /= (int)context.Options.TabSize;
}

return indentation;
return indentation;
}
catch (Exception ex)
{
throw new InvalidOperationException(
"Error occured when reflection invoking Roslyn's IIndentationService. Roslyn may have changed in an unexpected way.",
ex);
}
}

private TextEdit[] MapEditsToHostDocument(RazorCodeDocument codeDocument, TextEdit[] csharpEdits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ private List<TextChange> AdjustIndentation(FormattingContext context, Cancellati
var sourceMappingIndentations = new SortedDictionary<int, int>();
foreach (var mapping in context.CodeDocument.GetCSharpDocument().SourceMappings)
{
var mappingSpan = new TextSpan(mapping.OriginalSpan.AbsoluteIndex, mapping.OriginalSpan.Length);
var mappingRange = mappingSpan.AsRange(context.SourceText);
if (!ShouldFormat(context, mappingRange.Start))
{
// We don't care about this range as this can potentially lead to incorrect scopes.
continue;
}

var startIndentation = CSharpFormatter.GetCSharpIndentation(context, mapping.GeneratedSpan.AbsoluteIndex, cancellationToken);
sourceMappingIndentations[mapping.OriginalSpan.AbsoluteIndex] = startIndentation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Document CSharpWorkspaceDocument
var adhocWorkspace = new AdhocWorkspace();
var csharpOptions = adhocWorkspace.Options
.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.TabSize, LanguageNames.CSharp, (int)Options.TabSize)
.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.IndentationSize, LanguageNames.CSharp, (int)Options.TabSize)
.WithChangedOption(CodeAnalysis.Formatting.FormattingOptions.UseTabs, LanguageNames.CSharp, !Options.InsertSpaces);
adhocWorkspace.TryApplyChanges(adhocWorkspace.CurrentSolution.WithOptions(csharpOptions));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public async Task FormatsMixedRazorBlock()
{
await RunFormattingTestAsync(
input: @"|@page ""/test""
<div class=@className>Some Text</div>
@{
<p>
@if (true) {
Expand All @@ -158,6 +161,9 @@ await RunFormattingTestAsync(
}
|",
expected: @"@page ""/test""
<div class=@className>Some Text</div>
@{
<p>
@if (true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
~~~~~ ~~~~~~~

<div class=~~~~~~~~~~>Some Text</div>

~~
<p>
~~~ ~~~~~~ ~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
~~~~~ ~~~~~~~

<div class=~~~~~~~~~~>Some Text</div>

~~
<p>
~~~ ~~~~~~ ~
Expand Down

0 comments on commit a61047f

Please sign in to comment.