Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define a variable for the amount of indented spaces #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Nixfmt/Predoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ instance (Pretty a, Pretty b) => Pretty (a, b) where
instance (Pretty a, Pretty b, Pretty c) => Pretty (a, b, c) where
pretty (a, b, c) = pretty a <> pretty b <> pretty c

-- | Default indentation spacing.
indentWidth :: Int
toastal marked this conversation as resolved.
Show resolved Hide resolved
indentWidth = 2

text :: Text -> Doc
text "" = []
text t = [Text 0 0 RegularT t]
Expand Down Expand Up @@ -484,7 +488,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
case textNL `compare` nl of
-- Push the textNL onto the stack, but only increase the actual indentation (`ci`)
-- if this is the first one of a line. All subsequent nestings within the line effectively get "swallowed"
GT -> putR ((if cc == 0 then ci + 2 else ci, textNL) <| indents) >> go'
GT -> putR ((if cc == 0 then ci + indentWidth else ci, textNL) <| indents) >> go'
-- Need to go down one or more levels
-- Just pop from the stack and recurse until the indent matches again
LT -> putR (NonEmpty.fromList indents') >> putText textNL textOffset t
Expand Down Expand Up @@ -611,14 +615,14 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
_ -> grp
(nl, off) = nextIndent grp'

indentWillIncrease = if fst (nextIndent rest) > lineNL then 2 else 0
indentWillIncrease = if fst (nextIndent rest) > lineNL then indentWidth else 0
where
lastLineNL = snd $ NonEmpty.head ci
lineNL = lastLineNL + (if nl > lastLineNL then 2 else 0)
lineNL = lastLineNL + (if nl > lastLineNL then indentWidth else 0)
in fits indentWillIncrease (tw - firstLineWidth rest) grp'
<&> \t -> runState (putText nl off t) (cc, ci)
else
let indentWillIncrease = if fst (nextIndent rest) > lineNL then 2 else 0
let indentWillIncrease = if fst (nextIndent rest) > lineNL then indentWidth else 0
where
lineNL = snd $ NonEmpty.head ci
in fits (indentWillIncrease - cc) (tw - cc - firstLineWidth rest) grp
Expand Down