All Projects â†’ mvdan â†’ Gofumpt

mvdan / Gofumpt

Licence: bsd-3-clause
A stricter gofmt

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Gofumpt

Format.cmake
💅 Stylize your code! Automatic clang-format and cmake-format targets for CMake.
Stars: ✭ 94 (-89.6%)
Mutual labels:  format, style
react-native-styled-text
Styled Text for React Native
Stars: ✭ 57 (-93.69%)
Mutual labels:  format, style
Styled widget
Simplifying widget style in Flutter.
Stars: ✭ 424 (-53.1%)
Mutual labels:  style
Csstype
Strict TypeScript and Flow types for style based on MDN data
Stars: ✭ 760 (-15.93%)
Mutual labels:  style
Pastel
Terminal output styling with intuitive and clean API.
Stars: ✭ 569 (-37.06%)
Mutual labels:  style
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (-49.56%)
Mutual labels:  style
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (-31.64%)
Mutual labels:  format
Phpinsights
🔰 Instant PHP quality checks from your console
Stars: ✭ 4,442 (+391.37%)
Mutual labels:  style
Styledecorator
Easy string decoration with styles
Stars: ✭ 17 (-98.12%)
Mutual labels:  style
X Swiftformat
X-SwiftFormat extension for Xcode
Stars: ✭ 556 (-38.5%)
Mutual labels:  format
Rspec Style Guide
Best practices for writing your specs!
Stars: ✭ 735 (-18.69%)
Mutual labels:  style
Checkmail
Golang package for email validation
Stars: ✭ 554 (-38.72%)
Mutual labels:  format
Vue Json Pretty
A JSON tree view component that is easy to use and also supports data selection.
Stars: ✭ 477 (-47.23%)
Mutual labels:  format
Weui
A UI library by WeChat official design team, includes the most useful widgets/modules in mobile web applications.
Stars: ✭ 26,030 (+2779.42%)
Mutual labels:  style
React Phone Input 2
📞 Highly customizable phone input component with auto formatting
Stars: ✭ 446 (-50.66%)
Mutual labels:  format
Leetheme
äŧ˜é›…įš„ä¸ģéĸ˜įŽĄį†åē“- 一行äģŖį åŽŒæˆå¤šæ ˇåŧåˆ‡æĸ
Stars: ✭ 762 (-15.71%)
Mutual labels:  style
Fmt Obj
Stringifies any javascript object in your console for CLI inspection ✨
Stars: ✭ 428 (-52.65%)
Mutual labels:  format
Git Style Guide
A Git Style Guide
Stars: ✭ 4,851 (+436.62%)
Mutual labels:  style
Secureuxtheme
🎨 A secure boot compatible in-memory UxTheme patcher
Stars: ✭ 586 (-35.18%)
Mutual labels:  style
Translatedjs
Internationalization and localization for JavaScript and Node.js
Stars: ✭ 17 (-98.12%)
Mutual labels:  format

gofumpt

GO111MODULE=on go get mvdan.cc/gofumpt

Enforce a stricter format than gofmt, while being backwards compatible. That is, gofumpt is happy with a subset of the formats that gofmt is happy with.

The tool is a modified fork of gofmt, so it can be used as a drop-in replacement. Running gofmt after gofumpt should be a no-op. For example:

gofumpt -l -w .

Most of the Go source files in this repository belong to the Go project. The added formatting rules are in the format package.

Added rules

No empty lines at the beginning or end of a function

example
func foo() {
	println("bar")

}
func foo() {
	println("bar")
}

No empty lines around a lone statement (or comment) in a block

example
if err != nil {

	return err
}
if err != nil {
	return err
}

No empty lines before a simple error check

example
foo, err := processFoo()

if err != nil {
	return err
}
foo, err := processFoo()
if err != nil {
	return err
}

Composite literals should use newlines consistently

example
// A newline before or after an element requires newlines for the opening and
// closing braces.
var ints = []int{1, 2,
	3, 4}

// A newline between consecutive elements requires a newline between all
// elements.
var matrix = [][]int{
	{1},
	{2}, {
		3,
	},
}
var ints = []int{
	1, 2,
	3, 4,
}

var matrix = [][]int{
	{1},
	{2},
	{
		3,
	},
}

Empty field lists should use a single line

example
var V interface {
} = 3

type T struct {
}

func F(
)
var V interface{} = 3

type T struct{}

func F()

std imports must be in a separate group at the top

example
import (
	"foo.com/bar"

	"io"

	"io/ioutil"
)
import (
	"io"
	"io/ioutil"

	"foo.com/bar"
)

Short case clauses should take a single line

example
switch c {
case 'a', 'b',
	'c', 'd':
}
switch c {
case 'a', 'b', 'c', 'd':
}

Multiline top-level declarations must be separated by empty lines

example
func foo() {
	println("multiline foo")
}
func bar() {
	println("multiline bar")
}
func foo() {
	println("multiline foo")
}

func bar() {
	println("multiline bar")
}

Single var declarations should not be grouped with parentheses

example
var (
	foo = "bar"
)
var foo = "bar"

Contiguous top-level declarations should be grouped together

example
var nicer = "x"
var with = "y"
var alignment = "z"
var (
	nicer     = "x"
	with      = "y"
	alignment = "z"
)

Simple var-declaration statements should use short assignments

example
var s = "somestring"
s := "somestring"

The -s code simplification flag is enabled by default

example
var _ = [][]int{[]int{1}}
var _ = [][]int{{1}}

Octal integer literals should use the 0o prefix on modules using Go 1.13 and later

example
const perm = 0755
const perm = 0o755

Comments which aren't Go directives should start with a whitespace

example
//go:noinline

//Foo is awesome.
func Foo() {}
//go:noinline

// Foo is awesome.
func Foo() {}

Extra rules behind -extra

Adjacent parameters with the same type should be grouped together

example
func Foo(bar string, baz string) {}
func Foo(bar, baz string) {}

Installation

gofumpt is a replacement for gofmt, so you can simply go get it as described at the top of this README and use it.

When using an IDE or editor with Go integrations, it's best to use gofumpt as part of gopls. The instructions below show how to do that for some of the major editors out there.

Visual Studio Code

Enable the language server following the official docs, and then enable gopls's gofumpt option. Note that VS Code will complain about the gopls settings, but they will still work.

"go.useLanguageServer": true,
"gopls": {
	"formatting.gofumpt": true,
},

Goland

Once gofumpt is installed, follow the steps below:

  • Open Settings (File > Settings)
  • Open the Tools section
  • Find the File Watchers sub-section
  • Click on the + on the right side to add a new file watcher
  • Choose Custom Template

When a window asks for settings, you can enter the following:

  • File Types: Select all .go files
  • Scope: Project Files
  • Program: Select your gofumpt executable
  • Arguments: -w $FilePath$
  • Output path to refresh: $FilePath$
  • Working directory: $ProjectFileDir$
  • Environment variables: GOROOT=$GOROOT$;GOPATH=$GOPATH$;PATH=$GoBinDirs$

To avoid unecessary runs, you should disable all checkboxes in the Advanced section.

Vim-go

Ensure you are at least running version v1.24, and set up gopls for formatting code with gofumpt:

let g:go_fmt_command="gopls"
let g:go_gopls_gofumpt=1

Govim

With a new enough version of govim, simply configure gopls to use gofumpt:

call govim#config#Set("Gofumpt", 1)

Roadmap

This tool is a place to experiment. In the long term, the features that work well might be proposed for gofmt itself.

The tool is also compatible with gofmt and is aimed to be stable, so you can rely on it for your code as long as you pin a version of it.

License

Note that much of the code is copied from Go's gofmt command. You can tell which files originate from the Go repository from their copyright headers. Their license file is LICENSE.google.

gofumpt's original source files are also under the 3-clause BSD license, with the separate file LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].