Skip to content

Commit ae4455b

Browse files
committed
Fix pattern synonym, add tests
1 parent bc24187 commit ae4455b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

ghcide/src/Development/IDE/Plugin/TypeLenses.hs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Development.IDE.Plugin.TypeLenses
66
)
77
where
88

9+
import ConLike (ConLike (PatSynCon))
910
import Control.Applicative ((<|>))
1011
import Control.Monad.IO.Class
1112
import Data.Aeson.Types (Value (..), toJSON)
@@ -94,29 +95,30 @@ commandHandler _ideState wedit = do
9495
return $ Right Null
9596

9697
suggestSignature :: Bool -> Maybe TcModuleResult -> Maybe Bindings -> Diagnostic -> [(T.Text, [TextEdit])]
97-
suggestSignature isQuickFix mTmr mBindings Diagnostic {_message, _range = _range@Range {..}}
98+
suggestSignature isQuickFix mTmr mBindings Diagnostic {_message, _range = Range {..}}
9899
| _message
99100
=~ ("(Top-level binding|Polymorphic local binding|Pattern synonym) with no type signature" :: T.Text),
100101
Just bindings <- mBindings,
101102
Just TcModuleResult {tmrTypechecked = TcGblEnv {tcg_type_env, tcg_rn_decls, tcg_rdr_env}} <- mTmr,
102103
localScope <- getFuzzyScope bindings _start _end,
103104
Just group <- tcg_rn_decls,
104105
Just name <- getFirstIdAtLine (succ $ _line _start) group,
105-
Just ty <- (lookupTypeEnv tcg_type_env name >>= safeTyThingType) <|> (find (\(x, _) -> x == name) localScope >>= snd),
106+
Just (isPatSyn, ty) <-
107+
(lookupTypeEnv tcg_type_env name >>= \x -> (isTyThingPatSyn x,) <$> safeTyThingType x)
108+
<|> ((False,) <$> (find (\(x, _) -> x == name) localScope >>= snd)),
106109
tyMsg <- showSDocForUser unsafeGlobalDynFlags (mkPrintUnqualified unsafeGlobalDynFlags tcg_rdr_env) $ pprSigmaType ty,
107-
signature <- T.pack $ printName name <> " :: " <> tyMsg,
110+
signature <- T.pack $ (if isPatSyn then "pattern " else "") <> printName name <> " :: " <> tyMsg,
111+
startCharacter <- if "local binding" `T.isInfixOf` _message then _character _start else 0,
108112
startOfLine <- Position (_line _start) startCharacter,
109113
beforeLine <- Range startOfLine startOfLine,
110114
title <- if isQuickFix then "add signature: " <> signature else signature,
111115
action <- TextEdit beforeLine $ signature <> "\n" <> T.replicate startCharacter " " =
112116
[(title, [action])]
113117
| otherwise = []
114-
where
115-
startCharacter
116-
| "Polymorphic local binding" `T.isPrefixOf` _message =
117-
_character _start
118-
| otherwise =
119-
0
118+
119+
isTyThingPatSyn :: TyThing -> Bool
120+
isTyThingPatSyn (AConLike (PatSynCon _)) = True
121+
isTyThingPatSyn _ = False
120122

121123
getFirstIdAtLine :: Int -> HsGroup GhcRn -> Maybe Name
122124
getFirstIdAtLine line = something (mkQ Nothing f)

ghcide/test/exe/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,8 @@ addSigLensesTests = let
31833183
, sigSession enableWarnings "a `haha` b = a b" "haha :: (t1 -> t2) -> t1 -> t2"
31843184
, sigSession enableWarnings "pattern Some a = Just a" "pattern Some :: a -> Maybe a"
31853185
, sigSession enableWarnings "qualifiedSigTest= C.realPart" "qualifiedSigTest :: C.Complex a -> a"
3186+
, sigSession enableWarnings "head = 233" "head :: Integer"
3187+
, sigSession enableWarnings "a *.* b = a b" "(*.*) :: (t1 -> t2) -> t1 -> t2"
31863188
]
31873189
| (title, enableWarnings) <-
31883190
[("with warnings enabled", True)

0 commit comments

Comments
 (0)