public CSharpDisableWarningScope(CSharpCodeWriter writer, int warningNumber)
        {
            _writer = writer;
            _warningNumber = warningNumber;

            _writer.WritePragma("warning disable " + _warningNumber);
        }
Пример #2
0
 private CSharpLineMappingWriter([NotNull] CSharpCodeWriter writer, bool addLineMappings)
 {
     _writer = writer;
     _addLineMapping = addLineMappings;
     _startIndent = _writer.CurrentIndent;
     _writer.ResetIndent();
 }
        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);
        }
Пример #4
0
        public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength, string sourceFilename)
            : this(writer, documentLocation, contentLength)
        {
            _writePragmas = true;

            _writer.WriteLineNumberDirective(documentLocation, sourceFilename);
            _generatedLocation = _writer.GetCurrentSourceLocation();
        }
Пример #5
0
 protected virtual void BuildConstructor(CSharpCodeWriter writer)
 {
     writer.WriteLineHiddenDirective();
     using (writer.BuildConstructor(Context.ClassName))
     {
         // Any constructor based logic that we need to add?
     };
 }
Пример #6
0
        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();
        }
Пример #7
0
        protected virtual CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
        {
            var baseTypeVisitor = new CSharpBaseTypeVisitor(writer, Context);
            baseTypeVisitor.Accept(Tree.Chunks);

            var baseType = baseTypeVisitor.CurrentBaseType ?? Host.DefaultBaseClass;

            var baseTypes = string.IsNullOrEmpty(baseType) ? Enumerable.Empty<string>() : new string[] { baseType };

            return writer.BuildClassDeclaration("public", Context.ClassName, baseTypes);
        }
        /// <summary>
        /// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
        /// </summary>
        /// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
        /// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
        /// <param name="context">A <see cref="CodeBuilderContext"/> instance that contains information about
        /// the current code generation process.</param>
        public CSharpTagHelperCodeRenderer(
            [NotNull] IChunkVisitor bodyVisitor,
            [NotNull] CSharpCodeWriter writer,
            [NotNull] CodeBuilderContext context)
        {
            _bodyVisitor      = bodyVisitor;
            _writer           = writer;
            _context          = context;
            _tagHelperContext = context.Host.GeneratedClassContext.GeneratedTagHelperContext;
            _designTimeMode   = context.Host.DesignTimeMode;

            _literalBodyVisitor        = new CSharpLiteralCodeVisitor(this, writer, context);
            AttributeValueCodeRenderer = new TagHelperAttributeValueCodeRenderer();
        }
Пример #9
0
        public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength, string sourceFilename)
            : this(writer, documentLocation, contentLength)
        {
            _writePragmas = true;

            // TODO: Should this just be '\n'?
            if (!_writer.LastWrite.EndsWith("\n"))
            {
                _writer.WriteLine();
            }

            _writer.WriteLineNumberDirective(documentLocation.LineIndex + 1, sourceFilename);

            _generatedLocation = _writer.GetCurrentSourceLocation();
        }
Пример #10
0
        public void WriteLineNumberDirective_UsesFilePath_WhenFileInSourceLocationIsNull()
        {
            // Arrange
            var filePath = "some-path";
            var writer = new CSharpCodeWriter();
            var expected = $"#line 5 \"{filePath}\"" + writer.NewLine;
            var sourceLocation = new SourceLocation(10, 4, 3);

            // Act
            writer.WriteLineNumberDirective(sourceLocation, filePath);
            var code = writer.GenerateCode();

            // Assert
            Assert.Equal(expected, code);
        }
Пример #11
0
        public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength, string sourceFilename)
            : this(writer, documentLocation, contentLength)
        {
            _writePragmas = true;

            // TODO: Should this just be '\n'?
            if (!_writer.LastWrite.EndsWith("\n"))
            {
                _writer.WriteLine();
            }

            _writer.WriteLineNumberDirective(documentLocation.LineIndex + 1, sourceFilename);

            _generatedLocation = _writer.GetCurrentSourceLocation();
        }
        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);
        }
Пример #13
0
        public override CodeBuilderResult Build()
        {
            var writer = new CSharpCodeWriter();

            using (writer.BuildNamespace(Context.RootNamespace))
            {
                // Write out using directives
                AddImports(Tree, writer, Host.NamespaceImports);
                // Separate the usings and the class
                writer.WriteLine();

                new CSharpClassAttributeVisitor(writer, Context).Accept(Tree.Chunks);

                using (BuildClassDeclaration(writer))
                {
                    if (Host.DesignTimeMode)
                    {
                        writer.WriteLine("private static object @__o;");
                    }

                    new CSharpHelperVisitor(writer, Context).Accept(Tree.Chunks);
                    new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
                    new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);

                    BuildConstructor(writer);

                    // Add space inbetween constructor and method body
                    writer.WriteLine();

                    using (writer.BuildDisableWarningScope(DisableAsyncWarning))
                    {
                        using (writer.BuildMethodDeclaration("public override async", "Task", Host.GeneratedClassContext.ExecuteMethodName))
                        {
                            new CSharpCodeVisitor(writer, Context).Accept(Tree.Chunks);
                        }
                    }
                }
            }

            return new CodeBuilderResult(writer.GenerateCode(), writer.LineMappingManager.Mappings);
        }
Пример #14
0
        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);
        }
Пример #15
0
        private void AddImports(CodeTree codeTree, CSharpCodeWriter writer, IEnumerable<string> defaultImports)
        {
            // Write out using directives
            var usingVisitor = new CSharpUsingVisitor(writer, Context);
            foreach (Chunk chunk in Tree.Chunks)
            {
                usingVisitor.Accept(chunk);
            }

            defaultImports = defaultImports.Except(usingVisitor.ImportedUsings);

            foreach (string import in defaultImports)
            {
                writer.WriteUsing(import);
            }

            string taskNamespace = typeof(Task).Namespace;

            // We need to add the task namespace but ONLY if it hasn't been added by the default imports or using imports yet.
            if(!defaultImports.Contains(taskNamespace) && !usingVisitor.ImportedUsings.Contains(taskNamespace))
            {
                writer.WriteUsing(taskNamespace);
            }
        }
Пример #16
0
 public CSharpDisableWarningScope(CSharpCodeWriter writer) : this(writer, 219)
 { }
Пример #17
0
 public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
     : this(writer, addLineMappings: true)
 {
     _documentMapping = new MappingLocation(documentLocation, contentLength);
     _generatedLocation = _writer.GetCurrentSourceLocation();
 }
Пример #18
0
 public CSharpLineMappingWriter(CSharpCodeWriter writer, SourceLocation documentLocation, int contentLength)
     : this(writer, addLineMappings : true)
 {
     _documentMapping   = new MappingLocation(documentLocation, contentLength);
     _generatedLocation = _writer.GetCurrentSourceLocation();
 }