aptinio

aptinio

def_layout - deterministic function layout via mix format

I just published def_layout, a formatter plugin that lays out a module’s functions: callbacks first in source order, public functions alphabetical by name and arity, each private function just below its bottom-most caller.

Here’s the before/after that shows the tradeoff, on a GenServer:

# before (hand-ordered)
def start_link    # the entry point, conventionally first

@impl GenServer
def init

@impl GenServer
def handle_call({:get, key}, _from, state) do
  {:reply, fetch(state, key), state}
end

def take
def get
defp fetch
# after
@impl GenServer   # callbacks first, in source order
def init          # (alphabetical would flip these two)

@impl GenServer   # the attribute rides along
def handle_call({:get, key}, _from, state) do
  {:reply, fetch(state, key), state}
end

defp fetch        # private, sinks under its caller

def get           # publics, alphabetical
def start_link
def take

Yes, start_link lands mid-pack, wherever the alphabet drops it. That’s the honest cost of the rule, shown up front. Once you know publics are alphabetical you stop scanning for start_link; you jump to it. (The README FAQ takes this objection head-on - the section is literally titled start_link should be first.”)

The payoff is the private layout: each private function directly under its bottom-most caller means you read a function and its helpers are right there, in call order, instead of scattered across the file or piled at the bottom.

def_layout orders by AST but moves source text by line span, so comments and attached @doc/@spec/@impl ride along with their function. Quokka and Styler order the directive block; def_layout orders the functions below it. They compose - list DefLayout first, so the other plugin formats last. (The README has the full comparison.)

I ran it across fourteen codebases before publishing - Phoenix and LiveView apps, Ash, Absinthe, Plug, Nx, and my own production code. The README’s “Tested on real code” section has the methodology, and the sweep script ships in the repo so you can point it at your own tree. Modules it can’t reorder safely (an Ecto schema, a plug pipeline) are skipped - still formatted, just not laid out - and mix def_layout.skipped lists what was left alone and why.

Works with LSP format-on-save (tested with Expert in Neovim).

Setup is two lines:

# mix.exs
{:def_layout, "~> 0.1.0", only: [:dev, :test], runtime: false}

# .formatter.exs
[
  plugins: [DefLayout]
]

There are no ordering knobs. If you think public function order carries meaning worth maintaining by hand, this isn’t the tool for you. That’s by design. (The FAQ covers that one too.)

Requires Elixir 1.16+.

Hex: def_layout | Hex
Docs: def_layout v0.1.0 — Documentation

Where Next?

Popular in Announcing Top

kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 30338 241
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10719 134
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement