Skip to content

Commit 6fa3e64

Browse files
authored
Support call hierarchy on pattern matching (#2129)
* Support call hierarchy on pattern matching * Make result satisfied with the way VSCode processes data * Version bump
1 parent 32cd57d commit 6fa3e64

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

plugins/hls-call-hierarchy-plugin/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Enabled by default. You can disable it in your editor settings whenever you like
2020
{
2121
"haskell.plugin.callHierarchy.globalOn": true
2222
}
23+
```
2324

2425
## Change log
2526
### 1.0.0.1

plugins/hls-call-hierarchy-plugin/hls-call-hierarchy-plugin.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 2.4
22
name: hls-call-hierarchy-plugin
3-
version: 1.0.0.1
3+
version: 1.0.0.2
44
synopsis: Call hierarchy plugin for Haskell Language Server
55
license: Apache-2.0
66
license-file: LICENSE

plugins/hls-call-hierarchy-plugin/src/Ide/Plugin/CallHierarchy/Internal.hs

+19-11
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ prepareCallHierarchy state pluginId param
4747
liftIO (runAction "CallHierarchy.prepareHierarchy" state (prepareCallHierarchyItem nfp pos)) >>=
4848
\case
4949
Just items -> pure $ Right $ Just $ List items
50-
Nothing -> pure $ Left $ responseError "Call Hierarchy: No result"
50+
Nothing -> pure $ Right Nothing
5151
| otherwise = pure $ Left $ responseError $ T.pack $ "Call Hierarchy: uriToNormalizedFilePath failed for: " <> show uri
5252
where
5353
uri = param ^. (L.textDocument . L.uri)
@@ -67,23 +67,28 @@ constructFromAst nfp pos =
6767
resolveIntoCallHierarchy :: Applicative f => HieASTs a -> Position -> NormalizedFilePath -> f (Maybe [CallHierarchyItem])
6868
resolveIntoCallHierarchy hf pos nfp =
6969
case listToMaybe $ pointCommand hf pos extract of
70-
Just res -> pure $ Just $ mapMaybe (construct nfp hf) res
71-
Nothing -> pure Nothing
70+
Nothing -> pure Nothing
71+
Just infos ->
72+
case mapMaybe (construct nfp hf) infos of
73+
[] -> pure Nothing
74+
res -> pure $ Just res
7275

7376
extract :: HieAST a -> [(Identifier, S.Set ContextInfo, Span)]
7477
extract ast = let span = nodeSpan ast
7578
infos = M.toList $ M.map identInfo (Compat.getNodeIds ast)
7679
in [ (ident, contexts, span) | (ident, contexts) <- infos ]
7780

7881
recFieldInfo, declInfo, valBindInfo, classTyDeclInfo,
79-
useInfo, patternBindInfo, tyDeclInfo :: [ContextInfo] -> Maybe ContextInfo
80-
recFieldInfo ctxs = listToMaybe [ctx | ctx@RecField{} <- ctxs]
81-
declInfo ctxs = listToMaybe [ctx | ctx@Decl{} <- ctxs]
82-
valBindInfo ctxs = listToMaybe [ctx | ctx@ValBind{} <- ctxs]
83-
classTyDeclInfo ctxs = listToMaybe [ctx | ctx@ClassTyDecl{} <- ctxs]
84-
useInfo ctxs = listToMaybe [Use | Use <- ctxs]
85-
patternBindInfo ctxs = listToMaybe [ctx | ctx@PatternBind{} <- ctxs]
86-
tyDeclInfo ctxs = listToMaybe [TyDecl | TyDecl <- ctxs]
82+
useInfo, patternBindInfo, tyDeclInfo, matchBindInfo
83+
:: [ContextInfo] -> Maybe ContextInfo
84+
recFieldInfo ctxs = listToMaybe [ctx | ctx@RecField{} <- ctxs]
85+
declInfo ctxs = listToMaybe [ctx | ctx@Decl{} <- ctxs]
86+
valBindInfo ctxs = listToMaybe [ctx | ctx@ValBind{} <- ctxs]
87+
classTyDeclInfo ctxs = listToMaybe [ctx | ctx@ClassTyDecl{} <- ctxs]
88+
useInfo ctxs = listToMaybe [Use | Use <- ctxs]
89+
patternBindInfo ctxs = listToMaybe [ctx | ctx@PatternBind{} <- ctxs]
90+
tyDeclInfo ctxs = listToMaybe [TyDecl | TyDecl <- ctxs]
91+
matchBindInfo ctxs = listToMaybe [MatchBind | MatchBind <- ctxs]
8792

8893
construct :: NormalizedFilePath -> HieASTs a -> (Identifier, S.Set ContextInfo, Span) -> Maybe CallHierarchyItem
8994
construct nfp hf (ident, contexts, ssp)
@@ -93,6 +98,9 @@ construct nfp hf (ident, contexts, ssp)
9398
-- ignored type span
9499
= Just $ mkCallHierarchyItem' ident SkField ssp ssp
95100

101+
| isJust (matchBindInfo ctxList) && isNothing (valBindInfo ctxList)
102+
= Just $ mkCallHierarchyItem' ident SkFunction ssp ssp
103+
96104
| Just ctx <- valBindInfo ctxList
97105
= Just $ case ctx of
98106
ValBind _ _ span -> mkCallHierarchyItem' ident SkFunction (renderSpan span) ssp

plugins/hls-call-hierarchy-plugin/src/Ide/Plugin/CallHierarchy/Types.hs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ instance FromRow Vertex where
3535
<*> field <*> field <*> field
3636
<*> field <*> field <*> field
3737
<*> field <*> field
38+
3839
data SymbolPosition = SymbolPosition {
3940
psl :: Int
4041
, psc :: Int

plugins/hls-call-hierarchy-plugin/test/Main.hs

+18
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ prepareCallHierarchyTests =
178178
expected = mkCallHierarchyItemV "b" SkFunction range selRange
179179
oneCaseWithCreate contents 0 2 expected
180180
]
181+
, testCase "multi pattern" $ do
182+
let contents = T.unlines
183+
[ "f (Just _) = ()"
184+
, "f Nothing = ()"
185+
]
186+
range = mkRange 1 0 1 1
187+
selRange = mkRange 1 0 1 1
188+
expected = mkCallHierarchyItemV "f" SkFunction range selRange
189+
oneCaseWithCreate contents 1 0 expected
181190
]
182191

183192
incomingCallsTests :: TestTree
@@ -263,6 +272,15 @@ incomingCallsTests =
263272
positions = [(1, 5)]
264273
ranges = [mkRange 1 13 1 14]
265274
incomingCallTestCase contents 1 13 positions ranges
275+
, testCase "multi pattern" $ do
276+
let contents = T.unlines
277+
[ "f 1 = 1"
278+
, "f 2 = 2"
279+
, "g = f"
280+
]
281+
positions = [(2, 0)]
282+
ranges = [mkRange 2 4 2 5]
283+
incomingCallTestCase contents 1 0 positions ranges
266284
]
267285
, testGroup "multi file"
268286
[ testCase "1" $ do

0 commit comments

Comments
 (0)