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

Add support for enhanced line pragmas #33954

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 2 additions & 4 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@
<MicrosoftNETCoreAppRuntimeVersion>$(MicrosoftNETCoreAppRuntimewinx64Version)</MicrosoftNETCoreAppRuntimeVersion>
</PropertyGroup>
<PropertyGroup Label="Manual">
<!-- Packages from dotnet/roslyn -->
<MicrosoftNetCompilersToolsetVersion>3.10.0-1.final</MicrosoftNetCompilersToolsetVersion>
<!-- DiagnosticAdapter package pinned temporarily until migrated/deprecated -->
<MicrosoftExtensionsDiagnosticAdapterVersion>5.0.0-preview.4.20180.4</MicrosoftExtensionsDiagnosticAdapterVersion>
<!-- Build tool dependencies -->
Expand Down Expand Up @@ -190,8 +188,8 @@
<MicrosoftBuildUtilitiesCoreVersion>16.9.0</MicrosoftBuildUtilitiesCoreVersion>
<MicrosoftBuildLocatorVersion>1.2.6</MicrosoftBuildLocatorVersion>
<MicrosoftBuildUtilitiesCoreVersion>16.9.0</MicrosoftBuildUtilitiesCoreVersion>
<MicrosoftCodeAnalysisCommonVersion>3.8.0</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisCSharpVersion>3.8.0</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCommonVersion>4.0.0-2.21354.7</MicrosoftCodeAnalysisCommonVersion>
<MicrosoftCodeAnalysisCSharpVersion>4.0.0-2.21354.7</MicrosoftCodeAnalysisCSharpVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>3.8.0</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
<MicrosoftCodeAnalysisPublicApiAnalyzersVersion>3.3.0</MicrosoftCodeAnalysisPublicApiAnalyzersVersion>
<MicrosoftCssParserVersion>1.0.0-20200708.1</MicrosoftCssParserVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Renderin
#nullable disable
);
__builder.AddMarkupContent(2, "\r\n Hello world\r\n ");
__builder.AddContent(3,
#nullable restore
#line 4 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml"
string.Format("{0}", "Hello")
#line (4,6)-(4,35) 24 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/BasicComponent.cshtml"
__builder.AddContent(3, string.Format("{0}", "Hello"));

#line default
#line hidden
#nullable disable
);
__builder.CloseElement();
}
#pragma warning restore 1998
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ public static CodeWriter WriteUsing(this CodeWriter writer, string name, bool en
return writer;
}

public static CodeWriter WriteEnhancedLineNumberDirective(this CodeWriter writer, SourceSpan span, int characterOffset)
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
// All values here need to be offset by 1 since #line uses a 1-indexed numbering system.
var lineNumberAsString = (span.LineIndex + 1).ToString(CultureInfo.InvariantCulture);
var characterStartAsString = (span.CharacterIndex + 1).ToString(CultureInfo.InvariantCulture);
var lineEndAsString = (span.LineIndex + 1 + span.LineCount).ToString(CultureInfo.InvariantCulture);
var characterEndAsString = (span.EndCharacterIndex + 1).ToString(CultureInfo.InvariantCulture);
var characterOffsetAsString = characterOffset.ToString(CultureInfo.InvariantCulture);
return writer.Write("#line (")
.Write(lineNumberAsString)
.Write(",")
.Write(characterStartAsString)
.Write(")-(")
.Write(lineEndAsString)
.Write(",")
.Write(characterEndAsString)
.Write(") ")
.Write(characterOffsetAsString)
.Write(" \"").Write(span.FilePath).WriteLine("\"");
}

public static CodeWriter WriteLineNumberDirective(this CodeWriter writer, SourceSpan span)
{
if (writer.Length >= writer.NewLine.Length && !IsAtBeginningOfLine(writer))
Expand Down Expand Up @@ -462,7 +483,18 @@ public static IDisposable BuildLinePragma(this CodeWriter writer, SourceSpan? sp
return NullDisposable.Default;
}

return new LinePragmaWriter(writer, span.Value, context);
return new LinePragmaWriter(writer, span.Value, context, 0, false);
}

public static IDisposable BuildEnhancedLinePragma(this CodeWriter writer, SourceSpan? span, CodeRenderingContext context, int characterOffset = 0)
{
if (string.IsNullOrEmpty(span?.FilePath))
{
// Can't build a valid line pragma without a file path.
return NullDisposable.Default;
}

return new LinePragmaWriter(writer, span.Value, context, characterOffset, useEnhancedLinePragma: true);
}

private static void WriteVerbatimStringLiteral(CodeWriter writer, string literal)
Expand Down Expand Up @@ -619,7 +651,9 @@ private class LinePragmaWriter : IDisposable
public LinePragmaWriter(
CodeWriter writer,
SourceSpan span,
CodeRenderingContext context)
CodeRenderingContext context,
int characterOffset,
bool useEnhancedLinePragma = false)
{
if (writer == null)
{
Expand All @@ -643,7 +677,15 @@ public LinePragmaWriter(
_writer.WriteLine("#nullable restore");
}

WriteLineNumberDirective(writer, span);
if (useEnhancedLinePragma && _context.Options.UseEnhancedLinePragma)
{
WriteEnhancedLineNumberDirective(writer, span, characterOffset);
}
else
{
WriteLineNumberDirective(writer, span);
}


// Capture the line index after writing the #line directive.
_startLineIndex = writer.Location.LineIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public readonly struct LinePragma : IEquatable<LinePragma>
{
public LinePragma(int startLineIndex, int lineCount, string filePath)
: this(startLineIndex: startLineIndex, lineCount: lineCount, filePath: filePath, startCharacterIndex: null, endCharacterIndex: null, characterOffset: null)
{
}

public LinePragma(int startLineIndex, int lineCount, string filePath, int? startCharacterIndex, int? endCharacterIndex, int? characterOffset)
{
if (startLineIndex < 0)
{
Expand All @@ -24,6 +29,9 @@ public LinePragma(int startLineIndex, int lineCount, string filePath)
StartLineIndex = startLineIndex;
LineCount = lineCount;
FilePath = filePath;
StartCharacterIndex = startCharacterIndex;
EndCharacterIndex = endCharacterIndex;
CharacterOffset = characterOffset;
}

public int StartLineIndex { get; }
Expand All @@ -32,6 +40,12 @@ public LinePragma(int startLineIndex, int lineCount, string filePath)

public int LineCount { get; }

public int? StartCharacterIndex { get; }

public int? EndCharacterIndex { get; }

public int? CharacterOffset { get; }

public string FilePath { get; }

public override bool Equals(object obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,33 @@ public override void WriteCSharpExpression(CodeRenderingContext context, CSharpE
throw new ArgumentNullException(nameof(node));
}

// Since we're not in the middle of writing an element, this must evaluate as some
// text to display
context.CodeWriter
.WriteStartMethodInvocation($"{_scopeStack.BuilderVarName}.{ComponentsApi.RenderTreeBuilder.AddContent}")
.Write((_sourceSequence++).ToString(CultureInfo.InvariantCulture))
.WriteParameterSeparator();
var sourceSequenceAsString = _sourceSequence.ToString(CultureInfo.InvariantCulture);
var methodInvocation = _scopeStack.BuilderVarName + '.' + ComponentsApi.RenderTreeBuilder.AddContent +'(' + sourceSequenceAsString;
_sourceSequence++;
var parameterSeparatorLength = 2;

for (var i = 0; i < node.Children.Count; i++)
using (context.CodeWriter.BuildEnhancedLinePragma(node.Source.Value, context, methodInvocation.Length + parameterSeparatorLength))
{
if (node.Children[i] is IntermediateToken token && token.IsCSharp)
{
WriteCSharpToken(context, token);
}
else
// Since we're not in the middle of writing an element, this must evaluate as some
// text to display
context.CodeWriter
.Write(methodInvocation)
.WriteParameterSeparator();

for (var i = 0; i < node.Children.Count; i++)
{
// There may be something else inside the expression like a Template or another extension node.
context.RenderNode(node.Children[i]);
if (node.Children[i] is IntermediateToken token && token.IsCSharp)
{
WriteCSharpToken(context, token, includeLinePragma: false);
}
else
{
// There may be something else inside the expression like a Template or another extension node.
context.RenderNode(node.Children[i]);
}
}
context.CodeWriter.WriteEndMethodInvocation();
}

context.CodeWriter.WriteEndMethodInvocation();
}

public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node)
Expand Down Expand Up @@ -1030,7 +1036,7 @@ private static void WriteAttributeValue(CodeRenderingContext context, IList<Inte
}
}

private static void WriteCSharpToken(CodeRenderingContext context, IntermediateToken token)
private static void WriteCSharpToken(CodeRenderingContext context, IntermediateToken token, bool includeLinePragma = true)
{
if (string.IsNullOrWhiteSpace(token.Content))
{
Expand All @@ -1043,11 +1049,17 @@ private static void WriteCSharpToken(CodeRenderingContext context, IntermediateT
return;
}

using (context.CodeWriter.BuildLinePragma(token.Source, context))
if (includeLinePragma)
{
context.CodeWriter.WritePadding(0, token.Source.Value, context);
context.CodeWriter.Write(token.Content);
using (context.CodeWriter.BuildLinePragma(token.Source, context))
{
context.CodeWriter.WritePadding(0, token.Source.Value, context);
context.CodeWriter.Write(token.Content);
}
return;
}

context.CodeWriter.Write(token.Content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public DefaultRazorCodeGenerationOptions(
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement,
bool omitMinimizedComponentAttributeValues)
bool omitMinimizedComponentAttributeValues,
bool useEnhancedLinePragma)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
Expand All @@ -25,6 +26,7 @@ public DefaultRazorCodeGenerationOptions(
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
UseEnhancedLinePragma = useEnhancedLinePragma;
}

public override bool DesignTime { get; }
Expand All @@ -40,5 +42,7 @@ public DefaultRazorCodeGenerationOptions(
public override bool SuppressNullabilityEnforcement { get; }

public override bool OmitMinimizedComponentAttributeValues { get; }

public override bool UseEnhancedLinePragma { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime)

public override bool OmitMinimizedComponentAttributeValues { get; set; }

public override bool UseEnhancedLinePragma { get; set; }

public override RazorCodeGenerationOptions Build()
{
return new DefaultRazorCodeGenerationOptions(
Expand All @@ -52,7 +54,8 @@ public override RazorCodeGenerationOptions Build()
SuppressMetadataAttributes,
SuppressPrimaryMethodBody,
SuppressNullabilityEnforcement,
OmitMinimizedComponentAttributeValues)
OmitMinimizedComponentAttributeValues,
UseEnhancedLinePragma)
{
SuppressMetadataSourceChecksumAttributes = SuppressMetadataSourceChecksumAttributes,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,9 @@ public override void VisitCSharpTemplateBlock(CSharpTemplateBlockSyntax node)
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -661,7 +663,9 @@ public override void VisitCSharpExplicitExpression(CSharpExplicitExpressionSynta
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -698,7 +702,9 @@ public override void VisitCSharpImplicitExpression(CSharpImplicitExpressionSynta
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -1105,7 +1111,9 @@ private void Combine(HtmlContentIntermediateNode node, SyntaxNode item)
node.Source.Value.AbsoluteIndex,
node.Source.Value.LineIndex,
node.Source.Value.CharacterIndex,
node.Source.Value.Length + item.FullWidth);
node.Source.Value.Length + item.FullWidth,
node.Source.Value.LineCount,
node.Source.Value.EndCharacterIndex);
}
}

Expand Down Expand Up @@ -1277,7 +1285,7 @@ public override void VisitMarkupMinimizedAttributeBlock(MarkupMinimizedAttribute
Source = BuildSourceSpanFromNode(node),
});
}

// Example
// <input checked="hello-world `@false`"/>
// Prefix= (space)
Expand Down Expand Up @@ -1446,7 +1454,9 @@ public override void VisitCSharpTemplateBlock(CSharpTemplateBlockSyntax node)
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -1505,7 +1515,9 @@ public override void VisitCSharpExplicitExpression(CSharpExplicitExpressionSynta
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -1558,7 +1570,9 @@ public override void VisitCSharpImplicitExpression(CSharpImplicitExpressionSynta
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}
}
Expand Down Expand Up @@ -2059,7 +2073,9 @@ private void Combine(HtmlContentIntermediateNode node, SyntaxNode item)
node.Source.Value.AbsoluteIndex,
node.Source.Value.LineIndex,
node.Source.Value.CharacterIndex,
node.Source.Value.Length + item.FullWidth);
node.Source.Value.Length + item.FullWidth,
node.Source.Value.LineCount,
node.Source.Value.EndCharacterIndex);
}
}

Expand Down Expand Up @@ -2163,7 +2179,9 @@ public override void VisitCSharpImplicitExpression(CSharpImplicitExpressionSynta
sourceRangeStart.Value.AbsoluteIndex,
sourceRangeStart.Value.LineIndex,
sourceRangeStart.Value.CharacterIndex,
contentLength);
contentLength,
sourceRangeStart.Value.LineCount,
sourceRangeStart.Value.EndCharacterIndex);
}
}

Expand Down
Loading