Skip to content
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

Restore code actions order #1273

Merged
merged 3 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 17 additions & 28 deletions ghcide/src/Development/IDE/Plugin/CodeAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,59 +88,48 @@ codeAction lsp state _ (TextDocumentIdentifier uri) _range CodeActionContext{_di
df = ms_hspp_opts . pm_mod_summary <$> parsedModule
actions =
[ mkCA title [x] edit
| x <- xs, (title, tedit) <- suggestAction exportsMap ideOptions parsedModule text x
| x <- xs, (title, tedit) <- suggestAction exportsMap ideOptions parsedModule text df annotatedPS x
, let edit = WorkspaceEdit (Just $ Map.singleton uri $ List tedit) Nothing
]
actions' =
[mkCA title [x] edit
| x <- xs
, Just ps <- [annotatedPS]
, Just dynflags <- [df]
, (title, graft) <- suggestExactAction exportsMap dynflags ps x
, Right edit <-
[ -- either (Left . traceShow) Right $
rewriteToEdit dynflags uri (annsA ps) graft
]
]
actions'' = caRemoveRedundantImports parsedModule text diag xs uri
actions' = caRemoveRedundantImports parsedModule text diag xs uri
<> actions
<> actions'
<> caRemoveInvalidExports parsedModule text diag xs uri
pure $ Right $ List actions''
pure $ Right $ List actions'

mkCA :: T.Text -> [Diagnostic] -> WorkspaceEdit -> CAResult
mkCA title diags edit =
CACodeAction $ CodeAction title (Just CodeActionQuickFix) (Just $ List diags) (Just edit) Nothing

suggestExactAction ::
ExportsMap ->
DynFlags ->
Annotated ParsedSource ->
Diagnostic ->
[(T.Text, Rewrite)]
suggestExactAction exportsMap df ps x =
concat
[ suggestConstraint df (astA ps) x
, suggestImplicitParameter (astA ps) x
, suggestExtendImport exportsMap (astA ps) x
]
rewrite ::
Maybe DynFlags ->
Maybe (Annotated ParsedSource) ->
(DynFlags -> ParsedSource -> [(T.Text, Rewrite)]) ->
[(T.Text, [TextEdit])]
rewrite (Just df) (Just ps) f
| Right edit <- (traverse . traverse) (rewriteToEdit df (annsA ps)) (f df $ astA ps) = edit
rewrite _ _ _ = []

suggestAction
:: ExportsMap
-> IdeOptions
-> Maybe ParsedModule
-> Maybe T.Text
-> Maybe DynFlags
-> Maybe (Annotated ParsedSource)
-> Diagnostic
-> [(T.Text, [TextEdit])]
suggestAction packageExports ideOptions parsedModule text diag = concat
suggestAction packageExports ideOptions parsedModule text df annSource diag = concat
-- Order these suggestions by priority
[ suggestSignature True diag
, rewrite df annSource $ \_ ps -> suggestExtendImport packageExports ps diag
, suggestFillTypeWildcard diag
, suggestFixConstructorImport text diag
, suggestModuleTypo diag
, suggestReplaceIdentifier text diag
, removeRedundantConstraints text diag
, suggestAddTypeAnnotationToSatisfyContraints text diag
, rewrite df annSource $ \df ps -> suggestConstraint df ps diag
, rewrite df annSource $ \_ ps -> suggestImplicitParameter ps diag
] ++ concat
[ suggestNewDefinition ideOptions pm text diag
++ suggestNewImport packageExports pm diag
Expand Down
16 changes: 4 additions & 12 deletions ghcide/src/Development/IDE/Plugin/CodeAction/ExactPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import Control.Monad
import Control.Monad.Trans
import Data.Data (Data)
import Data.Functor
import qualified Data.HashMap.Strict as HMap
import qualified Data.Map.Strict as Map
import Data.Maybe (fromJust)
import qualified Data.Text as T
Expand Down Expand Up @@ -50,22 +49,15 @@ data Rewrite where
-- | Convert a 'Rewrite' into a 'WorkspaceEdit'.
rewriteToEdit ::
DynFlags ->
Uri ->
Anns ->
Rewrite ->
Either String WorkspaceEdit
rewriteToEdit dflags uri anns (Rewrite dst f) = do
Either String [TextEdit]
rewriteToEdit dflags anns (Rewrite dst f) = do
(ast, (anns, _), _) <- runTransformT anns $ f dflags
pepeiborra marked this conversation as resolved.
Show resolved Hide resolved
let editMap =
HMap.fromList
[ ( uri,
List
[ TextEdit (fromJust $ srcSpanToRange dst) $
let editMap = [ TextEdit (fromJust $ srcSpanToRange dst) $
T.pack $ tail $ exactPrint ast anns
pepeiborra marked this conversation as resolved.
Show resolved Hide resolved
]
)
]
pure $ WorkspaceEdit (Just editMap) Nothing
pure editMap

srcSpanToRange :: SrcSpan -> Maybe Range
srcSpanToRange (UnhelpfulSpan _) = Nothing
Expand Down