public void Execute(RazorCodeDocument document) { var chunkTree = document.GetChunkTree(); var classInfo = document.GetClassName(); var chunkGeneratorContext = new ChunkGeneratorContext(_host, classInfo.Class, classInfo.Namespace, document.Source.Filename, shouldGenerateLinePragmas: true); chunkGeneratorContext.ChunkTreeBuilder = new AspNetCore.Razor.Chunks.ChunkTreeBuilder(); chunkGeneratorContext.ChunkTreeBuilder.Root.Association = chunkTree.Root.Association; chunkGeneratorContext.ChunkTreeBuilder.Root.Start = chunkTree.Root.Start; foreach (var chunk in chunkTree.Root.Children) { chunkGeneratorContext.ChunkTreeBuilder.Root.Children.Add(chunk); } var codeGeneratorContext = new CodeGeneratorContext(chunkGeneratorContext, document.ErrorSink); var codeGenerator = new PageCodeGenerator(codeGeneratorContext); var codeGeneratorResult = codeGenerator.Generate(); document.SetGeneratedCSharpDocument(new GeneratedCSharpDocument() { GeneratedCode = codeGeneratorResult.Code, }); }
public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context) { //var tagHelperBlock = target as TagHelperBlock; //Debug.Assert( // tagHelperBlock != null, // $"A {nameof(TagHelperChunkGenerator)} must only be used with {nameof(TagHelperBlock)}s."); //var attributes = new List<TagHelperAttributeTracker>(); //// We need to create a chunk generator to create chunks for each of the attributes. //var chunkGenerator = context.Host.CreateChunkGenerator( // context.ClassName, // context.RootNamespace, // context.SourceFile); //foreach (var attribute in tagHelperBlock.Attributes) //{ // ParentChunk attributeChunkValue = null; // if (attribute.Value != null) // { // // Populates the chunk tree with chunks associated with attributes // attribute.Value.Accept(chunkGenerator); // var chunks = chunkGenerator.Context.ChunkTreeBuilder.Root.Children; // var first = chunks.FirstOrDefault(); // attributeChunkValue = new ParentChunk // { // Association = first?.Association, // Children = chunks, // Start = first == null ? SourceLocation.Zero : first.Start // }; // } // var attributeChunk = new TagHelperAttributeTracker( // attribute.Name, // attributeChunkValue, // attribute.ValueStyle); // attributes.Add(attributeChunk); // // Reset the chunk tree builder so we can build a new one for the next attribute // chunkGenerator.Context.ChunkTreeBuilder = new ChunkTreeBuilder(); //} //var unprefixedTagName = tagHelperBlock.TagName.Substring(_tagHelperDescriptors.First().Prefix.Length); //context.ChunkTreeBuilder.StartParentChunk( // new TagHelperChunk( // unprefixedTagName, // tagHelperBlock.TagMode, // attributes, // _tagHelperDescriptors), // target, // topLevel: false); }
protected ChunkGeneratorContext(ChunkGeneratorContext context) : this(context.Host, context.ClassName, context.RootNamespace, context.SourceFile, // True because we're pulling from the provided context's source file. shouldGenerateLinePragmas: true) { ChunkTreeBuilder = context.ChunkTreeBuilder; }
public CSharpCodeWriter WriteStartInstrumentationContext( ChunkGeneratorContext context, SyntaxTreeNode syntaxNode, bool isLiteral) { return(WriteStartInstrumentationContext( context, syntaxNode.Start.AbsoluteIndex, syntaxNode.Length, isLiteral)); }
protected ChunkGeneratorContext(ChunkGeneratorContext context) : this( context.Host, context.ClassName, context.RootNamespace, context.SourceFile, // True because we're pulling from the provided context's source file. shouldGenerateLinePragmas : true) { ChunkTreeBuilder = context.ChunkTreeBuilder; }
public CSharpCodeWriter WriteStartInstrumentationContext( ChunkGeneratorContext context, SyntaxTreeNode syntaxNode, bool isLiteral) { WriteStartMethodInvocation(context.Host.GeneratedClassContext.BeginContextMethodName); Write(syntaxNode.Start.AbsoluteIndex.ToString(CultureInfo.InvariantCulture)); WriteParameterSeparator(); Write(syntaxNode.Length.ToString(CultureInfo.InvariantCulture)); WriteParameterSeparator(); Write(isLiteral ? "true" : "false"); return(WriteEndMethodInvocation()); }
public static CodeGeneratorContext CreateCodeGeneratorContext() { var language = new CSharpRazorCodeLanguage(); var host = new RazorEngineHost(language); var chunkGeneratorContext = new ChunkGeneratorContext ( host, _testClass, _testNamespace, _testFile, shouldGenerateLinePragmas: false ); var codeGeneratorContext = new CodeGeneratorContext( chunkGeneratorContext, errorSink: new ErrorSink()); return(codeGeneratorContext); }
public void RenderAttributeValue_RendersModelExpressionsCorrectly( string modelExpressionType, string propertyType, string expectedValue) { // Arrange var renderer = new MvcTagHelperAttributeValueCodeRenderer( new GeneratedTagHelperAttributeContext { ModelExpressionTypeName = modelExpressionType, CreateModelExpressionMethodName = "SomeMethod", ModelExpressionProviderPropertyName = "Provider", ViewDataPropertyName = "ViewData" }); var attributeDescriptor = new TagHelperAttributeDescriptor { Name = "MyAttribute", PropertyName = "SomeProperty", TypeName = propertyType, }; var writer = new CSharpCodeWriter(); var generatorContext = new ChunkGeneratorContext( host: null, className: string.Empty, rootNamespace: string.Empty, sourceFile: string.Empty, shouldGenerateLinePragmas: true); var errorSink = new ErrorSink(); var context = new CodeGeneratorContext(generatorContext, errorSink); // Act renderer.RenderAttributeValue(attributeDescriptor, writer, context, (codeWriter) => { codeWriter.Write("MyValue"); }, complexValue: false); // Assert Assert.Equal(expectedValue, writer.GenerateCode()); }
public CSharpCodeWriter WriteEndInstrumentationContext(ChunkGeneratorContext context) { return(WriteMethodInvocation(context.Host.GeneratedClassContext.EndContextMethodName)); }
public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context) { var chunk = context.ChunkTreeBuilder.StartParentChunk <RazorDirectiveChunk>(target); chunk.Descriptor = _descriptor; }
public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context) { //context.ChunkTreeBuilder.StartParentChunk<TemplateChunk>(target); }
public void GenerateEndParentChunk(Block target, ChunkGeneratorContext context) { }
public static unsafe void Generator(ChunkGeneratorContext context) { var pos = context.Current.Position; var heights = new int[Chunk.RowSize, Chunk.RowSize]; var low = int.MaxValue; var high = int.MinValue; { for (var x = 0; x < Chunk.RowSize; x++) { for (var z = 0; z < Chunk.RowSize; z++) { var val = heights[x, z] = (int)PerlinNoise2D((pos.X * Chunk.RowSize + x) / NoiseScaleX, (pos.Z * Chunk.RowSize + z) / NoiseScaleZ) / 2 - 64; if (val < low) { low = val; } if (val > high) { high = val; } } } if (pos.Y * Chunk.RowSize > high && high >= 0) { context.EnableCopyOnWrite(StaticChunkPool.GetAirChunk()); return; } if (0 - Chunk.RowSize >= pos.Y * Chunk.RowSize && pos.Y * Chunk.RowSize > high) { context.EnableCopyOnWrite(StaticChunkPool.GetAirChunk()); return; } if (pos.Y * Chunk.RowSize < low - Chunk.RowSize - 3) { context.EnableCopyOnWrite(_rockChunkId); return; } } { context.EnableFullArray(); var blocks = context.Current.Blocks; for (var x = 0; x < Chunk.RowSize; x++) { for (var z = 0; z < Chunk.RowSize; z++) { var absHeight = heights[x, z]; var height = absHeight - pos.Y * Chunk.RowSize; var underWater = absHeight <= 0; for (var y = 0; y < Chunk.RowSize; y++) { ref var block = ref blocks[x * Chunk.RowSize * Chunk.RowSize + y * Chunk.RowSize + z]; if (y <= height) { if (y == height) { block.Id = underWater ? _sandId : _grassId; } else if (y >= height - 3) { block.Id = underWater ? _sandId : _dirtId; } else { block.Id = _rockId; } block.Brightness = 0; block.Data = 0; } else { block.Id = pos.Y * Chunk.RowSize + y <= 0 ? _waterId : (ushort)0; block.Brightness = (byte)context.DaylightBrightness; block.Data = 0; } } } } }
public override void GenerateChunk(Span target, ChunkGeneratorContext context) { var injectChunk = new InjectChunk(TypeName, PropertyName); context.ChunkTreeBuilder.AddChunk(injectChunk, target); }
/// <summary> /// Instantiates a new instance of the <see cref="CodeGeneratorContext"/> object. /// </summary> /// <param name="generatorContext">A <see cref="ChunkGeneratorContext"/> to copy information from.</param> /// <param name="errorSink"> /// The <see cref="ErrorSink"/> used to collect <see cref="RazorError"/>s encountered /// when parsing the current Razor document. /// </param> public CodeGeneratorContext(ChunkGeneratorContext generatorContext, ErrorSink errorSink) : base(generatorContext) { ErrorSink = errorSink; ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput; }
public override void GenerateChunk(Span target, ChunkGeneratorContext context) { //context.ChunkTreeBuilder.AddStatementChunk(target.Content, target); }
public virtual void GenerateStartParentChunk(Block target, ChunkGeneratorContext context) { }
private string GetBaseType(ChunkGeneratorContext context, string baseType)
public override void GenerateChunk(Span target, ChunkGeneratorContext context) { var modelChunk = new ModelChunk(ModelType); context.ChunkTreeBuilder.AddChunk(modelChunk, target, topLevel: true); }
public override void GenerateChunk(Span target, ChunkGeneratorContext context) { context.ChunkTreeBuilder.AddSetBaseTypeChunk(GetBaseType(context, BaseType), target); }
public override void GenerateEndParentChunk(Block target, ChunkGeneratorContext context) { //context.ChunkTreeBuilder.EndParentChunk(); }
public override void GenerateChunk(Span target, ChunkGeneratorContext context) { context.ChunkTreeBuilder.AddDirectiveToken(target.Content, _tokenDescriptor, target); }