diff --git a/ghcide/src/Development/IDE/Plugin/Completions.hs b/ghcide/src/Development/IDE/Plugin/Completions.hs index 09d3012b98..e23f1aa59d 100644 --- a/ghcide/src/Development/IDE/Plugin/Completions.hs +++ b/ghcide/src/Development/IDE/Plugin/Completions.hs @@ -38,6 +38,9 @@ import Ide.PluginUtils (getClientConfig) import Ide.Types import TcRnDriver (tcRnImportDecls) import Control.Concurrent.Async (concurrently) +import GHC.Exts (toList) +import Development.IDE.GHC.Error (rangeToSrcSpan) +import Development.IDE.GHC.Util (prettyPrint) #if defined(GHC_LIB) import Development.IDE.Import.DependencyInformation #endif @@ -159,13 +162,24 @@ extendImportCommand = PluginCommand (CommandId extendImportCommandId) "additional edits for a completion" extendImportHandler extendImportHandler :: CommandFunction IdeState ExtendImport -extendImportHandler ideState edit = do +extendImportHandler ideState edit@ExtendImport {..} = do res <- liftIO $ runMaybeT $ extendImportHandler' ideState edit - whenJust res $ \wedit -> + whenJust res $ \(nfp, wedit@WorkspaceEdit {_changes}) -> do + let (_, List (head -> TextEdit {_range})) = fromJust $ _changes >>= listToMaybe . toList + srcSpan = rangeToSrcSpan nfp _range + LSP.sendNotification SWindowShowMessage $ + ShowMessageParams MtInfo $ + "Import " + <> maybe ("‘" <> newThing) (\x -> "‘" <> x <> " (" <> newThing <> ")") thingParent + <> "’ from " + <> importName + <> " (at " + <> T.pack (prettyPrint srcSpan) + <> ")" void $ LSP.sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing wedit) (\_ -> pure ()) return $ Right Null -extendImportHandler' :: IdeState -> ExtendImport -> MaybeT IO WorkspaceEdit +extendImportHandler' :: IdeState -> ExtendImport -> MaybeT IO (NormalizedFilePath, WorkspaceEdit) extendImportHandler' ideState ExtendImport {..} | Just fp <- uriToFilePath doc, nfp <- toNormalizedFilePath' fp = @@ -181,7 +195,7 @@ extendImportHandler' ideState ExtendImport {..} wantedModule = mkModuleName (T.unpack importName) wantedQual = mkModuleName . T.unpack <$> importQual imp <- liftMaybe $ find (isWantedModule wantedModule wantedQual) imps - liftEither $ + fmap (nfp,) $ liftEither $ rewriteToWEdit df doc (annsA ps) $ extendImport (T.unpack <$> thingParent) (T.unpack newThing) imp | otherwise = diff --git a/ghcide/test/exe/Main.hs b/ghcide/test/exe/Main.hs index 831acf2e93..0a7465e519 100644 --- a/ghcide/test/exe/Main.hs +++ b/ghcide/test/exe/Main.hs @@ -3648,6 +3648,7 @@ completionCommandTest name src pos wanted expected = testSession name $ do executeCommand c if src /= expected then do + void $ skipManyTill anyMessage loggingNotification modifiedCode <- skipManyTill anyMessage (getDocumentEdit docId) liftIO $ modifiedCode @?= T.unlines expected else do