@@ -52,6 +52,11 @@ import Development.IDE.GHC.Compat hiding ((<+>))
5252import Development.IDE.GHC.Compat.Util (mkFastString )
5353import Development.IDE.Graph.Classes
5454import GHC.Generics (Generic )
55+ import GHC.Num (integerFromInt )
56+ import GHC.Parser.Annotation (EpAnn (entry ),
57+ HasLoc (getHasLoc ),
58+ realSrcSpan )
59+ import GHC.Types.PkgQual (RawPkgQual (NoRawPkgQual ))
5560import Ide.Plugin.Error (PluginError (.. ),
5661 getNormalizedFilePathE ,
5762 handleMaybe )
@@ -250,6 +255,23 @@ importPackageInlayHintProvider _ state _ InlayHintParams {_textDocument = TextDo
250255 ast <- handleMaybe
251256 (PluginRuleFailed " GetHieAst" )
252257 (getAsts hieAst Map. !? (HiePath . mkFastString . fromNormalizedFilePath) nfp)
258+ parsedModule <- runActionE " GADT.GetParsedModuleWithComments" state $ useE GetParsedModule nfp
259+ let (L _ hsImports) = hsmodImports <$> pm_parsed_source parsedModule
260+
261+ let isPackageImport :: ImportDecl GhcPs -> Bool
262+ isPackageImport ImportDecl {ideclPkgQual = NoRawPkgQual } = False
263+ isPackageImport _ = True
264+
265+ annotationToLineNumber :: EpAnn a -> Integer
266+ annotationToLineNumber = integerFromInt . srcSpanEndLine . realSrcSpan . getHasLoc . entry
267+
268+ packageImportLineNumbers :: S. Set Integer
269+ packageImportLineNumbers =
270+ S. fromList $
271+ hsImports
272+ & filter (\ (L _ importDecl) -> isPackageImport importDecl)
273+ & map (\ (L annotation _) -> annotationToLineNumber annotation)
274+
253275 hintsInfo <- liftIO $ getAllImportedPackagesHints (hscEnv hscEnvEq) (moduleName hieModule) ast
254276 -- Sort the hints by position and group them by line
255277 -- Show only first hint in each line
@@ -258,6 +280,8 @@ importPackageInlayHintProvider _ state _ InlayHintParams {_textDocument = TextDo
258280 compare l1 l2 <> compare c1 c2)
259281 & groupBy (\ (Range (Position l1 _) _, _) (Range (Position l2 _) _, _) -> l1 == l2)
260282 & map Data.List.NonEmpty. head
283+ -- adding 1 because RealSrcLoc begins with 1
284+ & filter (\ (Range (Position l _) _, _) -> S. notMember (toInteger l + 1 ) packageImportLineNumbers)
261285 let inlayHints = [ generateInlayHint newRange txt
262286 | (range, txt) <- selectedHintsInfo
263287 , Just newRange <- [toCurrentRange pmap range]
0 commit comments