Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q1 2024: as good a time as any for a little polishing #117

Merged
merged 15 commits into from
Jan 20, 2024
Merged
27 changes: 27 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: build

on:
push:

permissions:
contents: read

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: "1.21"
cache: false
- name: build golines
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: build --clean --snapshot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: lint
on:
push:

permissions:
contents: read

jobs:
golangci:
name: golines lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.21"
cache: false
- name: run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
install-mode: binary
skip-cache: true
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: release

on:
push:
tags:
- "*"

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: "1.21"
cache: false
- name: release golines
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15 changes: 7 additions & 8 deletions .github/workflows/go.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
name: golines test
name: test
on:
push:
branches:
- main
pull_request:

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
go: ['1.19']
go:
- "1.21"
- "1.20"
runs-on: ${{ matrix.os }}
name: golines test (using go ${{ matrix.go }} on ${{ matrix.os }})
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: test golines
uses: actions/setup-go@v3
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: true
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
/dist

# Test binary, build with `go test -c`
*.test
Expand Down
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
linters:
disable:
- errcheck

run:
timeout: 5m
43 changes: 43 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
project_name: golines
before:
hooks:
- go clean -x

builds:
- main: .
ldflags:
- -s -w
flags:
- -trimpath
- -v
goos:
- darwin
- linux
goarch:
- amd64
- arm64
env:
- GOAMD64=v3

universal_binaries:
- replace: true
mod_timestamp: "{{ .CommitTimestamp }}"

archives:
- files:
- README.md
- LICENSE
wrap_in_directory: true

snapshot:
name_template: "{{ incpatch .Version }}-pre+{{ .ShortCommit }}"

changelog:
use: github
filters:
exclude:
- "^docs:"
- "^test:"

announce:
skip: true
8 changes: 2 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
export GO111MODULE=on

.PHONY: all
all: install test

.PHONY: install
install:
go install .
all: build test

.PHONY: build
build:
go build .
goreleaser build --clean --snapshot --single-target

.PHONY: test
test: vet
Expand Down
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
[![Circle CI](https://circleci.com/gh/segmentio/golines.svg?style=svg&circle-token=b1d01d8b035ef0aa71ccd183580586a80cd85271)](https://circleci.com/gh/segmentio/golines)
[![golines test](https://github.com/segmentio/golines/actions/workflows/go.yml/badge.svg)](https://github.com/segmentio/golines/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/segmentio/golines)](https://goreportcard.com/report/github.com/segmentio/golines)
[![GoDoc](https://godoc.org/github.com/segmentio/golines?status.svg)](https://godoc.org/github.com/segmentio/golines)
[![Coverage](https://img.shields.io/badge/Go%20Coverage-84%25-brightgreen.svg?longCache=true&style=flat)](https://gocover.io/github.com/segmentio/golines?version=1.13.x)

# golines

Golines is a golang formatter that shortens long lines, in addition to all
Golines is a Go code formatter that shortens long lines, in addition to all
of the formatting fixes done by [`gofmt`](https://golang.org/cmd/gofmt/).

## Motivation

The standard golang formatting tools (`gofmt`, `goimports`, etc.) are great, but
[deliberately don't shorten long lines](https://github.com/golang/go/issues/11915); instead, this
is an activity left to developers.
The standard Go formatting tools (`gofmt`, `goimports`, etc.) are great, but
[deliberately don't shorten long lines](https://github.com/golang/go/issues/11915);
instead, this is an activity left to developers.

While there are different tastes when it comes to line lengths in go, we've generally found
that very long lines are more difficult to read than their shortened alternatives. As an example:
Expand All @@ -33,7 +32,7 @@ myMap := map[string]string{
}
```

We built `golines` to give go developers the option to automatically shorten long lines, like
We built `golines` to give Go developers the option to automatically shorten long lines, like
the one above, according to their preferences.

More background and technical details are available in
Expand All @@ -47,27 +46,29 @@ view of a file with very long lines. More example pairs can be found in the

## Version support

The latest version of `golines` requires golang 1.18 or newer due to generics-related dependencies.
If you need to use `golines` with an older version of go, install the tool from the `v0.9.0`
release.
Newer releases of `golines` require at least Go 1.18 due to generics-related dependencies.
However, the [minimum version](https://go.dev/ref/mod#go-mod-file-go) in [`go.mod`](./go.mod)
should be considered the minimum required version of Go for any given version
of `golines.` If you need to use `golines` with an older version of go, install
the tool from the `v0.9.0` release.

## Usage

First, install the tool. If you're using golang 1.18 or newer, run:
First, install the tool. If you're using Go 1.21 or newer, run:

```
```text
go install github.com/segmentio/golines@latest
```

Otherwise, for older golang versions, run:
Otherwise, for older Go versions, run:

```
```text
go install github.com/segmentio/golines@v0.9.0
```

Then, run:

```
```text
golines [paths to format]
```

Expand All @@ -79,10 +80,10 @@ files in place, use the `-w` flag.

## Options

Some other options are described in the sections below. Run `golines --help` to see
all available flags and settings.
Some other options are described in the sections below. Run `golines --help` to
see all available flags and settings.

#### Line length settings
### Line length settings

By default, the tool tries to shorten lines that are longer than 100 columns
and assumes that 1 tab = 4 columns. The latter can be changed via the
Expand All @@ -102,15 +103,15 @@ with the `--shorten-comments` flag.

#### Custom formatters

By default, the tool will use [`goimports`](https://godoc.org/golang.org/x/tools/cmd/goimports) as
the base formatter (if found), otherwise it will revert to `gofmt`. An explicit formatter can be
set via the `--base-formatter` flag; the command provided here should accept its input via
`stdin` and write its output to `stdout`.
By default, the tool will use [`goimports`](https://godoc.org/golang.org/x/tools/cmd/goimports)
as the base formatter (if found), otherwise it will revert to `gofmt`. An explicit
formatter can be set via the `--base-formatter` flag; the command provided here
should accept its input via `stdin` and write its output to `stdout`.

#### Generated files

By default, the tool will not format any files that look like they're generated. If you
want to reformat these too, run with the `--no-ignore-generated` flag.
By default, the tool will not format any files that look like they're generated.
If you want to reformat these too, run with the `--no-ignore-generated` flag.

#### Chained method splitting

Expand Down Expand Up @@ -140,8 +141,8 @@ myObj.Method(arg1, arg2, arg3).
AThirdMethod(arg1, arg2)
```

The original behavior can be used by running the tool with the `--no-chain-split-dots`
flag.
The original behavior can be used by running the tool with the
`--no-chain-split-dots` flag.

#### Struct tag reformatting

Expand All @@ -167,9 +168,8 @@ let g:go_fmt_options = {
1. Install the [Run on Save](https://marketplace.visualstudio.com/items?itemName=emeraldwalk.RunOnSave) extension
2. Go into the VSCode settings menu, scroll down to the section for the "Run on Save"
extension, click the "Edit in settings.json" link
3. Set the `emeraldwalk.runonsave` key as follows (adding other flags to the `golines`
command as desired):

3. Set the `emeraldwalk.runonsave` key as follows
(adding other flags to the `golines` command as desired):
```
"emeraldwalk.runonsave": {
"commands": [
Expand All @@ -181,18 +181,18 @@ let g:go_fmt_options = {
}
```

4. Save the settings and restart VSCode
1. Save the settings and restart VSCode

### Goland

1. Go into the Goland settings and click "Tools" -> "File Watchers" then click the plus to create a new file watcher
2. Set the following properties and confirm by clicking OK:
- __Name:__ `golines`
- __File type:__ `Go files`
- __Scope:__ `Project Files`
- __Program:__ `golines`
- __Arguments:__ `$FilePath$ -w`
- __Output paths to refresh:__ `$FilePath$`
- __Name:__ `golines`
- __File type:__ `Go files`
- __Scope:__ `Project Files`
- __Program:__ `golines`
- __Arguments:__ `$FilePath$ -w`
- __Output paths to refresh:__ `$FilePath$`
3. Activate your newly created file watcher in the Goland settings under "Tools" -> "Actions on save"

### Others
Expand Down
4 changes: 2 additions & 2 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/pmezard/go-difflib/difflib"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

// PrettyDiff prints colored, git-style diffs to the console.
Expand Down Expand Up @@ -36,7 +36,7 @@ func PrettyDiff(path string, contents []byte, results []byte) error {
for _, line := range strings.Split(text, "\n") {
line = strings.TrimRight(line, " ")
switch {
case !terminal.IsTerminal(int(os.Stdout.Fd())) && len(line) > 0:
case !term.IsTerminal(int(os.Stdout.Fd())) && len(line) > 0:
fmt.Printf("%s\n", line)
case strings.HasPrefix(line, "+"):
fmt.Printf("%s%s%s\n", ansiGreen, line, ansiEnd)
Expand Down
5 changes: 2 additions & 3 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
Command golines is a formatter that shortens long go code lines, in addition to the
formatting fixes done by gofmt.

$ go get -u github.com/segmentio/golines
$ go get -u github.com/segmentio/golines

See the project README for more details:

https://github.com/segmentio/golines/blob/master/README.md

File bugs or feature requests at:

https://github.com/segmentio/golines/issues

https://github.com/segmentio/golines/issues
*/
package main // import "github.com/segmentio/golines"
Loading