aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 531dd033867b66f8bfb89185122f4f1a8c9486c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# 🚀 Iku

> Grammar-Aware Go Formatter

Iku is a grammar-based Go formatter that enforces consistent blank-line placement by AST node type.

## Philosophy

Code structure should be visually apparent from its formatting. Iku groups statements by grammatical type and separates them with blank lines, making the code flow easier to read at a glance.

## Rules

1. **Same AST type means no blank line**: Consecutive statements of the same type stay together
2. **Different AST type means blank line**: Transitions between statement types get visual separation
3. **Scoped statements get blank lines**: `if`, `for`, `switch`, `select` always have blank lines before them
4. **Top-level declarations are separated**: Functions, types, and variables at the package level get blank lines between them

## How It Works

Iku applies standard Go formatting (via [go/format](https://pkg.go.dev/go/format)) first ([formatter.go#L33](https://github.com/Fuwn/iku/blob/main/formatter.go#L33)), then adds its grammar-based blank-line rules on top. Your code gets `go fmt` output plus structural separation.

## Installation

```bash
go install github.com/Fuwn/iku@latest
```

Or run with Nix:

```bash
nix run github:Fuwn/iku
```

## Usage

```bash
# Format stdin
echo 'package main...' | iku

# Format and print to stdout
iku file.go

# Format in-place
iku -w file.go

# Format entire directory
iku -w ./...

# List files that need formatting
iku -l .

# Show diff
iku -d file.go
```

### Flags

| Flag | Description |
|------|-------------|
| `-w` | Write result to file instead of stdout |
| `-l` | List files whose formatting differs |
| `-d` | Display diffs instead of rewriting |
| `--comments` | Comment attachment mode: `follow`, `precede`, `standalone` |
| `--version` | Print version |

## Examples

### Before

```go
package main

func main() {
    x := 1
    y := 2
    var config = loadConfig()
    defer cleanup()
    defer closeDB()
    if err != nil {
        return err
    }
    if x > 0 {
        process(x)
    }
    go worker()
    return nil
}
```

### After

```go
package main

func main() {
    x := 1
    y := 2

    var config = loadConfig()

    defer cleanup()
    defer closeDB()

    if err != nil {
        return err
    }

    if x > 0 {
        process(x)
    }

    go worker()

    return nil
}
```

Notice how:
- `x := 1` and `y := 2` (both `AssignStmt`) stay together
- `var config` (`DeclStmt`) gets separated from assignments
- `defer` statements stay grouped together
- Each `if` statement gets a blank line before it (scoped statement)
- `go worker()` (`GoStmt`) is separated from the `if` above
- `return` (`ReturnStmt`) is separated from the `go` statement

### Top-Level Declarations

```go
// Before
package main
type Config struct {
    Name string
}
var defaultConfig = Config{}
func main() {
    run()
}
func run() {
    process()
}

// After
package main

type Config struct {
    Name string
}

var defaultConfig = Config{}

func main() {
    run()
}

func run() {
    process()
}
```

### Switch Statements

```go
// Before
func process(x int) {
    result := compute(x)
    switch result {
    case 1:
        handleOne()
        if needsExtra {
            doExtra()
        }
    case 2:
        handleTwo()
    }
    cleanup()
}

// After
func process(x int) {
    result := compute(x)

    switch result {
    case 1:
        handleOne()

        if needsExtra {
            doExtra()
        }
    case 2:
        handleTwo()
    }

    cleanup()
}
```

## AST Node Types

For reference, here are common Go statement types that Iku distinguishes:

| Type | Examples |
|------|----------|
| `*ast.AssignStmt` | `x := 1`, `x = 2` |
| `*ast.DeclStmt` | `var x = 1` |
| `*ast.ExprStmt` | `fmt.Println()`, `doSomething()` |
| `*ast.ReturnStmt` | `return x` |
| `*ast.IfStmt` | `if x > 0 { }` |
| `*ast.ForStmt` | `for i := 0; i < n; i++ { }` |
| `*ast.RangeStmt` | `for k, v := range m { }` |
| `*ast.SwitchStmt` | `switch x { }` |
| `*ast.SelectStmt` | `select { }` |
| `*ast.DeferStmt` | `defer f()` |
| `*ast.GoStmt` | `go f()` |
| `*ast.SendStmt` | `ch <- x` |

## License

This project is licensed under the [GNU General Public License v3.0](./LICENSE.txt).