Skip to content

feedr-aps/sigfmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sigfmt

A Go CLI tool that reformats function signatures exceeding a configurable line length. All other code is left byte-for-byte identical.

Install

go install github.com/feedr-aps/sigfmt@latest

Usage

sigfmt [flags] [paths...]
Flag Description Default
-w Write result to source file off
-l List files that would be changed off
-max-len Maximum line length 100
-tab-width Tab display width for measurement 4

Examples

# Check which files would change
sigfmt -l ./...

# Reformat in place
sigfmt -w ./...

# Print reformatted output to stdout
sigfmt path/to/file.go

# Read from stdin
cat file.go | sigfmt

# Custom line length
sigfmt -w -max-len 120 ./...

-l exits with code 1 if any files would change, making it suitable for CI checks. -w and -l can be combined to list files as they are written.

What it does

Signatures that exceed the max line length are expanded — each parameter goes on its own line with a trailing comma:

// Before:
func (s *Server) HandleRequest(ctx context.Context, req *http.Request, logger *slog.Logger, opts ...Option) (Response, error) {

// After:
func (s *Server) HandleRequest(
	ctx context.Context,
	req *http.Request,
	logger *slog.Logger,
	opts ...Option,
) (Response, error) {

Multi-line signatures that fit on one line are collapsed back:

// Before:
func Short(
	a int,
	b int,
) error {

// After:
func Short(a int, b int) error {

Grouped parameters are expanded when splitting:

// Before:
func foo(a, b int, c string) ...

// After (when expanded):
func foo(
	a int,
	b int,
	c string,
) ...

Generic functions are supported. When type parameters are multi-line, the parameter line length is measured independently from the closing ](:

func ToMiddleware[
	TFunc func(ctx context.Context) error,
](fn TFunc) Middleware {

What it skips

  • Generated files (// Code generated ... DO NOT EDIT.)
  • Files matched by .gitignore
  • vendor/, testdata/, and hidden directories
  • Signatures containing comments within the signature span (left untouched to avoid losing comments)
  • Function literals (closures)
  • Interface method declarations
  • Return types are never split — only parameters are expanded

Scope

Only top-level *ast.FuncDecl nodes are processed. The tool uses go/ast to parse signatures and byte offsets to surgically replace only the signature spans in the original source, so formatting of all non-signature code is preserved exactly.

About

Function signature formatting tool for golang

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages