git.iko.soy/rowtype-yoga/pose
on main help sign in
Respect newlines between declarations
commit73927374ecd9ae7128b03f7b49a420bc35851f44 authorMark Eibes <mark.eibes@gmail.com> dateSun 4 Jul 2021, 22:20 (5 years ago) parent551c925
5 files changed+37−23
packages/core/src/Format.purs+30−16 packages/core/testfiles/golden/KindSignature.purs+4−3 packages/core/testfiles/golden/ParenthesesIndentation.purs−1 packages/core/testfiles/golden/Role.purs−3 packages/core/testfiles/original/KindSignature.purs+3
packages/core/src/Format.purs+30−16
@@ -38,6 +38,11 @@ isPrecededByBlankLines x = case TokensOf.head (tokensOf x) of
3838 Just { leadingComments } → precedingEmptyLines leadingComments > 1
3939 Nothing → unsafeCrashWith "Unexpectedly got no tokens"
4040
41+isPrecededByNewline ∷ ∀ t. TokensOf t ⇒ t → Boolean
42+isPrecededByNewline x = case TokensOf.head (tokensOf x) of
43+ Just { leadingComments } → precedingEmptyLines leadingComments > 0
44+ Nothing → unsafeCrashWith "Unexpectedly got no tokens"
45+
4146commentIsSpaceOrLineFeed ∷ ∀ c. CST.Comment c → Boolean
4247commentIsSpaceOrLineFeed = case _ of
4348 CST.Space _ → true
@@ -321,16 +326,24 @@ formatDeclarations ∷
321326 Settings →
322327 Array (CST.Declaration Void) →
323328 String
324−formatDeclarations settings = foldMap formatOne
329+formatDeclarations settings decls =
330+ foldMap
331+ formatWithNext
332+ (Array.zip decls (Array.snoc (Just <$> Array.drop 1 decls) Nothing))
325333 where
326− formatOne declaration = newline <> formatDeclaration settings blank declaration
334+ formatWithNext (declaration /\ maybeFollowingDeclaration) = do
335+ newline <> formatDeclaration settings blank followedByBlanks declaration
336+ where
337+ followedByBlanks = case maybeFollowingDeclaration of
338+ Nothing → true -- always true so we have a final newline
339+ Just following → isPrecededByBlankLines following
327340
328−formatDeclaration ∷ Settings → Indent → CST.Declaration Void → String
329−formatDeclaration settings@{ indentation } indent declaration = case declaration of
341+formatDeclaration ∷ Settings → Indent → Boolean → CST.Declaration Void → String
342+formatDeclaration settings@{ indentation } indent followedByBlankLines declaration = case declaration of
330343 CST.DeclData dataHead maybeConstructors → do
331344 formatDataHead settings indent dataHead
332345 <> foldMap formatDataConstructors maybeConstructors
333− <> newline
346+ <> if followedByBlankLines then newline else blank
334347 where
335348 formatDataConstructors (equals /\ constructors) =
336349 (newline <> indented)
@@ -349,7 +362,7 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
349362 <> formatSourceToken settings indented blank equals
350363 <> space
351364 <> formatType settings (indented <> indentation) (singleOrMultiline typo) typo
352− <> newline
365+ <> if followedByBlankLines then newline else blank
353366 CST.DeclNewtype dataHead equals name typo → do
354367 formatDataHead settings indent dataHead
355368 <> (newline <> indented)
@@ -358,7 +371,7 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
358371 <> formatName settings indented blank name
359372 <> prefix
360373 <> formatType settings indented lines typo
361− <> newline
374+ <> if followedByBlankLines then newline else blank
362375 where
363376 prefix = case singleOrMultilineBetween name typo of
364377 MultipleLines → newline <> indented
@@ -366,7 +379,7 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
366379 CST.DeclClass classHead members → do
367380 formatClassHead lines settings indent classHead
368381 <> foldMap formatMember members
369− <> newline
382+ <> if followedByBlankLines then newline else blank
370383 where
371384 formatMember (whereClause /\ labeleds) =
372385 formatSourceToken settings indent space whereClause
@@ -381,13 +394,13 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
381394 space
382395 (formatInstance settings indent)
383396 instances
384− <> newline
397+ <> if followedByBlankLines then newline else blank
385398 CST.DeclDerive sourceToken newtype' instanceHead →
386399 formatSourceToken settings indent blank sourceToken
387400 <> foldMap (formatSourceToken settings indent space) newtype'
388401 <> space
389402 <> formatInstanceHead settings indent instanceHead
390− <> newline
403+ <> if followedByBlankLines then newline else blank
391404 CST.DeclKindSignature kindOfDeclaration labeled →
392405 formatSourceToken settings indentation indent kindOfDeclaration
393406 <> space
@@ -395,14 +408,14 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
395408 CST.DeclSignature labeled → formatLabeledNameType settings indent labeled
396409 CST.DeclValue valueBindingFields →
397410 formatValueBindingFields settings indent valueBindingFields
398− <> newline
411+ <> if followedByBlankLines then newline else blank
399412 CST.DeclFixity fixityFields → formatFixityFields settings indent fixityFields <> newline
400413 CST.DeclForeign foreign'' import'' foreign''' →
401414 formatSourceToken settings indent blank foreign''
402415 <> formatSourceToken settings indent space import''
403416 <> space
404417 <> formatForeign settings indent foreign'''
405− <> newline
418+ <> if followedByBlankLines then newline else blank
406419 CST.DeclRole type'' role'' name' roles → do
407420 formatSourceToken settings indentation indent type''
408421 <> space
@@ -414,7 +427,7 @@ formatDeclaration settings@{ indentation } indent declaration = case declaration
414427 space <> formatRole settings indent role'
415428 )
416429 roles
417− <> newline
430+ <> if followedByBlankLines then newline else blank
418431 CST.DeclError e → absurd e
419432 where
420433 indented = indent <> indentation
@@ -610,6 +623,7 @@ formatBinder settings@{ indentation } indent' binder = case binder of
610623 <> foldMap
611624 (\binder' → prefix <> formatBinder settings indent' binder')
612625 binders
626+ -- with @ sign
613627 CST.BinderNamed name' at binder' → do
614628 formatName settings indent' blank name'
615629 <> formatSourceToken settings indent' blank at
@@ -621,6 +635,7 @@ formatBinder settings@{ indentation } indent' binder = case binder of
621635 formatBinder (settings { indentation = indent' }) prefix binder1
622636 <> foldMap formatNamedBinder binders
623637 where
638+
624639 indentTrick = indent' <> indentation
625640
626641 indent /\ prefix = case lines of
@@ -631,7 +646,8 @@ formatBinder settings@{ indentation } indent' binder = case binder of
631646 formatQualifiedName settings indent' prefix name
632647 <> prefix
633648 <> formatBinder settings indent binder
634− CST.BinderParens wrapped → formatParens lines settings indent' (formatBinder settings) wrapped
649+ CST.BinderParens wrapped → do
650+ formatParens lines settings indent' (formatBinder settings) wrapped
635651 CST.BinderRecord delimited → do
636652 formatRecord
637653 settings
@@ -1041,9 +1057,7 @@ formatRecordUpdate settings@{ indentation } indent' recordUpdate' = case recordU
10411057 CST.RecordUpdateLeaf label' equals expr' → do
10421058 let
10431059 lines = singleOrMultiline expr'
1044−
10451060 indentTrick = indent' <> indentation
1046−
10471061 indent /\ prefix = case lines of
10481062 MultipleLines → indentTrick /\ (newline <> indentTrick)
10491063 SingleLine → indent' /\ space
packages/core/testfiles/golden/KindSignature.purs+4−3
@@ -2,17 +2,14 @@ module KindSignature where
22
33class Foo :: Type -> Constraint
44class Foo a
5−
65data Bar :: Type -> Type
76data Bar a
87 = Bar
9−
108newtype Baz ::
119 Type ->
1210 Type
1311newtype Baz a
1412 = Baz (Bar a)
15−
1613type Qux ::
1714 forall a.
1815 a ->
@@ -20,3 +17,7 @@ type Qux ::
2017 Type
2118type Qux a b
2219 = Int
20+
21+type PureScriptOptions :: forall k. k -> Row k -> Row k
22+type PureScriptOptions a r
23+ = ( symbolStyle :: a | r )
packages/core/testfiles/golden/ParenthesesIndentation.purs−1
@@ -13,7 +13,6 @@ instance bazQux ::
1313 b
1414 ) =>
1515 Baz (Qux a b)
16−
1716foo ::
1817 ( Array
1918 Int
packages/core/testfiles/golden/Role.purs−3
@@ -1,9 +1,6 @@
11module Role where
22
33type role Foo nominal
4−
54type role Foo phantom
6−
75type role Foo representational
8−
96type role Bar nominal phantom representational
packages/core/testfiles/original/KindSignature.purs+3
@@ -10,3 +10,6 @@ newtype Baz a = Baz (Bar a)
1010type Qux :: forall a. a -> (a -> a) ->
1111 Type
1212type Qux a b = Int
13+
14+type PureScriptOptions :: forall k. k -> Row k -> Row k
15+type PureScriptOptions a r = (symbolStyle :: a | r)