Files
2025-11-29 18:49:58 +08:00

8.2 KiB

Language-Specific Options

← Prev: Includes | Back to Index | Next: Comments →

Navigation: Alignment | Breaking | Braces | Indentation | Spacing | Includes | Languages | Comments | Advanced

Options that apply to specific programming languages.

Language Selection

Language

Specify the language for formatting.

Type: LanguageKind Values:

  • None - Do not use
  • C - C
  • Cpp - C++
  • CSharp - C#
  • Java - Java
  • JavaScript - JavaScript
  • Json - JSON
  • ObjC - Objective-C, Objective-C++
  • Proto - Protocol Buffers
  • TableGen - LLVM TableGen
  • TextProto - Text format Protocol Buffers
  • Verilog - Verilog/SystemVerilog

Note: For .h files, specify the language explicitly with a comment:

// clang-format Language: Cpp

Usage:

In .clang-format:

---
Language: Cpp
# C++ options
---
Language: JavaScript
# JavaScript options
---

JavaScript/TypeScript

JavaScriptQuotes

Quote style for JavaScript strings.

Type: JavaScriptQuoteStyle Values:

  • Leave - Keep existing quotes
  • Single - Use single quotes
  • Double - Use double quotes

Example:

Single:

import { a } from "foo";
let x = "hello";

Double:

import { a } from "foo";
let x = "hello";

JavaScriptWrapImports

Wrap ES6 import/export statements.

Type: Boolean

Example:

true:

import { VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying } from "some/module.js";

false:

import { VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying } from "some/module.js";

BreakArrays

Break after JSON array opening bracket [.

Type: Boolean

Note: This is currently only for formatting JSON. When true, clang-format will always break after a JSON array [. When false, it will scan until the closing ] to determine if it should add newlines between elements (prettier compatible).

Example:

true:

[1, 2, 3, 4]

false:

[1, 2, 3, 4]

Java

JavaImportGroups

Define Java import groups.

Type: List of Strings

Example:

JavaImportGroups:
  - com.mycompany
  - com
  - org

Result:

import com.mycompany.Foo;
import com.mycompany.Bar;

import com.otherlibrary.Baz;

import org.apache.Something;

BreakAfterJavaFieldAnnotations

Break after field annotations in Java.

Type: Boolean

Example:

true:

@Annotation
private int myField;

false:

@Annotation private int myField;

SortJavaStaticImport

Control placement of static imports relative to non-static imports.

Type: SortJavaStaticImportOptions Values:

  • Before - Static imports are placed before non-static imports (default)
  • After - Static imports are placed after non-static imports

Example:

Before:

import static org.example.function1;

import org.example.ClassA;

After:

import org.example.ClassA;

import static org.example.function1;

Note: This option works in conjunction with JavaImportGroups to control Java import organization.

C

C# uses the CSharp language setting and shares most options with other C-family languages.

Protocol Buffers

Protocol buffer files use the Proto or TextProto language settings.

Example Configuration:

---
Language: Proto
BasedOnStyle: Google
IndentWidth: 2
---
Language: TextProto
BasedOnStyle: Google
---

TableGen (LLVM)

TableGen has specific alignment options (see Alignment):

  • AlignConsecutiveTableGenBreakingDAGArgColons
  • AlignConsecutiveTableGenCondOperatorColons
  • AlignConsecutiveTableGenDefinitionColons

Objective-C

ObjCBinPackProtocolList

Pack Objective-C protocol list.

Type: BinPackStyle Values:

  • Auto, Always, Never

Example:

Never:

@interface ccccccccccccc () <
  ccccccccccccc,
  ccccccccccccc,
  ccccccccccccc,
  ccccccccccccc>

Always:

@interface ccccccccccccc () <ccccccccccccc, ccccccccccccc, ccccccccccccc, ccccccccccccc>

ObjCBlockIndentWidth

Indent width for ObjC blocks.

Type: Unsigned

ObjCBreakBeforeNestedBlockParam

Break before nested block parameters.

Type: Boolean

ObjCPropertyAttributeOrder

Order of Objective-C property attributes.

Type: List of Strings

Example:

ObjCPropertyAttributeOrder:
  [
    class,
    direct,
    atomic,
    nonatomic,
    assign,
    retain,
    strong,
    copy,
    weak,
    unsafe_unretained,
    readonly,
    readwrite,
    getter,
    setter,
    nullable,
    nonnull,
    null_resettable,
    null_unspecified,
  ]

Warning: Using this option could lead to incorrect code formatting due to clang-format's lack of complete semantic information. Extra care should be taken to review code changes.

ObjCSpaceAfterProperty

Add space after @property.

Type: Boolean

Example:

true:

@property (readonly) int a;

false:

@property(readonly) int a;

ObjCSpaceBeforeProtocolList

Add space before protocol list.

Type: Boolean

Example:

true:

Foo <Protocol> *foo;

false:

Foo<Protocol> *foo;

Verilog/SystemVerilog

VerilogBreakBetweenInstancePorts

Break between instance ports.

Type: Boolean

Multi-Language Configuration

Use separate sections for different languages:

---
# C++ configuration
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 100
---
# JavaScript configuration
Language: JavaScript
BasedOnStyle: Google
IndentWidth: 2
JavaScriptQuotes: Single
JavaScriptWrapImports: true
---
# JSON configuration
Language: Json
IndentWidth: 2
BreakArrays: true
---
# Java configuration
Language: Java
BasedOnStyle: Google
JavaImportGroups:
  - com.mycompany
  - com
  - org
---

Language Detection

clang-format detects language from file extension:

  • .c → C
  • .cpp, .cc, .cxx, .h, .hpp → Cpp
  • .cs → CSharp
  • .java → Java
  • .js, .mjs, .ts → JavaScript
  • .json, .ipynb → Json
  • .m, .mm → ObjC
  • .proto → Proto
  • .td → TableGen
  • .txtpb, .textproto → TextProto
  • .sv, .v, .vh → Verilog

Note: For .h files that could be C, C++, or Objective-C, add a language comment at the top of the file to ensure correct detection.

Override with --assume-filename:

cat file.txt | clang-format --assume-filename=file.cpp

Common Patterns

JavaScript/TypeScript Project

---
Language: JavaScript
BasedOnStyle: Google
IndentWidth: 2
ColumnLimit: 100
JavaScriptQuotes: Single
JavaScriptWrapImports: true
SpacesInContainerLiterals: true
---

Java Enterprise Project

---
Language: Java
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 120
JavaImportGroups:
  - com.company.product
  - com.company
  - java
  - javax
BreakAfterJavaFieldAnnotations: true
---

Multi-Language Monorepo

---
Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
---
Language: JavaScript
BasedOnStyle: Google
IndentWidth: 2
JavaScriptQuotes: Single
---
Language: Json
IndentWidth: 2
---
Language: Proto
BasedOnStyle: Google
IndentWidth: 2
---

Tips

  1. Separate Configs: Use multiple --- sections for different languages
  2. Shared Base: Start each language with same BasedOnStyle for consistency
  3. Language Detection: Verify correct language is detected with --dump-config
  4. Import Organization: Configure Java imports and include sorting consistently
  5. Quote Style: Match JavaScript quotes to your linter/prettier settings
  6. JSON Formatting: Consider separate JSON formatter for complex configuration files

See Also


← Prev: Includes | Back to Index | Next: Comments →