public void WriterConstructedWithoutContentLengthAndSourceFile_AddsLinePragmas_OnDispose() { // Arrange var location = new SourceLocation(10, 1, 20); var expected = string.Join(Environment.NewLine, @"#line 2 ""myfile""", "Hello world", "", "#line default", "#line hidden", ""); var expectedMappings = new LineMapping( new MappingLocation(location, 30), new MappingLocation(new SourceLocation(18, 1, 0), 11)); var writer = new CSharpCodeWriter(); // Act using (var mappingWriter = new CSharpLineMappingWriter(writer, location, "myfile")) { writer.Write("Hello world"); } // Assert Assert.Equal(expected, writer.GenerateCode()); Assert.Empty(writer.LineMappingManager.Mappings); }
protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer) { // Grab the last model chunk so it gets intellisense. var modelChunk = ChunkHelper.GetModelChunk(Context.ChunkTreeBuilder.ChunkTree); Model = modelChunk != null ? modelChunk.ModelType : _defaultModel; // If there were any model chunks then we need to modify the class declaration signature. if (modelChunk != null) { writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName)); var modelVisitor = new ModelChunkVisitor(writer, Context); // This generates the base class signature modelVisitor.Accept(modelChunk); writer.WriteLine(); return new CSharpCodeWritingScope(writer); } else { return base.BuildClassDeclaration(writer); } }
/// <inheritdoc /> /// <remarks>If the attribute being rendered is of the type /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>. /// </remarks> public override void RenderAttributeValue( TagHelperAttributeDescriptor attributeDescriptor, CSharpCodeWriter writer, CodeGeneratorContext codeGeneratorContext, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { if (attributeDescriptor == null) { throw new ArgumentNullException(nameof(attributeDescriptor)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (codeGeneratorContext == null) { throw new ArgumentNullException(nameof(codeGeneratorContext)); } if (renderAttributeValue == null) { throw new ArgumentNullException(nameof(renderAttributeValue)); } if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal)) { writer .WriteStartMethodInvocation(_context.CreateModelExpressionMethodName) .Write(ModelLambdaVariableName) .Write(" => "); if (!complexValue) { writer .Write(ModelLambdaVariableName) .Write("."); } renderAttributeValue(writer); writer.WriteEndMethodInvocation(endLine: false); } else { base.RenderAttributeValue( attributeDescriptor, writer, codeGeneratorContext, renderAttributeValue, complexValue); } }
private void RenderBufferedAttributeValueAccessor(CSharpCodeWriter writer) { if (_designTimeMode) { // There is no value buffer in design time mode but we still want to write out a value. We write a // value to ensure the tag helper's property type is string. writer.Write("string.Empty"); } else { writer.WriteInstanceMethodInvocation(StringValueBufferVariableName, "ToString", endLine: false); } }
private void RenderBufferedAttributeValueAccessor(CSharpCodeWriter writer) { if (_designTimeMode) { // There is no value buffer in design time mode but we still want to write out a value. We write a // value to ensure the tag helper's property type is string. writer.Write("string.Empty"); } else { writer.WriteInstanceMethodInvocation( StringValueBufferVariableName, _tagHelperContext.TagHelperContentGetContentMethodName, endLine: false, parameters: new string[] { _tagHelperContext.HtmlEncoderPropertyName }); } }
public void WriterConstructedWithContentLength_AddsLineMappings_OnDispose() { // Arrange var location = new SourceLocation(10, 15, 20); var expected = new LineMapping( new MappingLocation(location, 30), new MappingLocation(new SourceLocation(0, 0, 0), 11)); var writer = new CSharpCodeWriter(); // Act using (var mappingWriter = new CSharpLineMappingWriter(writer, location, 30)) { writer.Write("Hello world"); } // Assert Assert.Equal("Hello world", writer.GenerateCode()); var mapping = Assert.Single(writer.LineMappingManager.Mappings); Assert.Equal(expected, mapping); }
public override void RenderAttributeValue( TagHelperAttributeDescriptor attributeInfo, CSharpCodeWriter writer, CodeGeneratorContext context, Action<CSharpCodeWriter> renderAttributeValue, bool complexValue) { writer.Write("**From custom attribute code renderer**: "); base.RenderAttributeValue(attributeInfo, writer, context, renderAttributeValue, complexValue); }