================
@@ -844,101 +870,18 @@ void 
WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) {
   const bool AlignArrowOrColon =
       IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons;
 
-  auto Matches = [&](const Change &C) {
-    if (AlignArrowOrColon)
-      return C.Tok->is(Type);
-
-    // Ignore 'IsInsideToken' to allow matching trailing comments which
-    // need to be reflowed as that causes the token to appear in two
-    // different changes, which will cause incorrect alignment as we'll
-    // reflow early due to detecting multiple aligning tokens per line.
-    return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type);
-  };
+  FormatStyle::AlignConsecutiveStyle AlignStyle{};
+  AlignStyle.AcrossComments = Option.AcrossComments;
+  AlignStyle.AcrossEmptyLines = Option.AcrossEmptyLines;
 
-  unsigned MinColumn = 0;
-
-  // Empty case statements don't break the alignment, but don't necessarily
-  // match our predicate, so we need to track their column so they can push out
-  // our alignment.
-  unsigned MinEmptyCaseColumn = 0;
-
-  // Start and end of the token sequence we're processing.
-  unsigned StartOfSequence = 0;
-  unsigned EndOfSequence = 0;
-
-  // Whether a matching token has been found on the current line.
-  bool FoundMatchOnLine = false;
-
-  bool LineIsComment = true;
-  bool LineIsEmptyCase = false;
-
-  unsigned I = 0;
-  for (unsigned E = Changes.size(); I != E; ++I) {
-    if (Changes[I].NewlinesBefore != 0) {
-      // Whether to break the alignment sequence because of an empty line.
-      bool EmptyLineBreak =
-          (Changes[I].NewlinesBefore > 1) &&
-          !Style.AlignConsecutiveShortCaseStatements.AcrossEmptyLines;
-
-      // Whether to break the alignment sequence because of a line without a
-      // match.
-      bool NoMatchBreak =
-          !FoundMatchOnLine &&
-          !(LineIsComment &&
-            Style.AlignConsecutiveShortCaseStatements.AcrossComments) &&
-          !LineIsEmptyCase;
-
-      if (EmptyLineBreak || NoMatchBreak) {
-        AlignMatchingTokenSequence(StartOfSequence, EndOfSequence, MinColumn,
-                                   Matches, Changes);
-        MinEmptyCaseColumn = 0;
-      }
-
-      // A new line starts, re-initialize line status tracking bools.
-      FoundMatchOnLine = false;
-      LineIsComment = true;
-      LineIsEmptyCase = false;
-    }
-
-    if (Changes[I].Tok->isNot(tok::comment))
-      LineIsComment = false;
-
-    if (Changes[I].Tok->is(Type)) {
-      LineIsEmptyCase =
-          !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment();
-
-      if (LineIsEmptyCase) {
-        if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) {
-          MinEmptyCaseColumn =
-              std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn);
-        } else {
-          MinEmptyCaseColumn =
-              std::max(MinEmptyCaseColumn, Changes[I].StartOfTokenColumn + 2);
-        }
-      }
-    }
-
-    if (!Matches(Changes[I]))
-      continue;
-
-    if (LineIsEmptyCase)
-      continue;
-
-    FoundMatchOnLine = true;
-
-    if (StartOfSequence == 0)
-      StartOfSequence = I;
-
-    EndOfSequence = I + 1;
-
-    MinColumn = std::max(MinColumn, Changes[I].StartOfTokenColumn);
-
-    // Allow empty case statements to push out our alignment.
-    MinColumn = std::max(MinColumn, MinEmptyCaseColumn);
+  auto Matches = [Type](const Change &C) { return C.Tok->is(Type); };
+  if (AlignArrowOrColon) {
+    AlignTokens<decltype(Matches) &, AlignStrategy::CaseColon>(
+        Style, Matches, Changes, /*StartAt=*/0, AlignStyle);
+  } else {
+    AlignTokens<decltype(Matches) &, AlignStrategy::CaseBody>(
+        Style, Matches, Changes, /*StartAt=*/0, AlignStyle);
   }
----------------
owenca wrote:

```suggestion
  AlignTokens(Style, Matches, Changes, /*StartAt=*/0, AlignStyle,
              /*RightJustify=*/false,
              AlignArrowOrColon ? AlignStrategy::CaseColon
                                : AlignStrategy::CaseBody);
```

https://github.com/llvm/llvm-project/pull/187811
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to