From 33fede4f4445db129fd376e1a78cec9528019185 Mon Sep 17 00:00:00 2001 From: Nathan Faubion Date: Sun, 23 Aug 2020 12:23:49 -0700 Subject: [PATCH] Don't skip indentation in flex groups --- src/Dodo.purs | 26 ++++++++------------- test/snapshots/DodoFlexGroupIndent.output | 8 +++++++ test/snapshots/DodoFlexGroupIndent.purs | 28 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test/snapshots/DodoFlexGroupIndent.output create mode 100644 test/snapshots/DodoFlexGroupIndent.purs diff --git a/src/Dodo.purs b/src/Dodo.purs index f860292..5cdb6ad 100644 --- a/src/Dodo.purs +++ b/src/Dodo.purs @@ -343,22 +343,16 @@ print (Printer printer) opts = flip go initState <<< pure <<< Doc { position { line = state.position.line + 1, column = 0 } , buffer = Buffer.modify printer.writeBreak state.buffer } - Indent doc1 - | isJust state.flexGroup -> - go (Doc doc1 : stk) state - | otherwise -> - go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state - { indent = state.indent + opts.indentWidth - , indentSpaces = state.indentSpaces <> opts.indentUnit - } - Align width doc1 - | isJust state.flexGroup -> - go (Doc doc1 : stk) state - | otherwise -> - go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state - { indent = state.indent + width - , indentSpaces = state.indentSpaces <> power " " width - } + Indent doc1 -> + go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state + { indent = state.indent + opts.indentWidth + , indentSpaces = state.indentSpaces <> opts.indentUnit + } + Align width doc1 -> + go (Doc doc1 : Dedent state.indentSpaces state.indent : stk) state + { indent = state.indent + width + , indentSpaces = state.indentSpaces <> power " " width + } FlexGroup doc1 -- We only track the first flex group. This is equivalent to -- end-of-line lookahead. diff --git a/test/snapshots/DodoFlexGroupIndent.output b/test/snapshots/DodoFlexGroupIndent.output new file mode 100644 index 0000000..ecb956c --- /dev/null +++ b/test/snapshots/DodoFlexGroupIndent.output @@ -0,0 +1,8 @@ + a b c + a + b + c + a b c + a + b + c diff --git a/test/snapshots/DodoFlexGroupIndent.purs b/test/snapshots/DodoFlexGroupIndent.purs new file mode 100644 index 0000000..66d4e1b --- /dev/null +++ b/test/snapshots/DodoFlexGroupIndent.purs @@ -0,0 +1,28 @@ +module DodoFlexGroupIndent where + +import Prelude + +import Dodo (Doc, align, flexGroup, fourSpaces, indent, paragraph, plainText, print, text) +import Effect (Effect) +import Effect.Class.Console as Console + +test1 :: forall a. Doc a +test1 = flexGroup $ indent $ paragraph + [ text "a" + , text "b" + , text "c" + ] + +test2 :: forall a. Doc a +test2 = flexGroup $ align 20 $ paragraph + [ text "a" + , text "b" + , text "c" + ] + +main :: Effect Unit +main = do + Console.log $ print plainText fourSpaces test1 + Console.log $ print plainText (fourSpaces { pageWidth = 0 }) test1 + Console.log $ print plainText fourSpaces test2 + Console.log $ print plainText (fourSpaces { pageWidth = 0 }) test2