Skip to content

Commit 2fece7f

Browse files
authored
Suggest open imports (#740)
Also fixes two bugs with qualified imports Fixes #480
1 parent 7b1de95 commit 2fece7f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Development/IDE/Plugin/CodeAction.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ suggestExportUnusedTopBinding srcOpt ParsedModule{pm_parsed_source = L _ HsModul
311311
$ hsmodDecls
312312
, Just pos <- _end . getLocatedRange <$> hsmodExports
313313
, Just needComma <- needsComma source <$> hsmodExports
314-
, let exportName = (if needComma then "," else "") <> printExport exportType name
314+
, let exportName = (if needComma then "," else "") <> printExport exportType name
315315
insertPos = pos {_character = pred $ _character pos}
316316
= [("Export ‘" <> name <> "", [TextEdit (Range insertPos insertPos) exportName])]
317317
| otherwise = []
@@ -833,19 +833,24 @@ suggestNewImport _ _ _ = []
833833
constructNewImportSuggestions
834834
:: PackageExportsMap -> NotInScope -> Maybe [T.Text] -> [T.Text]
835835
constructNewImportSuggestions exportsMap thingMissing notTheseModules = nubOrd
836-
[ renderNewImport identInfo m
836+
[ suggestion
837837
| (identInfo, m) <- fromMaybe [] $ Map.lookup name exportsMap
838838
, canUseIdent thingMissing identInfo
839839
, m `notElem` fromMaybe [] notTheseModules
840+
, suggestion <- renderNewImport identInfo m
840841
]
841842
where
842843
renderNewImport identInfo m
843-
| Just q <- qual = "import qualified " <> m <> " as " <> q
844-
| otherwise = "import " <> m <> " (" <> importWhat identInfo <> ")"
844+
| Just q <- qual
845+
, asQ <- if q == m then "" else " as " <> q
846+
= ["import qualified " <> m <> asQ]
847+
| otherwise
848+
= ["import " <> m <> " (" <> importWhat identInfo <> ")"
849+
,"import " <> m ]
845850

846851
(qual, name) = case T.splitOn "." (notInScope thingMissing) of
847852
[n] -> (Nothing, n)
848-
segments -> (Just (T.concat $ init segments), last segments)
853+
segments -> (Just (T.intercalate "." $ init segments), last segments)
849854
importWhat IdentInfo {parent, rendered}
850855
| Just p <- parent = p <> "(" <> rendered <> ")"
851856
| otherwise = rendered

test/exe/Main.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,17 +1038,22 @@ suggestImportTests = testGroup "suggest import actions"
10381038
[ test True [] "f = nonEmpty" [] "import Data.List.NonEmpty (nonEmpty)"
10391039
, test True [] "f = (:|)" [] "import Data.List.NonEmpty (NonEmpty((:|)))"
10401040
, test True [] "f :: Natural" ["f = undefined"] "import Numeric.Natural (Natural)"
1041+
, test True [] "f :: Natural" ["f = undefined"] "import Numeric.Natural"
10411042
, test True [] "f :: NonEmpty ()" ["f = () :| []"] "import Data.List.NonEmpty (NonEmpty)"
1043+
, test True [] "f :: NonEmpty ()" ["f = () :| []"] "import Data.List.NonEmpty"
10421044
, test True [] "f = First" [] "import Data.Monoid (First(First))"
10431045
, test True [] "f = Endo" [] "import Data.Monoid (Endo(Endo))"
10441046
, test True [] "f = Version" [] "import Data.Version (Version(Version))"
10451047
, test True [] "f ExitSuccess = ()" [] "import System.Exit (ExitCode(ExitSuccess))"
10461048
, test True [] "f = AssertionFailed" [] "import Control.Exception (AssertionFailed(AssertionFailed))"
10471049
, test True ["Prelude"] "f = nonEmpty" [] "import Data.List.NonEmpty (nonEmpty)"
10481050
, test True [] "f :: Alternative f => f ()" ["f = undefined"] "import Control.Applicative (Alternative)"
1051+
, test True [] "f :: Alternative f => f ()" ["f = undefined"] "import Control.Applicative"
10491052
, test True [] "f = empty" [] "import Control.Applicative (Alternative(empty))"
1053+
, test True [] "f = empty" [] "import Control.Applicative"
10501054
, test True [] "f = (&)" [] "import Data.Function ((&))"
10511055
, test True [] "f = NE.nonEmpty" [] "import qualified Data.List.NonEmpty as NE"
1056+
, test True [] "f = Data.List.NonEmpty.nonEmpty" [] "import qualified Data.List.NonEmpty"
10521057
, test True [] "f :: Typeable a => a" ["f = undefined"] "import Data.Typeable (Typeable)"
10531058
, test True [] "f = pack" [] "import Data.Text (pack)"
10541059
, test True [] "f :: Text" ["f = undefined"] "import Data.Text (Text)"

0 commit comments

Comments
 (0)