README.md

# DirectivesSorter

Small Mix formatter plugin that normalizes Elixir directives for consistent style.

It:
- Moves all directives (`use`, `import`, `alias`, `require`) in a module to the top (right after `@moduledoc` if present).
- Orders categories as: `use`, then `import`, then `alias`, then `require`.
- Preserves original order for `use` and `import` directives.
- Sorts `alias` and `require` directives within their categories by module name (stable, case-insensitive). For `alias as: X`, sorts by the visible alias first.
- Sorts items inside alias groups, preserving attached comments.

## Installation

Add to your `mix.exs`:

```elixir
defp deps do
  [
    {:directives_sorter, "~> 0.1.4", only: [:dev, :test], runtime: false},
  ]
end
```

Then run:

```sh
mix deps.get
```

## Usage

1) Add the plugin to your `.formatter.exs`:

```elixir
[
  plugins: [DirectivesSorter],
  inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
```

2) Run `mix format` or format from your editor. Examples:

Before (mixed order):

```elixir
alias MyApp.Users.{Zed, Alpha, Beta}
require MyApp.Logger
@attr :foo
import MyApp.Util, only: [foo: 1]
use MyApp.Feature
```

After formatting (note: `use` and `import` relative order is preserved; `alias` and `require` are sorted):

```elixir
use MyApp.Feature

import MyApp.Util, only: [foo: 1]

alias MyApp.Users.{Alpha, Beta, Zed}

require MyApp.Logger

@attr :foo
```

For more details see the [Specification](SPEC.md).