File: example_test.go

package info (click to toggle)
golang-mvdan-editorconfig 0.2.0%2Bgit20240816.eab549b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 388 kB
  • sloc: makefile: 9
file content (100 lines) | stat: -rw-r--r-- 1,641 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2019, Daniel Martí <mvdan@mvdan.cc>
// See LICENSE for licensing information

package editorconfig_test

import (
	"fmt"
	"strings"

	"mvdan.cc/editorconfig"
)

func ExampleFind() {
	props, err := editorconfig.Find("_sample/subdir/code.go", nil)
	if err != nil {
		panic(err)
	}
	fmt.Println(props)

	fmt.Println(props.Get("indent_style"))
	fmt.Println(props.IndentSize())
	fmt.Println(props.TrimTrailingWhitespace())
	fmt.Println(props.InsertFinalNewline())

	// Output:
	// indent_style=tab
	// indent_size=8
	// end_of_line=lf
	// insert_final_newline=true
	//
	// tab
	// 8
	// false
	// true
}

func ExampleParse() {
	config := `
root = true

[*] # match all files
end_of_line = lf
insert_final_newline = true

[*.go] # only match Go
indent_style = tab
indent_size = 8
`
	file, err := editorconfig.Parse(strings.NewReader(config))
	if err != nil {
		panic(err)
	}
	fmt.Println(file)

	// Output:
	// root=true
	//
	// [*]
	// end_of_line=lf
	// insert_final_newline=true
	//
	// [*.go]
	// indent_style=tab
	// indent_size=8
}

func ExampleFile_Filter_language() {
	config := `
[*]
end_of_line = lf

[[go]]
indent_style = tab
indent_size = 8

[*_test.go]
indent_size = 4
`
	file, err := editorconfig.Parse(strings.NewReader(config))
	if err != nil {
		panic(err)
	}

	fmt.Println("* main.go:")
	fmt.Println(file.Filter("main.go", []string{"go"}, nil))

	fmt.Println("* main_test.go:")
	fmt.Println(file.Filter("main_test.go", []string{"go"}, nil))

	// Output:
	// * main.go:
	// indent_style=tab
	// indent_size=8
	// end_of_line=lf
	//
	// * main_test.go:
	// indent_size=4
	// indent_style=tab
	// end_of_line=lf
}