Skip to content

Commit ffbe9a8

Browse files
committed
ghcide: Documentation: getDocumentationTryGhc: implement for 1 elem
1 parent b751674 commit ffbe9a8

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

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

Lines changed: 38 additions & 2 deletions
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)
@@ -70,7 +69,44 @@ lookupKind env mod =
7069

7170
getDocumentationTryGhc :: HscEnv -> Module -> Name -> IO SpanDoc
7271
-- 2021-11-17: FIXME: Code uses batch search for singleton & assumes that search always succeeds.
73-
getDocumentationTryGhc env mod n = fromJust . M.lookup n <$> getDocumentationsTryGhc env mod [n]
72+
getDocumentationTryGhc env mod = fun
73+
where
74+
fun :: Name -> IO SpanDoc
75+
fun name = do
76+
res <- getDocsNonInteractive env mod name
77+
case res of
78+
Left _ -> pure emptySpanDoc -- catchSrcErrors (hsc_dflags env) "docs"
79+
Right res -> uncurry unwrap res
80+
where
81+
unwrap :: Name -> Either a (Maybe HsDocString, b) -> IO SpanDoc
82+
unwrap name a = 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+
let
96+
toUriFileText :: (HscEnv -> Module -> IO (Maybe FilePath)) -> IO (Maybe T.Text)
97+
toUriFileText f = (fmap . fmap) (getUri . filePathToUri) $ f env mod
98+
doc <- toUriFileText lookupDocHtmlForModule
99+
src <- toUriFileText lookupSrcHtmlForModule
100+
return (doc, src)
101+
Nothing -> pure mempty
102+
let
103+
embelishUri :: Functor f => T.Text -> f T.Text -> f T.Text
104+
embelishUri f = fmap (<> "#" <> f <> showNameWithoutUniques name)
105+
106+
docUri = embelishUri (bool "t:" "v:" $ isValName name) docFu
107+
srcUri = embelishUri mempty srcFu
108+
109+
return $ SpanDocUris docUri srcUri
74110

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

0 commit comments

Comments
 (0)