Skip to content

Commit

Permalink
hls-class-plugin: Find methods even inside an EvidenceVarBind
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaymankar committed Jun 17, 2022
1 parent 3153125 commit da0d388
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
20 changes: 18 additions & 2 deletions plugins/hls-class-plugin/src/Ide/Plugin/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,29 @@ codeAction recorder state plId (CodeActionParams _ _ docId _ context) = liftIO $
$ pointCommand hf (instanceRange ^. J.start & J.character -~ 1)
$ map (T.pack . getOccString) . rights . findInstanceValBindIdentifiers

-- | Recurses through the given AST to find identifiers which are are
-- 'InstanceValBind's. Two types of nodes are chosen to look inside:
--
-- 1. The nodes which don't have any identifiers: these are the nodes which
-- somehow wrap an expression, like the instance declaration itself, or a
-- 'Match' which contains the 'InstanceValBind'.
--
-- 2. The nodes which have an 'EvidenceVarBind': if one of the implemented
-- methods uses functions like 'undefined', the binding for the '?callstack'
-- gets bound over the 'Match' which contains the 'InstanceValBind', so to
-- find the 'InstanceValBind' this function must look inside any
-- 'EvidenceVarBind's it finds.
findInstanceValBindIdentifiers :: HieAST a -> [Identifier]
findInstanceValBindIdentifiers ast
| Map.null (getNodeIds ast) = concatMap findInstanceValBindIdentifiers (nodeChildren ast)
| Map.null nodeIds
|| hasEvidenceBind nodeIds = concatMap findInstanceValBindIdentifiers (nodeChildren ast)
| otherwise = Map.keys
. Map.filter (not . Set.null)
. Map.map (Set.filter isInstanceValBind . identInfo)
$ getNodeIds ast
$ nodeIds
where
nodeIds = getNodeIds ast
hasEvidenceBind = not . Map.null . Map.filter (any isEvidenceBind . identInfo)

ghostSpan :: RealSrcSpan
ghostSpan = realSrcLocSpan $ mkRealSrcLoc (fsLit "<haskell-language-sever>") 1 1
Expand Down
5 changes: 4 additions & 1 deletion plugins/hls-class-plugin/test/testdata/T6.1.expected.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class Test a where
h :: a -> a
h = f

{-# MINIMAL f, g | g, h #-}
i :: a

{-# MINIMAL f, g, i | g, h #-}

instance Test X where
f X = X
f Y = Y
i = undefined
g = _
5 changes: 4 additions & 1 deletion plugins/hls-class-plugin/test/testdata/T6.2.expected.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ class Test a where
h :: a -> a
h = f

{-# MINIMAL f, g | g, h #-}
i :: a

{-# MINIMAL f, g, i | g, h #-}

instance Test X where
f X = X
f Y = Y
i = undefined
g = _
h = _
5 changes: 4 additions & 1 deletion plugins/hls-class-plugin/test/testdata/T6.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ class Test a where
h :: a -> a
h = f

{-# MINIMAL f, g | g, h #-}
i :: a

{-# MINIMAL f, g, i | g, h #-}

instance Test X where
f X = X
f Y = Y
i = undefined

0 comments on commit da0d388

Please sign in to comment.