[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 1 20:23:57 PST 2026


================
@@ -3769,7 +3774,20 @@ tooling::Replacements sortJavaImports(const FormatStyle &Style, StringRef Code,
     else if (isClangFormatOn(Trimmed))
       FormattingOff = false;
 
----------------
owenca wrote:

Handle `Trimmed.empty()` at the top of the chained conditional.
```suggestion
    } else if (Trimmed.starts_with("//")) {
      // Associating comments within the imports with the nearest import below
      AssociatedCommentLines.push_back(Line);
    } else if (Trimmed.starts_with("/*")) {
      ... // Extend `Line` for a multiline comment to include all lines the comment spans.
      AssociatedCommentLines.push_back(Line);
    } else if (...) { // Package statement
      ...
    } else if (ImportRegex.match(Trimmed, &Matches)) {
      // Lines 3773-3785 go here.
    } else { // `Trimmed` is neither empty, nor a comment or a package/import statement.
      break;
    }
```
And it seems that we don't need `JavaTypeDeclRegexPattern`.

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


More information about the cfe-commits mailing list