Skip to content

Commit 12a8bd3

Browse files
committed
haskell#3017 WIP: Add Action for all missing class methods
1 parent f629652 commit 12a8bd3

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

plugins/hls-class-plugin/src/Ide/Plugin/Class/CodeAction.hs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Control.Monad.Trans.Class (lift)
1313
import Control.Monad.Trans.Except (ExceptT, throwE)
1414
import Control.Monad.Trans.Maybe
1515
import Data.Aeson
16+
import Data.Bifunctor (second)
1617
import Data.Either.Extra (rights)
1718
import Data.List
1819
import qualified Data.Map.Strict as Map
@@ -113,14 +114,16 @@ codeAction recorder state plId (CodeActionParams _ _ docId _ context) = pluginRe
113114
logWith recorder Info (LogImplementedMethods cls implemented)
114115
pure
115116
$ concatMap mkAction
116-
$ fmap (filter (\(bind, _) -> bind `notElem` implemented))
117+
$ filter ((/=) mempty . snd)
118+
$ fmap (second (filter (\(bind, _) -> bind `notElem` implemented)))
119+
$ (<>) [foo range sigs]
117120
$ minDefToMethodGroups range sigs
118121
$ classMinimalDef cls
119122
where
120123
range = diag ^. J.range
121124

122-
mkAction :: [(T.Text, T.Text)] -> [Command |? CodeAction]
123-
mkAction methodGroup
125+
mkAction :: Suggestion -> [Command |? CodeAction]
126+
mkAction (name, methodGroup)
124127
= [ mkCodeAction title
125128
$ mkLspCommand plId codeActionCommandId title
126129
(Just $ mkCmdParams methodGroup False)
@@ -129,14 +132,8 @@ codeAction recorder state plId (CodeActionParams _ _ docId _ context) = pluginRe
129132
(Just $ mkCmdParams methodGroup True)
130133
]
131134
where
132-
title = mkTitle $ fst <$> methodGroup
133-
titleWithSig = mkTitleWithSig $ fst <$> methodGroup
134-
135-
mkTitle methodGroup
136-
= "Add placeholders for "
137-
<> mconcat (intersperse ", " (fmap (\m -> "'" <> m <> "'") methodGroup))
138-
139-
mkTitleWithSig methodGroup = mkTitle methodGroup <> " with signature(s)"
135+
title = "Add placeholders for " <> name
136+
titleWithSig = title <> " with signature(s)"
140137

141138
mkCmdParams methodGroup withSig =
142139
[toJSON (AddMinimalMethodsParams uri range (List methodGroup) withSig)]
@@ -211,15 +208,42 @@ isInstanceValBind :: ContextInfo -> Bool
211208
isInstanceValBind (ValBind InstanceBind _ _) = True
212209
isInstanceValBind _ = False
213210

211+
type MethodSig = T.Text
212+
type MethodName = T.Text
213+
type MethodGroup = (MethodName, MethodSig)
214+
type Suggestion = (T.Text, [MethodGroup])
215+
216+
makeMethodGroup :: InstanceBindTypeSig -> MethodGroup
217+
makeMethodGroup sig = (name, signature)
218+
where
219+
name = T.drop (T.length bindingPrefix) (printOutputable (bindName sig))
220+
signature = bindRendered sig
221+
222+
foo :: Range -> [InstanceBindTypeSig] -> Suggestion
223+
foo range sigs = ("all missing methods", methodGroups)
224+
where
225+
methodGroups = [ makeMethodGroup sig
226+
| sig <- sigs
227+
, inRange range (getSrcSpan $ bindName sig)
228+
]
229+
230+
214231
-- Return (name text, signature text)
215-
minDefToMethodGroups :: Range -> [InstanceBindTypeSig] -> BooleanFormula Name -> [[(T.Text, T.Text)]]
216-
minDefToMethodGroups range sigs = go
232+
minDefToMethodGroups :: Range -> [InstanceBindTypeSig] -> BooleanFormula Name -> [Suggestion]
233+
minDefToMethodGroups range sigs minDef = suggestions
217234
where
218-
go (Var mn) = [[ (T.pack . occNameString . occName $ mn, bindRendered sig)
235+
makeSuggestion methodGroup =
236+
let name = mconcat $ intersperse "," $ fst <$> methodGroup
237+
in (name, methodGroup)
238+
239+
suggestions = makeSuggestion <$> go minDef
240+
241+
go (Var mn) = [[ makeMethodGroup sig
219242
| sig <- sigs
220243
, inRange range (getSrcSpan $ bindName sig)
221244
, printOutputable mn == T.drop (T.length bindingPrefix) (printOutputable (bindName sig))
222245
]]
223246
go (Or ms) = concatMap (go . unLoc) ms
224247
go (And ms) = foldr (liftA2 (<>)) [[]] (fmap (go . unLoc) ms)
225248
go (Parens m) = go (unLoc m)
249+

0 commit comments

Comments
 (0)