Skip to content

Commit 6bed79b

Browse files
authored
[Support] Add scaling support in indent (#109478)
Scaled indent is useful when indentation is always in steps of a fixed number (the Scale) and still allow using the +/- operators to adjust indentation.
1 parent fd11c81 commit 6bed79b

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,18 +774,33 @@ class buffer_unique_ostream : public raw_svector_ostream {
774774
// you can use
775775
// OS << indent(6) << "more stuff";
776776
// which has better ergonomics (and clang-formats better as well).
777+
//
778+
// If indentation is always in increments of a fixed value, you can use Scale
779+
// to set that value once. So indent(1, 2) will add 2 spaces and
780+
// indent(1,2) + 1 will add 4 spaces.
777781
struct indent {
778-
unsigned NumSpaces;
779-
780-
explicit indent(unsigned NumSpaces) : NumSpaces(NumSpaces) {}
781-
void operator+=(unsigned N) { NumSpaces += N; }
782-
void operator-=(unsigned N) { NumSpaces -= N; }
783-
indent operator+(unsigned N) const { return indent(NumSpaces + N); }
784-
indent operator-(unsigned N) const { return indent(NumSpaces - N); }
782+
// Indentation is represented as `NumIndents` steps of size `Scale` each.
783+
unsigned NumIndents;
784+
unsigned Scale;
785+
786+
explicit indent(unsigned NumIndents, unsigned Scale = 1)
787+
: NumIndents(NumIndents), Scale(Scale) {}
788+
789+
// These arithmeric operators preserve scale.
790+
void operator+=(unsigned N) { NumIndents += N; }
791+
void operator-=(unsigned N) {
792+
assert(NumIndents >= N && "Indentation underflow");
793+
NumIndents -= N;
794+
}
795+
indent operator+(unsigned N) const { return indent(NumIndents + N, Scale); }
796+
indent operator-(unsigned N) const {
797+
assert(NumIndents >= N && "Indentation undeflow");
798+
return indent(NumIndents - N, Scale);
799+
}
785800
};
786801

787802
inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
788-
return OS.indent(Indent.NumSpaces);
803+
return OS.indent(Indent.NumIndents * Indent.Scale);
789804
}
790805

791806
class Error;

llvm/unittests/Support/raw_ostream_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ TEST(raw_ostreamTest, Indent) {
188188
EXPECT_EQ(Spaces(5), printToString(Indent));
189189
Indent -= 1;
190190
EXPECT_EQ(Spaces(4), printToString(Indent));
191+
192+
// Scaled indent.
193+
indent Scaled(4, 2);
194+
EXPECT_EQ(Spaces(8), printToString(Scaled));
195+
EXPECT_EQ(Spaces(10), printToString(Scaled + 1));
196+
EXPECT_EQ(Spaces(6), printToString(Scaled - 1));
197+
Scaled += 1;
198+
EXPECT_EQ(Spaces(10), printToString(Scaled));
199+
Scaled -= 1;
200+
EXPECT_EQ(Spaces(8), printToString(Scaled));
191201
}
192202

193203
TEST(raw_ostreamTest, FormatHex) {

0 commit comments

Comments
 (0)