[clang] [clang-format] java import sorting should ignore imports in comments and text blocks (PR #177326)

via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 8 00:22:34 PST 2026


================
@@ -3767,12 +3767,26 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
         Code.substr(Prev, (Pos != StringRef::npos ? Pos : Code.size()) - Prev);
 
     StringRef Trimmed = Line.trim();
-    if (isClangFormatOff(Trimmed))
+    if (Trimmed.empty()) {
+      // Skip empty lines.
+    } else if (isClangFormatOff(Trimmed)) {
       FormattingOff = true;
-    else if (isClangFormatOn(Trimmed))
+    } else if (isClangFormatOn(Trimmed)) {
       FormattingOff = false;
-
-    if (ImportRegex.match(Line, &Matches)) {
+    } else if (Trimmed.starts_with("//")) {
+      if (!ImportsInBlock.empty())
----------------
owenca wrote:

```suggestion
      // Associating comments within the imports with the nearest import below.
      if (HasImport)
```
Keep the original comment.

https://github.com/llvm/llvm-project/pull/177326


More information about the cfe-commits mailing list