fancyfmt

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2022 License: MIT Imports: 22 Imported by: 0

README

fancyfmt

A formatter for Go code with bells and whistles

Installation

  • Use

    go get github.com/sirkon/fancyfmt
    

    to get a library as a dependency of your custom formatter.

  • Type

    go install github.com/sirkon/cmd/fancyfmt@latest
    

    to install a ready to use formatter

Description

fancyfmt is a

  • library to make gofmt compliant formatters
  • a ready to use formatter
Brief functionality
  • Imports grouping and sorting. Import paths are:
    • Joined in one import declaration (except "C", which is always alone)
    • Splitted in groups (can be tweaked with custom imports grouper)
    • Sorted lexicographically within each group
  • Provides default formatting for
    • Multiline functions declarations
    • Multiline calls
    • Multiline composite literals, slices and arrays get a special care at that
    • Multiline chaining
Screencast

usage screencast

Remarks

  • fancyfmt stores a cache of packages from standard library in a os.TempDir() directory. This was done to speedup things as package.Load(cfg, "std") is slow, about 0.2s on my machine, cached access is about ten times faster. You may notice a slugishness in case of the first formatting in the screencast, that is it. The further formats are much faster.
  • fancyfmt mutates []byte{…} literals if they only have numbers replacing them with hex numbers.
  • You may fix composite literals formatting (except the new line before the first item and after the last one) by adding a comment after an element.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(fset *token.FileSet, file *ast.File, content []byte, grouper ImportsGrouper) (io.Reader, error)

Format formats given AST tree

Types

type ImportsGrouper

type ImportsGrouper interface {
	Weight(path string) int
}

ImportsGrouper a facility to give an import path a certain weight what will be used to group a set of imports by it, i.e. import paths with the same weight will be in one groups of imports

func DefaultImportGroupsWithCurrent

func DefaultImportGroupsWithCurrent(current string) ImportsGrouper

DefaultImportGroupsWithCurrent the same s DefaultImportsGroups just no current package set

func DefaultImportsGrouper

func DefaultImportsGrouper() (ImportsGrouper, error)

DefaultImportsGrouper provides an import grouper with a policy that is supposed to be the default:

"C" - 0
Standard library - 1
3rd party - 2
Current project - 3
Relative imports - 4

It tried to determine a current project once called and may return an error if it failed to detect it. Use DefaultImportGroupsWithCurrent if you don't need it or need to set up your own

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL