Skip to content

Commit c9732bb

Browse files
committed
ghcide: Documentation: getDocumentationTryGhc: implement for 1 elem
1 parent f5b8b20 commit c9732bb

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

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

+38-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module Development.IDE.Spans.Documentation (
1212
, mkDocMap
1313
) where
1414

15-
import Control.Monad
1615
import Control.Monad.IO.Class
1716
import Control.Monad.Extra (findM)
1817
import Data.Bool (bool)
@@ -65,7 +64,44 @@ lookupKind env mod =
6564

6665
getDocumentationTryGhc :: HscEnv -> Module -> Name -> IO SpanDoc
6766
-- 2021-11-17: FIXME: Code uses batch search for singleton & assumes that search always succeeds.
68-
getDocumentationTryGhc env mod n = fromJust . M.lookup n <$> getDocumentationsTryGhc env mod [n]
67+
getDocumentationTryGhc env mod = fun
68+
where
69+
fun :: Name -> IO SpanDoc
70+
fun name = do
71+
res <- getDocsNonInteractive env mod name
72+
case res of
73+
Left _ -> pure emptySpanDoc -- catchSrcErrors (hsc_dflags env) "docs"
74+
Right res -> uncurry unwrap res
75+
where
76+
unwrap :: Name -> Either a (Maybe HsDocString, b) -> IO SpanDoc
77+
unwrap name a = extractDocString a <$> getSpanDocUris name
78+
where
79+
extractDocString :: Either b1 (Maybe HsDocString, b2) -> SpanDocUris -> SpanDoc
80+
-- 2021-11-17: FIXME: ArgDocs get dropped here - instead propagate them.
81+
extractDocString (Right (Just docs, _)) = SpanDocString docs
82+
extractDocString _ = SpanDocText mempty
83+
84+
-- | Get the uris to the documentation and source html pages if they exist
85+
getSpanDocUris :: Name -> IO SpanDocUris
86+
getSpanDocUris name = do
87+
(docFu, srcFu) <-
88+
case nameModule_maybe name of
89+
Just mod -> liftIO $ do
90+
let
91+
toUriFileText :: (HscEnv -> Module -> IO (Maybe FilePath)) -> IO (Maybe T.Text)
92+
toUriFileText f = (fmap . fmap) (getUri . filePathToUri) $ f env mod
93+
doc <- toUriFileText lookupDocHtmlForModule
94+
src <- toUriFileText lookupSrcHtmlForModule
95+
return (doc, src)
96+
Nothing -> pure mempty
97+
let
98+
embelishUri :: Functor f => T.Text -> f T.Text -> f T.Text
99+
embelishUri f = fmap (<> "#" <> f <> showNameWithoutUniques name)
100+
101+
docUri = embelishUri (bool "t:" "v:" $ isValName name) docFu
102+
srcUri = embelishUri mempty srcFu
103+
104+
return $ SpanDocUris docUri srcUri
69105

70106
getDocumentationsTryGhc :: HscEnv -> Module -> [Name] -> IO (M.Map Name SpanDoc)
71107
getDocumentationsTryGhc env mod names = do

0 commit comments

Comments
 (0)