on master help sign in
Support more kinds of literals in hover (#339)

* Support more kinds of literals in hover * Fix for HLint * Fix for GHC 8.8 * Fix for 8.4 * Fix 8.4 + suggestions by @cocreature * More fixes for 8.4 * Deal with type sigs in all GHC versions * Additional case for 8.4 * Separate isLit and isChildLit Co-authored-by: Moritz Kiefer <moritz.kiefer@purelyfunctional.org>

commitea50c27fad0f5c38f5e5c10d9e9ed96bbe99cb9b authorAlejandro Serrano <trupill@gmail.com> dateMon 27 Jan 2020, 15:12 (7 years ago) parentae5c6d3
2 files changed+35−6
src/Development/IDE/Spans/Calculate.hs+31−2 test/exe/Main.hs+4−4
src/Development/IDE/Spans/Calculate.hs+31−2
@@ -158,8 +158,7 @@ getTypeLHsExpr tms e = do
158158 Nothing -> return Nothing
159159 where
160160 getSpanSource :: HsExpr GhcTc -> SpanSource
161− getSpanSource (HsLit U lit) = Lit (showGhc lit)
162− getSpanSource (HsOverLit U lit) = Lit (showGhc lit)
161+ getSpanSource xpr | isLit xpr = Lit (showGhc xpr)
163162 getSpanSource (HsVar U (L _ i)) = Named (getName i)
164163 getSpanSource (HsConLikeOut U (RealDataCon dc)) = Named (dataConName dc)
165164 getSpanSource RecordCon {rcon_con_name} = Named (getName rcon_con_name)
@@ -167,6 +166,36 @@ getTypeLHsExpr tms e = do
167166 getSpanSource (HsPar U xpr) = getSpanSource (unLoc xpr)
168167 getSpanSource _ = NoSource
169168
169+ isLit :: HsExpr GhcTc -> Bool
170+ isLit (HsLit U _) = True
171+ isLit (HsOverLit U _) = True
172+ isLit (ExplicitTuple U args _) = all (isTupLit . unLoc) args
173+#if MIN_GHC_API_VERSION(8,6,0)
174+ isLit (ExplicitSum U _ _ xpr) = isLitChild (unLoc xpr)
175+ isLit (ExplicitList U _ xprs) = all (isLitChild . unLoc) xprs
176+#else
177+ isLit (ExplicitSum _ _ xpr _) = isLitChild (unLoc xpr)
178+ isLit (ExplicitList _ _ xprs) = all (isLitChild . unLoc) xprs
179+#endif
180+ isLit _ = False
181+
182+ isTupLit (Present U xpr) = isLitChild (unLoc xpr)
183+ isTupLit _ = False
184+
185+ -- We need special treatment for children so things like [(1)] are still treated
186+ -- as a list literal while not treating (1) as a literal.
187+ isLitChild (HsWrap U _ xpr) = isLitChild xpr
188+ isLitChild (HsPar U xpr) = isLitChild (unLoc xpr)
189+#if MIN_GHC_API_VERSION(8,8,0)
190+ isLitChild (ExprWithTySig U xpr _) = isLitChild (unLoc xpr)
191+#elif MIN_GHC_API_VERSION(8,6,0)
192+ isLitChild (ExprWithTySig U xpr) = isLitChild (unLoc xpr)
193+#else
194+ isLitChild (ExprWithTySigOut xpr _) = isLitChild (unLoc xpr)
195+ isLitChild (ExprWithTySig xpr _) = isLitChild (unLoc xpr)
196+#endif
197+ isLitChild e = isLit e
198+
170199-- | Get the name and type of a pattern.
171200getTypeLPat :: (GhcMonad m)
172201 => [TypecheckedModule]
test/exe/Main.hs+4−4
@@ -1209,8 +1209,8 @@ findDefinitionAndHoverTests = let
12091209 tvrL40 = Position 40 37 ; kindV = [ExpectHoverText [":: * -> *\n"]]
12101210 intL41 = Position 41 20 ; litI = [ExpectHoverText ["7518"]]
12111211 chrL36 = Position 37 24 ; litC = [ExpectHoverText ["'f'"]]
1212− txtL8 = Position 8 14 ; litT = [ExpectHoverText ["\"dfgv\""]]
1213− lstL43 = Position 43 12 ; litL = [ExpectHoverText ["[ 8391 :: Int, 6268 ]"]]
1212+ txtL8 = Position 8 14 ; litT = [ExpectHoverText ["\"dfgy\""]]
1213+ lstL43 = Position 43 12 ; litL = [ExpectHoverText ["[8391 :: Int, 6268]"]]
12141214 outL45 = Position 45 3 ; outSig = [ExpectHoverText ["outer", "Bool"], mkR 46 0 46 5]
12151215 innL48 = Position 48 5 ; innSig = [ExpectHoverText ["inner", "Char"], mkR 49 2 49 7]
12161216 in
@@ -1246,8 +1246,8 @@ findDefinitionAndHoverTests = let
12461246 , test no broken tvrL40 kindV "kind of (* -> *) type variable #273"
12471247 , test no yes intL41 litI "literal Int in hover info #274"
12481248 , test no yes chrL36 litC "literal Char in hover info #274"
1249− , test no broken txtL8 litT "literal Text in hover info #274"
1250− , test no broken lstL43 litL "literal List in hover info #274"
1249+ , test no yes txtL8 litT "literal Text in hover info #274"
1250+ , test no yes lstL43 litL "literal List in hover info #274"
12511251 , test no yes docL41 constr "type constraint in hover info #283"
12521252 , test broken broken outL45 outSig "top-level signature #310"
12531253 , test broken broken innL48 innSig "inner signature #310"