Skip to content

go-xlan/clang-format

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

clang-format

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


CHINESE README

δΈ­ζ–‡θ―΄ζ˜Ž

Key Features

🎯 Intelligent Proto Formatting: Smart clang-format package with Google style defaults
⚑ Two Operation Modes: Both preview (DryRun) and in-place formatting support
πŸ”„ Batch Processing: Recursive project-wide .proto file formatting
🌍 Configurable Styles: Customizable formatting styles with JSON configuration
πŸ“‹ Comprehensive Logging: Detailed operation logs with structured output

Installation

Get the Package

go get github.com/go-xlan/clang-format@latest

Get CLI Command

go install github.com/go-xlan/clang-format/cmd/clang-format-batch@latest

Prerequisites

Setup clang-format as requirement:

# macOS
brew install clang-format

# Ubuntu/Debian
sudo apt-get install clang-format

# Verify installation
clang-format --version

Quick Start

Command Line Usage

# Format all .proto files in current project
clang-format-batch --extensions=".proto"

# Format C/C++ files
clang-format-batch --extensions=".c,.cpp,.h"

# Format multiple file types
clang-format-batch --extensions=".proto,.c,.cpp,.h"

# Use short flag
clang-format-batch -e ".proto,.cc,.hh"

Library Usage

Protocol Buffers Formatting (Primary Use Case)

package main

import (
    "fmt"
    "github.com/go-xlan/clang-format/protoformat"
    "github.com/yyle88/must"
    "github.com/yyle88/osexec"
    "github.com/yyle88/rese"
)

func main() {
    execConfig := osexec.NewExecConfig()
    style := protoformat.NewStyle()
    
    // Preview .proto file formatting (DryRun)
    output := rese.V1(protoformat.DryRun(execConfig, "example.proto", style))
    fmt.Println(string(output))
    
    // Format single .proto file
	rese.V1(protoformat.Format(execConfig, "example.proto", style))
    
    // Format entire project (batch processing)
    must.Done(protoformat.FormatProject(execConfig, "./proto-project", style))
}

Custom Style Configuration

customStyle := &clangformat.Style{
    BasedOnStyle:                "LLVM",
    IndentWidth:                 4,
    ColumnLimit:                 80,
    AlignConsecutiveAssignments: true,
}

output := rese.V1(protoformat.DryRun(execConfig, "example.proto", customStyle))

General File Formatting (C/C++ Support)

import "github.com/go-xlan/clang-format/clangformat"

// Format C/C++ files
output := rese.V1(clangformat.DryRun(execConfig, "example.cpp", style))
must.Done(clangformat.Format(execConfig, "example.cpp", style))

API Reference

clangformat Package

  • NewStyle() - Creates default Google-based style configuration
  • DryRun(config, path, style) - Preview formatting without file modification
  • Format(config, path, style) - Use formatting on file

protoformat Package

  • NewStyle() - Creates Protocol Buffers optimized style configuration
  • DryRun(config, path, style) - Preview .proto file formatting
  • Format(config, path, style) - Format single .proto file
  • FormatProject(config, path, style) - Batch format all .proto files in project

Style Configuration

type Style struct {
    BasedOnStyle                string // "Google", "LLVM", "Chromium", etc.
    IndentWidth                 int    // Count of spaces for indentation
    ColumnLimit                 int    // Maximum line length (0 = no limit)
    AlignConsecutiveAssignments bool   // Align assignments at assignment signs
}

πŸ“„ License

MIT License. See LICENSE.


🀝 Contributing

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

  • πŸ› Found a bug? Open an issue on GitHub with reproduction steps
  • πŸ’‘ Have a feature idea? Create an issue to discuss the suggestion
  • πŸ“– Documentation confusing? Report it so we can improve
  • πŸš€ Need new features? Share the use cases to help us understand requirements
  • ⚑ Performance issue? Help us optimize through reporting slow operations
  • πŸ”§ Configuration problem? Ask questions about complex setups
  • πŸ“’ Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • πŸ’¬ Feedback? We welcome suggestions and comments

πŸ”§ Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • ⭐ Give GitHub stars if this project helps you
  • 🀝 Share with teammates and (golang) programming friends
  • πŸ“ Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! πŸŽ‰


GitHub Stars

Stargazers

About

Using Clang-Format in Golang. Format Protobuf File Using Clang-Format in Golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published