Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 4f75be9

Browse files
harpocratesalexbiehl
authored andcommitted
Use the GHC lexer for the Hyperlinker backend (#714)
* Start changing to use GHC lexer * better cpp * Change SrcSpan to RealSrcSpan * Remove error * Try to stop too many open files * wip * wip * Revert "wip" This reverts commit b605510. Conflicts: haddock-api/haddock-api.cabal haddock-api/src/Haddock/Interface.hs * Remove pointless 'caching' * Use dlist rather than lists when finding vars * Use a map rather than list * Delete bogus comment * Rebase followup Things now run using the GHC lexer. There are still - stray debug statements - unnecessary changes w.r.t. master * Cleaned up differences w.r.t. current Haddock HEAD Things are looking good. quasiquotes in particular look beautiful: the TH ones (with Haskell source inside) colour/link their contents too! Haven't yet begun to check for possible performance problems. * Support CPP and top-level pragmas The support for these is hackier - but no more hacky than the existing support. * Tests pass, CPP is better recognized The tests were in some cases altered: I consider the new output to be more correct than the old one.... * Fix shrinking of source without tabs in test * Replace 'Position'/'Span' with GHC counterparts Replaces 'Position' -> 'GHC.RealSrcLoc' and 'Span' -> 'GHC.RealSrcSpan'. * Nits * Forgot entry in .cabal * Update changelog
1 parent 60e10eb commit 4f75be9

File tree

10 files changed

+522
-312
lines changed

10 files changed

+522
-312
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
* Fix: Generate constraint signatures for constructors exported as pattern
2020
synonyms (#663)
2121

22+
* The hyperlinker backend now uses the GHC lexer instead of a custom one.
23+
This notably fixes rendering of quasiquotes.
24+
2225
* Overhaul Haddock's rendering of kind signatures so that invisible kind
2326
parameters are not printed (#681)
2427

haddock-api/haddock-api.cabal

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,68 @@ test-suite spec
124124
test
125125
, src
126126

127-
-- NB: We only use a small subset of lib:haddock-api here, which
128-
-- explains why this component has a smaller build-depends set
129127
other-modules:
128+
Haddock
129+
Haddock.Backends.Hoogle
130+
Haddock.Backends.Hyperlinker
131+
Haddock.Backends.Hyperlinker.Ast
132+
Haddock.Backends.Hyperlinker.Renderer
133+
Haddock.Backends.Hyperlinker.Utils
134+
Haddock.Backends.LaTeX
135+
Haddock.Backends.Xhtml
136+
Haddock.Backends.Xhtml.Decl
137+
Haddock.Backends.Xhtml.DocMarkup
138+
Haddock.Backends.Xhtml.Layout
139+
Haddock.Backends.Xhtml.Meta
140+
Haddock.Backends.Xhtml.Names
141+
Haddock.Backends.Xhtml.Themes
142+
Haddock.Backends.Xhtml.Types
143+
Haddock.Backends.Xhtml.Utils
144+
Haddock.Convert
145+
Haddock.Doc
146+
Haddock.GhcUtils
147+
Haddock.Interface
148+
Haddock.Interface.AttachInstances
149+
Haddock.Interface.Create
150+
Haddock.Interface.Json
151+
Haddock.Interface.LexParseRn
152+
Haddock.Interface.ParseModuleHeader
153+
Haddock.Interface.Rename
154+
Haddock.Interface.Specialize
155+
Haddock.InterfaceFile
156+
Haddock.ModuleTree
157+
Haddock.Options
158+
Haddock.Parser
159+
Haddock.Syb
160+
Haddock.Types
161+
Haddock.Utils
162+
Haddock.Utils.Json
163+
Haddock.Version
164+
Paths_haddock_api
130165
Haddock.Backends.Hyperlinker.ParserSpec
131166
Haddock.Backends.Hyperlinker.Parser
132167
Haddock.Backends.Hyperlinker.Types
133168

134-
build-depends:
135-
ghc ^>= 8.4
136-
, hspec ^>= 2.4.4
137-
, QuickCheck ^>= 2.10
169+
build-depends: Cabal ^>= 2.0.0
170+
, ghc ^>= 8.4
171+
, ghc-paths ^>= 0.1.0.9
172+
, haddock-library ^>= 1.4.6
173+
, xhtml ^>= 3000.2.2
174+
, hspec ^>= 2.4.4
175+
, QuickCheck ^>= 2.10
138176

139177
-- Versions for the dependencies below are transitively pinned by
140178
-- the non-reinstallable `ghc` package and hence need no version
141179
-- bounds
142-
build-depends:
143-
base
144-
, containers
180+
build-depends: base
181+
, array
182+
, bytestring
183+
, containers
184+
, deepseq
185+
, directory
186+
, filepath
187+
, ghc-boot
188+
, transformers
145189

146190
build-tool-depends:
147191
hspec-discover:hspec-discover ^>= 2.4.4

haddock-api/src/Haddock.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ render dflags flags qual ifaces installedIfaces extSrcMap = do
277277
| Flag_HyperlinkedSource `elem` flags = Just hypSrcModuleUrlFormat
278278
| otherwise = srcModule
279279

280-
srcMap = mkSrcMap $ Map.union
280+
srcMap = Map.union
281281
(Map.map SrcExternal extSrcMap)
282282
(Map.fromList [ (ifaceMod iface, SrcLocal) | iface <- ifaces ])
283283

haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import qualified Haddock.Syb as Syb
1212
import Haddock.Backends.Hyperlinker.Types
1313

1414
import qualified GHC
15+
import qualified SrcLoc
1516

1617
import Control.Applicative
1718
import Control.Monad (guard)
@@ -51,28 +52,22 @@ type DetailsMap = Map.Map Position (Span, TokenDetails)
5152

5253
mkDetailsMap :: [(GHC.SrcSpan, TokenDetails)] -> DetailsMap
5354
mkDetailsMap xs =
54-
Map.fromListWith select_details [ (start, (token_span, token_details))
55+
Map.fromListWith select_details [ (start, (span, token_details))
5556
| (ghc_span, token_details) <- xs
56-
, Just !token_span <- [ghcSrcSpanToSpan ghc_span]
57-
, let start = spStart token_span
57+
, GHC.RealSrcSpan span <- [ghc_span]
58+
, let start = SrcLoc.realSrcSpanStart span
5859
]
5960
where
6061
-- favour token details which appear earlier in the list
6162
select_details _new old = old
6263

6364
lookupBySpan :: Span -> DetailsMap -> Maybe TokenDetails
6465
lookupBySpan span details = do
65-
(_, (tok_span, tok_details)) <- Map.lookupLE (spStart span) details
66-
guard (tok_span `containsSpan` span )
66+
let pos = SrcLoc.realSrcSpanStart span
67+
(_, (tok_span, tok_details)) <- Map.lookupLE pos details
68+
guard (tok_span `SrcLoc.containsSpan` span)
6769
return tok_details
6870

69-
ghcSrcSpanToSpan :: GHC.SrcSpan -> Maybe Span
70-
ghcSrcSpanToSpan (GHC.RealSrcSpan span) =
71-
Just (Span { spStart = Position (GHC.srcSpanStartLine span) (GHC.srcSpanStartCol span)
72-
, spEnd = Position (GHC.srcSpanEndLine span) (GHC.srcSpanEndCol span)
73-
})
74-
ghcSrcSpanToSpan _ = Nothing
75-
7671
enrichToken :: Token -> DetailsMap -> Maybe TokenDetails
7772
enrichToken (Token typ _ spn) dm
7873
| typ `elem` [TkIdentifier, TkOperator] = lookupBySpan spn dm

0 commit comments

Comments
 (0)