Skip to content

Commit 187985f

Browse files
authored
Merge pull request #209 from toastal/indent-space
Make indentation a (configurable) variable
2 parents a259670 + 0d7ea1d commit 187985f

File tree

4 files changed

+969
-18
lines changed

4 files changed

+969
-18
lines changed

main/Main.hs

+5-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Width = Int
4646
data Nixfmt = Nixfmt
4747
{ files :: [FilePath],
4848
width :: Width,
49+
indent :: Int,
4950
check :: Bool,
5051
quiet :: Bool,
5152
strict :: Bool,
@@ -62,13 +63,15 @@ versionFromFile = maybe (showVersion version) unpack $(embedFileIfExists ".versi
6263
options :: Nixfmt
6364
options =
6465
let defaultWidth = 100
66+
defaultIndent = 2
6567
addDefaultHint value message =
6668
message ++ "\n[default: " ++ show value ++ "]"
6769
in Nixfmt
6870
{ files = [] &= args &= typ "FILES",
6971
width =
7072
defaultWidth
7173
&= help (addDefaultHint defaultWidth "Maximum width in characters"),
74+
indent = defaultIndent &= help (addDefaultHint defaultIndent "Number of spaces to use for indentation"),
7275
check = False &= help "Check whether files are formatted without modifying them",
7376
quiet = False &= help "Do not report errors",
7477
strict = False &= help "Enable a stricter formatting mode that isn't influenced as much by how the input is formatted",
@@ -170,8 +173,8 @@ type Formatter = FilePath -> Text -> Either String Text
170173
toFormatter :: Nixfmt -> Formatter
171174
toFormatter Nixfmt{ast = True} = Nixfmt.printAst
172175
toFormatter Nixfmt{ir = True} = Nixfmt.printIR
173-
toFormatter Nixfmt{width, verify = True, strict} = Nixfmt.formatVerify (layout width strict)
174-
toFormatter Nixfmt{width, verify = False, strict} = Nixfmt.format (layout width strict)
176+
toFormatter Nixfmt{width, indent, verify = True, strict} = Nixfmt.formatVerify (layout width indent strict)
177+
toFormatter Nixfmt{width, indent, verify = False, strict} = Nixfmt.format (layout width indent strict)
175178

176179
type Operation = Formatter -> Target -> IO Result
177180

src/Nixfmt/Predoc.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ mergeSpacings Hardspace (Newlines x) = Newlines x
347347
mergeSpacings _ (Newlines x) = Newlines (x + 1)
348348
mergeSpacings _ y = y
349349

350-
layout :: (Pretty a, LanguageElement a) => Int -> Bool -> a -> Text
351-
layout width strict =
350+
layout :: (Pretty a, LanguageElement a) => Int -> Int -> Bool -> a -> Text
351+
layout width indentWidth strict =
352352
(<> "\n")
353353
. Text.strip
354-
. layoutGreedy width
354+
. layoutGreedy width indentWidth
355355
. fixup
356356
. pretty
357357
-- In strict mode, set the line number of all tokens to zero
@@ -480,8 +480,8 @@ indent n = Text.replicate n " "
480480
type St = (Int, NonEmpty (Int, Int))
481481

482482
-- tw Target Width
483-
layoutGreedy :: Int -> Doc -> Text
484-
layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, singleton (0, 0))
483+
layoutGreedy :: Int -> Int -> Doc -> Text
484+
layoutGreedy tw iw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, singleton (0, 0))
485485
where
486486
-- Simple helpers around `put` with a tuple state
487487
putL = modify . first . const
@@ -496,7 +496,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
496496
case textNL `compare` nl of
497497
-- Push the textNL onto the stack, but only increase the actual indentation (`ci`)
498498
-- if this is the first one of a line. All subsequent nestings within the line effectively get "swallowed"
499-
GT -> putR ((if cc == 0 then ci + 2 else ci, textNL) <| indents) >> go'
499+
GT -> putR ((if cc == 0 then ci + iw else ci, textNL) <| indents) >> go'
500500
-- Need to go down one or more levels
501501
-- Just pop from the stack and recurse until the indent matches again
502502
LT -> putR (NonEmpty.fromList indents') >> putText textNL textOffset t
@@ -623,14 +623,14 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s
623623
_ -> grp
624624
(nl, off) = nextIndent grp'
625625

626-
indentWillIncrease = if fst (nextIndent rest) > lineNL then 2 else 0
626+
indentWillIncrease = if fst (nextIndent rest) > lineNL then iw else 0
627627
where
628628
lastLineNL = snd $ NonEmpty.head ci
629-
lineNL = lastLineNL + (if nl > lastLineNL then 2 else 0)
629+
lineNL = lastLineNL + (if nl > lastLineNL then iw else 0)
630630
in fits indentWillIncrease (tw - firstLineWidth rest) grp'
631631
<&> \t -> runState (putText nl off t) (cc, ci)
632632
else
633-
let indentWillIncrease = if fst (nextIndent rest) > lineNL then 2 else 0
633+
let indentWillIncrease = if fst (nextIndent rest) > lineNL then iw else 0
634634
where
635635
lineNL = snd $ NonEmpty.head ci
636636
in fits (indentWillIncrease - cc) (tw - cc - firstLineWidth rest) grp

0 commit comments

Comments
 (0)