You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. [![GoDoc](https://godoc.org/gopkg.in/editorconfig/editorconfig-core-go.v1?status.svg)](https://godoc.org/gopkg.in/editorconfig/editorconfig-core-go.v1)
  2. [![Go Report Card](https://goreportcard.com/badge/gopkg.in/editorconfig/editorconfig-core-go.v1)](https://goreportcard.com/report/gopkg.in/editorconfig/editorconfig-core-go.v1)
  3. # Editorconfig Core Go
  4. A [Editorconfig][editorconfig] file parser and manipulator for Go.
  5. > This package is already working, but still under testing.
  6. ## Installing
  7. We recommend the use of [gopkg.in][gopkg] for this package:
  8. ```bash
  9. go get -u gopkg.in/editorconfig/editorconfig-core-go.v1
  10. ```
  11. Import by the same path. Tha package name you will use to access it is
  12. `editorconfig`.
  13. ```go
  14. import (
  15. "gopkg.in/editorconfig/editorconfig-core-go.v1"
  16. )
  17. ```
  18. ## Usage
  19. ### Parse from file
  20. ```go
  21. editorConfig, err := editorconfig.ParseFile("path/to/.editorconfig")
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. ```
  26. ### Parse from slice of bytes
  27. ```go
  28. data := []byte("...")
  29. editorConfig, err := editorconfig.ParseBytes(data)
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. ```
  34. ### Get definition to a given filename
  35. This method builds a definition to a given filename.
  36. This definition is a merge of the properties with selectors that matched the
  37. given filename.
  38. The lasts sections of the file have preference over the priors.
  39. ```go
  40. def := editorConfig.GetDefinitionForFilename("my/file.go")
  41. ```
  42. This definition have the following properties:
  43. ```go
  44. type Definition struct {
  45. Selector string
  46. Charset string
  47. IndentStyle string
  48. IndentSize string
  49. TabWidth int
  50. EndOfLine string
  51. TrimTrailingWhitespace bool
  52. InsertFinalNewline bool
  53. }
  54. ```
  55. #### Automatic search for `.editorconfig` files
  56. If you want a definition of a file without having to manually
  57. parse the `.editorconfig` files, you can then use the static version
  58. of `GetDefinitionForFilename`:
  59. ```go
  60. def, err := editorconfig.GetDefinitionForFilename("foo/bar/baz/my-file.go")
  61. ```
  62. In the example above, the package will automatically search for
  63. `.editorconfig` files on:
  64. - `foo/bar/baz/.editorconfig`
  65. - `foo/baz/.editorconfig`
  66. - `foo/.editorconfig`
  67. Until it reaches a file with `root = true` or the root of the filesystem.
  68. ### Generating a .editorconfig file
  69. You can easily convert a Editorconfig struct to a compatible INI file:
  70. ```go
  71. // serialize to slice of bytes
  72. data, err := editorConfig.Serialize()
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. // save directly to file
  77. err := editorConfig.Save("path/to/.editorconfig")
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. ```
  82. ## Contributing
  83. To run the tests:
  84. ```bash
  85. go test -v
  86. ```
  87. [editorconfig]: http://editorconfig.org/
  88. [gopkg]: https://gopkg.in