From 96000cfcccee0d702d1a32191db617b7afc9888d Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Mon, 15 Feb 2021 15:30:11 +0800 Subject: [PATCH 1/2] Show window message when auto extending import lists --- ghcide/src/Development/IDE/Plugin/Completions.hs | 7 +++++-- ghcide/test/exe/Main.hs | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/Completions.hs b/ghcide/src/Development/IDE/Plugin/Completions.hs index 09d3012b98..e4efc171c8 100644 --- a/ghcide/src/Development/IDE/Plugin/Completions.hs +++ b/ghcide/src/Development/IDE/Plugin/Completions.hs @@ -159,9 +159,12 @@ 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 $ \wedit -> do + LSP.sendNotification SWindowShowMessage $ + ShowMessageParams MtInfo $ + "Import " <> maybe ("‘" <> newThing) (\x -> "‘" <> x <> " (" <> newThing <> ")") thingParent <> "’ from " <> importName void $ LSP.sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing wedit) (\_ -> pure ()) return $ Right Null diff --git a/ghcide/test/exe/Main.hs b/ghcide/test/exe/Main.hs index a3c5e0f523..27ae5f7041 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 From a9da95650131c4cc97cfc7e2c55866f3e5c163ca Mon Sep 17 00:00:00 2001 From: Potato Hatsue <1793913507@qq.com> Date: Tue, 16 Feb 2021 16:24:16 +0800 Subject: [PATCH 2/2] Include range info in the message (using ghc style) --- .../src/Development/IDE/Plugin/Completions.hs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ghcide/src/Development/IDE/Plugin/Completions.hs b/ghcide/src/Development/IDE/Plugin/Completions.hs index e4efc171c8..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 @@ -161,14 +164,22 @@ extendImportCommand = extendImportHandler :: CommandFunction IdeState ExtendImport extendImportHandler ideState edit@ExtendImport {..} = do res <- liftIO $ runMaybeT $ extendImportHandler' ideState edit - whenJust res $ \wedit -> do + 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 + "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 = @@ -184,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 =