Skip to content

Commit 160c509

Browse files
isovectorjneira
andauthored
Wingman: maintain user-defined fixity for definitions (#1697)
* Pass the discovered node to withSmallest/Largest * Maintain fixity when doing case split * New layout tests * Fix tests * Fix tests wrt new infrastructure * Update stack yamls * Update cabal index state * Forgot a stack yaml * New commit to hopefully fix CI * operational is broken upstream * I dunno what I'm doing Co-authored-by: Javier Neira <atreyu.bbb@gmail.com>
1 parent 4d730b3 commit 160c509

29 files changed

+92
-35
lines changed

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ source-repository-package
3636

3737
write-ghc-environment-files: never
3838

39-
index-state: 2021-06-30T16:00:00Z
39+
index-state: 2021-07-12T16:00:00Z
4040

4141
constraints:
4242
-- Diagrams doesn't support optparse-applicative >= 0.16 yet

ghcide/src/Development/IDE/GHC/ExactPrint.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ genericGraftWithSmallestM ::
338338
-- | The type of nodes we'd like to consider when finding the smallest.
339339
Proxy (Located ast) ->
340340
SrcSpan ->
341-
(DynFlags -> GenericM (TransformT m)) ->
341+
(DynFlags -> ast -> GenericM (TransformT m)) ->
342342
Graft m a
343343
genericGraftWithSmallestM proxy dst trans = Graft $ \dflags ->
344344
smallestM (genericIsSubspan proxy dst) (trans dflags)
@@ -351,7 +351,7 @@ genericGraftWithLargestM ::
351351
-- | The type of nodes we'd like to consider when finding the largest.
352352
Proxy (Located ast) ->
353353
SrcSpan ->
354-
(DynFlags -> GenericM (TransformT m)) ->
354+
(DynFlags -> ast -> GenericM (TransformT m)) ->
355355
Graft m a
356356
genericGraftWithLargestM proxy dst trans = Graft $ \dflags ->
357357
largestM (genericIsSubspan proxy dst) (trans dflags)

ghcide/src/Generics/SYB/GHC.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ genericIsSubspan ::
2929
-- | The type of nodes we'd like to consider.
3030
Proxy (Located ast) ->
3131
SrcSpan ->
32-
GenericQ (Maybe Bool)
32+
GenericQ (Maybe (Bool, ast))
3333
genericIsSubspan _ dst = mkQ Nothing $ \case
34-
(L span _ :: Located ast) -> Just $ dst `isSubspanOf` span
34+
(L span ast :: Located ast) -> Just (dst `isSubspanOf` span, ast)
3535

3636

3737
-- | Lift a function that replaces a value with several values into a generic
@@ -70,19 +70,19 @@ type GenericMQ r m = forall a. Data a => a -> m (r, a)
7070
-- with data nodes, so for any given node we can only definitely return an
7171
-- answer if it's a 'Located'. See 'genericIsSubspan' for how this parameter is
7272
-- used.
73-
smallestM :: forall m. Monad m => GenericQ (Maybe Bool) -> GenericM m -> GenericM m
73+
smallestM :: forall m a. Monad m => GenericQ (Maybe (Bool, a)) -> (a -> GenericM m) -> GenericM m
7474
smallestM q f = fmap snd . go
7575
where
7676
go :: GenericMQ Any m
7777
go x = do
7878
case q x of
7979
Nothing -> gmapMQ go x
80-
Just True -> do
80+
Just (True, a) -> do
8181
it@(r, x') <- gmapMQ go x
8282
case r of
8383
Any True -> pure it
84-
Any False -> fmap (Any True,) $ f x'
85-
Just False -> pure (mempty, x)
84+
Any False -> fmap (Any True,) $ f a x'
85+
Just (False, _) -> pure (mempty, x)
8686

8787
------------------------------------------------------------------------------
8888
-- | Apply the given 'GenericM' at every node that passes the 'GenericQ', but
@@ -94,14 +94,14 @@ smallestM q f = fmap snd . go
9494
-- with data nodes, so for any given node we can only definitely return an
9595
-- answer if it's a 'Located'. See 'genericIsSubspan' for how this parameter is
9696
-- used.
97-
largestM :: forall m. Monad m => GenericQ (Maybe Bool) -> GenericM m -> GenericM m
97+
largestM :: forall m a. Monad m => GenericQ (Maybe (Bool, a)) -> (a -> GenericM m) -> GenericM m
9898
largestM q f = go
9999
where
100100
go :: GenericM m
101101
go x = do
102102
case q x of
103-
Just True -> f x
104-
Just False -> pure x
103+
Just (True, a) -> f a x
104+
Just (False, _) -> pure x
105105
Nothing -> gmapM go x
106106

107107
newtype MonadicQuery r m a = MonadicQuery

plugins/hls-tactics-plugin/hls-tactics-plugin.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ library
7676
, ghc
7777
, ghc-boot-th
7878
, ghc-exactprint
79-
, ghc-source-gen
79+
, ghc-source-gen ^>=0.4.1
8080
, ghcide ^>=1.4
8181
, hls-graph
8282
, hls-plugin-api ^>=1.1

plugins/hls-tactics-plugin/src/Wingman/CaseSplit.hs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Data.Set (Set)
1111
import qualified Data.Set as S
1212
import Development.IDE.GHC.Compat
1313
import GHC.Exts (IsString (fromString))
14-
import GHC.SourceGen (funBinds, match, wildP)
14+
import GHC.SourceGen (funBindsWithFixity, match, wildP)
1515
import OccName
1616
import Wingman.GHC
1717
import Wingman.Types
@@ -72,12 +72,16 @@ rewriteVarPat name rep = everywhere $
7272
------------------------------------------------------------------------------
7373
-- | Construct an 'HsDecl' from a set of 'AgdaMatch'es.
7474
splitToDecl
75-
:: OccName -- ^ The name of the function
75+
:: Maybe LexicalFixity
76+
-> OccName -- ^ The name of the function
7677
-> [AgdaMatch]
7778
-> LHsDecl GhcPs
78-
splitToDecl name ams = noLoc $ funBinds (fromString . occNameString . occName $ name) $ do
79-
AgdaMatch pats body <- ams
80-
pure $ match pats body
79+
splitToDecl fixity name ams = do
80+
traceX "fixity" fixity $
81+
noLoc $
82+
funBindsWithFixity fixity (fromString . occNameString . occName $ name) $ do
83+
AgdaMatch pats body <- ams
84+
pure $ match pats body
8185

8286

8387
------------------------------------------------------------------------------

plugins/hls-tactics-plugin/src/Wingman/Judgements.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ jHasBoundArgs
375375
. jLocalHypothesis
376376

377377

378+
jNeedsToBindArgs :: Judgement' CType -> Bool
379+
jNeedsToBindArgs = isFunTy . unCType . jGoal
380+
381+
378382
------------------------------------------------------------------------------
379383
-- | Fold a hypothesis into a single mapping from name to info. This
380384
-- unavoidably will cause duplicate names (things like methods) to shadow one

plugins/hls-tactics-plugin/src/Wingman/Plugin.hs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import System.Timeout
3131
import Wingman.CaseSplit
3232
import Wingman.EmptyCase
3333
import Wingman.GHC
34+
import Wingman.Judgements (jNeedsToBindArgs)
3435
import Wingman.LanguageServer
3536
import Wingman.LanguageServer.Metaprogram (hoverProvider)
3637
import Wingman.LanguageServer.TacticProviders
@@ -189,20 +190,36 @@ graftHole
189190
-> Graft (Either String) ParsedSource
190191
graftHole span rtr
191192
| _jIsTopHole (rtr_jdg rtr)
192-
= genericGraftWithSmallestM (Proxy @(Located [LMatch GhcPs (LHsExpr GhcPs)])) span $ \dflags ->
193-
everywhereM'
194-
$ mkBindListT $ \ix ->
195-
graftDecl dflags span ix $ \name pats ->
196-
splitToDecl (occName name)
197-
$ iterateSplit
198-
$ mkFirstAgda (fmap unXPat pats)
199-
$ unLoc
200-
$ rtr_extract rtr
193+
= genericGraftWithSmallestM
194+
(Proxy @(Located [LMatch GhcPs (LHsExpr GhcPs)])) span
195+
$ \dflags matches ->
196+
everywhereM'
197+
$ mkBindListT $ \ix ->
198+
graftDecl dflags span ix $ \name pats ->
199+
splitToDecl
200+
(case not $ jNeedsToBindArgs (rtr_jdg rtr) of
201+
-- If the user has explicitly bound arguments, use the
202+
-- fixity they wrote.
203+
True -> matchContextFixity . m_ctxt . unLoc
204+
=<< listToMaybe matches
205+
-- Otherwise, choose based on the name of the function.
206+
False -> Nothing
207+
)
208+
(occName name)
209+
$ iterateSplit
210+
$ mkFirstAgda (fmap unXPat pats)
211+
$ unLoc
212+
$ rtr_extract rtr
201213
graftHole span rtr
202214
= graft span
203215
$ rtr_extract rtr
204216

205217

218+
matchContextFixity :: HsMatchContext p -> Maybe LexicalFixity
219+
matchContextFixity (FunRhs _ l _) = Just l
220+
matchContextFixity _ = Nothing
221+
222+
206223
------------------------------------------------------------------------------
207224
-- | Helper function to route 'mergeFunBindMatches' into the right place in an
208225
-- AST --- correctly dealing with inserting into instance declarations.

plugins/hls-tactics-plugin/src/Wingman/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ instance Show TyCon where
154154
instance Show ConLike where
155155
show = unsafeRender
156156

157+
instance Show LexicalFixity where
158+
show = unsafeRender
159+
157160

158161
------------------------------------------------------------------------------
159162
-- | The state that should be shared between subgoals. Extracts move towards

plugins/hls-tactics-plugin/test/CodeAction/DestructSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ spec = do
113113
, (id, DestructLambdaCase, "")
114114
]
115115

116+
-- test layouts that maintain user-written fixities
117+
destructTest "b" 3 13 "LayoutInfixKeep"
118+
destructTest "b" 2 12 "LayoutPrefixKeep"
119+

plugins/hls-tactics-plugin/test/golden/KnownBigSemigroup.expected.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Data.Monoid
33
data Big a = Big [Bool] (Sum Int) String (Endo a) Any
44

55
instance Semigroup (Big a) where
6-
(<>) (Big bs sum s en any) (Big bs' sum' str en' any')
6+
(Big bs sum s en any) <> (Big bs' sum' str en' any')
77
= Big
88
(bs <> bs') (sum <> sum') (s <> str) (en <> en') (any <> any')
99

plugins/hls-tactics-plugin/test/golden/KnownCounterfactualSemigroup.expected.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
data Semi = Semi [String] Int
44

55
instance Semigroup Int => Semigroup Semi where
6-
(<>) (Semi ss n) (Semi strs i) = Semi (ss <> strs) (n <> i)
6+
(Semi ss n) <> (Semi strs i) = Semi (ss <> strs) (n <> i)
77

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
data Test a = Test [a]
22

33
instance Semigroup (Test a) where
4-
(<>) (Test a) (Test c) = Test (a <> c)
4+
(Test a) <> (Test c) = Test (a <> c)
55

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
data Semi = Semi [String] Int
22

33
instance Semigroup Semi where
4-
(<>) (Semi ss n) (Semi strs i) = Semi (ss <> strs) _
4+
(Semi ss n) <> (Semi strs i) = Semi (ss <> strs) _
55

plugins/hls-tactics-plugin/test/golden/KnownModuleInstanceSemigroup.expected.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ instance Semigroup Foo where
77
data Bar = Bar Foo Foo
88

99
instance Semigroup Bar where
10-
(<>) (Bar foo foo') (Bar foo2 foo3)
10+
(Bar foo foo') <> (Bar foo2 foo3)
1111
= Bar (foo <> foo2) (foo' <> foo3)
1212

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
data Semi a = Semi a
22

33
instance Semigroup a => Semigroup (Semi a) where
4-
(<>) (Semi a) (Semi a') = Semi (a <> a')
4+
(Semi a) <> (Semi a') = Semi (a <> a')
55

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- keep layout that was written by the user in infix
2+
foo :: Bool -> a -> a
3+
False `foo` a = _
4+
True `foo` a = _
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- keep layout that was written by the user in infix
2+
foo :: Bool -> a -> a
3+
b `foo` a = _
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(-/) :: Bool -> a -> a
2+
(-/) False a = _
3+
(-/) True a = _
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(-/) :: Bool -> a -> a
2+
(-/) b a = _
3+

stack-8.10.2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extra-deps:
4343
- ghc-exactprint-0.6.4
4444
- ghc-lib-8.10.4.20210206
4545
- ghc-lib-parser-8.10.4.20210206
46+
- ghc-source-gen-0.4.1.0
4647
- lsp-1.2.0.0
4748
- lsp-types-1.2.0.0
4849
- lsp-test-0.14.0.0

stack-8.10.3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extra-deps:
4343
- ghc-exactprint-0.6.4
4444
- ghc-lib-8.10.4.20210206
4545
- ghc-lib-parser-8.10.4.20210206
46+
- ghc-source-gen-0.4.1.0
4647
- heapsize-0.3.0
4748
- hie-bios-0.7.5
4849
- implicit-hie-cradle-0.3.0.2

stack-8.10.4.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extra-deps:
4141
commit: 8fee87eac97a538dbe81ff1ab18cff10f2f9fa15
4242
- ghc-check-0.5.0.4
4343
- ghc-exactprint-0.6.4
44+
- ghc-source-gen-0.4.1.0
4445
- heapsize-0.3.0
4546
- implicit-hie-cradle-0.3.0.2
4647
- implicit-hie-0.1.2.5

stack-8.10.5.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ extra-deps:
4343
commit: 8fee87eac97a538dbe81ff1ab18cff10f2f9fa15
4444
- ghc-check-0.5.0.4
4545
- ghc-exactprint-0.6.4
46+
- ghc-source-gen-0.4.1.0
4647
- heapsize-0.3.0
4748
- implicit-hie-cradle-0.3.0.2
4849
- implicit-hie-0.1.2.5

stack-8.6.4.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extra-deps:
5252
- ghc-lib-8.10.4.20210206
5353
- ghc-lib-parser-8.10.4.20210206
5454
- ghc-lib-parser-ex-8.10.0.17
55-
- ghc-source-gen-0.4.0.0
55+
- ghc-source-gen-0.4.1.0
5656
- ghc-trace-events-0.1.2.1
5757
- haddock-api-2.22.0@rev:1
5858
- haddock-library-1.10.0

stack-8.6.5.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extra-deps:
5353
- ghc-lib-8.10.4.20210206
5454
- ghc-lib-parser-8.10.4.20210206
5555
- ghc-lib-parser-ex-8.10.0.17
56-
- ghc-source-gen-0.4.0.0
56+
- ghc-source-gen-0.4.1.0
5757
- ghc-trace-events-0.1.2.1
5858
- haddock-api-2.22.0@rev:1
5959
- haddock-library-1.10.0

stack-8.8.3.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extra-deps:
4545
- ghc-exactprint-0.6.4
4646
- ghc-lib-8.10.4.20210206
4747
- ghc-lib-parser-8.10.4.20210206
48+
- ghc-source-gen-0.4.1.0
4849
- ghc-trace-events-0.1.2.1
4950
- haskell-src-exts-1.21.1
5051
- heapsize-0.3.0

stack-8.8.4.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extra-deps:
4545
- ghc-exactprint-0.6.4
4646
- ghc-lib-8.10.4.20210206
4747
- ghc-lib-parser-8.10.4.20210206
48+
- ghc-source-gen-0.4.1.0
4849
- ghc-trace-events-0.1.2.1
4950
- haskell-src-exts-1.21.1
5051
- heapsize-0.3.0

stack-9.0.1.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extra-deps:
4141
- ghc-lib-9.0.1.20210324@sha256:c8b9a2541ea3424c8d0e4f80584477d0f35be03f4a47d931152042d5f446c5fc,19279
4242
- ghc-lib-parser-9.0.1.20210324@sha256:fb680f78d4ab08b5d089a05bda3b84ad857e5edcc2e4ca7c188c0207d369af80
4343
- ghc-lib-parser-ex-9.0.0.4@sha256:8282b11c3797fc8ba225b245e736cc9a0745d9c48d0f9fea7f9bffb5c9997709,3642
44+
- ghc-source-gen-0.4.1.0
4445
- haddock-library-1.10.0@sha256:2a6c239da9225951a5d837e1ce373faeeae60d1345c78dd0a0b0f29df30c4fe9,4098
4546
- heapsize-0.3.0.1@sha256:0b69aa97a46d819b700ac7b145f3b5493c3565cf2c5b8298682238d405d0326e,1417
4647
- hiedb-0.4.0.0
@@ -94,6 +95,7 @@ extra-deps:
9495
commit: ca23431a8dfa013992f9164ccc882a3277361f17
9596
# https://github.com/diagrams/active/pull/36
9697

98+
9799
# benchmark dependency
98100
- github: HeinrichApfelmus/operational
99101
commit: 16e19aaf34e286f3d27b3988c61040823ec66537

stack.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ extra-deps:
3838
- fourmolu-0.3.0.0
3939
- ghc-api-compat-8.6
4040
- ghc-exactprint-0.6.4
41+
- ghc-source-gen-0.4.1.0
4142
- heapsize-0.3.0
4243
- implicit-hie-cradle-0.3.0.2
4344
- implicit-hie-0.1.2.5

0 commit comments

Comments
 (0)