-
-
Notifications
You must be signed in to change notification settings - Fork 408
Package ghcide code actions #1512
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
Conversation
|
There are many ways to skin this cat, I just don't know if this is the best one. Maybe it would help if you updated the PR description with an overview of the goals and rationale behind this change (and move the other remarks about |
|
I haven't had time to review this yet, but I think the new description is awesome |
|
Now that I understand what is going on, I think this is very cool. Nice Job! |
| data CodeActionArgs = CodeActionArgs | ||
| { caaExportsMap :: ExportsMap | ||
| , caaIdeOptions :: IdeOptions | ||
| , caaParsedModule :: Maybe ParsedModule | ||
| , caaContents :: Maybe T.Text | ||
| , caaDf :: Maybe DynFlags | ||
| , caaAnnSource :: Maybe (Annotated ParsedSource) | ||
| , caaTmr :: Maybe TcModuleResult | ||
| , caaHar :: Maybe HieAstResult | ||
| , caaBindings :: Maybe Bindings | ||
| , caaGblSigs :: Maybe GlobalBindingTypeSigsResult | ||
| , caaDiagnostics :: Diagnostic | ||
| { caaExportsMap :: ExportsMap, | ||
| caaIdeOptions :: IdeOptions, | ||
| caaParsedModule :: Maybe ParsedModule, | ||
| caaContents :: Maybe T.Text, | ||
| caaDf :: Maybe DynFlags, | ||
| caaAnnSource :: Maybe (Annotated ParsedSource), | ||
| caaTmr :: Maybe TcModuleResult, | ||
| caaHar :: Maybe HieAstResult, | ||
| caaBindings :: Maybe Bindings, | ||
| caaGblSigs :: Maybe GlobalBindingTypeSigsResult, | ||
| caaDiagnostics :: Diagnostic | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I run ormolu first and then used stylish-haskell, but the later one didn't revert the changes done by the former one... Is that what we expect? @Ailrun
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes in this case, as record formatting is disabled by default. Maybe it's better to enable it though.
| -- | A compact representation of 'Language.LSP.Types.CodeAction's | ||
| type GhcideCodeActions = [(T.Text, Maybe CodeActionKind, Maybe Bool, [TextEdit])] | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the Bool mean? A type synonym would go a long way:
type Meaning = Bool
type GhcideCodeActions = [ ..Meaning, ..]
Motivation
Previously, functions implement ghcide code actions were required to return
[(T.Text, [TextEdit])], and were wired together insuggestAction. In plugin handler, we used many shake rules and retrieved values used in concrete implementation of code actions, then calledsuggestActionhere, passing those values. However, with more and more code actions implemented by contributors, nowsuggestActionalready has 11 parameters, which we need name each of two times and pass all of by hand. So I think it's time to define a record type to gather them together. On the other hand, we introduced another potential return typeRewriteto represent the result of the code action and then usedrewriteto convert it toTextEdit, packaging it insuggestAction. With this abstraction, it's hard to express some cases that the code action returns eitherTextEditorRewrite, which depends on the runtime (likesuggestImportDisambiguationandsuggestNewOrExtendImportForClassMethod). Thus we could use type class to relax the return type of functions.Overview
First of all, we use a new record type
CodeActionArgsto grab them up:Then we have a type class
ToCodeAction:where
acan be converted into[(T.Text, [TextEdit])](representation of code actions we use insuggestAction) under the context ofCodeActionArgs. And we can define some instances of acceptable return types:For each field
fldofCodeActionArgs, we can make an instanceinstance ToCodeAction r => ToCodeAction (fld -> r)(fields ofCodeActionArgsdon't have overlapped types). In such instances, we takefldfromCodeActionArgsthen feed it into(fld -> r)to getr. IffldisMaybe a, we makeToCodeAction (Maybe a -> r)andToCodeAction (a -> r). TakecaaDfas an example:Once we have those instances, suggestX (functions providing code actions) could be free to use values from
CodeActionArgsimplicitly, withMaybeor withoutMaybe. And we keep backward compatibility -- this PR doesn't change any these kind of functions. Moreover, they could use[TextEdit |? Rewrite]as the return type, which addresses the above issue.