Skip to content

Commit 2792fcc

Browse files
authored
Extract the qualified name from already imported module (#1445)
* Extract the qualified name from already imported module * Minor format
1 parent 9f8a172 commit 2792fcc

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,11 @@ suggestNewImport packageExportsMap ParsedModule {pm_parsed_source = L _ HsModule
11951195
| msg <- unifySpaces _message
11961196
, Just thingMissing <- extractNotInScopeName msg
11971197
, qual <- extractQualifiedModuleName msg
1198+
, qual' <-
1199+
extractDoesNotExportModuleName msg
1200+
>>= (findImportDeclByModuleName hsmodImports . T.unpack)
1201+
>>= ideclAs . unLoc
1202+
<&> T.pack . moduleNameString . unLoc
11981203
, Just insertLine <- case hsmodImports of
11991204
[] -> case srcSpanStart $ getLoc (head hsmodDecls) of
12001205
RealSrcLoc s -> Just $ srcLocLine s - 1
@@ -1206,7 +1211,7 @@ suggestNewImport packageExportsMap ParsedModule {pm_parsed_source = L _ HsModule
12061211
, extendImportSuggestions <- matchRegexUnifySpaces msg
12071212
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
12081213
= [(imp, [TextEdit (Range insertPos insertPos) (imp <> "\n")])
1209-
| imp <- sort $ constructNewImportSuggestions packageExportsMap (qual, thingMissing) extendImportSuggestions
1214+
| imp <- sort $ constructNewImportSuggestions packageExportsMap (qual <|> qual', thingMissing) extendImportSuggestions
12101215
]
12111216
suggestNewImport _ _ _ = []
12121217

@@ -1272,6 +1277,37 @@ extractQualifiedModuleName x
12721277
| otherwise
12731278
= Nothing
12741279

1280+
-- | If a module has been imported qualified, and we want to ues the same qualifier for other modules
1281+
-- which haven't been imported, 'extractQualifiedModuleName' won't work. Thus we need extract the qualifier
1282+
-- from the imported one.
1283+
--
1284+
-- For example, we write f = T.putStrLn, where putStrLn comes from Data.Text.IO, with the following import(s):
1285+
-- 1.
1286+
-- import qualified Data.Text as T
1287+
--
1288+
-- Module ‘Data.Text’ does not export ‘putStrLn’.
1289+
--
1290+
-- 2.
1291+
-- import qualified Data.Text as T
1292+
-- import qualified Data.Functor as T
1293+
--
1294+
-- Neither ‘Data.Functor’ nor ‘Data.Text’ exports ‘putStrLn’.
1295+
--
1296+
-- 3.
1297+
-- import qualified Data.Text as T
1298+
-- import qualified Data.Functor as T
1299+
-- import qualified Data.Function as T
1300+
--
1301+
-- Neither ‘Data.Function’,
1302+
-- ‘Data.Functor’ nor ‘Data.Text’ exports ‘putStrLn’.
1303+
extractDoesNotExportModuleName :: T.Text -> Maybe T.Text
1304+
extractDoesNotExportModuleName x
1305+
| Just [m] <-
1306+
matchRegexUnifySpaces x "Module ‘([^’]*)’ does not export"
1307+
<|> matchRegexUnifySpaces x "nor ‘([^’]*)’ exports"
1308+
= Just m
1309+
| otherwise
1310+
= Nothing
12751311
-------------------------------------------------------------------------------------------------
12761312

12771313

ghcide/test/exe/Main.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,19 @@ suggestImportTests = testGroup "suggest import actions"
15591559
, test True [] "f = (&) [] id" [] "import Data.Function ((&))"
15601560
, test True [] "f = (.|.)" [] "import Data.Bits (Bits((.|.)))"
15611561
, test True [] "f = (.|.)" [] "import Data.Bits ((.|.))"
1562+
, test True
1563+
["qualified Data.Text as T"
1564+
] "f = T.putStrLn" [] "import qualified Data.Text.IO as T"
1565+
, test True
1566+
[ "qualified Data.Text as T"
1567+
, "qualified Data.Function as T"
1568+
] "f = T.putStrLn" [] "import qualified Data.Text.IO as T"
1569+
, test True
1570+
[ "qualified Data.Text as T"
1571+
, "qualified Data.Function as T"
1572+
, "qualified Data.Functor as T"
1573+
, "qualified Data.Data as T"
1574+
] "f = T.putStrLn" [] "import qualified Data.Text.IO as T"
15621575
]
15631576
]
15641577
where

0 commit comments

Comments
 (0)