aptinio

aptinio

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? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
woylie
Phoenix components for pagination, sortable tables and filter forms with Flop and (optionally) Ecto. pagination cursor pagination sorta...
New
kip
Please say hi to a new lib, Astro that aims to deliver easy-to-consume astronomy calculations of practical use. For now it only calculat...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

Other Trending Topics Top

juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement