|
1 | 1 | # clang-format |
2 | | -Using Clang-Format in Golang |
3 | 2 |
|
4 | | -## proto-format |
5 | | -Format Protobuf File Using Clang-Format in Golang |
| 3 | +Go wrapper for clang-format with Protocol Buffers formatting capabilities. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## CHINESE README |
| 8 | + |
| 9 | +[中文说明](README.zh.md) |
| 10 | + |
| 11 | +## Key Features |
| 12 | + |
| 13 | +🎯 **Intelligent Proto Formatting**: Smart clang-format wrapper with Google style defaults |
| 14 | +⚡ **Dual Operation Modes**: Both preview (DryRun) and in-place formatting support |
| 15 | +🔄 **Batch Processing**: Recursive project-wide .proto file formatting |
| 16 | +🌍 **Configurable Styles**: Customizable formatting styles with JSON configuration |
| 17 | +📋 **Comprehensive Logging**: Detailed operation logs with structured output |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +```bash |
| 22 | +go install github.com/go-xlan/clang-format@latest |
| 23 | +``` |
| 24 | + |
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +Install clang-format on your system: |
| 28 | + |
| 29 | +```bash |
| 30 | +# macOS |
| 31 | +brew install clang-format |
| 32 | + |
| 33 | +# Ubuntu/Debian |
| 34 | +sudo apt-get install clang-format |
| 35 | + |
| 36 | +# Verify installation |
| 37 | +clang-format --version |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Protocol Buffers Formatting (Primary Use Case) |
| 43 | + |
| 44 | +```go |
| 45 | +package main |
| 46 | + |
| 47 | +import ( |
| 48 | + "fmt" |
| 49 | + "github.com/go-xlan/clang-format/protoformat" |
| 50 | + "github.com/yyle88/must" |
| 51 | + "github.com/yyle88/osexec" |
| 52 | + "github.com/yyle88/rese" |
| 53 | +) |
| 54 | + |
| 55 | +func main() { |
| 56 | + execConfig := osexec.NewExecConfig() |
| 57 | + style := protoformat.NewStyle() |
| 58 | + |
| 59 | + // Preview .proto file formatting (DryRun) |
| 60 | + output := rese.V1(protoformat.DryRun(execConfig, "example.proto", style)) |
| 61 | + fmt.Println(string(output)) |
| 62 | + |
| 63 | + // Format single .proto file |
| 64 | + rese.V1(protoformat.Format(execConfig, "example.proto", style)) |
| 65 | + |
| 66 | + // Format entire project (batch processing) |
| 67 | + must.Done(protoformat.FormatProject(execConfig, "./proto-project", style)) |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Custom Style Configuration |
| 72 | + |
| 73 | +```go |
| 74 | +customStyle := &clangformat.Style{ |
| 75 | + BasedOnStyle: "LLVM", |
| 76 | + IndentWidth: 4, |
| 77 | + ColumnLimit: 80, |
| 78 | + AlignConsecutiveAssignments: true, |
| 79 | +} |
| 80 | + |
| 81 | +output := rese.V1(protoformat.DryRun(execConfig, "example.proto", customStyle)) |
| 82 | +``` |
| 83 | + |
| 84 | +### General File Formatting (C/C++ Support) |
| 85 | + |
| 86 | +```go |
| 87 | +import "github.com/go-xlan/clang-format/clangformat" |
| 88 | + |
| 89 | +// Format C/C++ files |
| 90 | +output := rese.V1(clangformat.DryRun(execConfig, "example.cpp", style)) |
| 91 | +must.Done(clangformat.Format(execConfig, "example.cpp", style)) |
| 92 | +``` |
| 93 | + |
| 94 | +## API Reference |
| 95 | + |
| 96 | +### clangformat Package |
| 97 | + |
| 98 | +- `NewStyle()` - Creates default Google-based style configuration |
| 99 | +- `DryRun(config, path, style)` - Preview formatting without file modification |
| 100 | +- `Format(config, path, style)` - Apply formatting directly to file |
| 101 | + |
| 102 | +### protoformat Package |
| 103 | + |
| 104 | +- `NewStyle()` - Creates Protocol Buffers optimized style configuration |
| 105 | +- `DryRun(config, path, style)` - Preview .proto file formatting |
| 106 | +- `Format(config, path, style)` - Format single .proto file |
| 107 | +- `FormatProject(config, path, style)` - Batch format all .proto files in project |
| 108 | + |
| 109 | +### Style Configuration |
| 110 | + |
| 111 | +```go |
| 112 | +type Style struct { |
| 113 | + BasedOnStyle string // "Google", "LLVM", "Chromium", etc. |
| 114 | + IndentWidth int // Number of spaces for indentation |
| 115 | + ColumnLimit int // Maximum line length (0 = no limit) |
| 116 | + AlignConsecutiveAssignments bool // Align assignments at equal signs |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +<!-- TEMPLATE (EN) BEGIN: STANDARD PROJECT FOOTER --> |
| 121 | + |
| 122 | +## 📄 License |
| 123 | + |
| 124 | +MIT License. See [LICENSE](LICENSE). |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## 🤝 Contributing |
| 129 | + |
| 130 | +Contributions are welcome! Report bugs, suggest features, and contribute code: |
| 131 | + |
| 132 | +- 🐛 **Found a bug?** Open an issue on GitHub with reproduction steps |
| 133 | +- 💡 **Have a feature idea?** Create an issue to discuss the suggestion |
| 134 | +- 📖 **Documentation confusing?** Report it so we can improve |
| 135 | +- 🚀 **Need new features?** Share your use cases to help us understand requirements |
| 136 | +- ⚡ **Performance issue?** Help us optimize by reporting slow operations |
| 137 | +- 🔧 **Configuration problem?** Ask questions about complex setups |
| 138 | +- 📢 **Follow project progress?** Watch the repo for new releases and features |
| 139 | +- 🌟 **Success stories?** Share how this package improved your workflow |
| 140 | +- 💬 **General feedback?** All suggestions and comments are welcome |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## 🔧 Development |
| 145 | + |
| 146 | +New code contributions, follow this process: |
| 147 | + |
| 148 | +1. **Fork**: Fork the repo on GitHub (using the webpage interface). |
| 149 | +2. **Clone**: Clone the forked project (`git clone https://github.com/yourname/repo-name.git`). |
| 150 | +3. **Navigate**: Navigate to the cloned project (`cd repo-name`) |
| 151 | +4. **Branch**: Create a feature branch (`git checkout -b feature/xxx`). |
| 152 | +5. **Code**: Implement your changes with comprehensive tests |
| 153 | +6. **Testing**: (Golang project) Ensure tests pass (`go test ./...`) and follow Go code style conventions |
| 154 | +7. **Documentation**: Update documentation for user-facing changes and use meaningful commit messages |
| 155 | +8. **Stage**: Stage changes (`git add .`) |
| 156 | +9. **Commit**: Commit changes (`git commit -m "Add feature xxx"`) ensuring backward compatible code |
| 157 | +10. **Push**: Push to the branch (`git push origin feature/xxx`). |
| 158 | +11. **PR**: Open a pull request on GitHub (on the GitHub webpage) with detailed description. |
| 159 | + |
| 160 | +Please ensure tests pass and include relevant documentation updates. |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## 🌟 Support |
| 165 | + |
| 166 | +Welcome to contribute to this project by submitting pull requests and reporting issues. |
| 167 | + |
| 168 | +**Project Support:** |
| 169 | + |
| 170 | +- ⭐ **Give GitHub stars** if this project helps you |
| 171 | +- 🤝 **Share with teammates** and (golang) programming friends |
| 172 | +- 📝 **Write tech blogs** about development tools and workflows - we provide content writing support |
| 173 | +- 🌟 **Join the ecosystem** - committed to supporting open source and the (golang) development scene |
| 174 | + |
| 175 | +**Happy Coding with this package!** 🎉 |
| 176 | + |
| 177 | +<!-- TEMPLATE (EN) END: STANDARD PROJECT FOOTER --> |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## GitHub Stars |
| 182 | + |
| 183 | +[](https://starchart.cc/go-xlan/clang-format) |
0 commit comments