Skip to content

Commit 63143dc

Browse files
committed
Format .edn files; add config option to select file extensions
1 parent 4233a79 commit 63143dc

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ This is a Clojure file containing a single map of options. Here's an example:
169169

170170
The configuration map may use the following keys:
171171

172+
### :extensions
173+
174+
This is a list of file extensions to format when cljfmt walks a directory.
175+
176+
If not set, the default list of extensions is
177+
178+
(".clj" ".cljs" ".cljc" ".edn")
179+
172180
### :indent-overrides
173181

174182
This is used to customize the indentation rules that cljfmt applies to

cljfmt/cljfmt.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ See the goclj README for more documentation of the available transforms.`)
2828
}
2929

3030
type config struct {
31+
extensions map[string]struct{}
3132
indentOverrides map[string]format.IndentStyle
3233
threadFirstOverrides map[string]format.ThreadFirstStyle
3334
transforms map[format.Transform]bool
@@ -42,6 +43,12 @@ func main() {
4243
p: defaultConfigPath(),
4344
}
4445
conf := config{
46+
extensions: map[string]struct{}{
47+
".clj": {},
48+
".cljs": {},
49+
".cljc": {},
50+
".edn": {},
51+
},
4552
transforms: make(map[format.Transform]bool),
4653
}
4754
flag.Var(&configFile, "c", "path to config file")
@@ -234,10 +241,8 @@ func (c *config) walkDir(path string) {
234241
if strings.HasPrefix(name, ".") {
235242
return nil
236243
}
237-
for _, ext := range []string{".clj", ".cljs", ".cljc"} {
238-
if strings.HasSuffix(name, ext) {
239-
return c.processFile(path, nil)
240-
}
244+
if _, ok := c.extensions[filepath.Ext(name)]; ok {
245+
return c.processFile(path, nil)
241246
}
242247
return nil // not a Clojure file
243248
}

cljfmt/config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ func (c *config) parseDotConfig(r io.Reader, name string) error {
4545
continue
4646
}
4747
switch sym.Val {
48+
case ":extensions":
49+
c.extensions = make(map[string]struct{})
50+
seq, err := sequence(m.Nodes[i+1])
51+
if err != nil {
52+
return err
53+
}
54+
for _, n := range seq {
55+
ext, err := stringNode(n)
56+
if err != nil {
57+
return err
58+
}
59+
c.extensions[ext] = struct{}{}
60+
}
4861
case ":indent-overrides", ":thread-first-overrides":
4962
seq, err := sequence(m.Nodes[i+1])
5063
if err != nil {
@@ -74,6 +87,8 @@ func (c *config) parseDotConfig(r io.Reader, name string) error {
7487
c.threadFirstOverrides[k] = style
7588
}
7689
}
90+
default:
91+
return fmt.Errorf("unknown configuration key %q", sym.Val)
7792
}
7893
}
7994
return nil

0 commit comments

Comments
 (0)