Skip to content

Commit 8697f5f

Browse files
authored
Semantic tokens: expand type synonym to checkout forall function type when possible (#3967)
* expand type synonym to extract function type when possible * rename coreFullView to avoid conliction in ghc 9.8
1 parent ccfc57b commit 8697f5f

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

plugins/hls-semantic-tokens-plugin/src/Ide/Plugin/SemanticTokens/Mappings.hs

+9-1
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,21 @@ tyThingSemantic ty = case ty of
8181
isFunVar :: Var -> Bool
8282
isFunVar var = isFunType $ varType var
8383

84+
-- expand the type synonym https://hackage.haskell.org/package/ghc-9.8.1/docs/src/GHC.Core.Type.html
85+
expandTypeSyn :: Type -> Type
86+
expandTypeSyn ty
87+
| Just ty' <- coreView ty = expandTypeSyn ty'
88+
| otherwise = ty
89+
8490
isFunType :: Type -> Bool
85-
isFunType a = case a of
91+
isFunType a = case expandTypeSyn a of
8692
ForAllTy _ t -> isFunType t
8793
-- Development.IDE.GHC.Compat.Core.FunTy(pattern synonym), FunTyFlag which is used to distinguish
8894
-- (->, =>, etc..)
8995
FunTy flg _ rhs -> isVisibleFunArg flg || isFunType rhs
9096
_x -> isFunTy a
9197

98+
9299
hieKindFunMasksKind :: HieKind a -> HieFunMaskKind a
93100
hieKindFunMasksKind hieKind = case hieKind of
94101
HieFresh -> HieFreshFun
@@ -119,6 +126,7 @@ recoverFunMaskArray flattened = unflattened
119126
go (HQualTy _constraint b) = b
120127
go (HCastTy b) = b
121128
go HCoercionTy = False
129+
-- we have no enough information to expand the type synonym
122130
go (HTyConApp _ _) = False
123131

124132
typeSemantic :: HieFunMaskKind hType -> hType -> Maybe HsSemanticTokenType

plugins/hls-semantic-tokens-plugin/test/Main.hs

+1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ semanticTokensFunctionTests =
215215
"get semantic of functions"
216216
[ goldenWithSemanticTokensWithDefaultConfig "functions" "TFunction",
217217
goldenWithSemanticTokensWithDefaultConfig "local functions" "TFunctionLocal",
218+
goldenWithSemanticTokensWithDefaultConfig "functions under type synonym" "TFunctionUnderTypeSynonym",
218219
goldenWithSemanticTokensWithDefaultConfig "function in let binding" "TFunctionLet",
219220
goldenWithSemanticTokensWithDefaultConfig "negative case non-function with constraint" "TNoneFunctionWithConstraint"
220221
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
3:6-8 TTypeSynonym "T1"
2+
3:11-14 TTypeConstructor "Int"
3+
3:18-21 TTypeConstructor "Int"
4+
4:6-8 TTypeSynonym "T2"
5+
4:18-19 TTypeVariable "a"
6+
4:21-22 TTypeVariable "a"
7+
4:26-27 TTypeVariable "a"
8+
5:1-3 TFunction "f1"
9+
5:7-9 TTypeSynonym "T1"
10+
6:1-3 TFunction "f1"
11+
6:4-5 TVariable "x"
12+
6:8-9 TVariable "x"
13+
7:1-3 TFunction "f2"
14+
7:7-9 TTypeSynonym "T2"
15+
8:1-3 TFunction "f2"
16+
8:4-5 TVariable "x"
17+
8:8-9 TVariable "x"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module TFunctionUnderTypeSynonym where
2+
3+
type T1 = Int -> Int
4+
type T2 = forall a. a -> a
5+
f1 :: T1
6+
f1 x = x
7+
f2 :: T2
8+
f2 x = x
9+

0 commit comments

Comments
 (0)