Skip to content

Commit

Permalink
Show window message when auto extending import lists (#1371)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
berberman and mergify[bot] committed Feb 16, 2021
1 parent 1b76c81 commit 3909291
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
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

0 comments on commit 3909291

Please sign in to comment.