Exemple #1
0
        protected override void Visit(ExpressionBlockChunk chunk)
        {
            var             firstChildExpression = chunk.Children.FirstOrDefault() as ExpressionChunk;
            var             padding          = 0;
            MappingLocation documentLocation = null;

            if (firstChildExpression != null)
            {
                padding          = _paddingBuilder.CalculateExpressionPadding((Span)firstChildExpression.Association);
                documentLocation = CreateMappingLocation(firstChildExpression.Start, firstChildExpression.Code.Length);
            }

            var expressionBlock = new CSharpBlock();

            using (_context.Builder.UseBlock(expressionBlock))
            {
                Accept(chunk.Children);
            }

            var renderExpression = new RenderExpression
            {
                Expression       = expressionBlock,
                Padding          = padding,
                DocumentLocation = documentLocation
            };

            _context.Builder.Add(renderExpression);
        }
Exemple #2
0
        public static void OnError(this ErrorSink errorSink, MappingLocation mappingLocation, string message)
        {
            var location = new SourceLocation(
                mappingLocation.FilePath,
                mappingLocation.AbsoluteIndex,
                mappingLocation.LineIndex,
                mappingLocation.CharacterIndex);

            errorSink.OnError(location, message, mappingLocation.ContentLength);
        }
        public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
        {
            _writer = writer;
            _documentMapping = new MappingLocation(documentLocation, contentLength);

            _startIndent = _writer.CurrentIndent;
            _generatedContentLength = 0;
            _writer.ResetIndent();
            
            _generatedLocation = _writer.GetCurrentSourceLocation();
        }
        public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
        {
            _writer          = writer;
            _documentMapping = new MappingLocation(documentLocation, contentLength);

            _startIndent            = _writer.CurrentIndent;
            _generatedContentLength = 0;
            _writer.ResetIndent();

            _generatedLocation = _writer.GetCurrentSourceLocation();
        }
Exemple #5
0
 /// <summary>
 /// Instantiates a new instance of range mapping comparison result.
 /// </summary>
 /// <param name="ssm">Store representation of shard map.</param>
 /// <param name="range">Range being considered.</param>
 /// <param name="mappingLocation">Location of mapping.</param>
 /// <param name="gsmMapping">Storage representation of GSM mapping.</param>
 /// <param name="lsmMapping">Storange representation of LSM mapping.</param>
 internal MappingComparisonResult(
     IStoreShardMap ssm,
     ShardRange range,
     MappingLocation mappingLocation,
     IStoreMapping gsmMapping,
     IStoreMapping lsmMapping)
 {
     this.ShardMap               = ssm;
     this.Range                  = range;
     this.MappingLocation        = mappingLocation;
     this.ShardMapManagerMapping = gsmMapping;
     this.ShardMapping           = lsmMapping;
 }
 /// <summary>
 /// Instantiates a new instance of range mapping comparison result.
 /// </summary>
 /// <param name="ssm">Store representation of shard map.</param>
 /// <param name="range">Range being considered.</param>
 /// <param name="mappingLocation">Location of mapping.</param>
 /// <param name="gsmMapping">Storage representation of GSM mapping.</param>
 /// <param name="lsmMapping">Storange representation of LSM mapping.</param>
 internal MappingComparisonResult(
     IStoreShardMap ssm,
     ShardRange range,
     MappingLocation mappingLocation,
     IStoreMapping gsmMapping,
     IStoreMapping lsmMapping)
 {
     this.ShardMap = ssm;
     this.Range = range;
     this.MappingLocation = mappingLocation;
     this.ShardMapManagerMapping = gsmMapping;
     this.ShardMapping = lsmMapping;
 }
Exemple #7
0
 internal MappingDifference(
     MappingDifferenceType type,
     MappingLocation location,
     IStoreShardMap shardMap,
     IStoreMapping mappingForShardMap,
     IStoreMapping mappingForShard
     )
 {
     this.Type               = type;
     this.Location           = location;
     this.ShardMap           = shardMap;
     this.MappingForShardMap = mappingForShardMap;
     this.MappingForShard    = mappingForShard;
 }
 internal MappingDifference(
     MappingDifferenceType type,
     MappingLocation location,
     IStoreShardMap shardMap,
     IStoreMapping mappingForShardMap,
     IStoreMapping mappingForShard
     )
 {
     this.Type = type;
     this.Location = location;
     this.ShardMap = shardMap;
     this.MappingForShardMap = mappingForShardMap;
     this.MappingForShard = mappingForShard;
 }
Exemple #9
0
        protected override void Visit(RazorDirectiveTokenChunk chunk)
        {
            var filePath           = chunk.Start.FilePath ?? _context.SourceFileName;
            var fileMappedLocation = new SourceLocation(filePath, chunk.Start.AbsoluteIndex, chunk.Start.LineIndex, chunk.Start.CharacterIndex);
            var documentLocation   = new MappingLocation(fileMappedLocation, chunk.Value.Length);
            var token = new RazorDirectiveToken()
            {
                Descriptor       = chunk.Descriptor,
                Value            = chunk.Value,
                DocumentLocation = documentLocation
            };

            _context.Builder.Add(token);
        }
Exemple #10
0
        public void Walk(CSharpBlock block)
        {
            for (var i = 0; i < block.Children.Count; i++)
            {
                var             literal         = false;
                MappingLocation mappingLocation = null;
                var             current         = block.Children[i];

                if (current is CSharpBlock)
                {
                    Walk((CSharpBlock)current);
                }
                else if (current is RenderHtml)
                {
                    var renderHtml = current as RenderHtml;
                    literal         = true;
                    mappingLocation = renderHtml.DocumentLocation;
                }
                else if (current is RenderExpression)
                {
                    var renderExpression = current as RenderExpression;
                    literal         = false;
                    mappingLocation = renderExpression.DocumentLocation;
                }
                else if (current is ExecuteTagHelpers)
                {
                    Debug.Assert(block is RenderTagHelper);
                    var renderTagHelper   = block as RenderTagHelper;
                    var executeTagHelpers = current as ExecuteTagHelpers;
                    literal         = false;
                    mappingLocation = renderTagHelper.DocumentLocation;
                }

                if (mappingLocation != null)
                {
                    var beginInstrumentation = new BeginInstrumentation
                    {
                        DocumentLocation = mappingLocation,
                        Literal          = literal,
                    };
                    block.Children.Insert(i, beginInstrumentation);
                    var endInstrumentation = new EndInstrumentation();
                    block.Children.Insert(i + 2, endInstrumentation);
                    i += 2;

                    mappingLocation = null;
                }
            }
        }
        public void Dispose()
        {
            if (_addLineMapping)
            {
                // Verify that the generated length has not already been calculated
                if (_generatedContentLength == 0)
                {
                    _generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
                }

                var generatedLocation = new MappingLocation(_generatedLocation, _generatedContentLength);
                var documentMapping = _documentMapping;
                if (documentMapping.ContentLength == -1)
                {
                    documentMapping = new MappingLocation(
                        location: new SourceLocation(
                            _documentMapping.AbsoluteIndex,
                            _documentMapping.LineIndex,
                            _documentMapping.CharacterIndex),
                        contentLength: _generatedContentLength);
                }

                _writer.LineMappingManager.AddMapping(
                    documentLocation: documentMapping,
                    generatedLocation: generatedLocation);
            }

            if (_writePragmas)
            {
                // 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 endsWithNewline = _writer.GenerateCode().EndsWith("\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();
            }

            // Reset indent back to when it was started
            _writer.SetIndent(_startIndent);
        }
Exemple #12
0
        public LinePragmaWriter(CSharpCodeWriter writer, MappingLocation documentLocation)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (documentLocation == null)
            {
                throw new ArgumentNullException(nameof(documentLocation));
            }

            _writer      = writer;
            _startIndent = _writer.CurrentIndent;
            _writer.ResetIndent();
            _writer.WriteLineNumberDirective(documentLocation, documentLocation.FilePath);
            _writer.SetIndent(_startIndent);
        }
        public void Dispose()
        {
            if (_addLineMapping)
            {
                // Verify that the generated length has not already been calculated
                if (_generatedContentLength == 0)
                {
                    _generatedContentLength = _writer.GenerateCode().Length - _generatedLocation.AbsoluteIndex;
                }

                var generatedLocation = new MappingLocation(_generatedLocation, _generatedContentLength);
                if (_documentMapping.ContentLength == -1)
                {
                    _documentMapping.ContentLength = generatedLocation.ContentLength;
                }

                _writer.LineMappingManager.AddMapping(
                    documentLocation: _documentMapping,
                    generatedLocation: new MappingLocation(_generatedLocation, _generatedContentLength));
            }

            if (_writePragmas)
            {
                // 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 endsWithNewline = _writer.GenerateCode().EndsWith("\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();
            }

            // Reset indent back to when it was started
            _writer.SetIndent(_startIndent);
        }
 private static void RenderTagHelperAttributeInline(
     ICSharpSource attributeValue,
     MappingLocation documentLocation,
     CSharpRenderingContext context)
 {
     if (attributeValue is CSharpSource)
     {
         context.Writer.Write(((CSharpSource)attributeValue).Code);
     }
     else if (attributeValue is RenderHtml)
     {
         context.Writer.Write(((RenderHtml)attributeValue).Html);
     }
     else if (attributeValue is RenderExpression)
     {
         RenderTagHelperAttributeInline(((RenderExpression)attributeValue).Expression, documentLocation, context);
     }
     else if (attributeValue is RenderStatement)
     {
         context.ErrorSink.OnError(
             documentLocation,
             "TODO: RazorResources.TagHelpers_CodeBlocks_NotSupported_InAttributes");
     }
     else if (attributeValue is Template)
     {
         context.ErrorSink.OnError(
             documentLocation,
             "TODO: RazorResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(_attributeTypeName)");
     }
     else if (attributeValue is CSharpBlock)
     {
         var expressionBlock = (CSharpBlock)attributeValue;
         for (var i = 0; i < expressionBlock.Children.Count; i++)
         {
             RenderTagHelperAttributeInline(expressionBlock.Children[i], documentLocation, context);
         }
     }
 }
Exemple #15
0
        protected override void Visit(UsingChunk chunk)
        {
            if (chunk == null)
            {
                throw new ArgumentNullException(nameof(chunk));
            }

            // We don't want to attempt to add duplicate namespace imports.
            if (!_addedImports.Add(chunk.Namespace))
            {
                return;
            }

            var documentContent  = ((Span)chunk.Association).Content.Trim();
            var documentLocation = new MappingLocation(chunk.Start, documentContent.Length);
            var importNamespace  = new ImportNamespace
            {
                // TODO: The document content has the entire "using X" string, this only has the X. Are mapping locations accurate?
                Namespace        = chunk.Namespace,
                DocumentLocation = documentLocation
            };

            _context.Builder.Add(importNamespace);
        }
        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);
        }
Exemple #17
0
 public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
     : this(writer, addLineMappings: true)
 {
     _documentMapping = new MappingLocation(documentLocation, contentLength);
     _generatedLocation = _writer.GetCurrentSourceLocation();
 }
Exemple #18
0
 public void AddMapping(MappingLocation documentLocation, MappingLocation generatedLocation)
 {
     Mappings.Add(new LineMapping(documentLocation, generatedLocation));
 }
Exemple #19
0
 public LineMapping(MappingLocation documentLocation, MappingLocation generatedLocation)
 {
     DocumentLocation  = documentLocation;
     GeneratedLocation = generatedLocation;
 }
Exemple #20
0
 public LineMapping(MappingLocation documentLocation, MappingLocation generatedLocation)
 {
     DocumentLocation = documentLocation;
     GeneratedLocation = generatedLocation;
 }
 public static IDisposable BuildLinePragma(this CSharpCodeWriter writer, MappingLocation documentLocation)
 {
     return(new LinePragmaWriter(writer, documentLocation));
 }
        public static CSharpCodeWriter WriteLineNumberDirective(this CSharpCodeWriter writer, MappingLocation location, string file)
        {
            if (location.FilePath != null)
            {
                file = location.FilePath;
            }

            if (writer.Builder.Length >= writer.NewLine.Length && !writer.IsAfterNewLine)
            {
                writer.WriteLine();
            }

            var lineNumberAsString = (location.LineIndex + 1).ToString(CultureInfo.InvariantCulture);

            return(writer.Write("#line ").Write(lineNumberAsString).Write(" \"").Write(file).WriteLine("\""));
        }
 public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
     : this(writer, addLineMappings : true)
 {
     _documentMapping   = new MappingLocation(documentLocation, contentLength);
     _generatedLocation = _writer.GetCurrentSourceLocation();
 }