Skip to content

Show window message when auto extending import lists #1371

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

Merged
merged 3 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 18 additions & 4 deletions ghcide/src/Development/IDE/Plugin/Completions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down
1 change: 1 addition & 0 deletions ghcide/test/exe/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down