Skip to content

Commit 940f414

Browse files
committed
Create shake getParsedModuleWithCommentsRule
1 parent f6c5d77 commit 940f414

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

ghcide/src/Development/IDE/Core/RuleTypes.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ data LinkableType = ObjectLinkable | BCOLinkable
5454
-- | The parse tree for the file using GetFileContents
5555
type instance RuleResult GetParsedModule = ParsedModule
5656

57+
-- | The parse tree for the file using GetFileContents,
58+
-- all comments included using Opt_KeepRawTokenStream
59+
type instance RuleResult GetParsedModuleWithComments = ParsedModule
60+
5761
-- | The dependency information produced by following the imports recursively.
5862
-- This rule will succeed even if there is an error, e.g., a module could not be located,
5963
-- a module could not be parsed or an import cycle.
@@ -302,6 +306,12 @@ instance Hashable GetParsedModule
302306
instance NFData GetParsedModule
303307
instance Binary GetParsedModule
304308

309+
data GetParsedModuleWithComments = GetParsedModuleWithComments
310+
deriving (Eq, Show, Typeable, Generic)
311+
instance Hashable GetParsedModuleWithComments
312+
instance NFData GetParsedModuleWithComments
313+
instance Binary GetParsedModuleWithComments
314+
305315
data GetLocatedImports = GetLocatedImports
306316
deriving (Eq, Show, Typeable, Generic)
307317
instance Hashable GetLocatedImports

ghcide/src/Development/IDE/Core/Rules.hs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Development.IDE.Core.Rules(
2828
getDependencies,
2929
getParsedModule,
3030
getClientConfigAction,
31+
getParsedModuleWithComments
3132
) where
3233

3334
import Fingerprint
@@ -242,9 +243,14 @@ getPackageHieFile ide mod file = do
242243
_ -> MaybeT $ return Nothing
243244
_ -> MaybeT $ return Nothing
244245

245-
-- | Parse the contents of a daml file.
246+
-- | Parse the contents of a haskell file.
246247
getParsedModule :: NormalizedFilePath -> Action (Maybe ParsedModule)
247-
getParsedModule file = use GetParsedModule file
248+
getParsedModule = use GetParsedModule
249+
250+
-- | Parse the contents of a haskell file,
251+
-- ensuring comments are preserved in annotations
252+
getParsedModuleWithComments :: NormalizedFilePath -> Action (Maybe ParsedModule)
253+
getParsedModuleWithComments = use GetParsedModule
248254

249255
------------------------------------------------------------
250256
-- Rules
@@ -307,8 +313,10 @@ getParsedModuleRule = defineEarlyCutoff $ \GetParsedModule file -> do
307313
pure res
308314

309315
withOptHaddock :: ModSummary -> ModSummary
310-
withOptHaddock ms = ms{ms_hspp_opts= gopt_set (ms_hspp_opts ms) Opt_Haddock}
316+
withOptHaddock = withOption Opt_Haddock
311317

318+
withOption :: GeneralFlag -> ModSummary -> ModSummary
319+
withOption opt ms = ms{ms_hspp_opts= gopt_set (ms_hspp_opts ms) opt}
312320

313321
-- | Given some normal parse errors (first) and some from Haddock (second), merge them.
314322
-- Ignore Haddock errors that are in both. Demote Haddock-only errors to warnings.
@@ -322,6 +330,16 @@ mergeParseErrorsHaddock normal haddock = normal ++
322330
fixMessage x | "parse error " `T.isPrefixOf` x = "Haddock " <> x
323331
| otherwise = "Haddock: " <> x
324332

333+
getParsedModuleWithCommentsRule :: Rules ()
334+
getParsedModuleWithCommentsRule = defineEarlyCutoff $ \GetParsedModuleWithComments file -> do
335+
(ms, _) <- use_ GetModSummary file
336+
sess <- use_ GhcSession file
337+
opt <- getIdeOptions
338+
339+
let ms' = withOption Opt_KeepRawTokenStream ms
340+
341+
liftIO $ getParsedModuleDefinition (hscEnv sess) opt file ms'
342+
325343
getParsedModuleDefinition :: HscEnv -> IdeOptions -> NormalizedFilePath -> ModSummary -> IO (Maybe ByteString, ([FileDiagnostic], Maybe ParsedModule))
326344
getParsedModuleDefinition packageState opt file ms = do
327345
let fp = fromNormalizedFilePath file
@@ -948,6 +966,7 @@ mainRule = do
948966
linkables <- liftIO $ newVar emptyModuleEnv
949967
addIdeGlobal $ CompiledLinkables linkables
950968
getParsedModuleRule
969+
getParsedModuleWithCommentsRule
951970
getLocatedImportsRule
952971
getDependencyInformationRule
953972
reportImportCyclesRule

0 commit comments

Comments
 (0)