From 68f4738711d80d2c5738b0b8ad7a475c31cc4824 Mon Sep 17 00:00:00 2001 From: toastal Date: Fri, 17 May 2024 17:26:56 +0700 Subject: [PATCH] define a variable for the amount of indented spaces makes it easier to override or, hopefully in the future, make configurable --- src/Nixfmt/Predoc.hs | 17 +++++++++++------ src/Nixfmt/Pretty.hs | 5 +++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Nixfmt/Predoc.hs b/src/Nixfmt/Predoc.hs index 2e91402f..f618ac38 100644 --- a/src/Nixfmt/Predoc.hs +++ b/src/Nixfmt/Predoc.hs @@ -2,6 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} module Nixfmt.Predoc ( + indentSpaces, text, comment, trailingComment, @@ -130,6 +131,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. +indentSpaces :: Int +indentSpaces = 2 + text :: Text -> Doc text "" = [] text t = [Text 0 0 RegularT t] @@ -337,7 +342,7 @@ mergeSpacings Break Softspace = Space mergeSpacings Break Hardspace = Space mergeSpacings Softbreak Hardspace = Softspace mergeSpacings (Newlines x) (Newlines y) = Newlines (x + y) -mergeSpacings Emptyline (Newlines x) = Newlines (x + 2) +mergeSpacings Emptyline (Newlines x) = Newlines (x + indentSpaces) mergeSpacings Hardspace (Newlines x) = Newlines x mergeSpacings _ (Newlines x) = Newlines (x + 1) mergeSpacings _ y = y @@ -484,7 +489,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 + indentSpaces 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 @@ -534,7 +539,7 @@ layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, s -- [ # comment -- 1 -- ] - Text _ _ TrailingComment t | cc == 2 && fst (nextIndent xs) > lineNL -> putText' [" ", t] + Text _ _ TrailingComment t | cc == indentSpaces && fst (nextIndent xs) > lineNL -> putText' [" ", t] where lineNL = snd $ NonEmpty.head indents Text nl off _ t -> putText nl off t @@ -611,14 +616,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 indentSpaces else 0 where lastLineNL = snd $ NonEmpty.head ci - lineNL = lastLineNL + (if nl > lastLineNL then 2 else 0) + lineNL = lastLineNL + (if nl > lastLineNL then indentSpaces 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 indentSpaces else 0 where lineNL = snd $ NonEmpty.head ci in fits (indentWillIncrease - cc) (tw - cc - firstLineWidth rest) grp diff --git a/src/Nixfmt/Pretty.hs b/src/Nixfmt/Pretty.hs index bd4fd480..1a0c4e8c 100644 --- a/src/Nixfmt/Pretty.hs +++ b/src/Nixfmt/Pretty.hs @@ -21,6 +21,7 @@ import Nixfmt.Predoc ( hardline, hardspace, hcat, + indentSpaces, line, line', nest, @@ -81,7 +82,7 @@ instance Pretty Trivium where comment (if isDoc then "/**" else "/*") <> hardline -- Indent the comment using offset instead of nest - <> offset 2 (hcat $ map prettyCommentLine c) + <> offset indentSpaces (hcat $ map prettyCommentLine c) <> comment "*/" <> hardline where @@ -612,7 +613,7 @@ instance Pretty Expression where absorbAbs _ expr | isAbsorbableExpr expr = hardspace <> group' Priority (absorbExpr False expr) -- Force the content onto a new line when it is not absorbable and there are more than two arguments absorbAbs depth x = - (if depth <= 2 then line else hardline) <> pretty x + (if depth <= indentSpaces then line else hardline) <> pretty x -- Attrset parameter pretty (Abstraction param colon (Term t))