Skip to content

Commit d566ba4

Browse files
committed
verible: formatting: make line terminator character configurable
Precise control of file contents is required. This makes verible handle files in `binary` mode, effectively disabling the platform-specific hooks that for example translate \n into \r\n for DOS systems. Let's introduce the line_terminator flag into BasicFormatStyle so that users can decide which line terminator they want to use. Current options are \n (LF) or \r\n (CRLF). LF is the default configuration.
1 parent 3972e0d commit d566ba4

File tree

7 files changed

+64
-7
lines changed

7 files changed

+64
-7
lines changed

verible/common/formatting/basic-format-style-init.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ ABSL_FLAG(int, over_column_limit_penalty, 100,
3737
ABSL_FLAG(int, line_break_penalty, 2,
3838
"Penalty added to solution for each introduced line break.");
3939

40+
ABSL_FLAG(verible::LineTerminatorStyle, line_terminator, verible::LineTerminatorStyle::kLF,
41+
"Line terminator");
42+
4043
namespace verible {
4144
void InitializeFromFlags(BasicFormatStyle *style) {
4245
#define STYLE_FROM_FLAG(name) style->name = absl::GetFlag(FLAGS_##name)
@@ -47,6 +50,7 @@ void InitializeFromFlags(BasicFormatStyle *style) {
4750
STYLE_FROM_FLAG(column_limit);
4851
STYLE_FROM_FLAG(over_column_limit_penalty);
4952
STYLE_FROM_FLAG(line_break_penalty);
53+
STYLE_FROM_FLAG(line_terminator);
5054

5155
#undef STYLE_FROM_FLAG
5256
}

verible/common/formatting/basic-format-style.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,34 @@ std::string AbslUnparseFlag(const IndentationStyle &mode) {
4848
return stream.str();
4949
}
5050

51+
static const verible::EnumNameMap<LineTerminatorStyle> &
52+
LineTerminatorStyleStrings() {
53+
static const verible::EnumNameMap<LineTerminatorStyle>
54+
kLineTerminatorStyleStringMap({
55+
{"CRLF", LineTerminatorStyle::kCRLF},
56+
{"LF", LineTerminatorStyle::kLF},
57+
});
58+
return kLineTerminatorStyleStringMap;
59+
}
60+
61+
std::ostream &operator<<(std::ostream &stream, LineTerminatorStyle style) {
62+
switch (style) {
63+
case LineTerminatorStyle::kLF:
64+
return stream << "\n";
65+
case LineTerminatorStyle::kCRLF:
66+
return stream << "\r\n";
67+
}
68+
__builtin_unreachable();
69+
}
70+
71+
bool AbslParseFlag(std::string_view text, LineTerminatorStyle *mode,
72+
std::string *error) {
73+
return LineTerminatorStyleStrings().Parse(text, mode, error,
74+
"LineTerminatorStyle");
75+
}
76+
77+
std::string AbslUnparseFlag(const LineTerminatorStyle &mode) {
78+
return std::string{LineTerminatorStyleStrings().EnumName(mode)};
79+
}
80+
5181
} // namespace verible

verible/common/formatting/basic-format-style.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121

2222
namespace verible {
2323

24+
25+
enum class LineTerminatorStyle {
26+
// Line Feed `\n` (UNIX Style)
27+
kLF,
28+
// Carriage return + Line Feed `\r\n` (DOS Style)
29+
kCRLF,
30+
};
31+
32+
std::ostream &operator<<(std::ostream &, LineTerminatorStyle);
33+
34+
bool AbslParseFlag(std::string_view, LineTerminatorStyle *, std::string *);
35+
36+
std::string AbslUnparseFlag(const LineTerminatorStyle &);
37+
2438
// Style configuration common to all languages.
2539
struct BasicFormatStyle {
2640
// Each indentation level adds this many spaces.
@@ -42,6 +56,9 @@ struct BasicFormatStyle {
4256
// Penalty added to solution for each introduced line break.
4357
int line_break_penalty = 2;
4458

59+
// Line terminator character sequence
60+
LineTerminatorStyle line_terminator = LineTerminatorStyle::kLF;
61+
4562
// -- Note: when adding new fields, add them in basic_format_style_init.cc
4663
};
4764

verible/verilog/formatting/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ cc_library(
215215
srcs = ["comment-controls.cc"],
216216
hdrs = ["comment-controls.h"],
217217
deps = [
218+
"//verible/common/formatting:basic-format-style",
218219
"//verible/common/strings:comment-utils",
219220
"//verible/common/strings:display-utils",
220221
"//verible/common/strings:line-column-map",

verible/verilog/formatting/comment-controls.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static size_t NewlineCount(std::string_view s) {
121121
void FormatWhitespaceWithDisabledByteRanges(
122122
std::string_view text_base, std::string_view space_text,
123123
const ByteOffsetSet &disabled_ranges, bool include_disabled_ranges,
124-
std::ostream &stream) {
124+
std::ostream &stream, verible::LineTerminatorStyle line_terminator_style) {
125125
VLOG(3) << __FUNCTION__;
126126
CHECK(verible::IsSubRange(space_text, text_base));
127127
const int start = std::distance(text_base.begin(), space_text.begin());
@@ -136,7 +136,7 @@ void FormatWhitespaceWithDisabledByteRanges(
136136
if (space_text.empty() && start != 0) {
137137
if (!disabled_ranges.Contains(start)) {
138138
VLOG(3) << "output: 1*\"\\n\" (empty space text)";
139-
stream << '\n';
139+
stream << line_terminator_style;
140140
return;
141141
}
142142
}
@@ -159,7 +159,9 @@ void FormatWhitespaceWithDisabledByteRanges(
159159
text_base.substr(range.first, range.second - range.first));
160160
const size_t newline_count = NewlineCount(enabled);
161161
VLOG(3) << "output: " << newline_count << "*\"\\n\" (formatted)";
162-
stream << verible::Spacer(newline_count, '\n');
162+
for (size_t i = 0; i < newline_count; i++) {
163+
stream << line_terminator_style;
164+
}
163165
partially_enabled = true;
164166
total_enabled_newlines += newline_count;
165167
}
@@ -177,7 +179,7 @@ void FormatWhitespaceWithDisabledByteRanges(
177179
// Print at least one newline if some subrange was format-enabled.
178180
if (partially_enabled && total_enabled_newlines == 0 && start != 0) {
179181
VLOG(3) << "output: 1*\"\\n\"";
180-
stream << '\n';
182+
stream << line_terminator_style;
181183
}
182184
}
183185

verible/verilog/formatting/comment-controls.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <ostream>
1919
#include <string_view>
2020

21+
#include "verible/common/formatting/basic-format-style.h"
2122
#include "verible/common/strings/line-column-map.h"
2223
#include "verible/common/strings/position.h" // for ByteOffsetSet, LineNumberSet
2324
#include "verible/common/text/token-stream-view.h"
@@ -48,7 +49,9 @@ verible::ByteOffsetSet EnabledLinesToDisabledByteRanges(
4849
void FormatWhitespaceWithDisabledByteRanges(
4950
std::string_view text_base, std::string_view space_text,
5051
const verible::ByteOffsetSet &disabled_ranges, bool include_disabled_ranges,
51-
std::ostream &stream);
52+
std::ostream &stream,
53+
verible::LineTerminatorStyle line_terminator_style =
54+
verible::LineTerminatorStyle::kLF);
5255

5356
} // namespace formatter
5457
} // namespace verilog

verible/verilog/formatting/formatter.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ void Formatter::Emit(bool include_disabled, std::ostream &stream) const {
993993
full_text.substr(position, front_offset - position));
994994
FormatWhitespaceWithDisabledByteRanges(full_text, leading_whitespace,
995995
disabled_ranges_, include_disabled,
996-
stream);
996+
stream, style_.line_terminator);
997997

998998
// When front of first token is format-disabled, the previous call will
999999
// already cover the space up to the front token, in which case,
@@ -1010,7 +1010,7 @@ void Formatter::Emit(bool include_disabled, std::ostream &stream) const {
10101010
const std::string_view trailing_whitespace(full_text.substr(position));
10111011
FormatWhitespaceWithDisabledByteRanges(full_text, trailing_whitespace,
10121012
disabled_ranges_, include_disabled,
1013-
stream);
1013+
stream, style_.line_terminator);
10141014
}
10151015

10161016
} // namespace formatter

0 commit comments

Comments
 (0)