Skip to content

Commit 0aff606

Browse files
Introduce purs-tidy formatter (#138)
* Add purs-tidy formatter * Run purs-tidy * review
1 parent 885f931 commit 0aff606

36 files changed

+401
-259
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515

1616
- name: Set up a PureScript toolchain
1717
uses: purescript-contrib/setup-purescript@main
18+
with:
19+
purs-tidy: "latest"
1820

1921
- name: Cache PureScript dependencies
2022
uses: actions/cache@v2
@@ -32,3 +34,6 @@ jobs:
3234

3335
- name: Run tests
3436
run: spago test --no-install
37+
38+
- name: Check formatting
39+
run: purs-tidy check src test examples

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
!.gitignore
33
!.github
44
!.editorconfig
5+
!.tidyrc.json
56

67
output
78
generated-docs

.tidyrc.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"importSort": "source",
3+
"importWrap": "source",
4+
"indent": 2,
5+
"operatorsFile": null,
6+
"ribbon": 1,
7+
"typeArrowPlacement": "first",
8+
"unicode": "never",
9+
"width": null
10+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ New features:
1212
Bugfixes:
1313

1414
Other improvements:
15+
- Added `purs-tidy` formatter (#138 by @thomashoneyman)
1516
- Replace manual tests with automated tests using `assert` (#135 by @neppord)
1617
- Improve documentation for `united` (#134 by @neppord)
1718
- Add guide on impredicativity explaining difference between `Lens` vs `ALens` (#136 by @i-am-tom and @JordanMartinez)

examples/src/PrismsForSumTypes.purs

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ See `README.md` if you're wondering why code is formatted the way it is.
2828
-}
2929

3030
import Prelude
31-
import Data.Lens (Prism', is, isn't, nearly, only, preview, prism, prism', review)
3231

3332
import Color (Color)
3433
import Color as Color
35-
34+
import Data.Either (Either(..))
3635
import Data.Generic.Rep (class Generic)
3736
import Data.Generic.Rep.Eq as GEq
3837
import Data.Generic.Rep.Show as GShow
38+
import Data.Lens (Prism', is, isn't, nearly, only, preview, prism, prism', review)
3939
import Data.Maybe (Maybe(..), maybe)
40-
import Data.Either (Either(..))
4140

42-
43-
{- The types in question -}
41+
{- The types in question -}
4442

4543
newtype Percent = Percent Number
4644
data Point = Point Number Number
@@ -51,7 +49,7 @@ data Fill -- think of a paint program filling a shape
5149
| RadialGradient Color Color Point
5250
| NoFill
5351

54-
{------ Some samples to work with ------}
52+
{------ Some samples to work with ------}
5553

5654
fillBlackToWhite :: Fill
5755
fillBlackToWhite = LinearGradient Color.black Color.white $ Percent 3.3
@@ -62,8 +60,7 @@ fillWhiteToBlack = LinearGradient Color.white Color.black $ Percent 3.3
6260
fillRadial :: Fill
6361
fillRadial = RadialGradient Color.white Color.black $ Point 1.0 3.4
6462

65-
66-
{------ Making prisms with Maybe and `prism'` ------}
63+
{------ Making prisms with Maybe and `prism'` ------}
6764

6865
-- `prism'` (note the apostrophe) takes two functions. One is a data
6966
-- constructor for the type in question. The other converts your
@@ -72,10 +69,10 @@ fillRadial = RadialGradient Color.white Color.black $ Point 1.0 3.4
7269
solidFocus :: Prism' Fill Color
7370
solidFocus = prism' constructor focus
7471
where
75-
constructor = Solid
76-
focus fill = case fill of
77-
Solid color -> Just color
78-
otherCases -> Nothing
72+
constructor = Solid
73+
focus fill = case fill of
74+
Solid color -> Just color
75+
otherCases -> Nothing
7976

8077
-- In real life, you might abbreviate the above to this:
8178

@@ -86,37 +83,40 @@ solidFocus' = prism' Solid case _ of
8683

8784
-- ... but being painfully explicit is better for examples.
8885

89-
90-
{------ Basic usage: `preview`, `review`, `is`, and `isn't` ------}
86+
{------ Basic usage: `preview`, `review`, `is`, and `isn't` ------}
9187

9288
-- After building a prism, you focus in on a color with `preview`:
9389

9490
s1 :: Maybe Color
9591
s1 = preview solidFocus (Solid Color.white)
92+
9693
-- (Just rgba 255 255 255 1.0)
9794

9895
s2 :: Maybe Color
9996
s2 = preview solidFocus fillRadial
97+
10098
-- Nothing
10199

102100
-- ... or you can create a Fill from a color with `review`:
103101

104102
s3 :: Fill
105103
s3 = review solidFocus Color.white
104+
106105
-- (Solid rgba 255 255 255 1.0)
107106

108107
-- ... or you can ask whether a given value matches the prism:
109108

110109
s4 :: Boolean
111110
s4 = is solidFocus (Solid Color.white) :: Boolean
111+
112112
-- true
113113

114114
s5 :: Boolean
115115
s5 = isn't solidFocus (Solid Color.white) :: Boolean
116-
-- false
117116

117+
-- false
118118

119-
{------ Making prisms with Either and `prism` ------}
119+
{------ Making prisms with Either and `prism` ------}
120120

121121
-- Since `LinearGradient` wraps two colors and a percent, they need to
122122
-- be bundled together into a single value for `preview` to
@@ -135,50 +135,53 @@ type LinearInterchange =
135135
linearFocus :: Prism' Fill LinearInterchange
136136
linearFocus = prism constructor focus
137137
where
138-
constructor {color1, color2, percent} =
139-
LinearGradient color1 color2 percent
140-
focus = case _ of
141-
LinearGradient color1 color2 percent ->
142-
Right {color1, color2, percent}
143-
otherCases ->
144-
Left otherCases
138+
constructor { color1, color2, percent } =
139+
LinearGradient color1 color2 percent
140+
focus = case _ of
141+
LinearGradient color1 color2 percent ->
142+
Right { color1, color2, percent }
143+
otherCases ->
144+
Left otherCases
145145

146146
-- Even though made differently than `solidFocus`, `linearFocus` is
147147
-- used the same way:
148148

149149
l1 :: String
150150
l1 = preview linearFocus fillBlackToWhite # maybe "!" show
151+
151152
-- "{ color1: rgba 0 0 0 1.0, color2: rgba 255 255 255 1.0, percent: (3.3%) }"
152153

153154
l2 :: Fill
154-
l2 = review linearFocus { color1 : Color.black
155-
, color2 : Color.white
156-
, percent : Percent 33.3
157-
}
158-
155+
l2 = review linearFocus
156+
{ color1: Color.black
157+
, color2: Color.white
158+
, percent: Percent 33.3
159+
}
159160

160-
{------ Use `only` to focus on specific values ------}
161+
{------ Use `only` to focus on specific values ------}
161162

162163
whiteToBlackFocus :: Prism' Fill Unit
163164
whiteToBlackFocus = only fillWhiteToBlack
164165

165166
o1 :: Boolean
166167
o1 = is whiteToBlackFocus fillWhiteToBlack :: Boolean
168+
167169
-- true
168170

169171
o2 :: Boolean
170172
o2 = is whiteToBlackFocus fillBlackToWhite :: Boolean
173+
171174
-- false
172175

173176
o3 :: Boolean
174177
o3 = is whiteToBlackFocus fillRadial :: Boolean
178+
175179
-- false
176180

177181
-- Note that `only` requires `Fill` to implement `Eq`.
178182
-- It's the only prism constructor that does.
179183

180-
{------ Use `nearly` to focus on a sub-case ------}
181-
184+
{------ Use `nearly` to focus on a sub-case ------}
182185

183186
-- `nearly` is typically used to look for a specific case (like other
184187
-- prisms), but also accepts only values that are close to some target
@@ -193,61 +196,69 @@ o3 = is whiteToBlackFocus fillRadial :: Boolean
193196
brightSolidFocus :: Prism' Fill Unit
194197
brightSolidFocus = nearly (Solid referenceColor) predicate
195198
where
196-
referenceColor = Color.graytone 0.8
197-
predicate = case _ of
198-
Solid color ->
199-
Color.brightness color >= Color.brightness referenceColor
200-
_ ->
201-
false
199+
referenceColor = Color.graytone 0.8
200+
predicate = case _ of
201+
Solid color ->
202+
Color.brightness color >= Color.brightness referenceColor
203+
_ ->
204+
false
202205

203206
-- Because a `nearly` prism focuses into `Unit`, you can get only two
204207
-- values from `preview`:
205208

206209
n1 :: Maybe Unit
207210
n1 = preview brightSolidFocus (Solid Color.white)
211+
208212
-- (Just unit)
209213

210214
n2 :: Maybe Unit
211215
n2 = preview brightSolidFocus (Solid Color.black)
216+
212217
-- Nothing
213218

214219
n3 :: Maybe Unit
215220
n3 = preview brightSolidFocus NoFill
216-
-- Nothing
217221

222+
-- Nothing
218223

219224
-- ... so you probably want to use `is` or `isn't`:
220225

221226
n4 :: Boolean
222227
n4 = is brightSolidFocus (Solid Color.white) :: Boolean
228+
223229
-- true
224230

225231
-- You can recover the reference value with `review`:
226232

227233
n5 :: Fill
228234
n5 = review brightSolidFocus unit
229-
-- (Solid rgba 204 204 204 1.0)
230-
231235

236+
-- (Solid rgba 204 204 204 1.0)
232237

233-
{------ Eq and Show are always nice ------}
238+
{------ Eq and Show are always nice ------}
234239

235240
-- ... although Eq is only required for `only`.
236241

237242
derive instance genericPercent :: Generic Percent _
243+
238244
instance eqPercent :: Eq Percent where
239245
eq = GEq.genericEq
246+
240247
instance showPercent :: Show Percent where
241248
show (Percent f) = "(" <> show f <> "%)"
242249

243250
derive instance genericPoint :: Generic Point _
251+
244252
instance eqPoint :: Eq Point where
245253
eq = GEq.genericEq
254+
246255
instance showPoint :: Show Point where
247256
show (Point x y) = "(" <> show x <> ", " <> show y <> ")"
248257

249258
derive instance genericFill :: Generic Fill _
259+
250260
instance eqFill :: Eq Fill where
251261
eq = GEq.genericEq
262+
252263
instance showFill :: Show Fill where
253264
show x = GShow.genericShow x

packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
let upstream =
2-
https://github.com/purescript/package-sets/releases/download/psc-0.14.3-20210722/packages.dhall sha256:1ceb43aa59436bf5601bac45f6f3781c4e1f0e4c2b8458105b018e5ed8c30f8c
2+
https://github.com/purescript/package-sets/releases/download/psc-0.14.5-20211116/packages.dhall sha256:7ba810597a275e43c83411d2ab0d4b3c54d0b551436f4b1632e9ff3eb62e327a
33

44
in upstream

src/Data/Lens.purs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ module Data.Lens
2323
, module Data.Lens.Common
2424
) where
2525

26-
import Data.Lens.Iso (AnIso, AnIso', Iso, Iso', Optic, Exchange(..), Re(..), au, auf, cloneIso, non, curried, flipped, iso, re, uncurried, under, withIso)
27-
import Data.Lens.Grate (Grate, Grate', zipWithOf, zipFWithOf, collectOf)
28-
import Data.Lens.Lens (ALens, ALens', Lens, Lens', cloneLens, lens, lens', withLens, lensStore)
26+
import Data.Lens.Common (_1, _2, _Just, _Left, _Nothing, _Right, first, left, right, second, united)
27+
import Data.Lens.Fold (Fold, Fold', allOf, andOf, anyOf, elemOf, filtered, findOf, firstOf, foldMapOf, foldOf, folded, foldlOf, foldrOf, has, hasn't, iallOf, ianyOf, ifoldMapOf, ifoldlOf, ifoldrOf, itoListOf, itraverseOf_, lastOf, lengthOf, maximumOf, minimumOf, notElemOf, orOf, preview, previewOn, productOf, replicated, sequenceOf_, sumOf, toArrayOf, toArrayOfOn, toListOf, toListOfOn, unfolded, (^..), (^?))
28+
import Data.Lens.Getter (Fold, Getter, Indexed(..), IndexedFold, IndexedGetter, Optic, cloneGetter, iuse, iview, takeBoth, to, use, view, viewOn, (^.))
29+
import Data.Lens.Grate (Grate, Grate', collectOf, zipFWithOf, zipWithOf)
30+
import Data.Lens.Iso (AnIso, AnIso', Exchange(..), Iso, Iso', Optic, Re(..), au, auf, cloneIso, curried, flipped, iso, non, re, uncurried, under, withIso)
31+
import Data.Lens.Lens (ALens, ALens', Lens, Lens', cloneLens, lens, lens', lensStore, withLens)
2932
import Data.Lens.Prism (APrism, APrism', Prism, Prism', Review, Review', clonePrism, is, isn't, matching, nearly, only, prism, prism', review, withPrism)
33+
import Data.Lens.Setter (Indexed(..), IndexedSetter, Setter, Setter', addModifying, addOver, appendModifying, appendOver, assign, assignJust, conjModifying, conjOver, disjModifying, disjOver, divModifying, divOver, iover, modifying, mulModifying, mulOver, over, set, setJust, subModifying, subOver, (%=), (%~), (&&=), (&&~), (*=), (*~), (+=), (+~), (-=), (-~), (.=), (.~), (//=), (//~), (<>=), (<>~), (?=), (?~), (||=), (||~))
3034
import Data.Lens.Traversal (Traversal, Traversal', element, elementsOf, failover, itraverseOf, sequenceOf, traverseOf, traversed)
31-
import Data.Lens.Types (class Wander, ALens, ALens', APrism, APrism', AnIso, AnIso', ATraversal, ATraversal', Fold, Fold', Getter, Getter', AGetter, AGetter', IndexedFold, IndexedFold', IndexedGetter, IndexedGetter', IndexedOptic, IndexedOptic', IndexedSetter, IndexedSetter', IndexedTraversal, IndexedTraversal', Iso, Iso', Lens, Lens', Optic, Optic', Prism, Prism', Review, Review', Setter, Setter', Traversal, Traversal', Exchange(..), Forget(..), Indexed(..), Market(..), Re(..), Shop(..), Tagged(..), wander)
32-
import Data.Lens.Setter (IndexedSetter, Setter, Setter', Indexed(..), addModifying, addOver, appendModifying, appendOver, assign, assignJust, conjModifying, conjOver, disjModifying, disjOver, divModifying, divOver, iover, modifying, mulModifying, mulOver, over, set, setJust, subModifying, subOver, (%=), (%~), (&&=), (&&~), (*=), (*~), (+=), (+~), (-=), (-~), (.=), (.~), (//=), (//~), (<>=), (<>~), (?=), (?~), (||=), (||~))
33-
import Data.Lens.Getter (Fold, Getter, IndexedFold, IndexedGetter, Optic, Indexed(..), iuse, iview, to, takeBoth, use, view, viewOn, (^.), cloneGetter)
34-
import Data.Lens.Fold (Fold, Fold', allOf, andOf, anyOf, elemOf, filtered, findOf, firstOf, foldMapOf, foldOf, folded, foldlOf, foldrOf, has, hasn't, iallOf, ianyOf, ifoldMapOf, ifoldlOf, ifoldrOf, itoListOf, itraverseOf_, lastOf, lengthOf, maximumOf, minimumOf, notElemOf, orOf, preview, previewOn, productOf, replicated, sequenceOf_, sumOf, toArrayOf, toArrayOfOn, toListOf, toListOfOn, unfolded, (^..), (^?))
35-
import Data.Lens.Common (_1, _2, _Just, _Left, _Nothing, _Right, first, left, right, second, united)
35+
import Data.Lens.Types (class Wander, AGetter, AGetter', ALens, ALens', APrism, APrism', ATraversal, ATraversal', AnIso, AnIso', Exchange(..), Fold, Fold', Forget(..), Getter, Getter', Indexed(..), IndexedFold, IndexedFold', IndexedGetter, IndexedGetter', IndexedOptic, IndexedOptic', IndexedSetter, IndexedSetter', IndexedTraversal, IndexedTraversal', Iso, Iso', Lens, Lens', Market(..), Optic, Optic', Prism, Prism', Re(..), Review, Review', Setter, Setter', Shop(..), Tagged(..), Traversal, Traversal', wander)

src/Data/Lens/AffineTraversal.purs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ import Data.Profunctor.Choice (right)
2020
import Data.Profunctor.Strong (second, (&&&))
2121
import Data.Tuple (Tuple(..))
2222

23-
affineTraversal ::
24-
forall s t a b .
25-
(s -> b -> t) ->
26-
(s -> Either t a) ->
27-
AffineTraversal s t a b
23+
affineTraversal
24+
:: forall s t a b
25+
. (s -> b -> t)
26+
-> (s -> Either t a)
27+
-> AffineTraversal s t a b
2828
affineTraversal set pre =
2929
affineTraversal' (set &&& pre)
3030

31-
affineTraversal' ::
32-
forall s t a b .
33-
(s -> Tuple (b -> t) (Either t a)) ->
34-
AffineTraversal s t a b
31+
affineTraversal'
32+
:: forall s t a b
33+
. (s -> Tuple (b -> t) (Either t a))
34+
-> AffineTraversal s t a b
3535
affineTraversal' to pab =
3636
dimap to (\(Tuple b f) -> either identity b f) (second (right pab))
3737

38-
withAffineTraversal ::
39-
forall s t a b r .
40-
AnAffineTraversal s t a b ->
41-
((s -> b -> t) -> (s -> Either t a) -> r) ->
42-
r
38+
withAffineTraversal
39+
:: forall s t a b r
40+
. AnAffineTraversal s t a b
41+
-> ((s -> b -> t) -> (s -> Either t a) -> r)
42+
-> r
4343
withAffineTraversal l f = case l (Stall (const identity) Right) of
4444
Stall g h -> f g h
4545

46-
cloneAffineTraversal ::
47-
forall s t a b .
48-
AnAffineTraversal s t a b ->
49-
AffineTraversal s t a b
46+
cloneAffineTraversal
47+
:: forall s t a b
48+
. AnAffineTraversal s t a b
49+
-> AffineTraversal s t a b
5050
cloneAffineTraversal l =
5151
withAffineTraversal l \x y p ->
5252
affineTraversal x y p

src/Data/Lens/At.purs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module Data.Lens.At
44
, sans
55
) where
66

7-
87
import Prelude
98

109
import Data.Identity (Identity(..))
@@ -45,12 +44,11 @@ instance atMaybe :: At (Maybe a) Unit a where
4544
instance atSet :: Ord v => At (S.Set v) v Unit where
4645
at x = lens get (flip update)
4746
where
48-
get xs =
49-
if S.member x xs
50-
then Just unit
51-
else Nothing
52-
update Nothing = S.delete x
53-
update (Just _) = S.insert x
47+
get xs =
48+
if S.member x xs then Just unit
49+
else Nothing
50+
update Nothing = S.delete x
51+
update (Just _) = S.insert x
5452

5553
instance atMap :: Ord k => At (M.Map k v) k v where
5654
at k =

0 commit comments

Comments
 (0)