Beispiel #1
0
        public override CodeBuilderResult Build()
        {
            var writer = new CSharpCodeWriter();

            if (!Host.DesignTimeMode && !string.IsNullOrEmpty(Context.Checksum))
            {
                writer.Write("#pragma checksum \"")
                .Write(Context.SourceFile)
                .Write("\" \"")
                .Write(Sha1AlgorithmId)
                .Write("\" \"")
                .Write(Context.Checksum)
                .WriteLine("\"");
            }

            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;");
                    }

                    var csharpCodeVisitor = CreateCSharpCodeVisitor(writer, Context);

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

                    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))
                        {
                            csharpCodeVisitor.Accept(Tree.Chunks);
                        }
                    }
                }
            }

            return(new CodeBuilderResult(writer.GenerateCode(), writer.LineMappingManager.Mappings));
        }
        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();
        }
        protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
        {
            // Grab the last model chunk so it gets intellisense.
            var modelChunk = ChunkHelper.GetModelChunk(Context.CodeTreeBuilder.CodeTree);

            Model = modelChunk != null ? modelChunk.ModelType : _defaultModel;

            // If there were any model chunks then we need to modify the class declaration signature.
            if (modelChunk != null)
            {
                writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName));

                var modelVisitor = new ModelChunkVisitor(writer, Context);
                // This generates the base class signature
                modelVisitor.Accept(modelChunk);

                writer.WriteLine();

                return new CSharpCodeWritingScope(writer);
            }
            else
            {
                return base.BuildClassDeclaration(writer);
            }
        }
Beispiel #4
0
        protected override CSharpCodeWritingScope BuildClassDeclaration(CSharpCodeWriter writer)
        {
            // Grab the last model chunk so it gets intellisense.
            // NOTE: If there's more than 1 model chunk there will be a Razor error BUT we want intellisense to 
            // show up on the current model chunk that the user is typing.
            var modelChunk = Context.CodeTreeBuilder.CodeTree.Chunks.OfType<ModelChunk>()
                                                                    .LastOrDefault();

            Model = modelChunk != null ? modelChunk.ModelType : _hostOptions.DefaultModel;

            // If there were any model chunks then we need to modify the class declaration signature.
            if (modelChunk != null)
            {
                writer.Write(string.Format(CultureInfo.InvariantCulture, "public class {0} : ", Context.ClassName));

                var modelVisitor = new ModelChunkVisitor(writer, Context);
                // This generates the base class signature
                modelVisitor.Accept(modelChunk);

                writer.WriteLine();

                return new CSharpCodeWritingScope(writer);
            }
            else
            {
                return base.BuildClassDeclaration(writer);
            }
        }
        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);
        }
Beispiel #6
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));
        }
Beispiel #7
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);
        }