[clang] [llvm] [Support] Add scaling support in `indent` (PR #109478)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 23 13:54:02 PDT 2024
================
@@ -774,18 +774,27 @@ class buffer_unique_ostream : public raw_svector_ostream {
// you can use
// OS << indent(6) << "more stuff";
// which has better ergonomics (and clang-formats better as well).
+//
+// If indentation is always in increments of a fixed value, you can use Scale
+// to set that value once. So indent(1, 2) will add 2 spaces and
+// indent(1,2) + 1 will add 4 spaces.
struct indent {
- unsigned NumSpaces;
-
- explicit indent(unsigned NumSpaces) : NumSpaces(NumSpaces) {}
- void operator+=(unsigned N) { NumSpaces += N; }
- void operator-=(unsigned N) { NumSpaces -= N; }
- indent operator+(unsigned N) const { return indent(NumSpaces + N); }
- indent operator-(unsigned N) const { return indent(NumSpaces - N); }
+ // Indentation is represented as `NumIndents` steps of size `Scale` each.
+ unsigned NumIndents;
+ unsigned Scale;
+
+ explicit indent(unsigned NumIndents, unsigned Scale = 1)
+ : NumIndents(NumIndents), Scale(Scale) {}
+
+ // These arithmeric operators preserve scale.
+ void operator+=(unsigned N) { NumIndents += N; }
+ void operator-=(unsigned N) { NumIndents -= N; }
+ indent operator+(unsigned N) const { return indent(NumIndents + N, Scale); }
+ indent operator-(unsigned N) const { return indent(NumIndents - N, Scale); }
----------------
jurahul wrote:
I'll add an assert for the underflow case
https://github.com/llvm/llvm-project/pull/109478
More information about the llvm-commits
mailing list