From 1b15ae6e0594ad25147574495fd67151146981b4 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Mon, 20 Apr 2015 09:59:23 -0700 Subject: [PATCH] Support end_of_line option. #16 --- src/dfmt/formatter.d | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/dfmt/formatter.d b/src/dfmt/formatter.d index 37ff0d8..d351285 100644 --- a/src/dfmt/formatter.d +++ b/src/dfmt/formatter.d @@ -298,7 +298,7 @@ private: } else if ((t == tok!"import" && !currentIs(tok!"import") && !currentIs(tok!"}"))) { - write("\n"); + simpleNewline(); currentLineLength = 0; justAddedExtraNewline = true; newline(); @@ -581,7 +581,7 @@ private: && astInformation.doubleNewlineLocations.canFindIndex(tokens[index].index) && !peekIs(tok!"}") && !peekIs(tok!";")) { - write("\n"); + simpleNewline(); currentLineLength = 0; justAddedExtraNewline = true; } @@ -926,10 +926,24 @@ private: regenLineBreakHints(i); } + void simpleNewline() + { + import dfmt.editorconfig : EOL; + + final switch (config.end_of_line) + { + case EOL.cr: output.put("\r"); break; + case EOL.lf: output.put("\n"); break; + case EOL.crlf: output.put("\r\n"); break; + case EOL.unspecified: assert(false, "config.end_of_line was unspecified"); + } + } + void newline() { import std.range : assumeSorted; import std.algorithm : max; + import dfmt.editorconfig : OptionalBoolean; if (currentIs(tok!"comment") && index > 0 && current.line == tokenEndLine(tokens[index - 1])) return; @@ -944,12 +958,12 @@ private: return; } - output.put("\n"); + simpleNewline(); if (!justAddedExtraNewline && index > 0 && hasCurrent && tokens[index].line - tokenEndLine(tokens[index - 1]) > 1) { - output.put("\n"); + simpleNewline(); } justAddedExtraNewline = false;