Skip to content

Commit 9ed3d97

Browse files
committed
Strengthen clang-format with Git validation and comprehensive documentation
- Add Git repo validation in CLI - Add comprehensive English/Chinese comments - Improve error handling with detailed messages - Update README with detailed usage examples and important notes - Update dependencies in go.mod and go.sum
1 parent b130f82 commit 9ed3d97

File tree

13 files changed

+650
-187
lines changed

13 files changed

+650
-187
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
runs-on: ubuntu-latest
2828
strategy:
2929
matrix:
30-
go: [ "1.22.x", "1.23.x", "1.24.x", "stable" ]
30+
go: [ "1.22.x", "1.23.x", "1.24.x", "1.25.x", "stable" ]
3131
steps:
3232
- uses: actions/checkout@v4
3333

README.md

Lines changed: 146 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/go-xlan/clang-format/release.yml?branch=main&label=BUILD)](https://github.com/go-xlan/clang-format/actions/workflows/release.yml?query=branch%3Amain)
22
[![GoDoc](https://pkg.go.dev/badge/github.com/go-xlan/clang-format)](https://pkg.go.dev/github.com/go-xlan/clang-format)
33
[![Coverage Status](https://img.shields.io/coveralls/github/go-xlan/clang-format/main.svg)](https://coveralls.io/github/go-xlan/clang-format?branch=main)
4-
[![Supported Go Versions](https://img.shields.io/badge/Go-1.22-lightgrey.svg)](https://go.dev/)
4+
[![Supported Go Versions](https://img.shields.io/badge/Go-1.22--1.25-lightgrey.svg)](https://github.com/go-xlan/clang-format)
55
[![GitHub Release](https://img.shields.io/github/release/go-xlan/clang-format.svg)](https://github.com/go-xlan/clang-format/releases)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-xlan/clang-format)](https://goreportcard.com/report/github.com/go-xlan/clang-format)
77

88
# clang-format
99

10-
Go package for clang-format with Protocol Buffers and C/C++ batch formatting capabilities.
10+
Go package supporting clang-format with Protocol Buffers and C/C++ batch formatting capabilities.
1111

1212
---
1313

@@ -17,12 +17,12 @@ Go package for clang-format with Protocol Buffers and C/C++ batch formatting cap
1717
[中文说明](README.zh.md)
1818
<!-- TEMPLATE (EN) END: LANGUAGE NAVIGATION -->
1919

20-
## Key Features
20+
## Main Features
2121

22-
🎯 **Intelligent Proto Formatting**: Smart clang-format package with Google style defaults
23-
**Two Operation Modes**: Both preview (DryRun) and in-place formatting support
24-
🔄 **Batch Processing**: Recursive project-wide .proto file formatting
25-
🌍 **Configurable Styles**: Customizable formatting styles with JSON configuration
22+
🎯 **Smart Proto Formatting**: Intelligent clang-format package with Google style defaults
23+
**Two Operation Modes**: Both preview (DryRun) and in-place formatting support
24+
🔄 **Batch Processing**: Project-wide .proto file formatting using recursive approach
25+
🌍 **Configurable Styles**: Customizable formatting with JSON configuration
2626
📋 **Comprehensive Logging**: Detailed operation logs with structured output
2727

2828
## Installation
@@ -50,16 +50,22 @@ brew install clang-format
5050
# Ubuntu/Debian
5151
sudo apt-get install clang-format
5252

53-
# Verify installation
53+
# Check setup
5454
clang-format --version
5555
```
5656

5757
## Quick Start
5858

5959
### Command Line Usage
6060

61+
The `clang-format-batch` command formats files in Git projects. It requires:
62+
- Running from within a Git repo
63+
- Valid file extensions specified via `--extensions` flag
64+
65+
**Basic Examples:**
66+
6167
```bash
62-
# Format all .proto files in current project
68+
# Format .proto files in current project
6369
clang-format-batch --extensions=".proto"
6470

6571
# Format C/C++ files
@@ -72,9 +78,23 @@ clang-format-batch --extensions=".proto,.c,.cpp,.h"
7278
clang-format-batch -e ".proto,.cc,.hh"
7379
```
7480

75-
## Library Usage
81+
**Supported File Extensions:**
82+
- `.proto` - Protocol Buffer files
83+
- `.c`, `.cpp`, `.cxx`, `.cc` - C/C++ source files
84+
- `.h`, `.hpp`, `.hxx` - C/C++ headers
85+
86+
**Important Notes:**
87+
- Command must be run from within a Git repo
88+
- Command processes matching files in project using recursive approach
89+
- Hidden files and paths (starting with `.`) are skipped
90+
- Source files are modified in-place (use version management!)
91+
92+
93+
## Package Usage
7694

77-
### Protocol Buffers Formatting (Primary Use Case)
95+
### Protocol Buffers Formatting (Main Use Case)
96+
97+
The `protoformat` package provides high-end API when formatting Protocol Buffer files:
7898

7999
```go
80100
package main
@@ -88,72 +108,162 @@ import (
88108
)
89109

90110
func main() {
111+
// Create execution configuration
91112
execConfig := osexec.NewExecConfig()
113+
114+
// Create default Proto style (Google-based, 2-space indent)
92115
style := protoformat.NewStyle()
93-
116+
94117
// Preview .proto file formatting (DryRun)
118+
// Returns formatted content without modifying the file
95119
output := rese.V1(protoformat.DryRun(execConfig, "example.proto", style))
96120
fmt.Println(string(output))
97-
98-
// Format single .proto file
99-
rese.V1(protoformat.Format(execConfig, "example.proto", style))
100-
121+
122+
// Format single .proto file in-place
123+
// Modifies the file with formatted content
124+
output = rese.V1(protoformat.Format(execConfig, "example.proto", style))
125+
101126
// Format entire project (batch processing)
127+
// Formats .proto files in DIR using recursive approach
102128
must.Done(protoformat.FormatProject(execConfig, "./proto-project", style))
103129
}
104130
```
105131

132+
**Main Points:**
133+
- `DryRun()` - Preview changes without modifying files (safe to use)
134+
- `Format()` - Commit changes to single file (modifies file in-place)
135+
- `FormatProject()` - Batch process .proto files in DIR
136+
- Uses Google Proto style as default (2-space indentation, no width limit)
137+
138+
106139
### Custom Style Configuration
107140

141+
You can customize formatting to match the project's coding standards:
142+
108143
```go
144+
// Create custom style based on LLVM conventions
109145
customStyle := &clangformat.Style{
110-
BasedOnStyle: "LLVM",
111-
IndentWidth: 4,
112-
ColumnLimit: 80,
113-
AlignConsecutiveAssignments: true,
146+
BasedOnStyle: "LLVM", // Base style template
147+
IndentWidth: 4, // 4 spaces at each indent
148+
ColumnLimit: 80, // Maximum line width
149+
AlignConsecutiveAssignments: true, // Align = signs in columns
114150
}
115151

152+
// Use custom style when formatting
116153
output := rese.V1(protoformat.DryRun(execConfig, "example.proto", customStyle))
117154
```
118155

119-
### General File Formatting (C/C++ Support)
156+
**Available Base Styles:**
157+
- `"Google"` - Google C++ Style Guide (default)
158+
- `"LLVM"` - LLVM coding standards
159+
- `"Chromium"` - Chromium project style
160+
- `"Mozilla"` - Mozilla style guide
161+
- `"WebKit"` - WebKit style guide
162+
163+
**Common Style Options:**
164+
- `IndentWidth` - Count of spaces when indenting (common: 2 and 4)
165+
- `ColumnLimit` - Maximum line width (0 = no limit)
166+
- `AlignConsecutiveAssignments` - Align assignment operators in columns
167+
168+
169+
### Wide File Formatting (C/C++ Support)
170+
171+
The `clangformat` package provides foundation API when formatting clang-format supported files:
120172

121173
```go
122174
import "github.com/go-xlan/clang-format/clangformat"
123175

124-
// Format C/C++ files
176+
// Create execution config and style
177+
execConfig := osexec.NewExecConfig()
178+
style := clangformat.NewStyle()
179+
180+
// Preview C++ file formatting
125181
output := rese.V1(clangformat.DryRun(execConfig, "example.cpp", style))
126-
must.Done(clangformat.Format(execConfig, "example.cpp", style))
182+
183+
// Format C++ file in-place
184+
output = rese.V1(clangformat.Format(execConfig, "example.cpp", style))
185+
186+
// Batch format .cpp files in project
187+
must.Done(clangformat.FormatProject(execConfig, "./src", ".cpp", style))
188+
```
189+
190+
**Note:** Unlike `protoformat.FormatProject()`, the `clangformat.FormatProject()` needs
191+
a single file extension. To format multiple kinds, make multiple invocations:
192+
193+
```go
194+
// Format both .cpp and .h files
195+
must.Done(clangformat.FormatProject(execConfig, "./src", ".cpp", style))
196+
must.Done(clangformat.FormatProject(execConfig, "./src", ".h", style))
127197
```
128198

199+
129200
## API Reference
130201

131202
### clangformat Package
132203

133-
- `NewStyle()` - Creates default Google-based style configuration
134-
- `DryRun(config, path, style)` - Preview formatting without file modification
135-
- `Format(config, path, style)` - Use formatting on file
204+
**Core Functions:**
205+
206+
- `NewStyle() *Style`
207+
- Creates default Google-based style configuration
208+
- Returns style with 2-space indentation and no width limit
209+
- Safe to change the data structure when customizing
210+
211+
- `DryRun(config *osexec.ExecConfig, path string, style *Style) ([]byte, error)`
212+
- Preview formatting without file modification
213+
- Returns formatted content as byte sequence
214+
- Source file stays unchanged
215+
- Use this when validating changes before commit
216+
217+
- `Format(config *osexec.ExecConfig, path string, style *Style) ([]byte, error)`
218+
- Commit formatting changes to file in-place
219+
- Modifies the file on disk
220+
- Returns zero-length byte sequence on success (clang-format action)
221+
- **Warning:** Source file is overwritten
222+
223+
- `FormatProject(config *osexec.ExecConfig, projectPath string, extension string, style *Style) error`
224+
- Batch format files with specific extension in project
225+
- Walks through DIR tree using recursive approach
226+
- Skips hidden files and paths (starting with `.`)
227+
- Processes one extension at a time
136228

137229
### protoformat Package
138230

139-
- `NewStyle()` - Creates Protocol Buffers optimized style configuration
140-
- `DryRun(config, path, style)` - Preview .proto file formatting
141-
- `Format(config, path, style)` - Format single .proto file
142-
- `FormatProject(config, path, style)` - Batch format all .proto files in project
231+
**Core Functions:**
232+
233+
- `NewStyle() *clangformat.Style`
234+
- Creates Protocol Buffers optimized style configuration
235+
- Same as `clangformat.NewStyle()` but provides Proto-specific context
236+
- Returns Google-based style tuned when formatting .proto files
237+
238+
- `DryRun(config *osexec.ExecConfig, protoPath string, style *clangformat.Style) ([]byte, error)`
239+
- Preview .proto file formatting without modification
240+
- Wraps `clangformat.DryRun()` with proto-specific context
241+
- Returns formatted Proto content
242+
243+
- `Format(config *osexec.ExecConfig, protoPath string, style *clangformat.Style) ([]byte, error)`
244+
- Commit formatting changes to single .proto file in-place
245+
- Wraps `clangformat.Format()` with proto-specific context
246+
- Modifies file on disk
247+
248+
- `FormatProject(config *osexec.ExecConfig, projectPath string, style *clangformat.Style) error`
249+
- Batch format .proto files in project
250+
- Targets `.proto` extension as default
251+
- Shows green success message on completion
252+
- Provides detailed logging when processing each file
143253

144254
### Style Configuration
145255

146256
```go
147257
type Style struct {
148258
BasedOnStyle string // "Google", "LLVM", "Chromium", etc.
149-
IndentWidth int // Count of spaces for indentation
150-
ColumnLimit int // Maximum line length (0 = no limit)
259+
IndentWidth int // Count of spaces when indenting
260+
ColumnLimit int // Maximum line width (0 = no limit)
151261
AlignConsecutiveAssignments bool // Align assignments at assignment signs
152262
}
153263
```
154264

155265
<!-- TEMPLATE (EN) BEGIN: STANDARD PROJECT FOOTER -->
156-
<!-- VERSION 2025-09-06 04:53:24.895249 +0000 UTC -->
266+
<!-- VERSION 2025-09-26 07:39:27.188023 +0000 UTC -->
157267

158268
## 📄 License
159269

@@ -165,7 +275,7 @@ MIT License. See [LICENSE](LICENSE).
165275

166276
Contributions are welcome! Report bugs, suggest features, and contribute code:
167277

168-
- 🐛 **Found a bug?** Open an issue on GitHub with reproduction steps
278+
- 🐛 **Found a mistake?** Open an issue on GitHub with reproduction steps
169279
- 💡 **Have a feature idea?** Create an issue to discuss the suggestion
170280
- 📖 **Documentation confusing?** Report it so we can improve
171281
- 🚀 **Need new features?** Share the use cases to help us understand requirements
@@ -191,7 +301,7 @@ New code contributions, follow this process:
191301
8. **Stage**: Stage changes (`git add .`)
192302
9. **Commit**: Commit changes (`git commit -m "Add feature xxx"`) ensuring backward compatible code
193303
10. **Push**: Push to the branch (`git push origin feature/xxx`).
194-
11. **PR**: Open a pull request on GitHub (on the GitHub webpage) with detailed description.
304+
11. **PR**: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
195305

196306
Please ensure tests pass and include relevant documentation updates.
197307

@@ -208,7 +318,7 @@ Welcome to contribute to this project via submitting merge requests and reportin
208318
- 📝 **Write tech blogs** about development tools and workflows - we provide content writing support
209319
- 🌟 **Join the ecosystem** - committed to supporting open source and the (golang) development scene
210320

211-
**Have Fun Coding with this package!** 🎉
321+
**Have Fun Coding with this package!** 🎉🎉🎉
212322

213323
<!-- TEMPLATE (EN) END: STANDARD PROJECT FOOTER -->
214324

0 commit comments

Comments
 (0)