Skip to content

Commit

Permalink
implement 0.9.0 behavior (#39)
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <[email protected]>
  • Loading branch information
greut committed Nov 2, 2019
1 parent 02484c2 commit 635a992
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 51 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ test-go:
go test -v ./...

test-core: editorconfig
cd core-test; cmake ..
cd core-test; ctest -E "^(comments_after_section|(escaped_)?octothorpe_(in_|comments_).*|indent_size_default_pre_0.9.0|root_file_mixed_case)$$" .
cd core-test; \
cmake ..
cd core-test; \
ctest \
-E "^(comments_after_section|(escaped_)?octothorpe_(in_|comments_).*|root_file_mixed_case)$$" \
--output-on-failure \
.
14 changes: 6 additions & 8 deletions cmd/editorconfig/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"path/filepath"

"gopkg.in/ini.v1"

Expand Down Expand Up @@ -42,12 +41,11 @@ func main() {
}

for _, file := range rest {
absolutePath, err := filepath.Abs(file)
if err != nil {
log.Fatal(err)
}

def, err := editorconfig.GetDefinitionForFilenameWithConfigname(absolutePath, configName)
def, err := editorconfig.NewDefinition(editorconfig.Config{
Path: file,
Name: configName,
Version: configVersion,
})
if err != nil {
log.Fatal(err)
}
Expand All @@ -59,7 +57,7 @@ func main() {
if len(rest) < 2 {
def.Selector = ini.DEFAULT_SECTION
} else {
def.Selector = absolutePath
def.Selector = file
}
def.InsertToIniFile(iniFile)
_, err = iniFile.WriteTo(os.Stdout)
Expand Down
130 changes: 89 additions & 41 deletions editorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"strings"

"github.com/blang/semver"
"gopkg.in/ini.v1"
)

Expand Down Expand Up @@ -48,6 +49,85 @@ const (
MaxValueLength = 255
)

var (
v0_10_0 = semver.Version{
Major: 0,
Minor: 10,
Patch: 0,
}
)

// Config holds the configuration
type Config struct {
Path string
Name string
Version string
}

// NewDefinition builds a definition from a given config
func NewDefinition(config Config) (*Definition, error) {
if config.Name == "" {
config.Name = ConfigNameDefault
}

abs, err := filepath.Abs(config.Path)
if err != nil {
return nil, err
}

config.Path = abs

return newDefinition(config)
}

// newDefinition recursively builds the definition
func newDefinition(config Config) (*Definition, error) {
definition := &Definition{}
definition.Raw = make(map[string]string)

if config.Version != "" {
version, err := semver.New(config.Version)
if err != nil {
return nil, err
}
definition.version = version
}

dir := config.Path
for dir != filepath.Dir(dir) {
dir = filepath.Dir(dir)
ecFile := filepath.Join(dir, config.Name)
fp, err := os.Open(ecFile)
if os.IsNotExist(err) {
continue
}
defer fp.Close()
ec, err := Parse(fp)
if err != nil {
return nil, err
}

relativeFilename := config.Path
if len(dir) < len(relativeFilename) {
relativeFilename = relativeFilename[len(dir):]
}

def, err := ec.GetDefinitionForFilename(relativeFilename)
if err != nil {
return nil, err
}

definition.merge(def)

if ec.Root {
break
}
}

return definition, nil

}

// Definition represents a definition inside the .editorconfig file.
// E.g. a section of the file.
// The definition is composed of the selector ("*", "*.go", "*.{js.css}", etc),
Expand All @@ -63,6 +143,7 @@ type Definition struct {
TrimTrailingWhitespace *bool `ini:"-" json:"-"`
InsertFinalNewline *bool `ini:"-" json:"-"`
Raw map[string]string `ini:"-" json:"-"`
version *semver.Version
}

// Editorconfig represents a .editorconfig file.
Expand Down Expand Up @@ -268,7 +349,7 @@ func (d *Definition) InsertToIniFile(iniFile *ini.File) {
// do nothing
} else if d.TabWidth > 0 {
iniSec.Key("indent_size").SetValue(strconv.Itoa(d.TabWidth))
} else if d.IndentStyle == IndentStyleTab {
} else if d.IndentStyle == IndentStyleTab && (d.version == nil || d.version.GTE(v0_10_0)) {
iniSec.Key("indent_size").SetValue(IndentStyleTab)
}
}
Expand Down Expand Up @@ -364,7 +445,9 @@ func (e *Editorconfig) Save(filename string) error {
// folder with `root = true`, and returns the right editorconfig
// definition for the given file.
func GetDefinitionForFilename(filename string) (*Definition, error) {
return GetDefinitionForFilenameWithConfigname(filename, ConfigNameDefault)
return NewDefinition(Config{
Path: filename,
})
}

// GetDefinitionForFilenameWithConfigname given a filename and a configname,
Expand All @@ -373,43 +456,8 @@ func GetDefinitionForFilename(filename string) (*Definition, error) {
// folder with `root = true`, and returns the right editorconfig
// definition for the given file.
func GetDefinitionForFilenameWithConfigname(filename string, configname string) (*Definition, error) {
abs, err := filepath.Abs(filename)
if err != nil {
return nil, err
}
definition := &Definition{}
definition.Raw = make(map[string]string)

dir := abs
for dir != filepath.Dir(dir) {
dir = filepath.Dir(dir)
ecFile := filepath.Join(dir, configname)
fp, err := os.Open(ecFile)
if os.IsNotExist(err) {
continue
}
defer fp.Close()
ec, err := Parse(fp)
if err != nil {
return nil, err
}

relativeFilename := filename
if len(dir) < len(abs) {
relativeFilename = abs[len(dir):]
}

def, err := ec.GetDefinitionForFilename(relativeFilename)
if err != nil {
return nil, err
}

definition.merge(def)

if ec.Root {
break
}
}

return definition, nil
return NewDefinition(Config{
Path: filename,
Name: configname,
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/editorconfig/editorconfig-core-go/v2
go 1.12

require (
github.com/blang/semver v3.5.1+incompatible
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
github.com/stretchr/testify v1.3.0
gopkg.in/ini.v1 v1.42.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
Expand Down

0 comments on commit 635a992

Please sign in to comment.