Skip to content

Semantic tokens: expand type synonym to checkout forall function type when possible #3967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ tyThingSemantic ty = case ty of
isFunVar :: Var -> Bool
isFunVar var = isFunType $ varType var

-- expand the type synonym https://hackage.haskell.org/package/ghc-9.8.1/docs/src/GHC.Core.Type.html
expandTypeSyn :: Type -> Type
expandTypeSyn ty
| Just ty' <- coreView ty = expandTypeSyn ty'
| otherwise = ty

isFunType :: Type -> Bool
isFunType a = case a of
isFunType a = case expandTypeSyn a of
ForAllTy _ t -> isFunType t
-- Development.IDE.GHC.Compat.Core.FunTy(pattern synonym), FunTyFlag which is used to distinguish
-- (->, =>, etc..)
FunTy flg _ rhs -> isVisibleFunArg flg || isFunType rhs
_x -> isFunTy a


hieKindFunMasksKind :: HieKind a -> HieFunMaskKind a
hieKindFunMasksKind hieKind = case hieKind of
HieFresh -> HieFreshFun
Expand Down Expand Up @@ -119,6 +126,7 @@ recoverFunMaskArray flattened = unflattened
go (HQualTy _constraint b) = b
go (HCastTy b) = b
go HCoercionTy = False
-- we have no enough information to expand the type synonym
go (HTyConApp _ _) = False

typeSemantic :: HieFunMaskKind hType -> hType -> Maybe HsSemanticTokenType
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-semantic-tokens-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ semanticTokensFunctionTests =
"get semantic of functions"
[ goldenWithSemanticTokensWithDefaultConfig "functions" "TFunction",
goldenWithSemanticTokensWithDefaultConfig "local functions" "TFunctionLocal",
goldenWithSemanticTokensWithDefaultConfig "functions under type synonym" "TFunctionUnderTypeSynonym",
goldenWithSemanticTokensWithDefaultConfig "function in let binding" "TFunctionLet",
goldenWithSemanticTokensWithDefaultConfig "negative case non-function with constraint" "TNoneFunctionWithConstraint"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
3:6-8 TTypeSynonym "T1"
3:11-14 TTypeConstructor "Int"
3:18-21 TTypeConstructor "Int"
4:6-8 TTypeSynonym "T2"
4:18-19 TTypeVariable "a"
4:21-22 TTypeVariable "a"
4:26-27 TTypeVariable "a"
5:1-3 TFunction "f1"
5:7-9 TTypeSynonym "T1"
6:1-3 TFunction "f1"
6:4-5 TVariable "x"
6:8-9 TVariable "x"
7:1-3 TFunction "f2"
7:7-9 TTypeSynonym "T2"
8:1-3 TFunction "f2"
8:4-5 TVariable "x"
8:8-9 TVariable "x"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TFunctionUnderTypeSynonym where

type T1 = Int -> Int
type T2 = forall a. a -> a
f1 :: T1
f1 x = x
f2 :: T2
f2 x = x