@@ -774,18 +774,33 @@ class buffer_unique_ostream : public raw_svector_ostream {
774
774
// you can use
775
775
// OS << indent(6) << "more stuff";
776
776
// 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.
777
781
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
+ }
785
800
};
786
801
787
802
inline raw_ostream &operator <<(raw_ostream &OS, const indent &Indent) {
788
- return OS.indent (Indent.NumSpaces );
803
+ return OS.indent (Indent.NumIndents * Indent. Scale );
789
804
}
790
805
791
806
class Error ;
0 commit comments