ormolu-0.7.4.0: A formatter for Haskell source code
Safe HaskellSafe-Inferred
LanguageGHC2021

Ormolu.Printer.Operators

Description

This module helps handle operator chains composed of different operators that may have different precedence and fixities.

Synopsis

Documentation

data OpTree ty op Source #

Intermediate representation of operator trees, where a branching is not just a binary branching (with a left node, right node, and operator like in the GHC's AST), but rather a n-ary branching, with n + 1 nodes and n operators (n >= 1).

This representation allows us to put all the operators with the same precedence level as direct siblings in this tree, to better represent the idea of a chain of operators.

Constructors

OpNode ty

A node which is not an operator application

OpBranches (NonEmpty (OpTree ty op)) [op]

A subtree of operator application(s); the invariant is: length exprs == length ops + 1. OpBranches [x, y, z] [op1, op2] represents the expression x op1 y op2 z.

Instances

Instances details
(Show ty, Show op) => Show (OpTree ty op) Source # 
Instance details

Defined in Ormolu.Printer.Operators

Methods

showsPrec :: Int -> OpTree ty op -> ShowS #

show :: OpTree ty op -> String #

showList :: [OpTree ty op] -> ShowS #

(Eq ty, Eq op) => Eq (OpTree ty op) Source # 
Instance details

Defined in Ormolu.Printer.Operators

Methods

(==) :: OpTree ty op -> OpTree ty op -> Bool #

(/=) :: OpTree ty op -> OpTree ty op -> Bool #

pattern BinaryOpBranches :: OpTree ty op -> op -> OpTree ty op -> OpTree ty op Source #

data OpInfo op Source #

Wrapper for an operator, carrying information about its name and fixity.

Constructors

OpInfo 

Fields

Instances

Instances details
Eq op => Eq (OpInfo op) Source # 
Instance details

Defined in Ormolu.Printer.Operators

Methods

(==) :: OpInfo op -> OpInfo op -> Bool #

(/=) :: OpInfo op -> OpInfo op -> Bool #

opTreeLoc :: HasSrcSpan l => OpTree (GenLocated l a) b -> SrcSpan Source #

Return combined SrcSpans of all elements in this OpTree.

reassociateOpTree Source #

Arguments

:: Bool

Whether to print debug info regarding fixity inference

-> (op -> Maybe RdrName)

How to get name of an operator

-> ModuleFixityMap

Fixity Map

-> OpTree ty op

Original OpTree

-> OpTree ty (OpInfo op)

Re-associated OpTree, with added context and info around operators

Re-associate an OpTree taking into account precedence of operators. Users are expected to first construct an initial OpTree, then re-associate it using this function before printing.

isHardSplitterOp :: FixityApproximation -> Bool Source #

Indicate if an operator has InfixR 0 fixity. We special-case this class of operators because they often have, like ($), a specific “separator” use-case, and we sometimes format them differently than other operators.