Skip to content

Commit 3909291

Browse files
Show window message when auto extending import lists (#1371)
* Show window message when auto extending import lists * Include range info in the message (using ghc style) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 1b76c81 commit 3909291

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

ghcide/src/Development/IDE/Plugin/Completions.hs

+18-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ import Ide.PluginUtils (getClientConfig)
3838
import Ide.Types
3939
import TcRnDriver (tcRnImportDecls)
4040
import Control.Concurrent.Async (concurrently)
41+
import GHC.Exts (toList)
42+
import Development.IDE.GHC.Error (rangeToSrcSpan)
43+
import Development.IDE.GHC.Util (prettyPrint)
4144
#if defined(GHC_LIB)
4245
import Development.IDE.Import.DependencyInformation
4346
#endif
@@ -159,13 +162,24 @@ extendImportCommand =
159162
PluginCommand (CommandId extendImportCommandId) "additional edits for a completion" extendImportHandler
160163

161164
extendImportHandler :: CommandFunction IdeState ExtendImport
162-
extendImportHandler ideState edit = do
165+
extendImportHandler ideState edit@ExtendImport {..} = do
163166
res <- liftIO $ runMaybeT $ extendImportHandler' ideState edit
164-
whenJust res $ \wedit ->
167+
whenJust res $ \(nfp, wedit@WorkspaceEdit {_changes}) -> do
168+
let (_, List (head -> TextEdit {_range})) = fromJust $ _changes >>= listToMaybe . toList
169+
srcSpan = rangeToSrcSpan nfp _range
170+
LSP.sendNotification SWindowShowMessage $
171+
ShowMessageParams MtInfo $
172+
"Import "
173+
<> maybe ("" <> newThing) (\x -> "" <> x <> " (" <> newThing <> ")") thingParent
174+
<> "’ from "
175+
<> importName
176+
<> " (at "
177+
<> T.pack (prettyPrint srcSpan)
178+
<> ")"
165179
void $ LSP.sendRequest SWorkspaceApplyEdit (ApplyWorkspaceEditParams Nothing wedit) (\_ -> pure ())
166180
return $ Right Null
167181

168-
extendImportHandler' :: IdeState -> ExtendImport -> MaybeT IO WorkspaceEdit
182+
extendImportHandler' :: IdeState -> ExtendImport -> MaybeT IO (NormalizedFilePath, WorkspaceEdit)
169183
extendImportHandler' ideState ExtendImport {..}
170184
| Just fp <- uriToFilePath doc,
171185
nfp <- toNormalizedFilePath' fp =
@@ -181,7 +195,7 @@ extendImportHandler' ideState ExtendImport {..}
181195
wantedModule = mkModuleName (T.unpack importName)
182196
wantedQual = mkModuleName . T.unpack <$> importQual
183197
imp <- liftMaybe $ find (isWantedModule wantedModule wantedQual) imps
184-
liftEither $
198+
fmap (nfp,) $ liftEither $
185199
rewriteToWEdit df doc (annsA ps) $
186200
extendImport (T.unpack <$> thingParent) (T.unpack newThing) imp
187201
| otherwise =

ghcide/test/exe/Main.hs

+1
Original file line numberDiff line numberDiff line change
@@ -3648,6 +3648,7 @@ completionCommandTest name src pos wanted expected = testSession name $ do
36483648
executeCommand c
36493649
if src /= expected
36503650
then do
3651+
void $ skipManyTill anyMessage loggingNotification
36513652
modifiedCode <- skipManyTill anyMessage (getDocumentEdit docId)
36523653
liftIO $ modifiedCode @?= T.unlines expected
36533654
else do

0 commit comments

Comments
 (0)