private bool ShouldIgnoreFile(string filePath)
 {
     return(GeneratedCodeUtilities.IsGeneratedCodeFile(filePath) ||
            this.IgnoreFile.IsIgnored(filePath));
 }
예제 #2
0
        public async Task <CSharpierResult> FormatAsync(
            string code,
            PrinterOptions printerOptions,
            CancellationToken cancellationToken
            )
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(
                code,
                new CSharpParseOptions(LanguageVersion.CSharp9, DocumentationMode.Diagnose),
                cancellationToken: cancellationToken
                );
            var syntaxNode = await syntaxTree.GetRootAsync(cancellationToken);

            if (syntaxNode is not CompilationUnitSyntax rootNode)
            {
                throw new Exception(
                          "Root was not CompilationUnitSyntax, it was " + syntaxNode.GetType()
                          );
            }

            if (GeneratedCodeUtilities.BeginsWithAutoGeneratedComment(rootNode))
            {
                return(new CSharpierResult {
                    Code = code
                });
            }

            var diagnostics = syntaxTree.GetDiagnostics(cancellationToken)
                              .Where(o => o.Severity == DiagnosticSeverity.Error && o.Id != "CS1029")
                              .ToList();

            if (diagnostics.Any())
            {
                return(new CSharpierResult
                {
                    Code = code,
                    Errors = diagnostics,
                    AST = printerOptions.IncludeAST ? this.PrintAST(rootNode) : string.Empty
                });
            }

            try
            {
                var document      = Node.Print(rootNode);
                var lineEnding    = GetLineEnding(code, printerOptions);
                var formattedCode = DocPrinter.DocPrinter.Print(
                    document,
                    printerOptions,
                    lineEnding
                    );
                return(new CSharpierResult
                {
                    Code = formattedCode,
                    DocTree = printerOptions.IncludeDocTree
                        ? DocSerializer.Serialize(document)
                        : string.Empty,
                    AST = printerOptions.IncludeAST ? this.PrintAST(rootNode) : string.Empty
                });
            }
            catch (InTooDeepException)
            {
                return(new CSharpierResult
                {
                    FailureMessage = "We can't handle this deep of recursion yet."
                });
            }
        }