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

Make indentation a (configurable) variable #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Width = Int
data Nixfmt = Nixfmt
{ files :: [FilePath],
width :: Width,
indent :: Int,
check :: Bool,
quiet :: Bool,
strict :: Bool,
Expand All @@ -62,13 +63,15 @@ versionFromFile = maybe (showVersion version) unpack $(embedFileIfExists ".versi
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",
strict = False &= help "Enable a stricter formatting mode that isn't influenced as much by how the input is formatted",
Expand Down Expand Up @@ -170,8 +173,8 @@ type Formatter = FilePath -> Text -> Either String Text
toFormatter :: Nixfmt -> Formatter
toFormatter Nixfmt{ast = True} = Nixfmt.printAst
toFormatter Nixfmt{ir = True} = Nixfmt.printIR
toFormatter Nixfmt{width, verify = True, strict} = Nixfmt.formatVerify (layout width strict)
toFormatter Nixfmt{width, verify = False, strict} = Nixfmt.format (layout width strict)
toFormatter Nixfmt{width, indent, verify = True, strict} = Nixfmt.formatVerify (layout width indent strict)
toFormatter Nixfmt{width, indent, verify = False, strict} = Nixfmt.format (layout width indent strict)

type Operation = Formatter -> Target -> IO Result

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

layout :: (Pretty a, LanguageElement a) => Int -> Bool -> a -> Text
layout width strict =
layout :: (Pretty a, LanguageElement a) => Int -> Int -> Bool -> a -> Text
layout width indentWidth strict =
(<> "\n")
. Text.strip
. layoutGreedy width
. layoutGreedy width indentWidth
. fixup
. pretty
-- In strict mode, set the line number of all tokens to zero
Expand Down Expand Up @@ -480,8 +480,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 @@ -496,7 +496,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 @@ -623,14 +623,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
Loading