Ejemplo n.º 1
0
        public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk <DynamicCodeAttributeChunk>(target);

            chunk.Start  = ValueStart;
            chunk.Prefix = Prefix;
        }
Ejemplo n.º 2
0
        public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk <CodeAttributeChunk>(target);

            chunk.Attribute = Name;
            chunk.Prefix    = Prefix;
            chunk.Suffix    = Suffix;
        }
Ejemplo n.º 3
0
        public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk<CodeAttributeChunk>(target);

            chunk.Attribute = Name;
            chunk.Prefix = Prefix;
            chunk.Suffix = Suffix;
        }
        /// <summary>
        /// Starts the generation of a <see cref="TagHelperChunk"/>.
        /// </summary>
        /// <param name="target">
        /// The <see cref="Block"/> responsible for this <see cref="TagHelperChunkGenerator"/>.
        /// </param>
        /// <param name="context">A <see cref="ChunkGeneratorContext"/> instance that contains information about
        /// the current chunk generation process.</param>
        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);
        }
Ejemplo n.º 5
0
        public override void GenerateChunk(Span target, ChunkGeneratorContext context)
        {
            var ns = Namespace;

            if (!string.IsNullOrEmpty(ns) && char.IsWhiteSpace(ns[0]))
            {
                ns = ns.Substring(1);
            }

            context.ChunkTreeBuilder.AddUsingChunk(ns, target);
        }
 private void EnsureContextInitialized()
 {
     if (_context == null)
     {
         _context = new ChunkGeneratorContext(Host,
                                              ClassName,
                                              RootNamespaceName,
                                              SourceFileName,
                                              GenerateLinePragmas);
         Initialize(_context);
     }
 }
        public override void GenerateChunk(Span target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk <LiteralCodeAttributeChunk>(target);

            chunk.Prefix = Prefix;
            chunk.Value  = Value;

            if (ValueGenerator != null)
            {
                chunk.ValueLocation = ValueGenerator.Location;

                ValueGenerator.Value.GenerateChunk(target, context);

                chunk.ValueLocation = ValueGenerator.Location;
            }

            context.ChunkTreeBuilder.EndParentChunk();
        }
        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());
        }
Ejemplo n.º 9
0
 public virtual void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
 {
 }
Ejemplo n.º 10
0
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     var injectChunk = new InjectChunk(TypeName, PropertyName);
     context.ChunkTreeBuilder.AddChunk(injectChunk, target);
 }
Ejemplo n.º 11
0
 public void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
 }
 public void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.StartParentChunk <ExpressionBlockChunk>(target);
 }
Ejemplo n.º 13
0
        public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk <SectionChunk>(target);

            chunk.Name = SectionName;
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Generates <see cref="RemoveTagHelperChunk"/>s.
 /// </summary>
 /// <param name="target">
 /// The <see cref="Span"/> responsible for this <see cref="RemoveTagHelperChunkGenerator"/>.
 /// </param>
 /// <param name="context">A <see cref="ChunkGeneratorContext"/> instance that contains information about
 /// the current chunk generation process.</param>
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddRemoveTagHelperChunk(_lookupText, target);
 }
 public void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddExpressionChunk(target.Content, target);
 }
 private string GetBaseType(ChunkGeneratorContext context, string baseType)
Ejemplo n.º 17
0
 protected virtual void Initialize(ChunkGeneratorContext context)
 {
 }
Ejemplo n.º 18
0
 public void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.StartParentChunk<ExpressionBlockChunk>(target);
 }
Ejemplo n.º 19
0
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddSetBaseTypeChunk(BaseType, target);
 }
Ejemplo n.º 20
0
 public void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
 }
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddSetBaseTypeChunk(BaseType, target);
 }
Ejemplo n.º 22
0
        public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
        {
            var chunk = context.ChunkTreeBuilder.StartParentChunk<SectionChunk>(target);

            chunk.Name = SectionName;
        }
Ejemplo n.º 23
0
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddStatementChunk(target.Content, target);
 }
 public override void GenerateStartParentChunk(Block target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.StartParentChunk <TemplateChunk>(target);
 }
 /// <summary>
 /// Generates <see cref="TagHelperPrefixDirectiveChunk"/>s.
 /// </summary>
 /// <param name="target">
 /// The <see cref="Span"/> responsible for this <see cref="TagHelperPrefixDirectiveChunkGenerator"/>.
 /// </param>
 /// <param name="context">A <see cref="ChunkGeneratorContext"/> instance that contains information about
 /// the current chunk generation process.</param>
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddTagHelperPrefixDirectiveChunk(_prefix, target);
 }
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddStatementChunk(target.Content, target);
 }
Ejemplo n.º 27
0
 public CSharpCodeWriter WriteEndInstrumentationContext(ChunkGeneratorContext context)
 {
     return WriteMethodInvocation(context.Host.GeneratedClassContext.EndContextMethodName);
 }
Ejemplo n.º 28
0
 public override void GenerateEndParentChunk(Block target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.EndParentChunk();
 }
Ejemplo n.º 29
0
 public CSharpCodeWriter WriteStartInstrumentationContext(
     ChunkGeneratorContext context,
     SyntaxTreeNode syntaxNode,
     bool isLiteral)
 {
     return WriteStartInstrumentationContext(
         context,
         syntaxNode.Start.AbsoluteIndex,
         syntaxNode.Length,
         isLiteral);
 }
 /// <summary>
 /// Generates <see cref="RemoveTagHelperChunk"/>s.
 /// </summary>
 /// <param name="target">
 /// The <see cref="Span"/> responsible for this <see cref="RemoveTagHelperChunkGenerator"/>.
 /// </param>
 /// <param name="context">A <see cref="ChunkGeneratorContext"/> instance that contains information about
 /// the current chunk generation process.</param>
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddRemoveTagHelperChunk(LookupText, target);
 }
Ejemplo n.º 31
0
 public CSharpCodeWriter WriteStartInstrumentationContext(
     ChunkGeneratorContext context,
     int absoluteIndex,
     int length,
     bool isLiteral)
 {
     WriteStartMethodInvocation(context.Host.GeneratedClassContext.BeginContextMethodName);
     Write(absoluteIndex.ToString(CultureInfo.InvariantCulture));
     WriteParameterSeparator();
     Write(length.ToString(CultureInfo.InvariantCulture));
     WriteParameterSeparator();
     Write(isLiteral ? "true" : "false");
     return WriteEndMethodInvocation();
 }
Ejemplo n.º 32
0
 public override void GenerateEndParentChunk(Block target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.EndParentChunk();
 }
Ejemplo n.º 33
0
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     var modelChunk = new ModelChunk(ModelType);
     context.ChunkTreeBuilder.AddChunk(modelChunk, target, topLevel: true);
 }
Ejemplo n.º 34
0
 /// <summary>
 /// Generates <see cref="TagHelperPrefixDirectiveChunk"/>s.
 /// </summary>
 /// <param name="target">
 /// The <see cref="Span"/> responsible for this <see cref="TagHelperPrefixDirectiveChunkGenerator"/>.
 /// </param>
 /// <param name="context">A <see cref="ChunkGeneratorContext"/> instance that contains information about
 /// the current chunk generation process.</param>
 public override void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddTagHelperPrefixDirectiveChunk(Prefix, target);
 }
Ejemplo n.º 35
0
 public void GenerateChunk(Span target, ChunkGeneratorContext context)
 {
     context.ChunkTreeBuilder.AddExpressionChunk(target.Content, target);
 }
Ejemplo n.º 36
0
 public void GenerateEndParentChunk(Block target, ChunkGeneratorContext context)
 {
 }
Ejemplo n.º 37
0
 /// <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;
 }