goimports-wasabi

command module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: BSD-3-Clause Imports: 17 Imported by: 0

README

goimports-wasabi

This is a fork of goimports, inspired by the gosimports project. goimports-wasabi also tries to solve the import grouping/ordering problem, but in a different way.

Overview

goimports and gosimports have the following issues (as of 2022/11).

  • goimports doesn't sort import specs deterministically.
  • goimports doesn't treat comments associated with import specs properly.
  • goimports doesn't work well for artificial inputs.
  • gosimports removes comments associated with import specs.

goimports-wasabi is designed to work deterministically with any valid input, leaving as many comments as possible in the (supposedly) correct place.

Installation

go install github.com/homuler/goimports-wasabi@latest

Examples

Grouping/Ordering Problem
Input
import (
	// doc comment for fmt
	"fmt"

	/*
	 * block comments
	 */

	 // doc comment for foo
	"github.com/homuler/foo"
	"context"
	// footer comment (maybe for context)

	"strings" // line comment for strings

	b "github.com/homuler/bar"

	_ "runtime/pprof" // line comment for pprof
)
Output
import (
	"context"
	// footer comment (maybe for context)

	// doc comment for fmt
	"fmt"
	_ "runtime/pprof" // line comment for pprof
	"strings"         // line comment for strings

	/*
	 * block comments
	 */

	b "github.com/homuler/bar"
	// doc comment for foo
	"github.com/homuler/foo"
)
goimports
import (
	// doc comment for fmt
	"fmt"

	/*
	 * block comments
	 */

	// doc comment for foo
	"context"

	"github.com/homuler/foo"

	// footer comment (maybe for context)

	"strings" // line comment for strings

	b "github.com/homuler/bar"

	_ "runtime/pprof" // line comment for pprof
)
Merging Problem
Input
// doc comment for fmt
import "fmt"

// doc comment for context
import "context"

// doc comment
import (
	// doc comment for errors
	"errors"
)
Output

goimports-wasabi

// doc comment
import (
	// doc comment for context
	"context"
	// doc comment for errors
	"errors"
	// doc comment for fmt
	"fmt"
)
goimports
// doc comment for fmt
import (
	"context"
	"errors"
	"fmt"
)

// doc comment for context

// doc comment

// doc comment for errors
Unusual Input
Input
import ("fmt";"fmt";"context";"github.com/homuler/foo")
Output

goimports-wasabi -format-only

import (
	"context"
	"fmt"

	"github.com/homuler/foo"
)
goimports -format-only
panic: invalid line number 2 (should be < 2)

Known Issues

goimports-wasabi runs format.Source internally only once. That means if gofmt is not idempotent, nor is goimports-wasabi.

Input
import (
	/* c (1) */"fmt" /* comment
	for fmt */ // comment for fmt
)

import "context"
// footer comment for context

// doc comment for fmt
import /* c (2) */ "fmt"
Output

cat input.go | goimports-wasabi

import (
	"context"
	// footer comment for context

	// doc comment for fmt
	/* c (1) */ /* c (2) */
	"fmt" /* comment
	for fmt */ // comment for fmt
)

cat input.go | goimports-wasabi | goimports-wasabi

import (
	"context"
	// footer comment for context

	// doc comment for fmt
	/* c (1) */ /* c (2) */
	"fmt"       /* comment
	for fmt */ // comment for fmt
)

License

BSD-3-Clause

Copyright (c) 2009 The Go Authors. All rights reserved.

Documentation

Overview

Command goimports-wasabi updates your Go import lines, adding missing ones and removing unreferenced ones.

$ go install github.com/homuler/goimports-wasabi@latest

In addition to fixing imports, goimports-wasabi also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook.

For emacs, make sure you have the latest go-mode.el:

https://github.com/dominikh/go-mode.el

Then in your .emacs file:

(setq gofmt-command "goimports-wasabi")
(add-hook 'before-save-hook 'gofmt-before-save)

For vim, set "gofmt_command" to "goimports-wasabi":

https://golang.org/change/39c724dd7f252
https://golang.org/wiki/IDEsAndTextEditorPlugins
etc

For GoSublime, follow the steps described here:

http://michaelwhatcott.com/gosublime-goimports/

For other editors, you probably know what to do.

To exclude directories in your $GOPATH from being scanned for Go files, goimports respects a configuration file at $GOPATH/src/.goimportsignore which may contain blank lines, comment lines (beginning with '#'), or lines naming a directory relative to the configuration file to ignore when scanning. No globbing or regex patterns are allowed. Use the "-v" verbose flag to verify it's working and see what goimports-wasabi is doing.

File bugs or feature requests at:

https://github.com/homuler/goimports-wasabi/issues

Happy hacking!

Directories

Path Synopsis
internal
event
Package event provides a set of packages that cover the main concepts of telemetry in an implementation agnostic way.
Package event provides a set of packages that cover the main concepts of telemetry in an implementation agnostic way.
event/core
Package core provides support for event based telemetry.
Package core provides support for event based telemetry.
event/export/eventtest
Package eventtest supports logging events to a test.
Package eventtest supports logging events to a test.
event/export/metric
Package metric aggregates events into metrics that can be exported.
Package metric aggregates events into metrics that can be exported.
event/export/ocagent
Package ocagent adds the ability to export all telemetry to an ocagent.
Package ocagent adds the ability to export all telemetry to an ocagent.
event/tag
Package tag provides the labels used for telemetry throughout gopls.
Package tag provides the labels used for telemetry throughout gopls.
fastwalk
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
Package fastwalk provides a faster version of filepath.Walk for file system scanning tools.
gocommand
Package gocommand is a helper for calling the go command.
Package gocommand is a helper for calling the go command.
gopathwalk
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
Package gopathwalk is like filepath.Walk but specialized for finding Go packages, particularly in $GOPATH and $GOROOT.
imports
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
Package imports implements a Go pretty-printer (like package "go/format") that also adds or removes import statements as necessary.
proxydir
Package proxydir provides functions for writing module data to a directory in proxy format, so that it can be used as a module proxy by setting GOPROXY="file://<dir>".
Package proxydir provides functions for writing module data to a directory in proxy format, so that it can be used as a module proxy by setting GOPROXY="file://<dir>".
testenv
Package testenv contains helper functions for skipping tests based on which tools are present in the environment.
Package testenv contains helper functions for skipping tests based on which tools are present in the environment.

Jump to

Keyboard shortcuts

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