fmtdiff

package module
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2020 License: MIT Imports: 5 Imported by: 0

README

fmtdiff

Go Doc codecov

A goimports client as well as a parser that parses the diff between an original file and formatted one. You can use it as a wrapper for golang.org/x/tools/imports.

goimports not only fixes imports, but also formats your code in the same style as gofmt, so fmtdiff means importsdiff substantially.

Installation

go get github.com/nakabonne/fmtdiff

Usage Example

package main	

import "github.com/nakabonne/fmtdiff"

func main() {	
	fileDiff, _ := fmtdiff.Process("/path/to/foo.go", &fmtdiff.Options{
		LocalPrefixes:  []string{"example.com/foo/bar"},
                IgnoreComments: true,
                FormatOnly:     true,
	})	
}

Thanks

Thanks to sourcegraph/go-diff for cool diff parser.

Documentation

Overview

Package fmtdiff implements a goimports client as well as a parser that parses the diff between an original file and formatted one. You can use it as a wrapper for golang.org/x/tools/imports.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileDiff

type FileDiff struct {
	// File name.
	Name string
	// Contents of original file.
	Before []byte
	// Contents of formatted file.
	After []byte
	Hunks []*Hunk
}

FileDiff represents a diff between an original file and a formatted one.

func Process added in v0.8.1

func Process(filename string, options *Options) (*FileDiff, error)

Process runs goimports and parses the diff between an original file and a formatted one.

Note that filename's directory influences which imports can be chosen, so it is important that filename be accurate. To process data “as if” it were in filename, pass the data as a non-nil src.

func (*FileDiff) NoDiff

func (f *FileDiff) NoDiff() bool

NoDiff checks if the original file and the formatted one are the same length and contain the same bytes.

type Hunk

type Hunk struct {
	// OrigStartLine is the starting line number in the original file.
	OrigStartLine int
	// OrigLines is the number of lines the hunk applies to in the original file.
	OrigLines int
	// NewStartLine is the starting line number in the new file.
	NewStartLine int
	// NewLines is the number of lines the hunk applies to in the new file.
	NewLines int
	// Body is lines prefixed with '-', '+', or ' '.
	// The number of lines of context to show is three.
	Body []byte
}

Hunk represents a series of changes in a file's unified diff.

type Options

type Options struct {
	// LocalPrefixes is a set of import path prefixes, which, if set,
	// instructs Process to sort the import paths with the given prefixes
	// into another group after 3rd-party packages. Empty list by default.
	LocalPrefixes []string
	// Accept fragment of a source file (no package statement). False by default.
	Fragment bool
	// Report all errors (not just the first 10 on different lines). False by default.
	AllErrors bool
	// Do not print comments. False by default.
	IgnoreComments bool
	// Use spaces for indent. False by default.
	SpaceIndent bool
	// 8 is populated if zero provided.
	TabWidth int
	// Disable the insertion and deletion of imports. False by default.
	FormatOnly bool
}

Options makes it possible to fine-tune behavior.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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