git.iko.soy/rowtype-yoga/pose
on main help sign in
Various positions
commit398075ff2bd35c13fa5a56de170902d44a99bf26 authorMark Eibes <mark.eibes@gmail.com> dateTue 6 Jul 2021, 16:24 (5 years ago) parent7230fec
6 files changed+151−34
packages/core/src/Format.purs+103−32 packages/core/testfiles/golden/Comments.purs+5 packages/core/testfiles/golden/Operators.purs+11 packages/core/testfiles/original/Comments.purs+6−1 packages/core/testfiles/original/Functions.purs+16−1 packages/core/testfiles/original/Operators.purs+10
packages/core/src/Format.purs+103−32
@@ -1,6 +1,7 @@
11module Format (format) where
22
33import Prelude
4+import Data.Array (any, foldl)
45import Data.Array as Array
56import Data.Array.NonEmpty as NE
67import Data.Array.NonEmpty.Internal (NonEmptyArray)
@@ -10,6 +11,8 @@ import Data.Maybe (Maybe(..), fromMaybe, maybe)
1011import Data.Monoid.Additive (Additive(..))
1112import Data.Newtype (ala)
1213import Data.Semigroup.Foldable (intercalateMap)
14+import Data.String.CodeUnits (lastIndexOf)
15+import Data.String.CodeUnits as String
1316import Data.Tuple (fst, snd)
1417import Data.Tuple.Nested (type (/\), (/\))
1518import Partial.Unsafe (unsafeCrashWith)
@@ -34,8 +37,11 @@ precedingEmptyLines =
3437 _ → 0
3538
3639isPrecededByBlankLines ∷ ∀ t. TokensOf t ⇒ t → Boolean
37−isPrecededByBlankLines x = case TokensOf.head (tokensOf x) of
38− Just { leadingComments } → precedingEmptyLines leadingComments > 1
40+isPrecededByBlankLines x =
41+ precedingEmptyLines (unsafeFirstTokenOf x).leadingComments > 1
42+
43+unsafeFirstTokenOf x = case TokensOf.head (tokensOf x) of
44+ Just sourceToken → sourceToken
3945 Nothing → unsafeCrashWith "Unexpectedly got no tokens"
4046
4147isPrecededByNewline ∷ ∀ t. TokensOf t ⇒ t → Boolean
@@ -43,6 +49,14 @@ isPrecededByNewline x = case TokensOf.head (tokensOf x) of
4349 Just { leadingComments } → precedingEmptyLines leadingComments > 0
4450 Nothing → unsafeCrashWith "Unexpectedly got no tokens"
4551
52+hasNonWhitespaceTrailingComment ∷ CST.SourceToken → Boolean
53+hasNonWhitespaceTrailingComment =
54+ _.trailingComments
55+ >>> any case _ of
56+ CST.Comment "" → false -- Don't think this should happen
57+ CST.Comment _ → true
58+ _ → false
59+
4660commentIsSpaceOrLineFeed ∷ ∀ c. CST.Comment c → Boolean
4761commentIsSpaceOrLineFeed = case _ of
4862 CST.Space _ → true
@@ -53,7 +67,8 @@ singleOrMultiline ∷ ∀ a. RangeOf a ⇒ a → Lines
5367singleOrMultiline value = singleOrMultilineFromRange (rangeOf value)
5468
5569singleOrMultilineFromRange ∷ CST.SourceRange → Lines
56−singleOrMultilineFromRange { start, end } = if start.line == end.line then SingleLine else MultipleLines
70+singleOrMultilineFromRange { start, end } =
71+ if start.line == end.line then SingleLine else MultipleLines
5772
5873singleOrMultilineBetween ∷ ∀ a b. RangeOf a ⇒ RangeOf b ⇒ a → b → Lines
5974singleOrMultilineBetween a b = do
@@ -187,7 +202,12 @@ formatImportDeclaration ∷
187202 CST.ImportDecl Void →
188203 String
189204formatImportDeclaration settings@{ indentation } indent'' importDecl' = case importDecl' of
190− CST.ImportDecl { keyword: import'', module: name'', names: imports'', qualified: rename } → do
205+ CST.ImportDecl
206+ { keyword: import''
207+ , module: name''
208+ , names: imports''
209+ , qualified: rename
210+ } → do
191211 let span = singleOrMultiline importDecl'
192212 let indent' = indent'' <> indentation
193213 formatSourceToken settings indent'' blank import''
@@ -923,9 +943,27 @@ formatExpr settings@{ indentation } indent'' expr'' = case expr'' of
923943 SingleLine → (indent'' <> indentation) /\ indent'' /\ space
924944 formatExpr settings indent'' expr1
925945 <> foldMap
926− ( \(op /\ expr) →
946+ ( \(op /\ expr) → do
947+ let
948+ -- To preserve alignment of brackets, commas, etc.
949+ -- we need to add a newline if there's a multiline record,
950+ -- array, etc.
951+ prefix' = case expr of
952+ CST.ExprRecord _ → case singleOrMultiline expr of
953+ MultipleLines →
954+ (newline <> indent'' <> indentation <> indentation)
955+ SingleLine → space
956+ CST.ExprRecordUpdate _ _ → case singleOrMultiline expr of
957+ MultipleLines →
958+ (newline <> indent'' <> indentation <> indentation)
959+ SingleLine → space
960+ CST.ExprArray _ → case singleOrMultiline expr of
961+ MultipleLines →
962+ (newline <> indent'' <> indentation <> indentation)
963+ SingleLine → space
964+ _ → space
927965 formatQualifiedName settings indent' prefix op
928− <> space
966+ <> prefix'
929967 <> formatExpr settings indent expr
930968 )
931969 expressions
@@ -998,7 +1036,7 @@ formatRecordNonEmpty ∷
9981036 (a → String) →
9991037 CST.DelimitedNonEmpty a →
10001038 String
1001−formatRecordNonEmpty settings indent formatValue record' = do
1039+formatRecordNonEmpty settings indent formatValue record'@(CST.Wrapped { open }) = do
10021040 let
10031041 (before /\ after) = case lines of
10041042 MultipleLines → (blank /\ blank)
@@ -1008,7 +1046,7 @@ formatRecordNonEmpty settings indent formatValue record' = do
10081046 indent
10091047 ( \separated' →
10101048 before
1011− <> formatSeparated settings lines indent space formatValue separated' -- [CHECK] is the lines correct
1049+ <> formatSeparated settings lines indent space formatValue separated'
10121050 <> after
10131051 )
10141052 record'
@@ -1049,7 +1087,6 @@ formatRecordUpdate settings@{ indentation } indent' recordUpdate' = case recordU
10491087 CST.RecordUpdateBranch label' delimitedNonEmpty' → do
10501088 let
10511089 lines = singleOrMultiline delimitedNonEmpty'
1052−
10531090 indentTrick = indent' <> indentation
10541091
10551092 indent /\ prefix = case lines of
@@ -1456,7 +1493,9 @@ formatDataConstructor ∷
14561493 Indent →
14571494 CST.DataCtor Void →
14581495 String
1459−formatDataConstructor settings@{ indentation } indent constructor@(CST.DataCtor { name, fields }) =
1496+formatDataConstructor
1497+ settings@{ indentation } indent
1498+ constructor@(CST.DataCtor { name, fields }) =
14601499 space
14611500 <> formatName settings indent blank name
14621501 <> foldMap formatField fields
@@ -1487,7 +1526,8 @@ formatTypeVarBindings ∷
14871526 Indent →
14881527 Array (CST.TypeVarBinding Void) →
14891528 String
1490−formatTypeVarBindings settings indent = foldMap (pure space <> formatTypeVarBinding settings indent)
1529+formatTypeVarBindings settings indent =
1530+ foldMap (pure space <> formatTypeVarBinding settings indent)
14911531
14921532formatTypeVarBinding ∷
14931533 Settings →
@@ -1512,7 +1552,9 @@ formatType settings@{ indentation } indent lines t = case t of
15121552 where
15131553 typesArray = NE.toArray types
15141554
1515− formatTypeWithPredecessor (curr /\ pred) = prefix <> formatType settings indented (singleOrMultiline curr) curr
1555+ formatTypeWithPredecessor (curr /\ pred) =
1556+ prefix
1557+ <> formatType settings indented (singleOrMultiline curr) curr
15161558 where
15171559 { indented, prefix } = case singleOrMultilineBetween curr pred of
15181560 MultipleLines → { indented: indent <> indentation, prefix: newline <> indent <> indentation }
@@ -1553,7 +1595,8 @@ formatType settings@{ indentation } indent lines t = case t of
15531595 <> formatType settings indented lines kind
15541596 where
15551597 { indented, prefix } = case lines of
1556− MultipleLines → { indented: indent <> indentation, prefix: newline <> indent }
1598+ MultipleLines →
1599+ { indented: indent <> indentation, prefix: newline <> indent }
15571600 SingleLine → { indented: indent, prefix: space }
15581601 CST.TypeOp typo types →
15591602 formatType settings indent (singleOrMultiline typo) typo
@@ -1562,10 +1605,12 @@ formatType settings@{ indentation } indent lines t = case t of
15621605 formatOp (op /\ anotherType) =
15631606 formatQualifiedName settings indented prefix op
15641607 <> prefix
1565− <> formatType settings indented (singleOrMultiline anotherType) anotherType
1608+ <> formatType settings indented (singleOrMultiline anotherType)
1609+ anotherType
15661610 where
15671611 { indented, prefix } = case lines of
1568− MultipleLines → { indented: indent <> indentation, prefix: newline <> indent }
1612+ MultipleLines →
1613+ { indented: indent <> indentation, prefix: newline <> indent }
15691614 SingleLine → { indented: indent, prefix: space }
15701615 CST.TypeOpName op → formatQualifiedName settings indent blank op
15711616 CST.TypeConstrained constraint arrow typo →
@@ -1604,7 +1649,8 @@ formatLabeled ∷
16041649 (Indent → b → String) →
16051650 CST.Labeled a b →
16061651 String
1607−formatLabeled settings@{ indentation } indent' formatLabel formatValue labeled@(CST.Labeled { label, separator, value }) =
1652+formatLabeled settings@{ indentation } indent' formatLabel formatValue
1653+ labeled@(CST.Labeled { label, separator, value }) =
16081654 formatLabel label
16091655 <> formatSourceToken settings indent space separator
16101656 <> prefix
@@ -1653,7 +1699,8 @@ formatRow ∷
16531699 Indent →
16541700 CST.Row Void →
16551701 String
1656−formatRow lines settings@{ indentation } indent (CST.Row { labels, tail }) = case labels, tail of
1702+formatRow lines settings@{ indentation } indent
1703+ (CST.Row { labels, tail }) = case labels, tail of
16571704 Nothing, Nothing → blank
16581705 Just ls, Nothing →
16591706 before
@@ -1661,7 +1708,8 @@ formatRow lines settings@{ indentation } indent (CST.Row { labels, tail }) = cas
16611708 <> after
16621709 where
16631710 { before, indented, after } = case lines of
1664− MultipleLines → { before: blank, indented: indent <> indentation, after: blank }
1711+ MultipleLines →
1712+ { before: blank, indented: indent <> indentation, after: blank }
16651713 SingleLine → { before: space, indented: indent, after: space }
16661714
16671715 f = formatLabeledName settings indented
@@ -1685,8 +1733,18 @@ formatRow lines settings@{ indentation } indent (CST.Row { labels, tail }) = cas
16851733 <> after
16861734 where
16871735 { before, indented, after, prefix } = case lines of
1688− MultipleLines → { before: blank, indented: indent <> indentation, after: blank, prefix: newline <> indent }
1689− SingleLine → { before: space, indented: indent, after: space, prefix: space }
1736+ MultipleLines →
1737+ { before: blank
1738+ , indented: indent <> indentation
1739+ , after: blank
1740+ , prefix: newline <> indent
1741+ }
1742+ SingleLine →
1743+ { before: space
1744+ , indented: indent
1745+ , after: space
1746+ , prefix: space
1747+ }
16901748
16911749 f = formatLabeledName settings indented
16921750
@@ -1699,7 +1757,8 @@ formatSeparated ∷
16991757 (a → String) →
17001758 CST.Separated a →
17011759 String
1702−formatSeparated settings lines indent prefix' formatValue (CST.Separated { head, tail }) = do
1760+formatSeparated settings lines indent prefix' formatValue
1761+ (CST.Separated { head, tail }) = do
17031762 formatValue head <> foldMap go tail
17041763 where
17051764 prefix = case lines of
@@ -1714,18 +1773,22 @@ formatSeparated settings lines indent prefix' formatValue (CST.Separated { head,
17141773 <> formatValue value
17151774
17161775formatName ∷ ∀ a. Settings → Indent → Prefix → CST.Name a → String
1717−formatName settings indent prefix (CST.Name { token }) = formatSourceToken settings indent prefix token
1776+formatName settings indent prefix (CST.Name { token }) =
1777+ formatSourceToken settings indent prefix token
17181778
1719−formatQualifiedName ∷ ∀ a. Settings → Indent → Prefix → CST.QualifiedName a → String
1720−formatQualifiedName settings indent prefix (CST.QualifiedName { token }) = formatSourceToken settings indent prefix token
1721−
1722−formatSourceToken ∷
1779+formatQualifiedName ∷
1780+ ∀ a.
17231781 Settings →
17241782 Indent →
17251783 Prefix →
1726− CST.SourceToken →
1784+ CST.QualifiedName a →
17271785 String
1728−formatSourceToken settings indent prefix { leadingComments, trailingComments, value } =
1786+formatQualifiedName settings indent prefix (CST.QualifiedName { token }) =
1787+ formatSourceToken settings indent prefix token
1788+
1789+formatSourceToken ∷ Settings → Indent → Prefix → CST.SourceToken → String
1790+formatSourceToken settings indent prefix
1791+ { leadingComments, trailingComments, value } =
17291792 formatCommentsLeading indent prefix leadingComments
17301793 <> prefix
17311794 <> print value
@@ -1801,7 +1864,8 @@ formatCommentsTrailingModule commentsTrailing' = do
18011864 comments = map formatComment commentsTrailing'
18021865 case NE.fromArray (Array.catMaybes comments) of
18031866 Nothing → blank
1804− Just comments → newline <> fold (NE.intersperse newline comments) <> newline
1867+ Just comments →
1868+ newline <> fold (NE.intersperse newline comments) <> newline
18051869
18061870formatComment ∷ ∀ lf. CST.Comment lf → Maybe String
18071871formatComment = case _ of
@@ -1816,8 +1880,10 @@ formatWrapped ∷
18161880 (a → String) →
18171881 CST.Wrapped a →
18181882 String
1819−formatWrapped settings indent formatValue wrapped@(CST.Wrapped { open, value, close }) =
1883+formatWrapped settings@{ indentation } indent formatValue
1884+ wrapped@(CST.Wrapped { open, value, close }) =
18201885 formatSourceToken settings indent blank open
1886+ <> ensureNewline
18211887 <> before
18221888 <> formatValue value
18231889 <> after
@@ -1826,13 +1892,19 @@ formatWrapped settings indent formatValue wrapped@(CST.Wrapped { open, value, cl
18261892 { before, after } = case singleOrMultiline wrapped of
18271893 MultipleLines → { before: space, after: newline <> indent }
18281894 SingleLine → { before: blank, after: blank }
1895+ ensureNewline =
1896+ if hasNonWhitespaceTrailingComment open then
1897+ newline <> indent <> before
1898+ else
1899+ blank
18291900
18301901formatWrappedRow ∷
18311902 Settings →
18321903 Indent →
18331904 CST.Wrapped (CST.Row Void) →
18341905 String
1835−formatWrappedRow settings@{ indentation } indent wrapped@(CST.Wrapped { open, value: row, close }) =
1906+formatWrappedRow settings@{ indentation } indent
1907+ wrapped@(CST.Wrapped { open, value: row, close }) =
18361908 formatSourceToken settings indent blank open
18371909 <> before
18381910 <> formatRow lines settings indent row
@@ -1840,7 +1912,6 @@ formatWrappedRow settings@{ indentation } indent wrapped@(CST.Wrapped { open, va
18401912 <> formatSourceToken settings indent blank close
18411913 where
18421914 lines = singleOrMultiline wrapped
1843−
18441915 before /\ after = case row, lines of
18451916 CST.Row { labels: Just _ }, MultipleLines → space /\ (newline <> indent)
18461917 CST.Row { labels: Nothing }, MultipleLines → blank /\ (newline <> indent)
packages/core/testfiles/golden/Comments.purs+5
@@ -41,6 +41,11 @@ x = X
4141y :: Y -> Y -> Y
4242y _ _ = Y
4343
44+commentInRecord =
45+ { -- don't destroy the code below
46+ x: 12
47+ }
48+
4449-- A trailing comment on the module
4550-- Another trailing module comment
4651{- A trailing block comment on the module -}
packages/core/testfiles/golden/Operators.purs+11
@@ -3,3 +3,14 @@ module Operators where
33foo = filter ((&&) <$> testK relation w <*> evaluate' x1) worlds
44
55bar = ((&&) <$> (||) <*> (&&))
6+
7+multiline =
8+ 12
9+ + 3
10+
11+withRecord =
12+ blurb
13+ <@>
14+ { a: 1
15+ , blurbi: 4
16+ }
packages/core/testfiles/original/Comments.purs+6−1
@@ -41,6 +41,11 @@ x = X
4141y :: Y -> Y -> Y
4242y _ _ = Y
4343
44+commentInRecord =
45+ { -- don't destroy the code below
46+ x: 12
47+ }
48+
4449-- A trailing comment on the module
4550-- Another trailing module comment
4651
@@ -50,4 +55,4 @@ y _ _ = Y
5055 trailing block comment
5156 across multiple lines
5257-}
53−-- | And another trailing line comment
58+-- | And another trailing line comment
packages/core/testfiles/original/Functions.purs+16−1
@@ -37,4 +37,19 @@ aRecordFunction {
3737aDeconstructedFunction blurb@{
3838 a,
3939 b
40−} = a + b
40+} = a + b
41+
42+aFunctionWithSignature :: Int -> Int
43+aFunctionWithSignature = identity
44+
45+aFunctionWithForallSignature :: forall a. a -> a
46+aFunctionWithSignature = identity
47+
48+aFunctionWithForallButOneLine ::
49+ forall a. a -> a
50+aFunctionWithForallButOneLine = identity
51+
52+aFunctionWithForallButTwoLines ::
53+ forall a.
54+ a -> a
55+aFunctionWithForallButTwoLines = identity
packages/core/testfiles/original/Operators.purs+10
@@ -3,3 +3,13 @@ module Operators where
33foo = filter ((&&) <$> testK relation w <*> evaluate' x1) worlds
44
55bar = ((&&) <$> (||) <*> (&&))
6+
7+multiline =
8+ 12 +
9+ 3
10+
11+withRecord =
12+ blurb <@>
13+ { a: 1
14+ , blurbi: 4
15+ }