Make CSharpRenderingContext and friends public

This commit is contained in:
Ajay Bhargav Baaskaran 2017-02-28 10:37:26 -08:00
parent 27e66c3750
commit d422e61c3e
10 changed files with 84 additions and 83 deletions

View File

@ -8,19 +8,19 @@ using Microsoft.AspNetCore.Razor.Evolution.Intermediate;
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
internal class CSharpRenderingContext public class CSharpRenderingContext
{ {
private CSharpRenderingConventions _renderingConventions; private CSharpRenderingConventions _renderingConventions;
public ICollection<DirectiveDescriptor> Directives { get; set; } internal ICollection<DirectiveDescriptor> Directives { get; set; }
public Func<string> IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N"); internal Func<string> IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N");
public List<LineMapping> LineMappings { get; } = new List<LineMapping>(); internal List<LineMapping> LineMappings { get; } = new List<LineMapping>();
public CSharpCodeWriter Writer { get; set; } public CSharpCodeWriter Writer { get; set; }
public CSharpRenderingConventions RenderingConventions internal CSharpRenderingConventions RenderingConventions
{ {
get get
{ {
@ -37,14 +37,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
} }
} }
public ErrorSink ErrorSink { get; } = new ErrorSink(); internal IList<RazorDiagnostic> Diagnostics { get; } = new List<RazorDiagnostic>();
public RazorSourceDocument SourceDocument { get; set; } internal RazorSourceDocument SourceDocument { get; set; }
public RazorParserOptions Options { get; set; } internal RazorParserOptions Options { get; set; }
public TagHelperRenderingContext TagHelperRenderingContext { get; set; } internal TagHelperRenderingContext TagHelperRenderingContext { get; set; }
public Action<RazorIRNode> RenderChildren { get; set; } internal Action<RazorIRNode> RenderChildren { get; set; }
} }
} }

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Razor.Evolution.Intermediate; using Microsoft.AspNetCore.Razor.Evolution.Intermediate;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
@ -23,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
if (node.Source != null) if (node.Source != null)
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
var padding = BuildOffsetPadding(RazorDesignTimeIRPass.DesignTimeVariable.Length, node.Source.Value, Context); var padding = BuildOffsetPadding(RazorDesignTimeIRPass.DesignTimeVariable.Length, node.Source.Value, Context);
@ -61,7 +62,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
if (node.Source.HasValue) if (node.Source.HasValue)
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
Context.Writer.WriteUsing(node.Content); Context.Writer.WriteUsing(node.Content);
} }
@ -76,7 +77,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
if (node.Source != null) if (node.Source != null)
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
var padding = BuildOffsetPadding(0, node.Source.Value, Context); var padding = BuildOffsetPadding(0, node.Source.Value, Context);
Context.Writer.Write(padding); Context.Writer.Write(padding);
@ -229,7 +230,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
var firstMappedChild = node.Children.FirstOrDefault(child => child.Source != null) as RazorIRNode; var firstMappedChild = node.Children.FirstOrDefault(child => child.Source != null) as RazorIRNode;
var valueStart = firstMappedChild?.Source; var valueStart = firstMappedChild?.Source;
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
var assignmentPrefixLength = propertyValueAccessor.Length + " = ".Length; var assignmentPrefixLength = propertyValueAccessor.Length + " = ".Length;
if (node.Descriptor.IsEnum && if (node.Descriptor.IsEnum &&
@ -330,18 +331,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
} }
else if (node is CSharpStatementIRNode) else if (node is CSharpStatementIRNode)
{ {
Context.ErrorSink.OnError( var error = new RazorError(
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes, LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes,
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
documentLocation.Length); documentLocation.Length);
Context.Diagnostics.Add(RazorDiagnostic.Create(error));
} }
else if (node is TemplateIRNode) else if (node is TemplateIRNode)
{ {
var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent; var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent;
Context.ErrorSink.OnError( var error = new RazorError(
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName), LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName),
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
documentLocation.Length); documentLocation.Length);
Context.Diagnostics.Add(RazorDiagnostic.Create(error));
} }
} }
} }

View File

@ -1,49 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{
internal class LinePragmaWriter : IDisposable
{
private readonly CSharpCodeWriter _writer;
private readonly int _startIndent;
public LinePragmaWriter(CSharpCodeWriter writer, SourceSpan documentLocation)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
_writer = writer;
_startIndent = _writer.CurrentIndent;
_writer.ResetIndent();
_writer.WriteLineNumberDirective(documentLocation, documentLocation.FilePath);
}
public void Dispose()
{
// Need to add an additional line at the end IF there wasn't one already written.
// This is needed to work with the C# editor's handling of #line ...
var builder = _writer.Builder;
var endsWithNewline = builder.Length > 0 && builder[builder.Length - 1] == '\n';
// Always write at least 1 empty line to potentially separate code from pragmas.
_writer.WriteLine();
// Check if the previous empty line wasn't enough to separate code from pragmas.
if (!endsWithNewline)
{
_writer.WriteLine();
}
_writer
.WriteLineDefaultDirective()
.WriteLineHiddenDirective()
.SetIndent(_startIndent);
}
}
}

View File

@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
IDisposable linePragmaScope = null; IDisposable linePragmaScope = null;
if (node.Source != null) if (node.Source != null)
{ {
linePragmaScope = new LinePragmaWriter(Context.Writer, node.Source.Value); linePragmaScope = Context.Writer.BuildLinePragma(node.Source.Value);
var padding = BuildOffsetPadding(Context.RenderingConventions.StartWriteMethod.Length, node.Source.Value, Context); var padding = BuildOffsetPadding(Context.RenderingConventions.StartWriteMethod.Length, node.Source.Value, Context);
Context.Writer.Write(padding); Context.Writer.Write(padding);
} }
@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
if (node.Source.HasValue) if (node.Source.HasValue)
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
Context.Writer.WriteUsing(node.Content); Context.Writer.WriteUsing(node.Content);
} }
@ -161,7 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
const string ValueWriterName = "__razor_attribute_value_writer"; const string ValueWriterName = "__razor_attribute_value_writer";
var expressionValue = node.Children.FirstOrDefault() as CSharpExpressionIRNode; var expressionValue = node.Children.FirstOrDefault() as CSharpExpressionIRNode;
var linePragma = expressionValue != null ? new LinePragmaWriter(Context.Writer, node.Source.Value) : null; var linePragma = expressionValue != null ? Context.Writer.BuildLinePragma(node.Source.Value) : null;
var prefixLocation = node.Source.Value.AbsoluteIndex; var prefixLocation = node.Source.Value.AbsoluteIndex;
var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length; var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length;
var valueLength = node.Source.Value.Length - node.Prefix.Length; var valueLength = node.Source.Value.Length - node.Prefix.Length;
@ -215,7 +215,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
if (node.Source != null) if (node.Source != null)
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
var padding = BuildOffsetPadding(0, node.Source.Value, Context); var padding = BuildOffsetPadding(0, node.Source.Value, Context);
Context.Writer Context.Writer
@ -455,7 +455,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
} }
else else
{ {
using (new LinePragmaWriter(Context.Writer, node.Source.Value)) using (Context.Writer.BuildLinePragma(node.Source.Value))
{ {
Context.Writer.WriteStartAssignment(propertyValueAccessor); Context.Writer.WriteStartAssignment(propertyValueAccessor);
@ -673,18 +673,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
} }
else if (node is CSharpStatementIRNode) else if (node is CSharpStatementIRNode)
{ {
Context.ErrorSink.OnError( var error = new RazorError(
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes, LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes,
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
documentLocation.Length); documentLocation.Length);
Context.Diagnostics.Add(RazorDiagnostic.Create(error));
} }
else if (node is TemplateIRNode) else if (node is TemplateIRNode)
{ {
var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent; var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent;
Context.ErrorSink.OnError( var error = new RazorError(
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName), LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName),
new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length),
documentLocation.Length); documentLocation.Length);
Context.Diagnostics.Add(RazorDiagnostic.Create(error));
} }
} }
} }

View File

@ -6,7 +6,7 @@ using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
{ {
internal class TagHelperRenderingContext public class TagHelperRenderingContext
{ {
private Dictionary<string, string> _renderedBoundAttributes; private Dictionary<string, string> _renderedBoundAttributes;
private HashSet<string> _verifiedPropertyDictionaries; private HashSet<string> _verifiedPropertyDictionaries;

View File

@ -63,9 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var diagnostics = new List<RazorDiagnostic>(); var diagnostics = new List<RazorDiagnostic>();
diagnostics.AddRange(syntaxTree.Diagnostics); diagnostics.AddRange(syntaxTree.Diagnostics);
diagnostics.AddRange(renderingContext.Diagnostics);
// Temporary code while we're still using legacy diagnostics in the SyntaxTree.
diagnostics.AddRange(renderingContext.ErrorSink.Errors.Select(error => RazorDiagnostic.Create(error)));
var csharpDocument = new RazorCSharpDocument() var csharpDocument = new RazorCSharpDocument()
{ {

View File

@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
{ {
public abstract class ExtensionIRNode : RazorIRNode public abstract class ExtensionIRNode : RazorIRNode
{ {
internal abstract void WriteNode(RuntimeTarget target, CSharpRenderingContext context); public abstract void WriteNode(RuntimeTarget target, CSharpRenderingContext context);
protected static void AcceptExtensionNode<TNode>(TNode node, RazorIRNodeVisitor visitor) protected static void AcceptExtensionNode<TNode>(TNode node, RazorIRNodeVisitor visitor)
where TNode : ExtensionIRNode where TNode : ExtensionIRNode

View File

@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
return AcceptExtensionNode<TemplateIRNode, TResult>(this, visitor); return AcceptExtensionNode<TemplateIRNode, TResult>(this, visitor);
} }
internal override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) public override void WriteNode(RuntimeTarget target, CSharpRenderingContext context)
{ {
var extension = target.GetExtension<ITemplateTargetExtension>(); var extension = target.GetExtension<ITemplateTargetExtension>();
extension.WriteTemplate(context, this); extension.WriteTemplate(context, this);

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Razor.Evolution.CodeGeneration;
namespace Microsoft.AspNetCore.Razor.Evolution.Legacy namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
{ {
@ -446,6 +447,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
return new CSharpCodeWritingScope(this); return new CSharpCodeWritingScope(this);
} }
public IDisposable BuildLinePragma(SourceSpan documentLocation)
{
return new LinePragmaWriter(this, documentLocation);
}
private void WriteVerbatimStringLiteral(string literal) private void WriteVerbatimStringLiteral(string literal)
{ {
Write("@\""); Write("@\"");
@ -525,5 +531,46 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
Write("\""); Write("\"");
} }
private class LinePragmaWriter : IDisposable
{
private readonly CSharpCodeWriter _writer;
private readonly int _startIndent;
public LinePragmaWriter(CSharpCodeWriter writer, SourceSpan documentLocation)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
_writer = writer;
_startIndent = _writer.CurrentIndent;
_writer.ResetIndent();
_writer.WriteLineNumberDirective(documentLocation, documentLocation.FilePath);
}
public void Dispose()
{
// Need to add an additional line at the end IF there wasn't one already written.
// This is needed to work with the C# editor's handling of #line ...
var builder = _writer.Builder;
var endsWithNewline = builder.Length > 0 && builder[builder.Length - 1] == '\n';
// Always write at least 1 empty line to potentially separate code from pragmas.
_writer.WriteLine();
// Check if the previous empty line wasn't enough to separate code from pragmas.
if (!endsWithNewline)
{
_writer.WriteLine();
}
_writer
.WriteLineDefaultDirective()
.WriteLineHiddenDirective()
.SetIndent(_startIndent);
}
}
} }
} }

View File

@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
return AcceptExtensionNode<TestExtensionIRNode, TResult>(this, visitor); return AcceptExtensionNode<TestExtensionIRNode, TResult>(this, visitor);
} }
internal override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) public override void WriteNode(RuntimeTarget target, CSharpRenderingContext context)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }