mirror of
https://github.com/jimeh/undent.git
synced 2026-02-19 11:56:39 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1148580df | ||
|
c3c5f71c7a
|
|||
|
2e276c037b
|
|||
| af31c21ec1 | |||
|
84715b90d0
|
|||
|
3878874bbe
|
|||
| 23c6c4cd7d | |||
|
|
1a0b0aafae | ||
|
bf5d7b709e
|
|||
|
5890256a45
|
|||
| 5e31bd0485 | |||
|
178d9b5bf6
|
|||
| 20ac9c9824 | |||
|
caeba6291d
|
|||
|
044587fb4c
|
|||
| 69ffce4dca | |||
|
a0b74d98db
|
|||
|
cf20ee4e75
|
55
.github/workflows/ci.yml
vendored
55
.github/workflows/ci.yml
vendored
@@ -7,11 +7,14 @@ jobs:
|
|||||||
name: Lint
|
name: Lint
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- name: golangci-lint
|
- uses: actions/setup-go@v3
|
||||||
uses: golangci/golangci-lint-action@v2
|
|
||||||
with:
|
with:
|
||||||
version: v1.37
|
go-version: 1.15
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v3
|
||||||
|
with:
|
||||||
|
version: v1.50
|
||||||
env:
|
env:
|
||||||
VERBOSE: "true"
|
VERBOSE: "true"
|
||||||
|
|
||||||
@@ -19,11 +22,11 @@ jobs:
|
|||||||
name: Tidy
|
name: Tidy
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -37,11 +40,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.ref != 'refs/heads/main'
|
if: github.ref != 'refs/heads/main'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -67,18 +70,18 @@ jobs:
|
|||||||
name: Coverage
|
name: Coverage
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
restore-keys: |
|
restore-keys: |
|
||||||
${{ runner.os }}-go-
|
${{ runner.os }}-go-
|
||||||
- name: Publish coverage
|
- name: Publish coverage
|
||||||
uses: paambaati/codeclimate-action@v2.7.4
|
uses: paambaati/codeclimate-action@v3.2.0
|
||||||
env:
|
env:
|
||||||
VERBOSE: "true"
|
VERBOSE: "true"
|
||||||
GOMAXPROCS: 4
|
GOMAXPROCS: 4
|
||||||
@@ -93,11 +96,11 @@ jobs:
|
|||||||
name: Test
|
name: Test
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -113,11 +116,11 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.ref == 'refs/heads/main'
|
if: github.ref == 'refs/heads/main'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v3
|
||||||
with:
|
with:
|
||||||
go-version: 1.15
|
go-version: 1.15
|
||||||
- uses: actions/cache@v2
|
- uses: actions/cache@v3
|
||||||
with:
|
with:
|
||||||
path: ~/go/pkg/mod
|
path: ~/go/pkg/mod
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
@@ -134,6 +137,16 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
tool: "go"
|
tool: "go"
|
||||||
output-file-path: output.txt
|
output-file-path: output.txt
|
||||||
github-token: ${{ secrets.GH_PUSH_TOKEN }}
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
comment-on-alert: true
|
comment-on-alert: true
|
||||||
auto-push: true
|
auto-push: true
|
||||||
|
|
||||||
|
release-please:
|
||||||
|
needs: [lint, tidy, test]
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
if: github.ref == 'refs/heads/main'
|
||||||
|
steps:
|
||||||
|
- uses: google-github-actions/release-please-action@v3
|
||||||
|
id: release-please
|
||||||
|
with:
|
||||||
|
command: manifest
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ linters-settings:
|
|||||||
golint:
|
golint:
|
||||||
min-confidence: 0
|
min-confidence: 0
|
||||||
govet:
|
govet:
|
||||||
check-shadowing: true
|
|
||||||
enable-all: true
|
enable-all: true
|
||||||
|
disable:
|
||||||
|
- fieldalignment
|
||||||
lll:
|
lll:
|
||||||
line-length: 80
|
line-length: 80
|
||||||
tab-width: 4
|
tab-width: 4
|
||||||
@@ -21,10 +22,10 @@ linters:
|
|||||||
disable-all: true
|
disable-all: true
|
||||||
enable:
|
enable:
|
||||||
- bodyclose
|
- bodyclose
|
||||||
- deadcode
|
|
||||||
- depguard
|
- depguard
|
||||||
- dupl
|
- dupl
|
||||||
- errcheck
|
- errcheck
|
||||||
|
- exportloopref
|
||||||
- funlen
|
- funlen
|
||||||
- gochecknoinits
|
- gochecknoinits
|
||||||
- goconst
|
- goconst
|
||||||
@@ -32,7 +33,6 @@ linters:
|
|||||||
- gocyclo
|
- gocyclo
|
||||||
- goerr113
|
- goerr113
|
||||||
- goimports
|
- goimports
|
||||||
- golint
|
|
||||||
- goprintffuncname
|
- goprintffuncname
|
||||||
- gosec
|
- gosec
|
||||||
- gosimple
|
- gosimple
|
||||||
@@ -44,19 +44,15 @@ linters:
|
|||||||
- nlreturn
|
- nlreturn
|
||||||
- noctx
|
- noctx
|
||||||
- nolintlint
|
- nolintlint
|
||||||
- scopelint
|
- revive
|
||||||
- sqlclosecheck
|
- sqlclosecheck
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- structcheck
|
|
||||||
- typecheck
|
- typecheck
|
||||||
- unconvert
|
- unconvert
|
||||||
- unused
|
- unused
|
||||||
- varcheck
|
|
||||||
- whitespace
|
- whitespace
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
include:
|
|
||||||
# - EXC0002 # disable excluding of issues about comments from golint
|
|
||||||
exclude:
|
exclude:
|
||||||
- Using the variable on range scope `tt` in function literal
|
- Using the variable on range scope `tt` in function literal
|
||||||
- Using the variable on range scope `tc` in function literal
|
- Using the variable on range scope `tc` in function literal
|
||||||
|
|||||||
3
.release-please-manifest.json
Normal file
3
.release-please-manifest.json
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
".": "1.1.2"
|
||||||
|
}
|
||||||
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,6 +1,18 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
## [1.1.2](https://github.com/jimeh/undent/compare/v1.1.1...v1.1.2) (2024-10-21)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* **perf:** minor improvement when no indent is found ([84715b9](https://github.com/jimeh/undent/commit/84715b90d000292e4e4fbe081e00a583acf0dada))
|
||||||
|
|
||||||
|
## [1.1.1](https://github.com/jimeh/undent/compare/v1.1.0...v1.1.1) (2022-12-05)
|
||||||
|
|
||||||
|
|
||||||
|
### Performance Improvements
|
||||||
|
|
||||||
|
* simplify line-feed and minimum indentation loops ([178d9b5](https://github.com/jimeh/undent/commit/178d9b5bf6dd090a406229e862c5da1ba26cbf5d))
|
||||||
|
|
||||||
## [1.1.0](https://github.com/jimeh/undent/compare/v1.0.2...v1.1.0) (2021-02-22)
|
## [1.1.0](https://github.com/jimeh/undent/compare/v1.0.2...v1.1.0) (2021-02-22)
|
||||||
|
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -48,7 +48,7 @@ endef
|
|||||||
|
|
||||||
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc))
|
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc))
|
||||||
$(eval $(call tool,gofumports,mvdan.cc/gofumpt/gofumports))
|
$(eval $(call tool,gofumports,mvdan.cc/gofumpt/gofumports))
|
||||||
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.37))
|
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50))
|
||||||
|
|
||||||
.PHONY: tools
|
.PHONY: tools
|
||||||
tools: $(TOOLS)
|
tools: $(TOOLS)
|
||||||
|
|||||||
30
README.md
30
README.md
@@ -9,29 +9,13 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://pkg.go.dev/github.com/jimeh/undent">
|
<a href="https://pkg.go.dev/github.com/jimeh/undent"><img src="https://img.shields.io/badge/%E2%80%8B-reference-387b97.svg?logo=go&logoColor=white" alt="Go Reference"></a>
|
||||||
<img src="https://img.shields.io/badge/%E2%80%8B-reference-387b97.svg?logo=go&logoColor=white"
|
<a href="https://github.com/jimeh/undent/releases"><img src="https://img.shields.io/github/v/tag/jimeh/undent?label=release" alt="GitHub tag (latest SemVer)"></a>
|
||||||
alt="Go Reference">
|
<a href="https://github.com/jimeh/undent/actions"><img src="https://img.shields.io/github/actions/workflow/status/jimeh/undent/ci.yml?logo=github" alt="Actions Status"></a>
|
||||||
</a>
|
<a href="https://codeclimate.com/github/jimeh/undent"><img src="https://img.shields.io/codeclimate/coverage/jimeh/undent.svg?logo=code%20climate" alt="Coverage"></a>
|
||||||
<a href="https://github.com/jimeh/undent/releases">
|
<a href="https://github.com/jimeh/undent/issues"><img src="https://img.shields.io/github/issues-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white" alt="GitHub issues"></a>
|
||||||
<img src="https://img.shields.io/github/v/tag/jimeh/undent?label=release" alt="GitHub tag (latest SemVer)">
|
<a href="https://github.com/jimeh/undent/pulls"><img src="https://img.shields.io/github/issues-pr-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests"></a>
|
||||||
</a>
|
<a href="https://github.com/jimeh/undent/blob/main/LICENSE"><img src="https://img.shields.io/github/license/jimeh/undent.svg?style=flat" alt="License Status"></a>
|
||||||
<a href="https://github.com/jimeh/undent/actions">
|
|
||||||
<img src="https://img.shields.io/github/workflow/status/jimeh/undent/CI.svg?logo=github" alt="Actions Status">
|
|
||||||
</a>
|
|
||||||
<a href="https://codeclimate.com/github/jimeh/undent">
|
|
||||||
<img src="https://img.shields.io/codeclimate/coverage/jimeh/undent.svg?logo=code%20climate" alt="Coverage">
|
|
||||||
</a>
|
|
||||||
<a href="https://github.com/jimeh/undent/issues">
|
|
||||||
<img src="https://img.shields.io/github/issues-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white"
|
|
||||||
alt="GitHub issues">
|
|
||||||
</a>
|
|
||||||
<a href="https://github.com/jimeh/undent/pulls">
|
|
||||||
<img src="https://img.shields.io/github/issues-pr-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests">
|
|
||||||
</a>
|
|
||||||
<a href="https://github.com/jimeh/undent/blob/main/LICENSE">
|
|
||||||
<img src="https://img.shields.io/github/license/jimeh/undent.svg?style=flat" alt="License Status">
|
|
||||||
</a>
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
14
release-please-config.json
Normal file
14
release-please-config.json
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"release-type": "go",
|
||||||
|
"bump-minor-pre-major": true,
|
||||||
|
"bump-patch-for-minor-pre-major": true,
|
||||||
|
"packages": {
|
||||||
|
".": {
|
||||||
|
"changelog-path": "CHANGELOG.md",
|
||||||
|
"draft": false,
|
||||||
|
"prerelease": false,
|
||||||
|
"extra-files": []
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json"
|
||||||
|
}
|
||||||
50
undent.go
50
undent.go
@@ -5,6 +5,7 @@ package undent
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -20,50 +21,37 @@ func Bytes(s string) []byte {
|
|||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find line feeds
|
// find smallest indent relative to each line-feed
|
||||||
lfs := []int{}
|
min := -1
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
lfs := make([]int, 0, strings.Count(s, "\n"))
|
||||||
if s[0] != lf {
|
if s[0] != lf {
|
||||||
lfs = append(lfs, -1)
|
lfs = append(lfs, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
indent := 0
|
||||||
for i := 0; i < len(s); i++ {
|
for i := 0; i < len(s); i++ {
|
||||||
if s[i] == lf {
|
if s[i] == lf {
|
||||||
lfs = append(lfs, i)
|
lfs = append(lfs, i)
|
||||||
}
|
indent = 0
|
||||||
}
|
} else if indent < min || min == -1 {
|
||||||
|
switch s[i] {
|
||||||
// find smallest indent relative to each line-feed
|
|
||||||
min := 99999999999
|
|
||||||
count := 0
|
|
||||||
|
|
||||||
for i := 0; i < len(lfs); i++ {
|
|
||||||
offset := lfs[i]
|
|
||||||
end := len(s) - 1
|
|
||||||
if i+1 < len(lfs) {
|
|
||||||
end = lfs[i+1]
|
|
||||||
}
|
|
||||||
|
|
||||||
if offset+1 >= end {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
indent := 0
|
|
||||||
lineSeek:
|
|
||||||
for n := offset + 1; n < end && indent < min; n++ {
|
|
||||||
switch s[n] {
|
|
||||||
case spc, tab:
|
case spc, tab:
|
||||||
indent++
|
indent++
|
||||||
default:
|
default:
|
||||||
break lineSeek
|
if indent > 0 {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
if indent < min || min == -1 {
|
||||||
|
min = indent
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if indent < min {
|
}
|
||||||
min = indent
|
|
||||||
}
|
if min == -1 {
|
||||||
if indent > 0 {
|
return []byte(s)
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// extract each line without indentation
|
// extract each line without indentation
|
||||||
|
|||||||
@@ -717,7 +717,7 @@ func TestPrint(t *testing.T) {
|
|||||||
for _, tt := range stringTestCases {
|
for _, tt := range stringTestCases {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := fakeio.Stdout().Do(func() {
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
Print(tt.s, 5, tt.s)
|
_, _ = Print(tt.s, 5, tt.s)
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -731,7 +731,7 @@ func TestPrintf(t *testing.T) {
|
|||||||
for _, tt := range stringfTestCases {
|
for _, tt := range stringfTestCases {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got, err := fakeio.Stdout().Do(func() {
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
Printf(tt.s, tt.a...)
|
_, _ = Printf(tt.s, tt.a...)
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@@ -746,7 +746,7 @@ func TestFprint(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
Fprint(&buf, tt.s, 5, tt.s)
|
_, _ = Fprint(&buf, tt.s, 5, tt.s)
|
||||||
got := buf.String()
|
got := buf.String()
|
||||||
|
|
||||||
assert.IsType(t, "", got)
|
assert.IsType(t, "", got)
|
||||||
@@ -760,7 +760,7 @@ func TestFprintf(t *testing.T) {
|
|||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
||||||
Fprintf(&buf, tt.s, tt.a...)
|
_, _ = Fprintf(&buf, tt.s, tt.a...)
|
||||||
got := buf.String()
|
got := buf.String()
|
||||||
|
|
||||||
assert.IsType(t, "", got)
|
assert.IsType(t, "", got)
|
||||||
|
|||||||
Reference in New Issue
Block a user