Skip to content

Commit 19f65cb

Browse files
committed
Development.IDE.Spans.Documentation: getDocumentationsTryGhc: structure
Make code easier to reason about & functionally enhancable.
1 parent 480aa51 commit 19f65cb

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

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

+29-23
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,38 @@ getDocumentationsTryGhc env mod names = do
7373
res <- fun
7474
case res of
7575
Left _ -> return mempty
76-
Right res -> fmap Map.fromList $ sequenceA $ unwrap <$> Map.toList res
76+
Right res -> fmap Map.fromList $ sequenceA $ uncurry unwrap <$> Map.toList res
7777
where
7878
fun :: IO (Either [FileDiagnostic] (Map.Map Name (Either String (Maybe HsDocString, Map.Map Int HsDocString))))
7979
fun = catchSrcErrors (hsc_dflags env) "docs" $ getDocsBatch env mod names
8080

81-
unwrap :: (Name, Either a (Maybe HsDocString, b)) -> IO (Name, SpanDoc)
82-
unwrap (name, Right (Just docs, _)) = (name,) . SpanDocString docs <$> getUris name
83-
unwrap (name, _) = (name,) . SpanDocText mempty <$> getUris name
84-
85-
-- Get the uris to the documentation and source html pages if they exist
86-
getUris name = do
87-
(docFu, srcFu) <-
88-
case nameModule_maybe name of
89-
Just mod -> liftIO $ do
90-
doc <- toFileUriText $ lookupDocHtmlForModule env mod
91-
src <- toFileUriText $ lookupSrcHtmlForModule env mod
92-
return (doc, src)
93-
Nothing -> pure (Nothing, Nothing)
94-
let docUri = (<> "#" <> selector <> showNameWithoutUniques name) <$> docFu
95-
srcUri = (<> "#" <> showNameWithoutUniques name) <$> srcFu
96-
selector
97-
| isValName name = "v:"
98-
| otherwise = "t:"
99-
return $ SpanDocUris docUri srcUri
100-
101-
toFileUriText = (fmap . fmap) (getUri . filePathToUri)
81+
unwrap :: Name -> Either a (Maybe HsDocString, b) -> IO (Name, SpanDoc)
82+
unwrap name a = (name,) . extractDocString a <$> getSpanDocUris name
83+
where
84+
extractDocString :: Either b1 (Maybe HsDocString, b2) -> SpanDocUris -> SpanDoc
85+
-- 2021-11-17: FIXME: ArgDocs get dropped here - instead propagate them.
86+
extractDocString (Right (Just docs, _)) = SpanDocString docs
87+
extractDocString _ = SpanDocText mempty
88+
89+
-- | Get the uris to the documentation and source html pages if they exist
90+
getSpanDocUris :: Name -> IO SpanDocUris
91+
getSpanDocUris name = do
92+
(docFu, srcFu) <-
93+
case nameModule_maybe name of
94+
Just mod -> liftIO $ do
95+
doc <- toFileUriText $ lookupDocHtmlForModule env mod
96+
src <- toFileUriText $ lookupSrcHtmlForModule env mod
97+
return (doc, src)
98+
Nothing -> pure mempty
99+
let docUri = (<> "#" <> selector <> showNameWithoutUniques name) <$> docFu
100+
srcUri = (<> "#" <> showNameWithoutUniques name) <$> srcFu
101+
selector
102+
| isValName name = "v:"
103+
| otherwise = "t:"
104+
return $ SpanDocUris docUri srcUri
105+
where
106+
toFileUriText :: IO (Maybe FilePath) -> IO (Maybe T.Text)
107+
toFileUriText = (fmap . fmap) (getUri . filePathToUri)
102108

103109
getDocumentation
104110
:: HasSrcSpan name
@@ -165,7 +171,7 @@ getDocumentation sources targetName = fromMaybe [] $ do
165171
docHeaders :: [RealLocated AnnotationComment]
166172
-> [T.Text]
167173
docHeaders = mapMaybe (\(L _ x) -> wrk x)
168-
where
174+
where
169175
wrk = \case
170176
-- When `Opt_Haddock` is enabled.
171177
AnnDocCommentNext s -> Just $ T.pack s

0 commit comments

Comments
 (0)