Skip to content

Commit

Permalink
Make indentation configurable
Browse files Browse the repository at this point in the history
With a CLI flag for now, though in the future we should use .editorconfig as the default

Co-Authored-By: toastal <toastal@posteo.net>
  • Loading branch information
infinisil and toastal committed Nov 29, 2024
1 parent 12e14c8 commit 8e47e87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 5 additions & 2 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Width = Int
data Nixfmt = Nixfmt
{ files :: [FilePath],
width :: Width,
indent :: Int,
check :: Bool,
quiet :: Bool,
verify :: Bool,
Expand All @@ -50,13 +51,15 @@ data Nixfmt = Nixfmt
options :: Nixfmt
options =
let defaultWidth = 100
defaultIndent = 2
addDefaultHint value message =
message ++ "\n[default: " ++ show value ++ "]"
in Nixfmt
{ files = [] &= args &= typ "FILES",
width =
defaultWidth
&= help (addDefaultHint defaultWidth "Maximum width in characters"),
indent = defaultIndent &= help (addDefaultHint defaultIndent "Number of spaces to use for indentation"),
check = False &= help "Check whether files are formatted without modifying them",
quiet = False &= help "Do not report errors",
verify =
Expand Down Expand Up @@ -135,8 +138,8 @@ type Formatter = FilePath -> Text -> Either String Text

toFormatter :: Nixfmt -> Formatter
toFormatter Nixfmt{ast = True} = Nixfmt.printAst
toFormatter Nixfmt{width, verify = True} = Nixfmt.formatVerify (layout width)
toFormatter Nixfmt{width, verify = False} = Nixfmt.format (layout width)
toFormatter Nixfmt{width, indent, verify = True} = Nixfmt.formatVerify (layout width indent)
toFormatter Nixfmt{width, indent, verify = False} = Nixfmt.format (layout width indent)

type Operation = Formatter -> Target -> IO Result

Expand Down
16 changes: 8 additions & 8 deletions src/Nixfmt/Predoc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ mergeSpacings Hardspace (Newlines x) = Newlines x
mergeSpacings _ (Newlines x) = Newlines (x + 1)
mergeSpacings _ y = y

layout :: (Pretty a) => Int -> a -> Text
layout w = (<> "\n") . Text.strip . layoutGreedy w . fixup . pretty
layout :: (Pretty a) => Int -> Int -> a -> Text
layout w i = (<> "\n") . Text.strip . layoutGreedy w i . fixup . pretty

-- 1. Move and merge Spacings.
-- 2. Convert Softlines to Grouped Lines and Hardspaces to Texts.
Expand Down Expand Up @@ -468,8 +468,8 @@ indent n = Text.replicate n " "
type St = (Int, NonEmpty (Int, Int))

-- tw Target Width
layoutGreedy :: Int -> Doc -> Text
layoutGreedy tw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, singleton (0, 0))
layoutGreedy :: Int -> Int -> Doc -> Text
layoutGreedy tw iw doc = Text.concat $ evalState (go [Group RegularG doc] []) (0, singleton (0, 0))
where
-- Simple helpers around `put` with a tuple state
putL = modify . first . const
Expand All @@ -484,7 +484,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 + iw 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 +611,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 iw else 0
where
lastLineNL = snd $ NonEmpty.head ci
lineNL = lastLineNL + (if nl > lastLineNL then 2 else 0)
lineNL = lastLineNL + (if nl > lastLineNL then iw 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 iw else 0
where
lineNL = snd $ NonEmpty.head ci
in fits (indentWillIncrease - cc) (tw - cc - firstLineWidth rest) grp
Expand Down

0 comments on commit 8e47e87

Please sign in to comment.