Skip to content

eval: more robust way to extract comments from ParsedModule #4113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 14 additions & 28 deletions plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Rules.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}

-- To avoid warning "Pattern match has inaccessible right hand side"
{-# OPTIONS_GHC -Wno-overlapping-patterns #-}
module Ide.Plugin.Eval.Rules (GetEvalComments(..), rules,queueForEvaluation, unqueueForEvaluation, Log) where

import Control.Lens (toListOf)
import Control.Monad.IO.Class (MonadIO (liftIO))
import qualified Data.ByteString as BS
import Data.Data.Lens (biplate)
import Data.HashSet (HashSet)
import qualified Data.HashSet as Set
import Data.IORef
Expand All @@ -24,8 +24,7 @@ import Development.IDE (GetModSummaryWithoutTimes
fromNormalizedFilePath,
msrModSummary,
realSrcSpanToRange,
useWithStale_,
use_)
useWithStale_, use_)
import Development.IDE.Core.PositionMapping (toCurrentRange)
import Development.IDE.Core.Rules (computeLinkableTypeForDynFlags,
needsCompilationRule)
Expand All @@ -39,14 +38,12 @@ import Development.IDE.GHC.Compat
import qualified Development.IDE.GHC.Compat as SrcLoc
import qualified Development.IDE.GHC.Compat.Util as FastString
import Development.IDE.Graph (alwaysRerun)
import Ide.Logger (Pretty (pretty),
import GHC.Parser.Annotation
import Ide.Logger (Pretty (pretty),
Recorder, WithPriority,
cmapWithPrio)
import GHC.Parser.Annotation
import Ide.Plugin.Eval.Types

import qualified Data.ByteString as BS

newtype Log = LogShake Shake.Log deriving Show

instance Pretty Log where
Expand Down Expand Up @@ -74,28 +71,17 @@ unqueueForEvaluation ide nfp = do
-- remove the module from the Evaluating state, so that next time it won't evaluate to True
atomicModifyIORef' var $ \fs -> (Set.delete nfp fs, ())

#if MIN_VERSION_ghc(9,5,0)
getAnnotations :: Development.IDE.GHC.Compat.Located (HsModule GhcPs) -> [LEpaComment]
getAnnotations (L _ m@(HsModule { hsmodExt = XModulePs {hsmodAnn = anns'}})) =
#else
getAnnotations :: Development.IDE.GHC.Compat.Located HsModule -> [LEpaComment]
getAnnotations (L _ m@(HsModule { hsmodAnn = anns'})) =
#endif
priorComments annComments <> getFollowingComments annComments
<> concatMap getCommentsForDecl (hsmodImports m)
<> concatMap getCommentsForDecl (hsmodDecls m)
where
annComments = epAnnComments anns'

getCommentsForDecl :: GenLocated (SrcSpanAnn' (EpAnn ann)) e
-> [LEpaComment]
getCommentsForDecl (L (SrcSpanAnn (EpAnn _ _ cs) _) _) = priorComments cs <> getFollowingComments cs
getCommentsForDecl (L (SrcSpanAnn (EpAnnNotUsed) _) _) = []

apiAnnComments' :: ParsedModule -> [SrcLoc.RealLocated EpaCommentTok]
apiAnnComments' pm = do
L span (EpaComment c _) <- getAnnotations $ pm_parsed_source pm
L span (EpaComment c _) <- getEpaComments $ pm_parsed_source pm
pure (L (anchor span) c)
where
#if MIN_VERSION_ghc(9,5,0)
getEpaComments :: Development.IDE.GHC.Compat.Located (HsModule GhcPs) -> [LEpaComment]
#else
getEpaComments :: Development.IDE.GHC.Compat.Located HsModule -> [LEpaComment]
#endif
getEpaComments = toListOf biplate

pattern RealSrcSpanAlready :: SrcLoc.RealSrcSpan -> SrcLoc.RealSrcSpan
pattern RealSrcSpanAlready x = x
Expand Down
1 change: 1 addition & 0 deletions plugins/hls-eval-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ tests =
, goldenWithEval ":kind treats a multilined result properly" "T25" "hs"
, goldenWithEvalAndFs "local imports" (FS.directProjectMulti ["T26.hs", "Util.hs"]) "T26" "hs"
, goldenWithEval "Preserves one empty comment line after prompt" "T27" "hs"
, goldenWithEval "Evaluate comment after multiline function definition" "T28" "hs"
, goldenWithEval "Multi line comments" "TMulti" "hs"
, goldenWithEval "Multi line comments, with the last test line ends without newline" "TEndingMulti" "hs"
, goldenWithEval "Evaluate expressions in Plain comments in both single line and multi line format" "TPlainComment" "hs"
Expand Down
7 changes: 7 additions & 0 deletions plugins/hls-eval-plugin/test/testdata/T28.expected.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module T28 where

f True = True
f False = False

-- >>> 1+1
-- 2
6 changes: 6 additions & 0 deletions plugins/hls-eval-plugin/test/testdata/T28.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module T28 where

f True = True
f False = False

-- >>> 1+1