Skip to content

Commit 0d56127

Browse files
committed
Hide inlay hint when using package import
1 parent 621f2d6 commit 0d56127

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

plugins/hls-explicit-imports-plugin/src/Ide/Plugin/ExplicitImports.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ import Development.IDE.GHC.Compat hiding ((<+>))
5252
import Development.IDE.GHC.Compat.Util (mkFastString)
5353
import Development.IDE.Graph.Classes
5454
import 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))
5560
import 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

Comments
 (0)