Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Commit 2e35f73

Browse files
committed
a dirty PoC (GHC 8.10.2)
1 parent c928569 commit 2e35f73

File tree

6 files changed

+88
-29
lines changed

6 files changed

+88
-29
lines changed

ghcide.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ library
5050
filepath,
5151
fingertree,
5252
Glob,
53-
haddock-library >= 1.8,
53+
haddock-library >= 1.9,
54+
haddock-api >= 2.24.0,
5455
hashable,
5556
haskell-lsp-types == 0.22.*,
5657
haskell-lsp == 0.22.*,

hie.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ cradle:
77
- path: "./"
88
config:
99
cradle:
10-
cabal:
10+
stack:
1111
- path: "./src"
12-
component: "ghcide:lib:ghcide"
12+
component: "ghcide:lib"
1313
- path: "./exe"
1414
component: "ghcide:exe:ghcide"
1515
- path: "./session-loader"
16-
component: "ghcide:lib:ghcide"
16+
component: "ghcide:lib"
1717
- path: "./test"
1818
component: "ghcide:test:ghcide-tests"
1919
- path: "./bench"

src/Development/IDE/Plugin/Completions/Logic.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ cacheDataProducer packageState tm deps = do
290290
varToCompl var = do
291291
let typ = Just $ varType var
292292
name = Var.varName var
293-
docs <- evalGhcEnv packageState $ getDocumentationTryGhc curMod (tm_parsed_module tm : deps) name
293+
docs <- evalGhcEnv packageState $ getDocumentationTryGhc Map.empty curMod (tm_parsed_module tm : deps) name
294294
return $ mkNameCompItem name curModName typ Nothing docs
295295

296296
toCompItem :: Module -> ModuleName -> Name -> IO CompItem
297297
toCompItem m mn n = do
298-
docs <- evalGhcEnv packageState $ getDocumentationTryGhc curMod (tm_parsed_module tm : deps) n
298+
docs <- evalGhcEnv packageState $ getDocumentationTryGhc Map.empty curMod (tm_parsed_module tm : deps) n
299299
ty <- evalGhcEnv packageState $ catchSrcErrors "completion" $ do
300300
name' <- lookupName m n
301301
return $ name' >>= safeTyThingType

src/Development/IDE/Spans/Documentation.hs

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ import Development.IDE.Spans.Common
2828
import Development.IDE.Core.RuleTypes
2929
import System.Directory
3030
import System.FilePath
31-
31+
import qualified Documentation.Haddock as H
3232
import FastString
3333
import SrcLoc (RealLocated)
3434
import GhcMonad
3535
import Packages
3636
import Name
3737
import Language.Haskell.LSP.Types (getUri, filePathToUri)
3838
import Data.Either
39+
import Control.Arrow (Arrow((&&&)))
3940

4041
mkDocMap
4142
:: GhcMonad m
@@ -47,12 +48,15 @@ mkDocMap
4748
mkDocMap sources rm hmi deps =
4849
do mapM_ (`loadDepModule` Nothing) (reverse deps)
4950
loadDepModule hmi Nothing
50-
d <- foldrM getDocs M.empty names
51+
df <- getSessionDynFlags
52+
nml <- findNameToHaddockModuleLinks df names
53+
54+
d <- foldrM (getDocs nml) M.empty names
5155
k <- foldrM getType M.empty names
5256
pure $ DKMap d k
5357
where
54-
getDocs n map = do
55-
doc <- getDocumentationTryGhc mod sources n
58+
getDocs nml n map = do
59+
doc <- getDocumentationTryGhc nml mod sources n
5660
pure $ M.insert n doc map
5761
getType n map
5862
| isTcOcc $ occName n = do
@@ -67,13 +71,13 @@ lookupKind :: GhcMonad m => Module -> Name -> m (Maybe Type)
6771
lookupKind mod =
6872
fmap (either (const Nothing) (safeTyThingType =<<)) . catchSrcErrors "span" . lookupName mod
6973

70-
getDocumentationTryGhc :: GhcMonad m => Module -> [ParsedModule] -> Name -> m SpanDoc
71-
getDocumentationTryGhc mod deps n = head <$> getDocumentationsTryGhc mod deps [n]
74+
getDocumentationTryGhc :: GhcMonad m => M.Map Name (FilePath, ModuleName) -> Module -> [ParsedModule] -> Name -> m SpanDoc
75+
getDocumentationTryGhc nml mod deps n = head <$> getDocumentationsTryGhc nml mod deps [n]
7276

73-
getDocumentationsTryGhc :: GhcMonad m => Module -> [ParsedModule] -> [Name] -> m [SpanDoc]
77+
getDocumentationsTryGhc :: GhcMonad m => M.Map Name (FilePath, ModuleName) -> Module -> [ParsedModule] -> [Name] -> m [SpanDoc]
7478
-- Interfaces are only generated for GHC >= 8.6.
7579
-- In older versions, interface files do not embed Haddocks anyway
76-
getDocumentationsTryGhc mod sources names = do
80+
getDocumentationsTryGhc nml mod sources names = do
7781
res <- catchSrcErrors "docs" $ getDocsBatch mod names
7882
case res of
7983
Left _ -> mapM mkSpanDocText names
@@ -91,7 +95,8 @@ getDocumentationsTryGhc mod sources names = do
9195
(docFu, srcFu) <-
9296
case nameModule_maybe name of
9397
Just mod -> liftIO $ do
94-
doc <- toFileUriText $ lookupDocHtmlForModule df mod
98+
-- doc <- toFileUriText $ lookupDocHtmlForModule df mod
99+
doc <- toFileUriText $ lookupHtmlDocForName (\pkgDocDir modDocName -> pkgDocDir </> modDocName <.> "html") nml name
95100
src <- toFileUriText $ lookupSrcHtmlForModule df mod
96101
return (doc, src)
97102
Nothing -> pure (Nothing, Nothing)
@@ -215,6 +220,51 @@ lookupHtmlForModule mkDocPath df m = do
215220
-- The file might use "." or "-" as separator
216221
map (`intercalate` chunks) [".", "-"]
217222

223+
224+
lookupHtmlDocForName :: (FilePath -> FilePath -> FilePath) -> M.Map Name (FilePath, ModuleName) -> Name -> IO (Maybe FilePath)
225+
lookupHtmlDocForName mkDocPath nml n = do
226+
let mfs = concatMap go dirs
227+
html <- findM doesFileExist mfs
228+
-- canonicalize located html to remove /../ indirection which can break some clients
229+
-- (vscode on Windows at least)
230+
traverse canonicalizePath html
231+
where
232+
go pkgDocDir = map (mkDocPath pkgDocDir) mns
233+
x = M.lookup n nml
234+
dirs = fromMaybe [] $ (:[]) . fst <$> x
235+
chunks = splitOn "." $ fromMaybe "" $ (moduleNameString . snd <$> x)
236+
mns = map (`intercalate` chunks) [".", "-"]
237+
238+
239+
findNameToHaddockModuleLinks :: GhcMonad m => DynFlags -> [Name] -> m (M.Map Name (FilePath, ModuleName))
240+
findNameToHaddockModuleLinks df names = M.fromList . concat <$> mapM findNameUris fins
241+
where
242+
findNameUris (fi, ns) = do
243+
-- TODO: resolve mangled html file name and anchor using Haddock api? is it possible?
244+
-- TODO: fall back to old guesswork solution if haddock doesn't work ?
245+
-- TODO : clean up / ugly
246+
247+
let dir = takeDirectory fi
248+
exists <- liftIO $ doesFileExist fi
249+
if exists
250+
then do
251+
ioe <- H.readInterfaceFile H.nameCacheFromGhc fi False
252+
case ioe of
253+
Left _ -> return []
254+
Right (i :: H.InterfaceFile) ->
255+
let le = H.ifLinkEnv i
256+
in return $ catMaybes $ map (\n -> (n,) . (dir,) . moduleName <$> (M.lookup n le)) ns
257+
else
258+
return []
259+
260+
nameHaddockInterface_maybe n = do
261+
m <- nameModule_maybe n
262+
p <- lookupPackage df $ moduleUnitId m
263+
i <- listToMaybe $ haddockInterfaces p
264+
return (i, n)
265+
fins = map (fst . head &&& map snd) $ groupOn fst $ catMaybes $ map nameHaddockInterface_maybe names
266+
267+
218268
lookupHtmls :: DynFlags -> UnitId -> Maybe [FilePath]
219269
lookupHtmls df ui =
220270
-- use haddockInterfaces instead of haddockHTMLs: GHC treats haddockHTMLs as URL not path

stack-ghc-lib.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extra-deps:
1313
- shake-0.18.5
1414
- regex-base-0.94.0.0
1515
- regex-tdfa-1.3.1.0
16-
- haddock-library-1.8.0
16+
- haddock-library-1.9.0
1717
- ghc-check-0.5.0.1
1818
- parser-combinators-1.2.1
1919
nix:

stack.yaml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
1-
resolver: nightly-2019-09-21
1+
resolver: nightly-2020-06-19
2+
allow-newer: true
23
packages:
34
- .
45
extra-deps:
5-
- aeson-1.4.6.0
66
- haskell-lsp-0.22.0.0
77
- haskell-lsp-types-0.22.0.0
88
- lsp-test-0.11.0.6
9-
- hie-bios-0.7.1@rev:2
9+
- ghc-check-0.5.0.1
10+
- hie-bios-0.7.1
11+
12+
# not yet in stackage
13+
- Chart-diagrams-1.9.3
14+
- SVGFonts-1.7.0.1
15+
- diagrams-1.4
16+
- diagrams-svg-1.4.3
17+
- diagrams-contrib-1.4.4
18+
- diagrams-core-1.4.2
19+
- diagrams-lib-1.4.3
20+
- diagrams-postscript-1.5
21+
- monoid-extras-0.5.1
22+
- svg-builder-0.1.1
23+
- active-0.2.0.14
24+
- dual-tree-0.2.2.1
25+
- force-layout-0.4.0.6
26+
- statestack-0.3
1027
- implicit-hie-0.1.1.0
1128
- implicit-hie-cradle-0.2.0.1
12-
- fuzzy-0.1.0.0
13-
- regex-pcre-builtin-0.95.1.1.8.43
14-
- regex-base-0.94.0.0
15-
- regex-tdfa-1.3.1.0
16-
- shake-0.18.5
17-
- parser-combinators-1.2.1
18-
- haddock-library-1.8.0
19-
- tasty-rerun-1.1.17
20-
- ghc-check-0.5.0.1
21-
- extra-1.7.2
29+
- haddock-api-2.24.0
2230

2331
nix:
2432
packages: [zlib]

0 commit comments

Comments
 (0)