Skip to content

Commit 411db02

Browse files
drsoochAnton-Latukhamergify[bot]
authored
Add pre-commit hook for cleaning up mixed-line endings (#2679)
* Update pre-commit hook to include changing line endings * Fix non-lf lines * Check pre-commit excludes files * Revert "Check pre-commit excludes files" This reverts commit 7b9670f. * Actually add the exclude to contributing docs * Fix merge failure with previous patch * Inadvertently overwrote merge * Add LF option for stylish-haskell and pre-commit file Co-authored-by: Anton Latukha <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 9faa179 commit 411db02

File tree

25 files changed

+3232
-3189
lines changed

25 files changed

+3232
-3189
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test/testdata/**/hie.yaml
3232
.shake/
3333

3434
# pre-commit-hook.nix
35-
.pre-commit-config.yaml
35+
#.pre-commit-config.yaml
3636

3737
# direnv
3838
/.direnv/

Diff for: .pre-commit-config.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"repos": [
3+
{
4+
"hooks": [
5+
{
6+
"entry": "stylish-haskell --inplace",
7+
"exclude": "(^Setup.hs$|test/testdata/.*$|test/data/.*$|test/manual/lhs/.*$|^hie-compat/.*$|^plugins/hls-tactics-plugin/.*$|^ghcide/src/Development/IDE/GHC/Compat.hs$|^ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs$|^ghcide/src/Development/IDE/GHC/Compat/Core.hs$|^ghcide/src/Development/IDE/Spans/Pragmas.hs$|^ghcide/src/Development/IDE/LSP/Outline.hs$|^plugins/hls-splice-plugin/src/Ide/Plugin/Splice.hs$|^ghcide/test/exe/Main.hs$|ghcide/src/Development/IDE/Core/Rules.hs|^hls-test-utils/src/Test/Hls/Util.hs$)",
8+
"files": "\\.l?hs$",
9+
"id": "stylish-haskell",
10+
"language": "system",
11+
"name": "stylish-haskell",
12+
"pass_filenames": true,
13+
"types": [
14+
"file"
15+
]
16+
}
17+
],
18+
"repo": "local"
19+
},
20+
{
21+
"repo": "https://github.com/pre-commit/pre-commit-hooks",
22+
"rev": "v4.1.0",
23+
"hooks": [
24+
{
25+
"id": "mixed-line-ending",
26+
"args": ["--fix", "lf"],
27+
"exclude": "test/testdata/.*CRLF*.hs$"
28+
}
29+
]
30+
}
31+
]
32+
}

Diff for: .stylish-haskell.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ steps:
5353

5454
columns: 80
5555

56-
newline: native
56+
newline: lf
5757

5858
language_extensions:
5959
- BangPatterns

Diff for: docs/contributing/contributing.md

+11
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ If you don't want to use [nix](https://nixos.org/guides/install-nix.html), you c
199199
}
200200
],
201201
"repo": "local"
202+
},
203+
{
204+
"repo": "https://github.com/pre-commit/pre-commit-hooks",
205+
"rev": "v4.1.0",
206+
"hooks": [
207+
{
208+
"id": "mixed-line-ending",
209+
"args": ["--fix", "lf"],
210+
"exclude": "test/testdata/.*CRLF*.hs$"
211+
}
212+
]
202213
}
203214
]
204215
}

Diff for: ghcide/src/Development/IDE/Core/UseStale.hs

+162-162
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,162 @@
1-
{-# LANGUAGE DerivingVia #-}
2-
{-# LANGUAGE GADTs #-}
3-
{-# LANGUAGE KindSignatures #-}
4-
{-# LANGUAGE RankNTypes #-}
5-
6-
module Development.IDE.Core.UseStale
7-
( Age(..)
8-
, Tracked
9-
, unTrack
10-
, PositionMap
11-
, TrackedStale (..)
12-
, untrackedStaleValue
13-
, unsafeMkStale
14-
, unsafeMkCurrent
15-
, unsafeCopyAge
16-
, MapAge (..)
17-
, dualPositionMap
18-
, useWithStale
19-
, useWithStale_
20-
) where
21-
22-
import Control.Arrow
23-
import Control.Category (Category)
24-
import qualified Control.Category as C
25-
import Control.DeepSeq (NFData)
26-
import Data.Aeson
27-
import Data.Coerce (coerce)
28-
import Data.Functor ((<&>))
29-
import Data.Functor.Identity (Identity (Identity))
30-
import Data.Kind (Type)
31-
import Data.String (fromString)
32-
import Development.IDE (Action, IdeRule,
33-
NormalizedFilePath,
34-
Range,
35-
rangeToRealSrcSpan,
36-
realSrcSpanToRange)
37-
import qualified Development.IDE.Core.PositionMapping as P
38-
import qualified Development.IDE.Core.Shake as IDE
39-
import Development.IDE.GHC.Compat (RealSrcSpan, srcSpanFile)
40-
import Development.IDE.GHC.Compat.Util (unpackFS)
41-
42-
43-
------------------------------------------------------------------------------
44-
-- | A data kind for 'Tracked'.
45-
data Age = Current | Stale Type
46-
47-
48-
------------------------------------------------------------------------------
49-
-- | Some value, tagged with its age. All 'Current' ages are considered to be
50-
-- the same thing, but 'Stale' values are protected by an untouchable variable
51-
-- to ensure they can't be unified.
52-
newtype Tracked (age :: Age) a = UnsafeTracked
53-
{ unTrack :: a
54-
}
55-
deriving stock (Functor, Foldable, Traversable)
56-
deriving newtype (Eq, Ord, Show, Read, ToJSON, FromJSON, NFData)
57-
deriving (Applicative, Monad) via Identity
58-
59-
60-
------------------------------------------------------------------------------
61-
-- | Like 'P.PositionMapping', but with annotated ages for how 'Tracked' values
62-
-- change. Use the 'Category' instance to compose 'PositionMapping's in order
63-
-- to transform between values of different stale ages.
64-
newtype PositionMap (from :: Age) (to :: Age) = PositionMap
65-
{ _getPositionMapping :: P.PositionMapping
66-
}
67-
68-
instance Category PositionMap where
69-
id = coerce P.zeroMapping
70-
(.) = coerce P.composeDelta
71-
72-
73-
------------------------------------------------------------------------------
74-
-- | Get a 'PositionMap' that runs in the opposite direction.
75-
dualPositionMap :: PositionMap from to -> PositionMap to from
76-
dualPositionMap (PositionMap (P.PositionMapping (P.PositionDelta from to))) =
77-
PositionMap $ P.PositionMapping $ P.PositionDelta to from
78-
79-
80-
------------------------------------------------------------------------------
81-
-- | A pair containing a @'Tracked' 'Stale'@ value, as well as
82-
-- a 'PositionMapping' that will fast-forward values to the current age.
83-
data TrackedStale a where
84-
TrackedStale
85-
:: Tracked (Stale s) a
86-
-> PositionMap (Stale s) Current
87-
-> TrackedStale a
88-
89-
instance Functor TrackedStale where
90-
fmap f (TrackedStale t pm) = TrackedStale (fmap f t) pm
91-
92-
93-
untrackedStaleValue :: TrackedStale a -> a
94-
untrackedStaleValue (TrackedStale ta _) = coerce ta
95-
96-
97-
------------------------------------------------------------------------------
98-
-- | A class for which 'Tracked' values can be run across a 'PositionMapping'
99-
-- to change their ages.
100-
class MapAge a where
101-
{-# MINIMAL mapAgeFrom | mapAgeTo #-}
102-
mapAgeFrom :: PositionMap from to -> Tracked to a -> Maybe (Tracked from a)
103-
mapAgeFrom = mapAgeTo . dualPositionMap
104-
105-
mapAgeTo :: PositionMap from to -> Tracked from a -> Maybe (Tracked to a)
106-
mapAgeTo = mapAgeFrom . dualPositionMap
107-
108-
109-
instance MapAge Range where
110-
mapAgeFrom = coerce P.fromCurrentRange
111-
mapAgeTo = coerce P.toCurrentRange
112-
113-
114-
instance MapAge RealSrcSpan where
115-
mapAgeFrom =
116-
invMapAge (\fs -> rangeToRealSrcSpan (fromString $ unpackFS fs))
117-
(srcSpanFile &&& realSrcSpanToRange)
118-
. mapAgeFrom
119-
120-
121-
------------------------------------------------------------------------------
122-
-- | Helper function for deriving 'MapAge' for values in terms of other
123-
-- instances.
124-
invMapAge
125-
:: (c -> a -> b)
126-
-> (b -> (c, a))
127-
-> (Tracked from a -> Maybe (Tracked to a))
128-
-> Tracked from b
129-
-> Maybe (Tracked to b)
130-
invMapAge to from f t =
131-
let (c, t') = unTrack $ fmap from t
132-
in fmap (fmap $ to c) $ f $ UnsafeTracked t'
133-
134-
135-
unsafeMkCurrent :: age -> Tracked 'Current age
136-
unsafeMkCurrent = coerce
137-
138-
139-
unsafeMkStale :: age -> Tracked (Stale s) age
140-
unsafeMkStale = coerce
141-
142-
143-
unsafeCopyAge :: Tracked age a -> b -> Tracked age b
144-
unsafeCopyAge _ = coerce
145-
146-
147-
-- | Request a Rule result, it not available return the last computed result, if any, which may be stale
148-
useWithStale :: IdeRule k v
149-
=> k -> NormalizedFilePath -> Action (Maybe (TrackedStale v))
150-
useWithStale key file = do
151-
x <- IDE.useWithStale key file
152-
pure $ x <&> \(v, pm) ->
153-
TrackedStale (coerce v) (coerce pm)
154-
155-
-- | Request a Rule result, it not available return the last computed result which may be stale.
156-
-- Errors out if none available.
157-
useWithStale_ :: IdeRule k v
158-
=> k -> NormalizedFilePath -> Action (TrackedStale v)
159-
useWithStale_ key file = do
160-
(v, pm) <- IDE.useWithStale_ key file
161-
pure $ TrackedStale (coerce v) (coerce pm)
162-
1+
{-# LANGUAGE DerivingVia #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE KindSignatures #-}
4+
{-# LANGUAGE RankNTypes #-}
5+
6+
module Development.IDE.Core.UseStale
7+
( Age(..)
8+
, Tracked
9+
, unTrack
10+
, PositionMap
11+
, TrackedStale (..)
12+
, untrackedStaleValue
13+
, unsafeMkStale
14+
, unsafeMkCurrent
15+
, unsafeCopyAge
16+
, MapAge (..)
17+
, dualPositionMap
18+
, useWithStale
19+
, useWithStale_
20+
) where
21+
22+
import Control.Arrow
23+
import Control.Category (Category)
24+
import qualified Control.Category as C
25+
import Control.DeepSeq (NFData)
26+
import Data.Aeson
27+
import Data.Coerce (coerce)
28+
import Data.Functor ((<&>))
29+
import Data.Functor.Identity (Identity (Identity))
30+
import Data.Kind (Type)
31+
import Data.String (fromString)
32+
import Development.IDE (Action, IdeRule,
33+
NormalizedFilePath,
34+
Range,
35+
rangeToRealSrcSpan,
36+
realSrcSpanToRange)
37+
import qualified Development.IDE.Core.PositionMapping as P
38+
import qualified Development.IDE.Core.Shake as IDE
39+
import Development.IDE.GHC.Compat (RealSrcSpan, srcSpanFile)
40+
import Development.IDE.GHC.Compat.Util (unpackFS)
41+
42+
43+
------------------------------------------------------------------------------
44+
-- | A data kind for 'Tracked'.
45+
data Age = Current | Stale Type
46+
47+
48+
------------------------------------------------------------------------------
49+
-- | Some value, tagged with its age. All 'Current' ages are considered to be
50+
-- the same thing, but 'Stale' values are protected by an untouchable variable
51+
-- to ensure they can't be unified.
52+
newtype Tracked (age :: Age) a = UnsafeTracked
53+
{ unTrack :: a
54+
}
55+
deriving stock (Functor, Foldable, Traversable)
56+
deriving newtype (Eq, Ord, Show, Read, ToJSON, FromJSON, NFData)
57+
deriving (Applicative, Monad) via Identity
58+
59+
60+
------------------------------------------------------------------------------
61+
-- | Like 'P.PositionMapping', but with annotated ages for how 'Tracked' values
62+
-- change. Use the 'Category' instance to compose 'PositionMapping's in order
63+
-- to transform between values of different stale ages.
64+
newtype PositionMap (from :: Age) (to :: Age) = PositionMap
65+
{ _getPositionMapping :: P.PositionMapping
66+
}
67+
68+
instance Category PositionMap where
69+
id = coerce P.zeroMapping
70+
(.) = coerce P.composeDelta
71+
72+
73+
------------------------------------------------------------------------------
74+
-- | Get a 'PositionMap' that runs in the opposite direction.
75+
dualPositionMap :: PositionMap from to -> PositionMap to from
76+
dualPositionMap (PositionMap (P.PositionMapping (P.PositionDelta from to))) =
77+
PositionMap $ P.PositionMapping $ P.PositionDelta to from
78+
79+
80+
------------------------------------------------------------------------------
81+
-- | A pair containing a @'Tracked' 'Stale'@ value, as well as
82+
-- a 'PositionMapping' that will fast-forward values to the current age.
83+
data TrackedStale a where
84+
TrackedStale
85+
:: Tracked (Stale s) a
86+
-> PositionMap (Stale s) Current
87+
-> TrackedStale a
88+
89+
instance Functor TrackedStale where
90+
fmap f (TrackedStale t pm) = TrackedStale (fmap f t) pm
91+
92+
93+
untrackedStaleValue :: TrackedStale a -> a
94+
untrackedStaleValue (TrackedStale ta _) = coerce ta
95+
96+
97+
------------------------------------------------------------------------------
98+
-- | A class for which 'Tracked' values can be run across a 'PositionMapping'
99+
-- to change their ages.
100+
class MapAge a where
101+
{-# MINIMAL mapAgeFrom | mapAgeTo #-}
102+
mapAgeFrom :: PositionMap from to -> Tracked to a -> Maybe (Tracked from a)
103+
mapAgeFrom = mapAgeTo . dualPositionMap
104+
105+
mapAgeTo :: PositionMap from to -> Tracked from a -> Maybe (Tracked to a)
106+
mapAgeTo = mapAgeFrom . dualPositionMap
107+
108+
109+
instance MapAge Range where
110+
mapAgeFrom = coerce P.fromCurrentRange
111+
mapAgeTo = coerce P.toCurrentRange
112+
113+
114+
instance MapAge RealSrcSpan where
115+
mapAgeFrom =
116+
invMapAge (\fs -> rangeToRealSrcSpan (fromString $ unpackFS fs))
117+
(srcSpanFile &&& realSrcSpanToRange)
118+
. mapAgeFrom
119+
120+
121+
------------------------------------------------------------------------------
122+
-- | Helper function for deriving 'MapAge' for values in terms of other
123+
-- instances.
124+
invMapAge
125+
:: (c -> a -> b)
126+
-> (b -> (c, a))
127+
-> (Tracked from a -> Maybe (Tracked to a))
128+
-> Tracked from b
129+
-> Maybe (Tracked to b)
130+
invMapAge to from f t =
131+
let (c, t') = unTrack $ fmap from t
132+
in fmap (fmap $ to c) $ f $ UnsafeTracked t'
133+
134+
135+
unsafeMkCurrent :: age -> Tracked 'Current age
136+
unsafeMkCurrent = coerce
137+
138+
139+
unsafeMkStale :: age -> Tracked (Stale s) age
140+
unsafeMkStale = coerce
141+
142+
143+
unsafeCopyAge :: Tracked age a -> b -> Tracked age b
144+
unsafeCopyAge _ = coerce
145+
146+
147+
-- | Request a Rule result, it not available return the last computed result, if any, which may be stale
148+
useWithStale :: IdeRule k v
149+
=> k -> NormalizedFilePath -> Action (Maybe (TrackedStale v))
150+
useWithStale key file = do
151+
x <- IDE.useWithStale key file
152+
pure $ x <&> \(v, pm) ->
153+
TrackedStale (coerce v) (coerce pm)
154+
155+
-- | Request a Rule result, it not available return the last computed result which may be stale.
156+
-- Errors out if none available.
157+
useWithStale_ :: IdeRule k v
158+
=> k -> NormalizedFilePath -> Action (TrackedStale v)
159+
useWithStale_ key file = do
160+
(v, pm) <- IDE.useWithStale_ key file
161+
pure $ TrackedStale (coerce v) (coerce pm)
162+

0 commit comments

Comments
 (0)