| 1 |
 |
| 2 |
[](https://pkg.go.dev/github.com/editorconfig/editorconfig-core-go/v2) |
| 3 |
[](https://goreportcard.com/report/github.com/editorconfig/editorconfig-core-go) |
| 4 |
|
| 5 |
# Editorconfig Core Go |
| 6 |
|
| 7 |
A [Editorconfig][editorconfig] file parser and manipulator for Go. |
| 8 |
|
| 9 |
## Missing features |
| 10 |
|
| 11 |
- escaping comments in values, probably in [go-ini/ini](https://github.com/go-ini/ini) |
| 12 |
- [adjacent nested braces](https://github.com/editorconfig/editorconfig-core-test/pull/44) |
| 13 |
|
| 14 |
## Installing |
| 15 |
|
| 16 |
We recommend the use of Go 1.17+ modules for this package. Lower versions, such as 1.13, should be fine. |
| 17 |
|
| 18 |
Import by the same path. The package name you will use to access it is |
| 19 |
`editorconfig`. |
| 20 |
|
| 21 |
```go |
| 22 |
import "github.com/editorconfig/editorconfig-core-go/v2" |
| 23 |
``` |
| 24 |
|
| 25 |
## Usage |
| 26 |
|
| 27 |
### Parse from a file |
| 28 |
|
| 29 |
```go |
| 30 |
fp, err := os.Open("path/to/.editorconfig") |
| 31 |
if err != nil { |
| 32 |
log.Fatal(err) |
| 33 |
} |
| 34 |
defer fp.Close() |
| 35 |
|
| 36 |
editorConfig, err := editorconfig.Parse(fp) |
| 37 |
if err != nil { |
| 38 |
log.Fatal(err) |
| 39 |
} |
| 40 |
``` |
| 41 |
|
| 42 |
### Graceful parsing from a file |
| 43 |
|
| 44 |
```go |
| 45 |
fp, err := os.Open("path/to/.editorconfig") |
| 46 |
if err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
defer fp.Close() |
| 50 |
|
| 51 |
editorConfig, warning, err := editorconfig.ParseGraceful(fp) |
| 52 |
if err != nil { |
| 53 |
log.Fatal(err) |
| 54 |
} |
| 55 |
|
| 56 |
// Log the warning(s) encountered while reading the editorconfig file |
| 57 |
if warning != nil { |
| 58 |
log.Print(warning) |
| 59 |
} |
| 60 |
``` |
| 61 |
|
| 62 |
### Parse from slice of bytes |
| 63 |
|
| 64 |
```go |
| 65 |
data := []byte("...") |
| 66 |
editorConfig, err := editorconfig.ParseBytes(data) |
| 67 |
if err != nil { |
| 68 |
log.Fatal(err) |
| 69 |
} |
| 70 |
``` |
| 71 |
|
| 72 |
### Get definition to a given filename |
| 73 |
|
| 74 |
This method builds a definition to a given filename. |
| 75 |
This definition is a merge of the properties with selectors that matched the |
| 76 |
given filename. |
| 77 |
The lasts sections of the file have preference over the priors. |
| 78 |
|
| 79 |
```go |
| 80 |
def := editorConfig.GetDefinitionForFilename("my/file.go") |
| 81 |
``` |
| 82 |
|
| 83 |
This definition have the following properties: |
| 84 |
|
| 85 |
```go |
| 86 |
type Definition struct { |
| 87 |
Selector string |
| 88 |
|
| 89 |
Charset string |
| 90 |
IndentStyle string |
| 91 |
IndentSize string |
| 92 |
TabWidth int |
| 93 |
EndOfLine string |
| 94 |
TrimTrailingWhitespace *bool |
| 95 |
InsertFinalNewline *bool |
| 96 |
Raw map[string]string |
| 97 |
} |
| 98 |
``` |
| 99 |
|
| 100 |
#### Automatic search for `.editorconfig` files |
| 101 |
|
| 102 |
If you want a definition of a file without having to manually |
| 103 |
parse the `.editorconfig` files, you can then use the static version |
| 104 |
of `GetDefinitionForFilename`: |
| 105 |
|
| 106 |
```go |
| 107 |
def, err := editorconfig.GetDefinitionForFilename("foo/bar/baz/my-file.go") |
| 108 |
``` |
| 109 |
|
| 110 |
In the example above, the package will automatically search for |
| 111 |
`.editorconfig` files on: |
| 112 |
|
| 113 |
- `foo/bar/baz/.editorconfig` |
| 114 |
- `foo/baz/.editorconfig` |
| 115 |
- `foo/.editorconfig` |
| 116 |
|
| 117 |
Until it reaches a file with `root = true` or the root of the filesystem. |
| 118 |
|
| 119 |
### Generating a .editorconfig file |
| 120 |
|
| 121 |
You can easily convert a Editorconfig struct to a compatible INI file: |
| 122 |
|
| 123 |
```go |
| 124 |
// serialize to slice of bytes |
| 125 |
data, err := editorConfig.Serialize() |
| 126 |
if err != nil { |
| 127 |
log.Fatal(err) |
| 128 |
} |
| 129 |
|
| 130 |
// save directly to file |
| 131 |
err := editorConfig.Save("path/to/.editorconfig") |
| 132 |
if err != nil { |
| 133 |
log.Fatal(err) |
| 134 |
} |
| 135 |
``` |
| 136 |
|
| 137 |
## Contributing |
| 138 |
|
| 139 |
To run the tests: |
| 140 |
|
| 141 |
```bash |
| 142 |
go test -v ./... |
| 143 |
``` |
| 144 |
|
| 145 |
To run the [integration tests](https://github.com/editorconfig/editorconfig-core-test): |
| 146 |
|
| 147 |
```bash |
| 148 |
make test-core |
| 149 |
``` |
| 150 |
|
| 151 |
[editorconfig]: https://editorconfig.org/ |