Beispiel #1
0
        public void CreateCodeMapping(string padding, string code, Chunk chunk)
        {
            using (CSharpLineMappingWriter mappingWriter = Writer.BuildLineMapping(chunk.Start, code.Length, Context.SourceFile))
            {
                Writer.Write(padding);

                mappingWriter.MarkLineMappingStart();
                Writer.Write(code);
                mappingWriter.MarkLineMappingEnd();
            }
        }
        public static CSharpLineMappingWriter BuildCodeMapping(this CSharpCodeWriter writer, MappingLocation documentLocation)
        {
            // TODO: Update the primary API to accept mapping locations
            var sourceLocation = new SourceLocation(
                documentLocation.FilePath,
                documentLocation.AbsoluteIndex,
                documentLocation.LineIndex,
                documentLocation.CharacterIndex);
            var lineMappingWriter = new CSharpLineMappingWriter(writer, sourceLocation, documentLocation.ContentLength);

            return(lineMappingWriter);
        }
Beispiel #3
0
        private void RenderRuntimeExpressionBlockChunkWithContentSpan(ExpressionBlockChunk chunk, Span contentSpan)
        {
            var generateInstrumentation = ShouldGenerateInstrumentationForExpressions();

            if (generateInstrumentation)
            {
                Writer.WriteStartInstrumentationContext(Context, contentSpan, isLiteral: false);
            }

            using (var mappingWriter = new CSharpLineMappingWriter(Writer, chunk.Start, Context.SourceFile))
            {
                if (!string.IsNullOrEmpty(Context.TargetWriterName))
                {
                    var generatedStart =
                        WriteToMethodName.Length +
                        Context.TargetWriterName.Length +
                        3; // 1 for the opening '(' and 2 for ', '

                    var padding = _paddingBuilder.BuildExpressionPadding(contentSpan, generatedStart);

                    Writer
                    .Write(padding)
                    .WriteStartMethodInvocation(WriteToMethodName)
                    .Write(Context.TargetWriterName)
                    .WriteParameterSeparator();
                }
                else
                {
                    var generatedStart =
                        WriteMethodName.Length +
                        1;  // for the opening '('

                    var padding = _paddingBuilder.BuildExpressionPadding(contentSpan, generatedStart);

                    Writer
                    .Write(padding)
                    .WriteStartMethodInvocation(WriteMethodName);
                }

                Accept(chunk.Children);

                Writer.WriteEndMethodInvocation();
            }

            if (generateInstrumentation)
            {
                Writer.WriteEndInstrumentationContext(Context);
            }
        }
        protected override void Visit(SetBaseTypeChunk chunk)
        {
            if (Context.Host.DesignTimeMode)
            {
                using (CSharpLineMappingWriter lineMappingWriter = Writer.BuildLineMapping(chunk.Start, chunk.TypeName.Length, Context.SourceFile))
                {
                    Writer.Indent(chunk.Start.CharacterIndex);

                    lineMappingWriter.MarkLineMappingStart();
                    Writer.Write(chunk.TypeName);
                    lineMappingWriter.MarkLineMappingEnd();

                    Writer.Write(" ").Write(InheritsHelper).Write(" = null;");
                }
            }
        }
        protected override void Visit(DynamicCodeAttributeChunk chunk)
        {
            if (Context.Host.DesignTimeMode)
            {
                // Render the children as is without wrapping them in calls to WriteAttribute
                Accept(chunk.Children);
                return;
            }

            var currentRenderingMode    = Context.ExpressionRenderingMode;
            var currentTargetWriterName = Context.TargetWriterName;

            CSharpLineMappingWriter lineMappingWriter = null;
            var code = chunk.Children.FirstOrDefault();

            if (code is ExpressionChunk || code is ExpressionBlockChunk)
            {
                // We only want to render the #line pragma if the attribute value will be in-lined.
                // Ex: WriteAttributeValue("", 0, DateTime.Now, 0, 0, false)
                // For non-inlined scenarios: WriteAttributeValue("", 0, (_) => ..., 0, 0, false)
                // the line pragma will be generated inside the lambda.
                lineMappingWriter = new CSharpLineMappingWriter(Writer, chunk.Start, Context.SourceFile);
            }

            if (!string.IsNullOrEmpty(currentTargetWriterName))
            {
                Writer.WriteStartMethodInvocation(Context.Host.GeneratedClassContext.WriteAttributeValueToMethodName)
                .Write(currentTargetWriterName)
                .WriteParameterSeparator();
            }
            else
            {
                Writer.WriteStartMethodInvocation(WriteAttributeValueMethodName);
            }

            Context.TargetWriterName = ValueWriterName;

            if (code is ExpressionChunk || code is ExpressionBlockChunk)
            {
                Debug.Assert(lineMappingWriter != null);

                Writer
                .WriteLocationTaggedString(chunk.Prefix)
                .WriteParameterSeparator();

                Context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode;

                Accept(code);

                Writer
                .WriteParameterSeparator()
                .Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.InvariantCulture))
                .WriteParameterSeparator()
                .Write(chunk.Association.Length.ToString(CultureInfo.InvariantCulture))
                .WriteParameterSeparator()
                .WriteBooleanLiteral(value: false)
                .WriteEndMethodInvocation();

                lineMappingWriter.Dispose();
            }
            else
            {
                Writer
                .WriteLocationTaggedString(chunk.Prefix)
                .WriteParameterSeparator()
                .WriteStartNewObject(Context.Host.GeneratedClassContext.TemplateTypeName);

                using (Writer.BuildAsyncLambda(endLine: false, parameterNames: ValueWriterName))
                {
                    Accept(chunk.Children);
                }

                Writer
                .WriteEndMethodInvocation(false)
                .WriteParameterSeparator()
                .Write(chunk.Start.AbsoluteIndex.ToString(CultureInfo.InvariantCulture))
                .WriteParameterSeparator()
                .Write(chunk.Association.Length.ToString(CultureInfo.InvariantCulture))
                .WriteParameterSeparator()
                .WriteBooleanLiteral(false)
                .WriteEndMethodInvocation();
            }

            Context.TargetWriterName        = currentTargetWriterName;
            Context.ExpressionRenderingMode = currentRenderingMode;
        }