Skip to content

Commit 174136a

Browse files
drone29awz1000
authored andcommitted
Filter generated Core variable names from hovertip documentation (#3316)
* Filter names from hovertip documentation which match patterns of generated Core variables. * Added comment for isInternal function. * Relocated list of OccName prefixes from the Completions plugin to the CoreFile module. Updated the check for GHC-generated OccName prefixes in AtPoint to use the shared list of OccName prefixes. * Added test data and test for ensuring Core-generated variables are not included in hover info. * Switched to using isEvidenceContext to filter out evidence bindings. * Relocated list of OccName prefixes from the Completions plugin to the CoreFile module. Updated the check for GHC-generated OccName prefixes in AtPoint to use the shared list of OccName prefixes. * Removed unused import. * Relocated list of OccName prefixes from the Completions plugin to the CoreFile module. Updated the check for GHC-generated OccName prefixes in AtPoint to use the shared list of OccName prefixes. * Removed unused import. * Fixed incorrect definition of mkRecordSnippetCompItem introduced when rebasing. * Add preprocessor check for GHC version before using isEvidenceContext. Updated related comment.
1 parent b0a53fb commit 174136a

File tree

5 files changed

+71
-47
lines changed

5 files changed

+71
-47
lines changed

ghcide/src/Development/IDE/GHC/CoreFile.hs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ module Development.IDE.GHC.CoreFile
1010
, typecheckCoreFile
1111
, readBinCoreFile
1212
, writeBinCoreFile
13-
, getImplicitBinds) where
13+
, getImplicitBinds
14+
, occNamePrefixes) where
1415

1516
import Control.Monad
1617
import Control.Monad.IO.Class
1718
import Data.Foldable
1819
import Data.IORef
1920
import Data.List (isPrefixOf)
2021
import Data.Maybe
22+
import qualified Data.Text as T
2123
import GHC.Fingerprint
2224

2325
import Development.IDE.GHC.Compat
@@ -228,3 +230,45 @@ tc_iface_bindings (TopIfaceNonRec v e) = do
228230
tc_iface_bindings (TopIfaceRec vs) = do
229231
vs' <- traverse (\(v, e) -> (,) <$> pure v <*> tcIfaceExpr e) vs
230232
pure $ Rec vs'
233+
234+
-- | Prefixes that can occur in a GHC OccName
235+
occNamePrefixes :: [T.Text]
236+
occNamePrefixes =
237+
[
238+
-- long ones
239+
"$con2tag_"
240+
, "$tag2con_"
241+
, "$maxtag_"
242+
243+
-- four chars
244+
, "$sel:"
245+
, "$tc'"
246+
247+
-- three chars
248+
, "$dm"
249+
, "$co"
250+
, "$tc"
251+
, "$cp"
252+
, "$fx"
253+
254+
-- two chars
255+
, "$W"
256+
, "$w"
257+
, "$m"
258+
, "$b"
259+
, "$c"
260+
, "$d"
261+
, "$i"
262+
, "$s"
263+
, "$f"
264+
, "$r"
265+
, "C:"
266+
, "N:"
267+
, "D:"
268+
, "$p"
269+
, "$L"
270+
, "$f"
271+
, "$t"
272+
, "$c"
273+
, "$m"
274+
]

ghcide/src/Development/IDE/Plugin/Completions/Logic.hs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Development.IDE.Core.PositionMapping
4141
import Development.IDE.GHC.Compat hiding (ppr)
4242
import qualified Development.IDE.GHC.Compat as GHC
4343
import Development.IDE.GHC.Compat.Util
44+
import Development.IDE.GHC.CoreFile (occNamePrefixes)
4445
import Development.IDE.GHC.Error
4546
import Development.IDE.GHC.Util
4647
import Development.IDE.Plugin.Completions.Types
@@ -767,50 +768,7 @@ openingBacktick line prefixModule prefixText Position { _character=(fromIntegral
767768
-- TODO: Turn this into an alex lexer that discards prefixes as if they were whitespace.
768769
stripPrefix :: T.Text -> T.Text
769770
stripPrefix name = T.takeWhile (/=':') $ fromMaybe name $
770-
getFirst $ foldMap (First . (`T.stripPrefix` name)) prefixes
771-
772-
-- | Prefixes that can occur in a GHC OccName
773-
prefixes :: [T.Text]
774-
prefixes =
775-
[
776-
-- long ones
777-
"$con2tag_"
778-
, "$tag2con_"
779-
, "$maxtag_"
780-
781-
-- four chars
782-
, "$sel:"
783-
, "$tc'"
784-
785-
-- three chars
786-
, "$dm"
787-
, "$co"
788-
, "$tc"
789-
, "$cp"
790-
, "$fx"
791-
792-
-- two chars
793-
, "$W"
794-
, "$w"
795-
, "$m"
796-
, "$b"
797-
, "$c"
798-
, "$d"
799-
, "$i"
800-
, "$s"
801-
, "$f"
802-
, "$r"
803-
, "C:"
804-
, "N:"
805-
, "D:"
806-
, "$p"
807-
, "$L"
808-
, "$f"
809-
, "$t"
810-
, "$c"
811-
, "$m"
812-
]
813-
771+
getFirst $ foldMap (First . (`T.stripPrefix` name)) occNamePrefixes
814772

815773
mkRecordSnippetCompItem :: Uri -> Maybe T.Text -> T.Text -> [T.Text] -> Provenance -> Maybe (LImportDecl GhcPs) -> CompItem
816774
mkRecordSnippetCompItem uri parent ctxStr compl importedFrom imp = r

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,19 @@ atPoint IdeOptions{} (HAR _ hf _ _ kind) (DKMap dm km) env pos = listToMaybe $ p
227227
wrapHaskell x = "\n```haskell\n"<>x<>"\n```\n"
228228
info = nodeInfoH kind ast
229229
names = M.assocs $ nodeIdentifiers info
230+
-- Check for evidence bindings
231+
isInternal :: (Identifier, IdentifierDetails a) -> Bool
232+
isInternal (Right _, dets) =
233+
#if MIN_VERSION_ghc(9,0,1)
234+
any isEvidenceContext $ identInfo dets
235+
#else
236+
False
237+
#endif
238+
isInternal (Left _, _) = False
239+
filteredNames = filter (not . isInternal) names
230240
types = nodeType info
231-
232241
prettyNames :: [T.Text]
233-
prettyNames = map prettyName names
242+
prettyNames = map prettyName filteredNames
234243
prettyName (Right n, dets) = T.unlines $
235244
wrapHaskell (printOutputable n <> maybe "" (" :: " <>) ((prettyType <$> identType dets) <|> maybeKind))
236245
: maybeToList (pretty (definedAt n) (prettyPackageName n))

ghcide/test/data/hover/GotoHover.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ hole = _
6464

6565
hole2 :: a -> Maybe a
6666
hole2 = _
67+
68+
-- A comment above a type defnition with a deriving clause
69+
data Example = Example
70+
deriving (Eq)

ghcide/test/exe/Main.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,7 @@ findDefinitionAndHoverTests = let
10261026
ExpectRange expectedRange -> checkHoverRange expectedRange rangeInHover msg
10271027
ExpectHoverRange expectedRange -> checkHoverRange expectedRange rangeInHover msg
10281028
ExpectHoverText snippets -> liftIO $ traverse_ (`assertFoundIn` msg) snippets
1029+
ExpectHoverExcludeText snippets -> liftIO $ traverse_ (`assertNotFoundIn` msg) snippets
10291030
ExpectHoverTextRegex re -> liftIO $ assertBool ("Regex not found in " <> T.unpack msg) (msg =~ re :: Bool)
10301031
ExpectNoHover -> liftIO $ assertFailure $ "Expected no hover but got " <> show hover
10311032
_ -> pure () -- all other expectations not relevant to hover
@@ -1054,6 +1055,11 @@ findDefinitionAndHoverTests = let
10541055
(T.unpack $ "failed to find: `" <> part <> "` in hover message:\n" <> whole)
10551056
(part `T.isInfixOf` whole)
10561057

1058+
assertNotFoundIn :: T.Text -> T.Text -> Assertion
1059+
assertNotFoundIn part whole = assertBool
1060+
(T.unpack $ "found unexpected: `" <> part <> "` in hover message:\n" <> whole)
1061+
(not . T.isInfixOf part $ whole)
1062+
10571063
sourceFilePath = T.unpack sourceFileName
10581064
sourceFileName = "GotoHover.hs"
10591065

@@ -1130,6 +1136,7 @@ findDefinitionAndHoverTests = let
11301136
imported = Position 56 13 ; importedSig = getDocUri "Foo.hs" >>= \foo -> return [ExpectHoverText ["foo", "Foo", "Haddock"], mkL foo 5 0 5 3]
11311137
reexported = Position 55 14 ; reexportedSig = getDocUri "Bar.hs" >>= \bar -> return [ExpectHoverText ["Bar", "Bar", "Haddock"], mkL bar 3 (if ghcVersion >= GHC94 then 5 else 0) 3 (if ghcVersion >= GHC94 then 8 else 14)]
11321138
thLocL57 = Position 59 10 ; thLoc = [ExpectHoverText ["Identity"]]
1139+
cmtL68 = Position 67 0 ; lackOfdEq = [ExpectHoverExcludeText ["$dEq"]]
11331140
in
11341141
mkFindTests
11351142
-- def hover look expect
@@ -1173,6 +1180,7 @@ findDefinitionAndHoverTests = let
11731180
, test no broken chrL36 litC "literal Char in hover info #1016"
11741181
, test no broken txtL8 litT "literal Text in hover info #1016"
11751182
, test no broken lstL43 litL "literal List in hover info #1016"
1183+
, test yes yes cmtL68 lackOfdEq "no Core symbols #3280"
11761184
, if ghcVersion >= GHC90 then
11771185
test no yes docL41 constr "type constraint in hover info #1012"
11781186
else
@@ -2390,6 +2398,7 @@ data Expect
23902398
-- | ExpectDefRange Range -- Only gotoDef should report this range
23912399
| ExpectHoverRange Range -- Only hover should report this range
23922400
| ExpectHoverText [T.Text] -- the hover message must contain these snippets
2401+
| ExpectHoverExcludeText [T.Text] -- the hover message must _not_ contain these snippets
23932402
| ExpectHoverTextRegex T.Text -- the hover message must match this pattern
23942403
| ExpectExternFail -- definition lookup in other file expected to fail
23952404
| ExpectNoDefinitions

0 commit comments

Comments
 (0)