public static string GetLineEnding(string code, PrinterOptions printerOptions) { if (printerOptions.EndOfLine == EndOfLine.Auto) { var lineIndex = code.IndexOf('\n'); if (lineIndex <= 0) { return("\n"); } if (code[lineIndex - 1] == '\r') { return("\r\n"); } return("\n"); } return(printerOptions.EndOfLine == EndOfLine.CRLF ? "\r\n" : "\n"); }
protected CommandLineFormatter( string baseDirectoryPath, string path, CommandLineOptions commandLineOptions, PrinterOptions printerOptions, IFileSystem fileSystem, IConsole console, IgnoreFile ignoreFile, CommandLineFormatterResult result ) { this.BaseDirectoryPath = baseDirectoryPath; this.Path = path; this.PrinterOptions = printerOptions; this.CommandLineOptions = commandLineOptions; this.FileSystem = fileSystem; this.Console = console; this.IgnoreFile = ignoreFile; this.Result = result; }
public static async Task <int> Format( CommandLineOptions commandLineOptions, IFileSystem fileSystem, IConsole console, CancellationToken cancellationToken ) { var stopwatch = Stopwatch.StartNew(); var result = new CommandLineFormatterResult(); foreach (var path in commandLineOptions.DirectoryOrFilePaths) { var normalizedPath = path.Replace('\\', '/'); var baseDirectoryPath = fileSystem.File.Exists(normalizedPath) ? fileSystem.Path.GetDirectoryName(normalizedPath) : path; if (baseDirectoryPath == null) { throw new Exception( $"The path of {normalizedPath} does not appear to point to a directory or a file." ); } var configurationFileOptions = ConfigurationFileOptions.Create( baseDirectoryPath, fileSystem ); var ignoreFile = await IgnoreFile.Create( baseDirectoryPath, fileSystem, console, cancellationToken ); if (ignoreFile is null) { return(1); } var printerOptions = new PrinterOptions { TabWidth = configurationFileOptions.TabWidth, UseTabs = configurationFileOptions.UseTabs, Width = configurationFileOptions.PrintWidth, EndOfLine = configurationFileOptions.EndOfLine }; var commandLineFormatter = new CommandLineFormatter( baseDirectoryPath, normalizedPath, commandLineOptions, printerOptions, fileSystem, console, ignoreFile, result ); await commandLineFormatter.FormatFiles(cancellationToken); } result.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds; ResultPrinter.PrintResults(result, console, commandLineOptions); return(ReturnExitCode(commandLineOptions, result)); }
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." }); } }
public CSharpierResult Format(string code, PrinterOptions printerOptions) { return(this.FormatAsync(code, printerOptions, CancellationToken.None).Result); }