Skip to content

Commit 5192cfb

Browse files
committed
ghcide: Spans.Documentation: getDocumentationsTryGhc: clean-up
1 parent 8aaf5ca commit 5192cfb

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ module Development.IDE.Spans.Documentation (
1313
) where
1414

1515
import Control.Monad
16-
import Control.Monad.Extra (findM)
1716
import Control.Monad.IO.Class
17+
import Control.Monad.Extra (findM)
18+
import Data.Bool (bool)
1819
import Data.Either
1920
import Data.Foldable
2021
import Data.List.Extra
@@ -32,7 +33,6 @@ import System.Directory
3233
import System.FilePath
3334

3435
import Language.LSP.Types (filePathToUri, getUri)
35-
import qualified Data.Map as Map
3636

3737
mkDocMap
3838
:: HscEnv
@@ -70,14 +70,14 @@ lookupKind env mod =
7070

7171
getDocumentationTryGhc :: HscEnv -> Module -> Name -> IO SpanDoc
7272
-- 2021-11-17: FIXME: Code uses batch search for singleton & assumes that search always succeeds.
73-
getDocumentationTryGhc env mod n = fromJust . Map.lookup n <$> getDocumentationsTryGhc env mod [n]
73+
getDocumentationTryGhc env mod n = fromJust . M.lookup n <$> getDocumentationsTryGhc env mod [n]
7474

75-
getDocumentationsTryGhc :: HscEnv -> Module -> [Name] -> IO (Map.Map Name SpanDoc)
75+
getDocumentationsTryGhc :: HscEnv -> Module -> [Name] -> IO (M.Map Name SpanDoc)
7676
getDocumentationsTryGhc env mod names = do
7777
res <- getDocsBatch env mod names
7878
case res of
7979
Left _ -> return mempty -- catchSrcErrors (hsc_dflags env) "docs"
80-
Right res -> sequenceA $ Map.mapWithKey unwrap res
80+
Right res -> sequenceA $ M.mapWithKey unwrap res
8181
where
8282
unwrap :: Name -> Either a (Maybe HsDocString, b) -> IO SpanDoc
8383
unwrap name a = extractDocString a <$> getSpanDocUris name
@@ -93,19 +93,21 @@ getDocumentationsTryGhc env mod names = do
9393
(docFu, srcFu) <-
9494
case nameModule_maybe name of
9595
Just mod -> liftIO $ do
96-
doc <- toFileUriText $ lookupDocHtmlForModule env mod
97-
src <- toFileUriText $ lookupSrcHtmlForModule env mod
96+
let
97+
toUriFileText :: (HscEnv -> Module -> IO (Maybe FilePath)) -> IO (Maybe T.Text)
98+
toUriFileText f = (fmap . fmap) (getUri . filePathToUri) $ f env mod
99+
doc <- toUriFileText lookupDocHtmlForModule
100+
src <- toUriFileText lookupSrcHtmlForModule
98101
return (doc, src)
99102
Nothing -> pure mempty
100-
let docUri = (<> "#" <> selector <> showNameWithoutUniques name) <$> docFu
101-
srcUri = (<> "#" <> showNameWithoutUniques name) <$> srcFu
102-
selector
103-
| isValName name = "v:"
104-
| otherwise = "t:"
103+
let
104+
embelishUri :: Functor f => T.Text -> f T.Text -> f T.Text
105+
embelishUri f = fmap (<> "#" <> f <> showNameWithoutUniques name)
106+
107+
docUri = embelishUri (bool "t:" "v:" $ isValName name) docFu
108+
srcUri = embelishUri mempty srcFu
109+
105110
return $ SpanDocUris docUri srcUri
106-
where
107-
toFileUriText :: IO (Maybe FilePath) -> IO (Maybe T.Text)
108-
toFileUriText = (fmap . fmap) (getUri . filePathToUri)
109111

110112
getDocumentation
111113
:: HasSrcSpan name

0 commit comments

Comments
 (0)