Skip to content

Commit a30e55f

Browse files
Use qualified module name from diagnostics in suggestNewImport (haskell/ghcide#945)
* Use qualified module name from diagnostics in suggestNewImport * Update tests * Add newline * Use qualified module name from diagnostics in suggestNewImport * Update tests * Add newline * Remove unused renderImport Co-authored-by: Pepe Iborra <[email protected]>
1 parent fcbf640 commit a30e55f

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,8 @@ suggestExtendImport exportsMap contents Diagnostic{_range=_range,..}
622622
importLine <- textInRange range c,
623623
Just ident <- lookupExportMap binding mod,
624624
Just result <- addBindingToImportList ident importLine
625-
= [("Add " <> renderImport ident <> " to the import list of " <> mod, [TextEdit range result])]
625+
= [("Add " <> renderIdentInfo ident <> " to the import list of " <> mod, [TextEdit range result])]
626626
| otherwise = []
627-
renderImport IdentInfo {parent, rendered}
628-
| Just p <- parent = p <> "(" <> rendered <> ")"
629-
| otherwise = rendered
630627
lookupExportMap binding mod
631628
| Just match <- Map.lookup binding (getExportsMap exportsMap)
632629
, [(ident, _)] <- filter (\(_,m) -> mod == m) (Set.toList match)
@@ -899,7 +896,8 @@ removeRedundantConstraints mContents Diagnostic{..}
899896
suggestNewImport :: ExportsMap -> ParsedModule -> Diagnostic -> [(T.Text, [TextEdit])]
900897
suggestNewImport packageExportsMap ParsedModule {pm_parsed_source = L _ HsModule {..}} Diagnostic{_message}
901898
| msg <- unifySpaces _message
902-
, Just name <- extractNotInScopeName msg
899+
, Just thingMissing <- extractNotInScopeName msg
900+
, qual <- extractQualifiedModuleName msg
903901
, Just insertLine <- case hsmodImports of
904902
[] -> case srcSpanStart $ getLoc (head hsmodDecls) of
905903
RealSrcLoc s -> Just $ srcLocLine s - 1
@@ -911,15 +909,16 @@ suggestNewImport packageExportsMap ParsedModule {pm_parsed_source = L _ HsModule
911909
, extendImportSuggestions <- matchRegexUnifySpaces msg
912910
"Perhaps you want to add ‘[^’]*’ to the import list in the import of ‘([^’]*)’"
913911
= [(imp, [TextEdit (Range insertPos insertPos) (imp <> "\n")])
914-
| imp <- sort $ constructNewImportSuggestions packageExportsMap name extendImportSuggestions
912+
| imp <- sort $ constructNewImportSuggestions packageExportsMap (qual, thingMissing) extendImportSuggestions
915913
]
916914
suggestNewImport _ _ _ = []
917915

918916
constructNewImportSuggestions
919-
:: ExportsMap -> NotInScope -> Maybe [T.Text] -> [T.Text]
920-
constructNewImportSuggestions exportsMap thingMissing notTheseModules = nubOrd
917+
:: ExportsMap -> (Maybe T.Text, NotInScope) -> Maybe [T.Text] -> [T.Text]
918+
constructNewImportSuggestions exportsMap (qual, thingMissing) notTheseModules = nubOrd
921919
[ suggestion
922-
| (identInfo, m) <- maybe [] Set.toList $ Map.lookup name (getExportsMap exportsMap)
920+
| Just name <- [T.stripPrefix (maybe "" (<> ".") qual) $ notInScope thingMissing]
921+
, (identInfo, m) <- maybe [] Set.toList $ Map.lookup name (getExportsMap exportsMap)
923922
, canUseIdent thingMissing identInfo
924923
, m `notElem` fromMaybe [] notTheseModules
925924
, suggestion <- renderNewImport identInfo m
@@ -930,16 +929,9 @@ constructNewImportSuggestions exportsMap thingMissing notTheseModules = nubOrd
930929
, asQ <- if q == m then "" else " as " <> q
931930
= ["import qualified " <> m <> asQ]
932931
| otherwise
933-
= ["import " <> m <> " (" <> importWhat identInfo <> ")"
932+
= ["import " <> m <> " (" <> renderIdentInfo identInfo <> ")"
934933
,"import " <> m ]
935934

936-
(qual, name) = case T.splitOn "." (notInScope thingMissing) of
937-
[n] -> (Nothing, n)
938-
segments -> (Just (T.intercalate "." $ init segments), last segments)
939-
importWhat IdentInfo {parent, rendered}
940-
| Just p <- parent = p <> "(" <> rendered <> ")"
941-
| otherwise = rendered
942-
943935
canUseIdent :: NotInScope -> IdentInfo -> Bool
944936
canUseIdent NotInScopeDataConstructor{} = isDatacon
945937
canUseIdent _ = const True
@@ -972,6 +964,13 @@ extractNotInScopeName x
972964
| otherwise
973965
= Nothing
974966

967+
extractQualifiedModuleName :: T.Text -> Maybe T.Text
968+
extractQualifiedModuleName x
969+
| Just [m] <- matchRegexUnifySpaces x "module named [^‘]*‘([^’]*)’"
970+
= Just m
971+
| otherwise
972+
= Nothing
973+
975974
-------------------------------------------------------------------------------------------------
976975

977976

@@ -1171,3 +1170,8 @@ matchRegExMultipleImports message = do
11711170
_ -> Nothing
11721171
imps <- regExImports imports
11731172
return (binding, imps)
1173+
1174+
renderIdentInfo :: IdentInfo -> T.Text
1175+
renderIdentInfo IdentInfo {parent, rendered}
1176+
| Just p <- parent = p <> "(" <> rendered <> ")"
1177+
| otherwise = rendered

ghcide/test/exe/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ suggestImportTests = testGroup "suggest import actions"
12201220
, test True [] "f :: Text" ["f = undefined"] "import Data.Text (Text)"
12211221
, test True [] "f = [] & id" [] "import Data.Function ((&))"
12221222
, test True [] "f = (&) [] id" [] "import Data.Function ((&))"
1223+
, test True [] "f = (.|.)" [] "import Data.Bits (Bits((.|.)))"
12231224
]
12241225
]
12251226
where

0 commit comments

Comments
 (0)