Suggest open imports (#740)

Also fixes two bugs with qualified imports

Fixes #480
This commit is contained in:
Pepe Iborra 2020-09-02 19:41:41 +01:00 committed by Pepe Iborra
parent 77136d027d
commit eade7a1dc8
2 changed files with 15 additions and 5 deletions

View File

@ -224,7 +224,7 @@ suggestExportUnusedTopBinding srcOpt ParsedModule{pm_parsed_source = L _ HsModul
$ hsmodDecls
, Just pos <- fmap _end . getLocatedRange =<< hsmodExports
, Just needComma <- needsComma source <$> hsmodExports
, let exportName = (if needComma then "," else "") <> printExport exportType name
, let exportName = (if needComma then "," else "") <> printExport exportType name
insertPos = pos {_character = pred $ _character pos}
= [("Export " <> name <> "", [TextEdit (Range insertPos insertPos) exportName])]
| otherwise = []
@ -749,19 +749,24 @@ suggestNewImport _ _ _ = []
constructNewImportSuggestions
:: PackageExportsMap -> NotInScope -> Maybe [T.Text] -> [T.Text]
constructNewImportSuggestions exportsMap thingMissing notTheseModules = nubOrd
[ renderNewImport identInfo m
[ suggestion
| (identInfo, m) <- fromMaybe [] $ Map.lookup name exportsMap
, canUseIdent thingMissing identInfo
, m `notElem` fromMaybe [] notTheseModules
, suggestion <- renderNewImport identInfo m
]
where
renderNewImport identInfo m
| Just q <- qual = "import qualified " <> m <> " as " <> q
| otherwise = "import " <> m <> " (" <> importWhat identInfo <> ")"
| Just q <- qual
, asQ <- if q == m then "" else " as " <> q
= ["import qualified " <> m <> asQ]
| otherwise
= ["import " <> m <> " (" <> importWhat identInfo <> ")"
,"import " <> m ]
(qual, name) = case T.splitOn "." (notInScope thingMissing) of
[n] -> (Nothing, n)
segments -> (Just (T.concat $ init segments), last segments)
segments -> (Just (T.intercalate "." $ init segments), last segments)
importWhat IdentInfo {parent, rendered}
| Just p <- parent = p <> "(" <> rendered <> ")"
| otherwise = rendered

View File

@ -1037,7 +1037,9 @@ suggestImportTests = testGroup "suggest import actions"
[ test True [] "f = nonEmpty" [] "import Data.List.NonEmpty (nonEmpty)"
, test True [] "f = (:|)" [] "import Data.List.NonEmpty (NonEmpty((:|)))"
, test True [] "f :: Natural" ["f = undefined"] "import Numeric.Natural (Natural)"
, test True [] "f :: Natural" ["f = undefined"] "import Numeric.Natural"
, test True [] "f :: NonEmpty ()" ["f = () :| []"] "import Data.List.NonEmpty (NonEmpty)"
, test True [] "f :: NonEmpty ()" ["f = () :| []"] "import Data.List.NonEmpty"
, test True [] "f = First" [] "import Data.Monoid (First(First))"
, test True [] "f = Endo" [] "import Data.Monoid (Endo(Endo))"
, test True [] "f = Version" [] "import Data.Version (Version(Version))"
@ -1045,9 +1047,12 @@ suggestImportTests = testGroup "suggest import actions"
, test True [] "f = AssertionFailed" [] "import Control.Exception (AssertionFailed(AssertionFailed))"
, test True ["Prelude"] "f = nonEmpty" [] "import Data.List.NonEmpty (nonEmpty)"
, test True [] "f :: Alternative f => f ()" ["f = undefined"] "import Control.Applicative (Alternative)"
, test True [] "f :: Alternative f => f ()" ["f = undefined"] "import Control.Applicative"
, test True [] "f = empty" [] "import Control.Applicative (Alternative(empty))"
, test True [] "f = empty" [] "import Control.Applicative"
, test True [] "f = (&)" [] "import Data.Function ((&))"
, test True [] "f = NE.nonEmpty" [] "import qualified Data.List.NonEmpty as NE"
, test True [] "f = Data.List.NonEmpty.nonEmpty" [] "import qualified Data.List.NonEmpty"
, test True [] "f :: Typeable a => a" ["f = undefined"] "import Data.Typeable (Typeable)"
, test True [] "f = pack" [] "import Data.Text (pack)"
, test True [] "f :: Text" ["f = undefined"] "import Data.Text (Text)"