Skip to content

Faveod/oppen-ruby

Repository files navigation

Oppen's Pretty Printer

CI badge Docs latest badge rubygems.org badge

An implementation of the pretty printing algorithm described by Derek C. Oppen.

We also provide an API similar to ruby/prettyprint, which we call Wadler, in reference to Philip Wadler's paper, A prettier printer, the basis for prettyprint. This can be really helpful if you decide to transition from ruby/prettyprint to this gem.

Wadler is implemented on top of Oppen, and it provides more options than ruby/prettyprint, notably:

  1. Consistent and inconsistent breaking.
  2. Explicit breaking, which is achievable in ruby/prettyprint with some monkeypatching.

Caution

This is still under development.

Usage

Warning

Lands when the APIs are stable.

Oppen vs Wadler

Wadler calls Oppen under the hood, so it's not a separate implementation, and it's not calling ruby's prettyprint.

Both implementations have their use cases:

  1. Oppen gives more control over tokens sent to the printer.
  2. Wadler gives a more "functional" API, which is far nicer to work with.

That being said, both APIs in this gem can achieve the same results, especially on consistent and inconsistent breaking.

Noteworthy details

Difference with Oppen's original algorithm

  1. We took liberty to rename functions to make the API more modern and closer to what we expect when writing Ruby code. All correspondences with the algorithm as described in Oppen's paper are noted in the comments of classes and methods.
  2. We do not raise exceptions when we overflow the margin. The only exceptions that we raise indicate a bug in the implementation. Please report them.

Difference with ruby/prettyprint

Oppen's algorithm and ruby/prettyprint do not have the same starting positions for a group's indentation. That's why you need to pay particular attention to calls for nest; you might want to decrease them by 1 if you care about keeping the same behavior.

This is what we do in our test suite to verify the correspondence of the Wadler API and the ruby/prettyprint. We decided to shift the burden to the user because we think that the deicision taken by ruby/prettyprint does not suit us.

Related projects

  1. ruby/prettyprint
  2. rustc implementation
  3. stevej2608/oppen-pretty-printer as a library.