Skip to content

Commit d015a99

Browse files
committed
docs docs docs docs docs!
1 parent ceb827a commit d015a99

File tree

5 files changed

+149
-69
lines changed

5 files changed

+149
-69
lines changed

README.md

+10-69
Original file line numberDiff line numberDiff line change
@@ -11,77 +11,18 @@ You can learn more about the history, purpose and implementation of Styler from
1111

1212
## Features
1313

14-
### AST Rewrites as part of `mix format`
14+
Styler fixes a plethora of elixir style and optimization issues automatically as part of mix format.
1515

16-
[See our Rewrites documentation on hexdocs](https://hexdocs.pm/styler/styles.html)
17-
Styler fixes a plethora of elixir style and optimization issues automatically as part of `mix format`. In addition to automating corrections for [many credo rules](docs/credo.md) (meaning you can turn them off to speed credo up), Styler:
16+
[See Styler's documentation on Hex](https://hexdocs.pm/styler/styles.html) for the comprehensive list of its features.
1817

19-
- [keeps a strict module layout](docs/module_directives.md#directive-organization)
20-
- alphabetizes module directives
21-
- [extracts repeated aliases](docs/module_directives.md#alias-lifting)
22-
- [makes your pipe chains pretty as can be](docs/pipes.md)
23-
- pipes and unpipes function calls based on the number of calls
24-
- optimizes standard library calls (`a |> Enum.map(m) |> Enum.into(Map.new)` => `Map.new(a, m)`)
25-
- replaces strings with sigils when the string has many escaped quotes
26-
- ... and so much more
18+
The fastest way to see what all it can do you for you is to just try it out in your codebase... but here's a list of a few features to help you decide if you're interested in Styler:
2719

28-
### Maintain static list order via `# styler:sort`
29-
30-
Styler can keep static values sorted for your team as part of its formatting pass. To instruct it to do so, replace any `# Please keep this list sorted!` notes you wrote to your teammates with `# styler:sort`.
31-
32-
#### Examples
33-
34-
```elixir
35-
# styler:sort
36-
[:c, :a, :b]
37-
38-
# styler:sort
39-
~w(a list of words)
40-
41-
# styler:sort
42-
@country_codes ~w(
43-
en_US
44-
po_PO
45-
fr_CA
46-
ja_JP
47-
)
48-
49-
# styler:sort
50-
a_var =
51-
[
52-
Modules,
53-
In,
54-
A,
55-
List
56-
]
57-
```
58-
59-
Would yield:
60-
61-
```elixir
62-
# styler:sort
63-
[:a, :b, :c]
64-
65-
# styler:sort
66-
~w(a list of words)
67-
68-
# styler:sort
69-
@country_codes ~w(
70-
en_US
71-
fr_CA
72-
ja_JP
73-
po_PO
74-
)
75-
76-
# styler:sort
77-
a_var =
78-
[
79-
A,
80-
In,
81-
List,
82-
Modules
83-
]
84-
```
20+
- sorts and organizes `import`/`alias`/`require` and other [module directives](docs/module_directives.md)
21+
- keeps lists, sigils, and even arbitrary code sorted with the `# styler:sort` [comment directive](docs/comment_directives.md)
22+
- automatically creates aliases for repeatedly referenced modules names ([_"alias lifting"_](docs/module_directives.md#alias-lifting))
23+
- optimizes pipe chains for [readability and performance](docs/pipes.md)
24+
- rewrites strings as sigils when it results in fewer escapes
25+
- auto-fixes [many credo rules](docs/credo.md), meaning you can spend less time fighting with CI
8526

8627
## Who is Styler for?
8728

@@ -101,7 +42,7 @@ Add `:styler` as a dependency to your project's `mix.exs`:
10142
```elixir
10243
def deps do
10344
[
104-
{:styler, "~> 1.2", only: [:dev, :test], runtime: false},
45+
{:styler, "~> 1.4", only: [:dev, :test], runtime: false},
10546
]
10647
end
10748
```

docs/comment_directives.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Comment Directives
2+
3+
Comment Directives are a Styler feature that let you instruct Styler to do maintain additional formatting via comments.
4+
5+
The plural in the name is optimistic as there's currently only one, but who knows
6+
7+
### `# styler:sort`
8+
9+
Styler can keep static values sorted for your team as part of its formatting pass. To instruct it to do so, replace any `# Please keep this list sorted!` notes you wrote to your teammates with `# styler:sort`
10+
11+
Sorting is done via string comparison of the code.
12+
13+
Styler knows how to sort the following things:
14+
15+
- lists of elements
16+
- arbitrary code within `do end` blocks (helpful for schema-like macros)
17+
- `~w` sigils elements
18+
- keyword shapes (structs, maps, and keywords)
19+
20+
Since you can't have comments in arbitrary places when using Elixir's formatter,
21+
Styler will apply those sorts when they're on the righthand side fo the following operators:
22+
23+
- module directives (eg `@my_dir ~w(a list of things)`)
24+
- assignments (eg `x = ~w(a list again)`)
25+
- `defstruct`
26+
27+
#### Examples
28+
29+
**Before**
30+
31+
```elixir
32+
# styler:sort
33+
[:c, :a, :b]
34+
35+
# styler:sort
36+
~w(a list of words)
37+
38+
# styler:sort
39+
@country_codes ~w(
40+
en_US
41+
po_PO
42+
fr_CA
43+
ja_JP
44+
)
45+
46+
# styler:sort
47+
a_var =
48+
[
49+
Modules,
50+
In,
51+
A,
52+
List
53+
]
54+
55+
# styler:sort
56+
my_macro "some arg" do
57+
another_macro :q
58+
another_macro :w
59+
another_macro :e
60+
another_macro :r
61+
another_macro :t
62+
another_macro :y
63+
end
64+
```
65+
66+
**After**
67+
68+
```elixir
69+
# styler:sort
70+
[:a, :b, :c]
71+
72+
# styler:sort
73+
~w(a list of words)
74+
75+
# styler:sort
76+
@country_codes ~w(
77+
en_US
78+
fr_CA
79+
ja_JP
80+
po_PO
81+
)
82+
83+
# styler:sort
84+
a_var =
85+
[
86+
A,
87+
In,
88+
List,
89+
Modules
90+
]
91+
92+
# styler:sort
93+
my_macro "some arg" do
94+
another_macro :e
95+
another_macro :q
96+
another_macro :r
97+
another_macro :t
98+
another_macro :w
99+
another_macro :y
100+
end
101+
```

docs/deprecations.md

+23
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,33 @@ This is covered by the Elixir Formatter with the `--migrate` flag, but Styler br
2525

2626
Rewrite `unless x` to `if !x`
2727

28+
### 1.19
29+
30+
#### Change Struct Updates to Map Updates (Experimental)
31+
32+
1.19 deprecates struct update syntax in favor of map update syntax. Styler will do this update for you if you're on Elixir 1.19.0-dev or later.
33+
34+
```elixir
35+
# This
36+
%Struct{x | y}
37+
# Styles to this
38+
%{x | y}
39+
```
40+
41+
**WARNING** Double check your diffs to make sure your variable is pattern matching against the same struct if you want to harness 1.18's type checking features.
42+
43+
A future version of Styler may be smart enough to do this check for you and perform the appropriate updates to the assignment location; no guarantees though. Track via #199, h/t @SteffenDE
44+
45+
### 1.18
46+
47+
None?
48+
2849
### 1.17
2950

3051
[1.17 Deprecations](https://hexdocs.pm/elixir/1.17.0/changelog.html#4-hard-deprecations)
3152

53+
- Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`
54+
3255
#### Range Matching Without Step
3356

3457
```elixir

docs/module_directives.md

+14
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,20 @@ C.foo()
162162
C.bar()
163163
```
164164

165+
Styler also notices when you have a module aliased and aren't employing that alias and will do the updates for you.
166+
167+
```elixir
168+
# Given
169+
alias My.Apps.Widget
170+
171+
x = Repo.get(My.Apps.Widget, id)
172+
173+
# Styled
174+
alias My.Apps.Widget
175+
176+
x = Repo.get(Widget, id)
177+
```
178+
165179
### Collisions
166180

167181
Styler won't lift aliases that will collide with existing aliases, and likewise won't lift any module whose name would collide with a standard library name.

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ defmodule Styler.MixProject do
7070
"docs/control_flow_macros.md": [title: "Control Flow Macros (if, case, ...)"],
7171
"docs/mix_configs.md": [title: "Mix Configs (config/*.exs)"],
7272
"docs/module_directives.md": [title: "Module Directives (use, alias, ...)"],
73+
"docs/comment_directives.md": [title: "Comment Directives (# styler:sort)"],
7374
"docs/credo.md": [title: "Styler & Credo"],
7475
"README.md": [title: "Styler"]
7576
]

0 commit comments

Comments
 (0)