Skip to content

Commit

Permalink
Format .edn files; add config option to select file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
cespare committed Jul 30, 2023
1 parent 4233a79 commit 63143dc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ This is a Clojure file containing a single map of options. Here's an example:

The configuration map may use the following keys:

### :extensions

This is a list of file extensions to format when cljfmt walks a directory.

If not set, the default list of extensions is

(".clj" ".cljs" ".cljc" ".edn")

### :indent-overrides

This is used to customize the indentation rules that cljfmt applies to
Expand Down
13 changes: 9 additions & 4 deletions cljfmt/cljfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ See the goclj README for more documentation of the available transforms.`)
}

type config struct {
extensions map[string]struct{}
indentOverrides map[string]format.IndentStyle
threadFirstOverrides map[string]format.ThreadFirstStyle
transforms map[format.Transform]bool
Expand All @@ -42,6 +43,12 @@ func main() {
p: defaultConfigPath(),
}
conf := config{
extensions: map[string]struct{}{
".clj": {},
".cljs": {},
".cljc": {},
".edn": {},
},
transforms: make(map[format.Transform]bool),
}
flag.Var(&configFile, "c", "path to config file")
Expand Down Expand Up @@ -234,10 +241,8 @@ func (c *config) walkDir(path string) {
if strings.HasPrefix(name, ".") {
return nil
}
for _, ext := range []string{".clj", ".cljs", ".cljc"} {
if strings.HasSuffix(name, ext) {
return c.processFile(path, nil)
}
if _, ok := c.extensions[filepath.Ext(name)]; ok {
return c.processFile(path, nil)
}
return nil // not a Clojure file
}
Expand Down
15 changes: 15 additions & 0 deletions cljfmt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ func (c *config) parseDotConfig(r io.Reader, name string) error {
continue
}
switch sym.Val {
case ":extensions":
c.extensions = make(map[string]struct{})
seq, err := sequence(m.Nodes[i+1])
if err != nil {
return err
}
for _, n := range seq {
ext, err := stringNode(n)
if err != nil {
return err
}
c.extensions[ext] = struct{}{}
}
case ":indent-overrides", ":thread-first-overrides":
seq, err := sequence(m.Nodes[i+1])
if err != nil {
Expand Down Expand Up @@ -74,6 +87,8 @@ func (c *config) parseDotConfig(r io.Reader, name string) error {
c.threadFirstOverrides[k] = style
}
}
default:
return fmt.Errorf("unknown configuration key %q", sym.Val)
}
}
return nil
Expand Down

0 comments on commit 63143dc

Please sign in to comment.