• Stars
    star
    129
  • Rank 269,540 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 8 years ago
  • Updated 24 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

EditorConfig Core written in Go

Build Status Go Reference Go Report Card

Editorconfig Core Go

A Editorconfig file parser and manipulator for Go.

Missing features

Installing

We recommend the use of Go 1.17+ modules for this package. Lower versions, such as 1.13, should be fine.

Import by the same path. The package name you will use to access it is editorconfig.

import "github.com/editorconfig/editorconfig-core-go/v2"

Usage

Parse from a file

fp, err := os.Open("path/to/.editorconfig")
if err != nil {
	log.Fatal(err)
}
defer fp.Close()

editorConfig, err := editorconfig.Parse(fp)
if err != nil {
	log.Fatal(err)
}

Graceful parsing from a file

fp, err := os.Open("path/to/.editorconfig")
if err != nil {
	log.Fatal(err)
}
defer fp.Close()

editorConfig, warning, err := editorconfig.ParseGraceful(fp)
if err != nil {
	log.Fatal(err)
}

// Log the warning(s) encountered while reading the editorconfig file
if warning != nil {
	log.Print(warning)
}

Parse from slice of bytes

data := []byte("...")
editorConfig, err := editorconfig.ParseBytes(data)
if err != nil {
	log.Fatal(err)
}

Get definition to a given filename

This method builds a definition to a given filename. This definition is a merge of the properties with selectors that matched the given filename. The lasts sections of the file have preference over the priors.

def := editorConfig.GetDefinitionForFilename("my/file.go")

This definition have the following properties:

type Definition struct {
	Selector string

	Charset                string
	IndentStyle            string
	IndentSize             string
	TabWidth               int
	EndOfLine              string
	TrimTrailingWhitespace *bool
	InsertFinalNewline     *bool
	Raw                    map[string]string
}

Automatic search for .editorconfig files

If you want a definition of a file without having to manually parse the .editorconfig files, you can then use the static version of GetDefinitionForFilename:

def, err := editorconfig.GetDefinitionForFilename("foo/bar/baz/my-file.go")

In the example above, the package will automatically search for .editorconfig files on:

  • foo/bar/baz/.editorconfig
  • foo/baz/.editorconfig
  • foo/.editorconfig

Until it reaches a file with root = true or the root of the filesystem.

Generating a .editorconfig file

You can easily convert a Editorconfig struct to a compatible INI file:

// serialize to slice of bytes
data, err := editorConfig.Serialize()
if err != nil {
	log.Fatal(err)
}

// save directly to file
err := editorConfig.Save("path/to/.editorconfig")
if err != nil {
	log.Fatal(err)
}

Contributing

To run the tests:

go test -v ./...

To run the integration tests:

make test-core

More Repositories

1

editorconfig-vim

EditorConfig plugin for Vim
Vim Script
3,101
star
2

editorconfig

EditorConfig universal issue tracker and wiki
3,082
star
3

editorconfig-emacs

EditorConfig plugin for Emacs
Emacs Lisp
724
star
4

editorconfig-visualstudio

EditorConfig Visual Studio Addin
C#
352
star
5

editorconfig-core-c

EditorConfig core library written in C (for use by plugins supporting EditorConfig parsing)
C
292
star
6

editorconfig-jetbrains

A JetBrains IDE plugin supporting the EditorConfig standard
Java
292
star
7

editorconfig-core-js

EditorConfig Core library and command line utility written in JavaScript
TypeScript
266
star
8

editorconfig.github.com

Configuration file format for defining coding styles in shared projects
HTML
265
star
9

editorconfig-notepad-plus-plus

EditorConfig plugin for Notepad++
C++
186
star
10

editorconfig-core-py

Clone of EditorConfig core written in Python
Python
90
star
11

specification

Specification of EditorConfig file format
Python
83
star
12

editorconfig-core-net

EditorConfig Core library and command line utility written in C# for .NET/Mono http://editorconfig.org
C#
70
star
13

editorconfig-core-java

Clone of EditorConfig core written in Java
Java
60
star
14

editorconfig-gedit

EditorConfig plugin for Gedit
Python
54
star
15

editorconfig-geany

EditorConfig plugin for Geany
C
45
star
16

editorconfig-qtcreator

EditorConfig Plugin for QtCreator
C++
40
star
17

editorconfig-core-ruby

Clone of EditorConfig core written in Ruby
Ruby
27
star
18

editorconfig-core-test

Testings for EditorConfig Core
CMake
25
star
19

editorconfig-defaults

23
star
20

editorconfig-jedit

EditorConfig plugin for jEdit
Java
16
star
21

editorconfig-plugin-tests

Tests for EditorConfig plugins
Python
16
star
22

editorconfig-core-lua

EditorConfig Core bindings for Lua
CMake
15
star
23

live-demo

Work in progress: EditorConfig in-browser live demo
JavaScript
13
star
24

editorconfig-textadept

EditorConfig plugin for Textadept
Lua
11
star
25

utilities

Utilities for EditorConfig development
Ruby
11
star
26

editorconfig-codeblocks

EditorConfig plugin for Code::Blocks
C++
11
star
27

docs.editorconfig.org

EditorConfig C Core library documentation
JavaScript
10
star
28

editorconfig-core-qt

EditorConfig core written for Qt
C++
9
star
29

editorconfig-core-java-binding

EditorConfig Core Java Binding
Java
8
star
30

editorconfig-vote

The issue tracker is for voting purpose only. Only @editorconfig/board-member members can comment.
4
star
31

javadocs.editorconfig.org

EditorConfig library Java API documentation
CSS
3
star