Date: Mon, 15 Jun 2015 00:06:08 -0400
Subject: Link to the definitions to themselves
Currently, the definitions already have an anchor tag that allows URLs
with fragment identifiers to locate them, but it is rather inconvenient
to obtain such a URL (so-called "permalink") as it would require finding
the a link to the corresponding item in the Synopsis or elsewhere. This
commit adds hyperlinks to the definitions themselves, allowing users to
obtain links to them easily.
To preserve the original aesthetics of the definitions, we alter the
color of the link so as to be identical to what it was, except it now
has a hover effect indicating that it is clickable.
Additionally, the anchor now uses the 'id' attribute instead of the
(obsolete) 'name' attribute.
Closes #407
---
.../resources/html/Ocean.std-theme/ocean.css | 3 +++
haddock-api/src/Haddock/Backends/Xhtml/Names.hs | 21 +++++++++++++--------
2 files changed, 16 insertions(+), 8 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/resources/html/Ocean.std-theme/ocean.css b/haddock-api/resources/html/Ocean.std-theme/ocean.css
index 1110b407..1cc55cb6 100644
--- a/haddock-api/resources/html/Ocean.std-theme/ocean.css
+++ b/haddock-api/resources/html/Ocean.std-theme/ocean.css
@@ -41,6 +41,9 @@ a[href]:link { color: rgb(196,69,29); }
a[href]:visited { color: rgb(171,105,84); }
a[href]:hover { text-decoration:underline; }
+a[href].def:link, a[href].def:visited { color: black; }
+a[href].def:hover { color: rgb(78, 98, 114); }
+
/* @end */
/* @group Fonts & Sizes */
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Names.hs b/haddock-api/src/Haddock/Backends/Xhtml/Names.hs
index cf12da40..c69710d1 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Names.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Names.hs
@@ -110,16 +110,21 @@ ppName notation name = wrapInfix notation (getOccName name) $ toHtml (getOccStri
ppBinder :: Bool -> OccName -> Html
--- The Bool indicates whether we are generating the summary, in which case
--- the binder will be a link to the full definition.
-ppBinder True n = linkedAnchor (nameAnchorId n) << ppBinder' Prefix n
-ppBinder False n = namedAnchor (nameAnchorId n) ! [theclass "def"]
- << ppBinder' Prefix n
+ppBinder = ppBinderWith Prefix
ppBinderInfix :: Bool -> OccName -> Html
-ppBinderInfix True n = linkedAnchor (nameAnchorId n) << ppBinder' Infix n
-ppBinderInfix False n = namedAnchor (nameAnchorId n) ! [theclass "def"]
- << ppBinder' Infix n
+ppBinderInfix = ppBinderWith Infix
+
+ppBinderWith :: Notation -> Bool -> OccName -> Html
+-- 'isRef' indicates whether this is merely a reference from another part of
+-- the documentation or is the actual definition; in the latter case, we also
+-- set the 'id' and 'class' attributes.
+ppBinderWith notation isRef n =
+ linkedAnchor name ! attributes << ppBinder' notation n
+ where
+ name = nameAnchorId n
+ attributes | isRef = []
+ | otherwise = [identifier name, theclass "def"]
ppBinder' :: Notation -> OccName -> Html
ppBinder' notation n = wrapInfix notation n $ ppOccName n
--
cgit v1.2.3
From e190efe089a0bd038c7de41c1c52ef580e52712c Mon Sep 17 00:00:00 2001
From: Phil Ruffwind
Date: Sun, 14 Jun 2015 22:47:13 -0400
Subject: Fix typo in Haddock.Backends.Xhtml.Layout: divSynposis -> divSynopsis
Closes #408
---
haddock-api/src/Haddock/Backends/Xhtml.hs | 2 +-
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 6 +++---
2 files changed, 4 insertions(+), 4 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml.hs b/haddock-api/src/Haddock/Backends/Xhtml.hs
index 90cb9fa4..e5e4db3f 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml.hs
@@ -546,7 +546,7 @@ ifaceToHtml maybe_source_url maybe_wiki_url iface unicode qual
synopsis
| no_doc_at_all = noHtml
| otherwise
- = divSynposis $
+ = divSynopsis $
paragraph ! collapseControl "syn" False "caption" << "Synopsis" +++
shortDeclList (
mapMaybe (processExport True linksInfo unicode qual) exports
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 914a7a7e..e79c2c3d 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -14,7 +14,7 @@ module Haddock.Backends.Xhtml.Layout (
miniBody,
divPackageHeader, divContent, divModuleHeader, divFooter,
- divTableOfContents, divDescription, divSynposis, divInterface,
+ divTableOfContents, divDescription, divSynopsis, divInterface,
divIndex, divAlphabet, divModuleList,
sectionName,
@@ -76,7 +76,7 @@ nonEmptySectionName c
divPackageHeader, divContent, divModuleHeader, divFooter,
- divTableOfContents, divDescription, divSynposis, divInterface,
+ divTableOfContents, divDescription, divSynopsis, divInterface,
divIndex, divAlphabet, divModuleList
:: Html -> Html
@@ -86,7 +86,7 @@ divModuleHeader = sectionDiv "module-header"
divFooter = sectionDiv "footer"
divTableOfContents = sectionDiv "table-of-contents"
divDescription = sectionDiv "description"
-divSynposis = sectionDiv "synopsis"
+divSynopsis = sectionDiv "synopsis"
divInterface = sectionDiv "interface"
divIndex = sectionDiv "index"
divAlphabet = sectionDiv "alphabet"
--
cgit v1.2.3
From c274363d5d868c838c382a52428c667090514f86 Mon Sep 17 00:00:00 2001
From: Phil Ruffwind
Date: Mon, 27 Jul 2015 05:58:58 -0400
Subject: Fix record field alignment when name is too long
Change
to
and use display:table rather than floats to layout
the record fields. This avoids bug #301 that occurs whenever the field
name gets too long.
Slight aesthetic change: the entire cell of the field's source code is
now shaded gray rather than just the area where text exists.
Fixes #301. Closes #421
---
.../resources/html/Ocean.std-theme/ocean.css | 29 +++++++++++-----------
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 10 +++-----
2 files changed, 19 insertions(+), 20 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/resources/html/Ocean.std-theme/ocean.css b/haddock-api/resources/html/Ocean.std-theme/ocean.css
index 1cc55cb6..9ad9f9d2 100644
--- a/haddock-api/resources/html/Ocean.std-theme/ocean.css
+++ b/haddock-api/resources/html/Ocean.std-theme/ocean.css
@@ -427,30 +427,31 @@ div#style-menu-holder {
visibility: hidden;
}
-.subs dl {
+.subs ul {
+ list-style: none;
+ display: table;
margin: 0;
}
-.subs dt {
- float: left;
- clear: left;
- display: block;
+.subs ul li {
+ display: table-row;
+}
+
+.subs ul li dfn {
+ display: table-cell;
+ font-style: normal;
+ font-weight: bold;
margin: 1px 0;
+ white-space: nowrap;
}
-.subs dd {
- float: right;
- width: 90%;
- display: block;
+.subs ul li > .doc {
+ display: table-cell;
padding-left: 0.5em;
margin-bottom: 0.5em;
}
-.subs dd.empty {
- display: none;
-}
-
-.subs dd p {
+.subs ul li > .doc p {
margin: 0;
}
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index e79c2c3d..4714c1b6 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -127,14 +127,12 @@ divSubDecls cssClass captionName = maybe noHtml wrap
subDlist :: Qualification -> [SubDecl] -> Maybe Html
subDlist _ [] = Nothing
-subDlist qual decls = Just $ dlist << map subEntry decls +++ clearDiv
+subDlist qual decls = Just $ ulist << map subEntry decls
where
subEntry (decl, mdoc, subs) =
- dterm ! [theclass "src"] << decl
- +++
- docElement ddef << (fmap (docToHtml Nothing qual) mdoc +++ subs)
-
- clearDiv = thediv ! [ theclass "clear" ] << noHtml
+ li <<
+ (define ! [theclass "src"] << decl +++
+ docElement thediv << (fmap (docToHtml Nothing qual) mdoc +++ subs))
subTable :: Qualification -> [SubDecl] -> Maybe Html
--
cgit v1.2.3
From 7e8330944666064f12f067970de2936b58589785 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sat, 25 Jul 2015 18:54:30 +0200
Subject: Add some utility definitions for generating line anchors.
---
.../src/Haddock/Backends/Hyperlinker/Utils.hs | 25 ++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs b/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
index db2bfc76..9de4a03d 100644
--- a/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
+++ b/haddock-api/src/Haddock/Backends/Hyperlinker/Utils.hs
@@ -1,13 +1,18 @@
module Haddock.Backends.Hyperlinker.Utils
( hypSrcDir, hypSrcModuleFile, hypSrcModuleFile'
- , hypSrcModuleUrl, hypSrcModuleUrl', hypSrcNameUrl, hypSrcModuleNameUrl
- , hypSrcModuleUrlFormat, hypSrcModuleNameUrlFormat,
+ , hypSrcModuleUrl, hypSrcModuleUrl'
+ , hypSrcNameUrl
+ , hypSrcLineUrl
+ , hypSrcModuleNameUrl, hypSrcModuleLineUrl
+ , hypSrcModuleUrlFormat
+ , hypSrcModuleNameUrlFormat, hypSrcModuleLineUrlFormat
) where
import Haddock.Backends.Xhtml.Utils
import GHC
+import FastString
import System.FilePath.Posix ((>))
@@ -31,17 +36,33 @@ hypSrcNameUrl :: Name -> String
hypSrcNameUrl name = spliceURL
Nothing Nothing (Just name) Nothing nameFormat
+hypSrcLineUrl :: Int -> String
+hypSrcLineUrl line = spliceURL
+ Nothing Nothing Nothing (Just spn) lineFormat
+ where
+ loc = mkSrcLoc nilFS line 1
+ spn = mkSrcSpan loc loc
+
hypSrcModuleNameUrl :: Module -> Name -> String
hypSrcModuleNameUrl mdl name = hypSrcModuleUrl mdl ++ "#" ++ hypSrcNameUrl name
+hypSrcModuleLineUrl :: Module -> Int -> String
+hypSrcModuleLineUrl mdl line = hypSrcModuleUrl mdl ++ "#" ++ hypSrcLineUrl line
+
hypSrcModuleUrlFormat :: String
hypSrcModuleUrlFormat = hypSrcDir > moduleFormat
hypSrcModuleNameUrlFormat :: String
hypSrcModuleNameUrlFormat = hypSrcModuleUrlFormat ++ "#" ++ nameFormat
+hypSrcModuleLineUrlFormat :: String
+hypSrcModuleLineUrlFormat = hypSrcModuleUrlFormat ++ "#" ++ lineFormat
+
moduleFormat :: String
moduleFormat = "%{MODULE}.html"
nameFormat :: String
nameFormat = "%{NAME}"
+
+lineFormat :: String
+lineFormat = "line-%{LINE}"
--
cgit v1.2.3
From 241346e4e275bdde2d28f90df3225057f4a09cfc Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sat, 25 Jul 2015 19:48:08 +0200
Subject: Make hyperlinked source renderer generate line anchors.
---
.../src/Haddock/Backends/Hyperlinker/Renderer.hs | 26 +++++++++++++++++++---
1 file changed, 23 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Hyperlinker/Renderer.hs b/haddock-api/src/Haddock/Backends/Hyperlinker/Renderer.hs
index 5037421a..15793f0c 100644
--- a/haddock-api/src/Haddock/Backends/Hyperlinker/Renderer.hs
+++ b/haddock-api/src/Haddock/Backends/Hyperlinker/Renderer.hs
@@ -1,3 +1,6 @@
+{-# LANGUAGE RecordWildCards #-}
+
+
module Haddock.Backends.Hyperlinker.Renderer (render) where
@@ -78,10 +81,11 @@ header mcss mjs =
tokenGroup :: SrcMap -> TokenGroup -> Html
-tokenGroup _ (GrpNormal tok) =
- tokenSpan tok ! attrs
+tokenGroup _ (GrpNormal tok@(Token { .. }))
+ | tkType == TkSpace = renderSpace (posRow . spStart $ tkSpan) tkValue
+ | otherwise = tokenSpan tok ! attrs
where
- attrs = [ multiclass . tokenStyle . tkType $ tok ]
+ attrs = [ multiclass . tokenStyle $ tkType ]
tokenGroup srcs (GrpRich det tokens) =
externalAnchor det . internalAnchor det . hyperlink srcs det $ content
where
@@ -167,3 +171,19 @@ externalModHyperlink (_, srcs) name content = case Map.lookup name srcs of
Just (SrcExternal path) -> Html.anchor content !
[ Html.href $ path > hypSrcModuleUrl' name ]
Nothing -> content
+
+
+renderSpace :: Int -> String -> Html
+renderSpace _ [] = Html.noHtml
+renderSpace line ('\n':rest) = mconcat
+ [ Html.thespan . Html.toHtml $ "\n"
+ , lineAnchor (line + 1)
+ , renderSpace (line + 1) rest
+ ]
+renderSpace line space =
+ let (hspace, rest) = span (/= '\n') space
+ in (Html.thespan . Html.toHtml) hspace <> renderSpace line rest
+
+
+lineAnchor :: Int -> Html
+lineAnchor line = Html.anchor Html.noHtml ! [ Html.name $ hypSrcLineUrl line ]
--
cgit v1.2.3
From acdbdc035468374cbea2ba2043987dac95c44bfe Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Thu, 6 Aug 2015 20:54:59 +0200
Subject: Fix quote syntax for promoted types.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 6 ++++--
haddock-api/src/Haddock/Backends/Xhtml/Utils.hs | 7 ++++++-
2 files changed, 10 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index df85a492..d3900f16 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -874,8 +874,10 @@ ppr_mono_ty _ (HsSpliceTy {}) _ _ = error "ppr_mono_ty HsSpliceTy"
ppr_mono_ty _ (HsQuasiQuoteTy {}) _ _ = error "ppr_mono_ty HsQuasiQuoteTy"
ppr_mono_ty _ (HsRecTy {}) _ _ = error "ppr_mono_ty HsRecTy"
ppr_mono_ty _ (HsCoreTy {}) _ _ = error "ppr_mono_ty HsCoreTy"
-ppr_mono_ty _ (HsExplicitListTy _ tys) u q = quote $ brackets $ hsep $ punctuate comma $ map (ppLType u q) tys
-ppr_mono_ty _ (HsExplicitTupleTy _ tys) u q = quote $ parenList $ map (ppLType u q) tys
+ppr_mono_ty _ (HsExplicitListTy _ tys) u q =
+ promoQuote $ brackets $ hsep $ punctuate comma $ map (ppLType u q) tys
+ppr_mono_ty _ (HsExplicitTupleTy _ tys) u q =
+ promoQuote $ parenList $ map (ppLType u q) tys
ppr_mono_ty _ (HsWrapTy {}) _ _ = error "ppr_mono_ty HsWrapTy"
ppr_mono_ty ctxt_prec (HsEqTy ty1 ty2) unicode qual
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Utils.hs b/haddock-api/src/Haddock/Backends/Xhtml/Utils.hs
index 26bcbf6d..98ff4007 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Utils.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Utils.hs
@@ -21,7 +21,7 @@ module Haddock.Backends.Xhtml.Utils (
keyword, punctuate,
braces, brackets, pabrackets, parens, parenList, ubxParenList,
- arrow, comma, dcolon, dot, darrow, equals, forallSymbol, quote,
+ arrow, comma, dcolon, dot, darrow, equals, forallSymbol, quote, promoQuote,
hsep, vcat,
@@ -150,6 +150,11 @@ quote :: Html -> Html
quote h = char '`' +++ h +++ '`'
+-- | Promoted type quote (e.g. @'[a, b]@, @'(a, b, c)@).
+promoQuote :: Html -> Html
+promoQuote h = char '\'' +++ h
+
+
parens, brackets, pabrackets, braces :: Html -> Html
parens h = char '(' +++ h +++ char ')'
brackets h = char '[' +++ h +++ char ']'
--
cgit v1.2.3
From 33fe6286907592b1783a4b9b7c4b7f63ae080cde Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Thu, 6 Aug 2015 21:12:00 +0200
Subject: Apply promoted type quoting to type-level consing.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index d3900f16..5f6f60eb 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -892,7 +892,12 @@ ppr_mono_ty ctxt_prec (HsOpTy ty1 (_, op) ty2) unicode qual
= maybeParen ctxt_prec pREC_FUN $
ppr_mono_lty pREC_OP ty1 unicode qual <+> ppr_op <+> ppr_mono_lty pREC_OP ty2 unicode qual
where
- ppr_op = ppLDocName qual Infix op
+ -- `(:)` is valid in type signature only as constructor to promoted list
+ -- and needs to be quoted in code so we explicitly quote it here too.
+ ppr_op
+ | (getOccString . getName . unLoc) op == ":" = promoQuote ppr_op'
+ | otherwise = ppr_op'
+ ppr_op' = ppLDocName qual Infix op
ppr_mono_ty ctxt_prec (HsParTy ty) unicode qual
-- = parens (ppr_mono_lty pREC_TOP ty)
--
cgit v1.2.3
From e9d61b79faf40200d8f9806d83a05ece272cd7d3 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 10 Jul 2015 11:42:18 +0200
Subject: Move SYB utilities to standalone module.
---
haddock-api/haddock-api.cabal | 1 +
.../src/Haddock/Backends/Hyperlinker/Ast.hs | 15 +------------
haddock-api/src/Haddock/Syb.hs | 26 ++++++++++++++++++++++
haddock.cabal | 1 +
4 files changed, 29 insertions(+), 14 deletions(-)
create mode 100644 haddock-api/src/Haddock/Syb.hs
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/haddock-api.cabal b/haddock-api/haddock-api.cabal
index 4db05de8..bfdb2179 100644
--- a/haddock-api/haddock-api.cabal
+++ b/haddock-api/haddock-api.cabal
@@ -94,6 +94,7 @@ library
Haddock.InterfaceFile
Haddock.Options
Haddock.GhcUtils
+ Haddock.Syb
Haddock.Convert
Paths_haddock_api
diff --git a/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs b/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
index 71b73663..5eca973e 100644
--- a/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
+++ b/haddock-api/src/Haddock/Backends/Hyperlinker/Ast.hs
@@ -6,6 +6,7 @@
module Haddock.Backends.Hyperlinker.Ast (enrich) where
+import Haddock.Syb
import Haddock.Backends.Hyperlinker.Types
import qualified GHC
@@ -179,17 +180,3 @@ matches tspan (GHC.RealSrcSpan aspan)
saspan = (GHC.srcSpanStartLine aspan, GHC.srcSpanStartCol aspan)
easpan = (GHC.srcSpanEndLine aspan, GHC.srcSpanEndCol aspan)
matches _ _ = False
-
--- | Perform a query on each level of a tree.
---
--- This is stolen directly from SYB package and copied here to not introduce
--- additional dependencies.
-everything :: (r -> r -> r) -> (forall a. Data a => a -> r)
- -> (forall a. Data a => a -> r)
-everything k f x = foldl k (f x) (gmapQ (everything k f) x)
-
--- | Combine two queries into one using alternative combinator.
-combine :: Alternative f => (forall a. Data a => a -> f r)
- -> (forall a. Data a => a -> f r)
- -> (forall a. Data a => a -> f r)
-combine f g x = f x <|> g x
diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs
new file mode 100644
index 00000000..dd7ffc1b
--- /dev/null
+++ b/haddock-api/src/Haddock/Syb.hs
@@ -0,0 +1,26 @@
+{-# LANGUAGE Rank2Types #-}
+
+
+module Haddock.Syb
+ ( everything
+ , combine
+ ) where
+
+
+import Data.Data
+import Control.Applicative
+
+
+-- | Perform a query on each level of a tree.
+--
+-- This is stolen directly from SYB package and copied here to not introduce
+-- additional dependencies.
+everything :: (r -> r -> r) -> (forall a. Data a => a -> r)
+ -> (forall a. Data a => a -> r)
+everything k f x = foldl k (f x) (gmapQ (everything k f) x)
+
+-- | Combine two queries into one using alternative combinator.
+combine :: Alternative f => (forall a. Data a => a -> f r)
+ -> (forall a. Data a => a -> f r)
+ -> (forall a. Data a => a -> f r)
+combine f g x = f x <|> g x
diff --git a/haddock.cabal b/haddock.cabal
index 27ae8967..b0c6c34f 100644
--- a/haddock.cabal
+++ b/haddock.cabal
@@ -116,6 +116,7 @@ executable haddock
Haddock.InterfaceFile
Haddock.Options
Haddock.GhcUtils
+ Haddock.Syb
Haddock.Convert
else
build-depends: haddock-api == 2.16.*
--
cgit v1.2.3
From e4740dd872d1110247557eb7b20124c22e427789 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 10 Jul 2015 16:23:15 +0200
Subject: Create simple utility module for type specialization.
---
haddock-api/haddock-api.cabal | 1 +
.../src/Haddock/Backends/Xhtml/Specialize.hs | 21 +++++++++++++++++++++
haddock.cabal | 1 +
3 files changed, 23 insertions(+)
create mode 100644 haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/haddock-api.cabal b/haddock-api/haddock-api.cabal
index bfdb2179..abe5adbe 100644
--- a/haddock-api/haddock-api.cabal
+++ b/haddock-api/haddock-api.cabal
@@ -75,6 +75,7 @@ library
Haddock.Backends.Xhtml.DocMarkup
Haddock.Backends.Xhtml.Layout
Haddock.Backends.Xhtml.Names
+ Haddock.Backends.Xhtml.Specialize
Haddock.Backends.Xhtml.Themes
Haddock.Backends.Xhtml.Types
Haddock.Backends.Xhtml.Utils
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
new file mode 100644
index 00000000..a0d64c0f
--- /dev/null
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -0,0 +1,21 @@
+{-# LANGUAGE FlexibleContexts #-}
+
+
+module Haddock.Backends.Xhtml.Specialize (specialize) where
+
+
+import Haddock.Syb
+
+import GHC
+
+import Data.Data
+
+
+specialize :: (Eq name, Data (HsType name))
+ => name -> HsType name -> HsType name -> HsType name
+specialize name details = everywhere (mkT $ specialize' name details)
+
+specialize' :: Eq name => name -> HsType name -> HsType name -> HsType name
+specialize' name details (HsTyVar name') | name == name' = details
+specialize' _ _ typ = typ
+
\ No newline at end of file
diff --git a/haddock.cabal b/haddock.cabal
index b0c6c34f..4ea2a82a 100644
--- a/haddock.cabal
+++ b/haddock.cabal
@@ -97,6 +97,7 @@ executable haddock
Haddock.Backends.Xhtml.DocMarkup
Haddock.Backends.Xhtml.Layout
Haddock.Backends.Xhtml.Names
+ Haddock.Backends.Xhtml.Specialize
Haddock.Backends.Xhtml.Themes
Haddock.Backends.Xhtml.Types
Haddock.Backends.Xhtml.Utils
--
cgit v1.2.3
From 79f475a93be3de376bc264c53d2ba8eb61ffea42 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 10 Jul 2015 16:30:11 +0200
Subject: Make type of type specialization function more general.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index a0d64c0f..e8e80da1 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -11,11 +11,11 @@ import GHC
import Data.Data
-specialize :: (Eq name, Data (HsType name))
- => name -> HsType name -> HsType name -> HsType name
+specialize :: (Eq name, Typeable name)
+ => Data a
+ => name -> HsType name -> a -> a
specialize name details = everywhere (mkT $ specialize' name details)
specialize' :: Eq name => name -> HsType name -> HsType name -> HsType name
specialize' name details (HsTyVar name') | name == name' = details
specialize' _ _ typ = typ
-
\ No newline at end of file
--
cgit v1.2.3
From 92f0b1eacb2e1169dedd22df26976219c3fbc637 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 14 Jul 2015 18:03:58 +0200
Subject: Make HTML class instance printer take optional signature argument.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 23 ++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 5f6f60eb..a5f3676e 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -268,7 +268,7 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
= subEquations qual $ map (ppTyFamEqn . unLoc) eqns
| otherwise
- = ppInstances links instances docname unicode qual
+ = ppInstances links instances Nothing docname unicode qual
-- Individual equation of a closed type family
ppTyFamEqn TyFamEqn { tfe_tycon = n, tfe_rhs = rhs
@@ -439,6 +439,8 @@ ppClassDecl summary links instances fixities loc d subdocs
| otherwise = classheader +++ docSection Nothing qual d
+++ minimalBit +++ atBit +++ methodBit +++ instancesBit
where
+ sigs = map unLoc lsigs
+
classheader
| any isVanillaLSig lsigs = topDeclElem links loc splice [nm] (hdr unicode qual <+> keyword "where" <+> fixs)
| otherwise = topDeclElem links loc splice [nm] (hdr unicode qual <+> fixs)
@@ -458,7 +460,7 @@ ppClassDecl summary links instances fixities loc d subdocs
subfixs = [ f | f@(n',_) <- fixities, n == n' ] ]
methodBit = subMethods [ ppFunSig summary links loc doc names typ subfixs splice unicode qual
- | L _ (TypeSig lnames (L _ typ) _) <- lsigs
+ | TypeSig lnames (L _ typ) _ <- sigs
, let doc = lookupAnySubdoc (head names) subdocs
subfixs = [ f | n <- names
, f@(n',_) <- fixities
@@ -468,15 +470,15 @@ ppClassDecl summary links instances fixities loc d subdocs
-- there are different subdocs for different names in a single
-- type signature?
- minimalBit = case [ s | L _ (MinimalSig _ s) <- lsigs ] of
+ minimalBit = case [ s | MinimalSig _ s <- sigs ] of
-- Miminal complete definition = every shown method
And xs : _ | sort [getName n | Var (L _ n) <- xs] ==
- sort [getName n | L _ (TypeSig ns _ _) <- lsigs, L _ n <- ns]
+ sort [getName n | TypeSig ns _ _ <- sigs, L _ n <- ns]
-> noHtml
-- Minimal complete definition = the only shown method
Var (L _ n) : _ | [getName n] ==
- [getName n' | L _ (TypeSig ns _ _) <- lsigs, L _ n' <- ns]
+ [getName n' | TypeSig ns _ _ <- sigs, L _ n' <- ns]
-> noHtml
-- Minimal complete definition = nothing
@@ -490,13 +492,16 @@ ppClassDecl summary links instances fixities loc d subdocs
ppMinimal p (Or fs) = wrap $ foldr1 (\a b -> a+++" | "+++b) $ map (ppMinimal False) fs
where wrap | p = parens | otherwise = id
- instancesBit = ppInstances links instances nm unicode qual
+ instancesBit = ppInstances links instances (Just sigs) nm unicode qual
ppClassDecl _ _ _ _ _ _ _ _ _ _ _ = error "declaration type not supported by ppShortClassDecl"
-ppInstances :: LinksInfo -> [DocInstance DocName] -> DocName -> Unicode -> Qualification -> Html
-ppInstances links instances baseName unicode qual
+ppInstances :: LinksInfo
+ -> [DocInstance DocName] -> Maybe [Sig DocName] -> DocName
+ -> Unicode -> Qualification
+ -> Html
+ppInstances links instances _ baseName unicode qual
= subInstances qual instName links True (map instDecl instances)
-- force Splice = True to use line URLs
where
@@ -581,7 +586,7 @@ ppDataDecl summary links instances fixities subdocs loc doc dataDecl
(map unLoc (con_names (unLoc c)))) fixities
]
- instancesBit = ppInstances links instances docname unicode qual
+ instancesBit = ppInstances links instances Nothing docname unicode qual
--
cgit v1.2.3
From 2070c0fa9354365e3e672f5cbee2e04d0ef1fd02 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 14 Jul 2015 19:59:08 +0200
Subject: Refactor instance head type to record instead of a meaningless tuple.
---
haddock-api/src/Haddock/Backends/LaTeX.hs | 14 ++++----
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 21 ++++++++----
haddock-api/src/Haddock/Convert.hs | 38 +++++++++++++---------
.../src/Haddock/Interface/AttachInstances.hs | 2 +-
haddock-api/src/Haddock/Interface/Rename.hs | 19 +++++++----
haddock-api/src/Haddock/Types.hs | 7 +++-
6 files changed, 63 insertions(+), 38 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/LaTeX.hs b/haddock-api/src/Haddock/Backends/LaTeX.hs
index 2febd5ae..59e5af3e 100644
--- a/haddock-api/src/Haddock/Backends/LaTeX.hs
+++ b/haddock-api/src/Haddock/Backends/LaTeX.hs
@@ -1,4 +1,5 @@
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
+{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module : Haddock.Backends.LaTeX
@@ -560,12 +561,13 @@ ppInstDecl unicode instHead = keyword "instance" <+> ppInstHead unicode instHead
ppInstHead :: Bool -> InstHead DocName -> LaTeX
-ppInstHead unicode (n, ks, ts, ClassInst ctx) = ppContextNoLocs ctx unicode <+> ppAppNameTypes n ks ts unicode
-ppInstHead unicode (n, ks, ts, TypeInst rhs) = keyword "type"
- <+> ppAppNameTypes n ks ts unicode
- <+> maybe empty (\t -> equals <+> ppType unicode t) rhs
-ppInstHead _unicode (_n, _ks, _ts, DataInst _dd) =
- error "data instances not supported by --latex yet"
+ppInstHead unicode (InstHead {..}) = case ihdInstType of
+ ClassInst ctx -> ppContextNoLocs ctx unicode <+> typ
+ TypeInst rhs -> keyword "type" <+> typ <+> tibody rhs
+ DataInst _ -> error "data instances not supported by --latex yet"
+ where
+ typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode
+ tibody = maybe empty (\t -> equals <+> ppType unicode t)
lookupAnySubdoc :: (Eq name1) =>
name1 -> [(name1, DocForDecl name2)] -> DocForDecl name2
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index a5f3676e..afbbaad1 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE TransformListComp #-}
+{-# LANGUAGE RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module : Haddock.Backends.Html.Decl
@@ -507,15 +508,21 @@ ppInstances links instances _ baseName unicode qual
where
instName = getOccString $ getName baseName
instDecl :: DocInstance DocName -> (SubDecl,Located DocName)
- instDecl (inst, maybeDoc,l) = ((instHead inst, maybeDoc, []),l)
- instHead (n, ks, ts, ClassInst cs) = ppContextNoLocs cs unicode qual
- <+> ppAppNameTypes n ks ts unicode qual
- instHead (n, ks, ts, TypeInst rhs) = keyword "type"
- <+> ppAppNameTypes n ks ts unicode qual
+ instDecl (inst, maybeDoc,l) =
+ ((ppInstHead links unicode qual inst, maybeDoc, []),l)
+
+ppInstHead :: LinksInfo -> Unicode -> Qualification
+ -> InstHead DocName
+ -> Html
+ppInstHead _ unicode qual (InstHead {..}) = case ihdInstType of
+ ClassInst cs -> ppContextNoLocs cs unicode qual <+> typ
+ TypeInst rhs -> keyword "type" <+> typ
<+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
- instHead (n, ks, ts, DataInst dd) = keyword "data"
- <+> ppAppNameTypes n ks ts unicode qual
+ DataInst dd -> keyword "data" <+> typ
<+> ppShortDataDecl False True dd unicode qual
+ where
+ typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode qual
+
lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
lookupAnySubdoc n = fromMaybe noDocForDecl . lookup n
diff --git a/haddock-api/src/Haddock/Convert.hs b/haddock-api/src/Haddock/Convert.hs
index 5cbf5f97..e51d9df7 100644
--- a/haddock-api/src/Haddock/Convert.hs
+++ b/haddock-api/src/Haddock/Convert.hs
@@ -390,23 +390,29 @@ synifyKindSig :: Kind -> LHsKind Name
synifyKindSig k = synifyType WithinType k
synifyInstHead :: ([TyVar], [PredType], Class, [Type]) -> InstHead Name
-synifyInstHead (_, preds, cls, types) =
- ( getName cls
- , map (unLoc . synifyType WithinType) ks
- , map (unLoc . synifyType WithinType) ts
- , ClassInst $ map (unLoc . synifyType WithinType) preds
- )
+synifyInstHead (_, preds, cls, types) = InstHead
+ { ihdClsName = getName cls
+ , ihdKinds = map (unLoc . synifyType WithinType) ks
+ , ihdTypes = map (unLoc . synifyType WithinType) ts
+ , ihdInstType = ClassInst $ map (unLoc . synifyType WithinType) preds
+ }
where (ks,ts) = break (not . isKind) types
-- Convert a family instance, this could be a type family or data family
synifyFamInst :: FamInst -> Bool -> Either ErrMsg (InstHead Name)
-synifyFamInst fi opaque =
- let fff = case fi_flavor fi of
- SynFamilyInst | opaque -> return $ TypeInst Nothing
- SynFamilyInst ->
- return . TypeInst . Just . unLoc . synifyType WithinType $ fi_rhs fi
- DataFamilyInst c ->
- synifyTyCon (Just $ famInstAxiom fi) c >>= return . DataInst
- in fff >>= \f' -> return (fi_fam fi , map (unLoc . synifyType WithinType) ks,
- map (unLoc . synifyType WithinType) ts , f')
- where (ks,ts) = break (not . isKind) $ fi_tys fi
+synifyFamInst fi opaque = do
+ ityp' <- ityp $ fi_flavor fi
+ return InstHead
+ { ihdClsName = fi_fam fi
+ , ihdKinds = synifyTypes ks
+ , ihdTypes = synifyTypes ts
+ , ihdInstType = ityp'
+ }
+ where
+ ityp SynFamilyInst | opaque = return $ TypeInst Nothing
+ ityp SynFamilyInst =
+ return . TypeInst . Just . unLoc . synifyType WithinType $ fi_rhs fi
+ ityp (DataFamilyInst c) =
+ DataInst <$> synifyTyCon (Just $ famInstAxiom fi) c
+ (ks,ts) = break (not . isKind) $ fi_tys fi
+ synifyTypes = map (unLoc. synifyType WithinType)
diff --git a/haddock-api/src/Haddock/Interface/AttachInstances.hs b/haddock-api/src/Haddock/Interface/AttachInstances.hs
index fc530507..e2fd24ee 100644
--- a/haddock-api/src/Haddock/Interface/AttachInstances.hs
+++ b/haddock-api/src/Haddock/Interface/AttachInstances.hs
@@ -108,7 +108,7 @@ attachToExportItem expInfo iface ifaceMap instIfaceMap export =
attachFixities e = e
-- spanName: attach the location to the name that is the same file as the instance location
- spanName s (clsn,_,_,_) (L instL instn) =
+ spanName s (InstHead { ihdClsName = clsn }) (L instL instn) =
let s1 = getSrcSpan s
sn = if srcSpanFileName_maybe s1 == srcSpanFileName_maybe instL
then instn
diff --git a/haddock-api/src/Haddock/Interface/Rename.hs b/haddock-api/src/Haddock/Interface/Rename.hs
index 1a559764..d222c6d2 100644
--- a/haddock-api/src/Haddock/Interface/Rename.hs
+++ b/haddock-api/src/Haddock/Interface/Rename.hs
@@ -1,4 +1,5 @@
{-# LANGUAGE TypeFamilies #-}
+{-# LANGUAGE RecordWildCards #-}
----------------------------------------------------------------------------
-- |
-- Module : Haddock.Interface.Rename
@@ -261,16 +262,20 @@ renameLContext (L loc context) = do
return (L loc context')
renameInstHead :: InstHead Name -> RnM (InstHead DocName)
-renameInstHead (className, k, types, rest) = do
- className' <- rename className
- k' <- mapM renameType k
- types' <- mapM renameType types
- rest' <- case rest of
+renameInstHead InstHead {..} = do
+ cname <- rename ihdClsName
+ kinds <- mapM renameType ihdKinds
+ types <- mapM renameType ihdTypes
+ itype <- case ihdInstType of
ClassInst cs -> ClassInst <$> mapM renameType cs
TypeInst ts -> TypeInst <$> traverse renameType ts
DataInst dd -> DataInst <$> renameTyClD dd
- return (className', k', types', rest')
-
+ return InstHead
+ { ihdClsName = cname
+ , ihdKinds = kinds
+ , ihdTypes = types
+ , ihdInstType = itype
+ }
renameLDecl :: LHsDecl Name -> RnM (LHsDecl DocName)
renameLDecl (L loc d) = return . L loc =<< renameDecl d
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index 6dd64506..d9ae6cab 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -308,7 +308,12 @@ type DocInstance name = (InstHead name, Maybe (MDoc name), Located name)
-- | The head of an instance. Consists of a class name, a list of kind
-- parameters, a list of type parameters and an instance type
-type InstHead name = (name, [HsType name], [HsType name], InstType name)
+data InstHead name = InstHead
+ { ihdClsName :: name
+ , ihdKinds :: [HsType name]
+ , ihdTypes :: [HsType name]
+ , ihdInstType :: InstType name
+ }
-----------------------------------------------------------------------------
-- * Documentation comments
--
cgit v1.2.3
From 05f35d7defbf702e27211628e26a738fa97ecde8 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 14:27:28 +0200
Subject: Add expandable method section for each class instance declaration.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 52 ++++++++++++++++--------
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 16 +++++++-
2 files changed, 50 insertions(+), 18 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index afbbaad1..22b34228 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -269,7 +269,7 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
= subEquations qual $ map (ppTyFamEqn . unLoc) eqns
| otherwise
- = ppInstances links instances Nothing docname unicode qual
+ = ppInstances links instances Nothing docname splice unicode qual
-- Individual equation of a closed type family
ppTyFamEqn TyFamEqn { tfe_tycon = n, tfe_rhs = rhs
@@ -493,37 +493,54 @@ ppClassDecl summary links instances fixities loc d subdocs
ppMinimal p (Or fs) = wrap $ foldr1 (\a b -> a+++" | "+++b) $ map (ppMinimal False) fs
where wrap | p = parens | otherwise = id
- instancesBit = ppInstances links instances (Just sigs) nm unicode qual
+ instancesBit = ppInstances links instances (Just sigs) nm splice unicode qual
ppClassDecl _ _ _ _ _ _ _ _ _ _ _ = error "declaration type not supported by ppShortClassDecl"
ppInstances :: LinksInfo
-> [DocInstance DocName] -> Maybe [Sig DocName] -> DocName
- -> Unicode -> Qualification
+ -> Splice -> Unicode -> Qualification
-> Html
-ppInstances links instances _ baseName unicode qual
- = subInstances qual instName links True (map instDecl instances)
+ppInstances links instances msigs baseName splice unicode qual
+ = subInstances qual instName links True (zipWith instDecl [1..] instances)
-- force Splice = True to use line URLs
where
instName = getOccString $ getName baseName
- instDecl :: DocInstance DocName -> (SubDecl,Located DocName)
- instDecl (inst, maybeDoc,l) =
- ((ppInstHead links unicode qual inst, maybeDoc, []),l)
+ instDecl :: Int -> DocInstance DocName -> (SubDecl,Located DocName)
+ instDecl iid (inst, maybeDoc,l) =
+ ((ppInstHead links splice unicode qual msigs iid inst, maybeDoc, []),l)
-ppInstHead :: LinksInfo -> Unicode -> Qualification
- -> InstHead DocName
+
+ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> Maybe [Sig DocName] -> Int -> InstHead DocName
-> Html
-ppInstHead _ unicode qual (InstHead {..}) = case ihdInstType of
- ClassInst cs -> ppContextNoLocs cs unicode qual <+> typ
- TypeInst rhs -> keyword "type" <+> typ
- <+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
- DataInst dd -> keyword "data" <+> typ
- <+> ppShortDataDecl False True dd unicode qual
+ppInstHead links splice unicode qual msigs iid (InstHead {..}) =
+ case ihdInstType of
+ ClassInst cs | Just sigs <- msigs ->
+ subClsInstance (nameStr ++ "-" ++ show iid) hdr (mets sigs)
+ where
+ hdr = ppContextNoLocs cs unicode qual <+> typ
+ mets = ppInstanceSigs links splice unicode qual
+ nameStr = occNameString . nameOccName $ getName ihdClsName
+ ClassInst cs -> ppContextNoLocs cs unicode qual <+> typ
+ TypeInst rhs -> keyword "type" <+> typ
+ <+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
+ DataInst dd -> keyword "data" <+> typ
+ <+> ppShortDataDecl False True dd unicode qual
where
typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode qual
+ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> [Sig DocName]
+ -> [Html]
+ppInstanceSigs links splice unicode qual sigs = do
+ TypeSig lnames (L sspan typ) _ <- sigs
+ let names = map unLoc lnames
+ return $ ppFunSig False links sspan noDocForDecl names typ [] splice unicode qual
+
+
lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
lookupAnySubdoc n = fromMaybe noDocForDecl . lookup n
@@ -593,7 +610,8 @@ ppDataDecl summary links instances fixities subdocs loc doc dataDecl
(map unLoc (con_names (unLoc c)))) fixities
]
- instancesBit = ppInstances links instances Nothing docname unicode qual
+ instancesBit = ppInstances links instances Nothing docname
+ splice unicode qual
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 4714c1b6..188b4243 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -31,7 +31,7 @@ module Haddock.Backends.Xhtml.Layout (
subConstructors,
subEquations,
subFields,
- subInstances,
+ subInstances, subClsInstance,
subMethods,
subMinimal,
@@ -200,6 +200,20 @@ subInstances qual nm lnks splice = maybe noHtml wrap . instTable
subCaption = paragraph ! collapseControl id_ True "caption" << "Instances"
id_ = makeAnchorId $ "i:" ++ nm
+
+-- | Generate class instance div with specialized methods.
+subClsInstance :: String -- ^ Section unique id
+ -> Html -- ^ Header contents (instance name and type)
+ -> [Html] -- ^ Method contents (pretty-printed signatures)
+ -> Html
+subClsInstance sid hdr mets =
+ hdrDiv <+> methodDiv
+ where
+ anchorId = makeAnchorId $ "i:" ++ sid
+ hdrDiv = thediv ! collapseControl anchorId False "instance" << hdr
+ methodDiv = thediv ! collapseSection anchorId False [] << subMethods mets
+
+
subMethods :: [Html] -> Html
subMethods = divSubDecls "methods" "Methods" . subBlock
--
cgit v1.2.3
From 6fc527b41b1ba80c706a375420f40e6eed8c81c8 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 18:25:36 +0200
Subject: Create convenience functions for type specialization module.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 31 ++++++++++++++++++----
1 file changed, 26 insertions(+), 5 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index e8e80da1..fa5ba536 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -1,7 +1,10 @@
{-# LANGUAGE FlexibleContexts #-}
-module Haddock.Backends.Xhtml.Specialize (specialize) where
+module Haddock.Backends.Xhtml.Specialize
+ ( specialize, specialize'
+ , specializeTyVarBndrs
+ ) where
import Haddock.Syb
@@ -14,8 +17,26 @@ import Data.Data
specialize :: (Eq name, Typeable name)
=> Data a
=> name -> HsType name -> a -> a
-specialize name details = everywhere (mkT $ specialize' name details)
+specialize name details = everywhere (mkT $ specializeStep name details)
-specialize' :: Eq name => name -> HsType name -> HsType name -> HsType name
-specialize' name details (HsTyVar name') | name == name' = details
-specialize' _ _ typ = typ
+
+specialize' :: (Eq name, Typeable name)
+ => Data a
+ => [(name, HsType name)] -> a -> a
+specialize' = flip $ foldr (uncurry specialize)
+
+
+specializeStep :: Eq name => name -> HsType name -> HsType name -> HsType name
+specializeStep name details (HsTyVar name') | name == name' = details
+specializeStep _ _ typ = typ
+
+
+specializeTyVarBndrs :: (Eq name, Typeable name, DataId name)
+ => LHsTyVarBndrs name -> [HsType name]
+ -> HsType name -> HsType name
+specializeTyVarBndrs bndrs typs =
+ specialize' $ zip bndrs' typs
+ where
+ bndrs' = map (bname . unLoc) . hsq_tvs $ bndrs
+ bname (UserTyVar name) = name
+ bname (KindedTyVar (L _ name) _) = name
--
cgit v1.2.3
From d6741ee8d407a8ac3c16e5bbddb657cab442a14c Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 18:28:17 +0200
Subject: Hook type specialization logic with HTML pretty-printer.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 28 +++++++++++++++-----------
haddock-api/src/Haddock/Types.hs | 6 ++++++
2 files changed, 22 insertions(+), 12 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 22b34228..2a820531 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -22,6 +22,7 @@ module Haddock.Backends.Xhtml.Decl (
import Haddock.Backends.Xhtml.DocMarkup
import Haddock.Backends.Xhtml.Layout
import Haddock.Backends.Xhtml.Names
+import Haddock.Backends.Xhtml.Specialize
import Haddock.Backends.Xhtml.Types
import Haddock.Backends.Xhtml.Utils
import Haddock.GhcUtils
@@ -493,32 +494,33 @@ ppClassDecl summary links instances fixities loc d subdocs
ppMinimal p (Or fs) = wrap $ foldr1 (\a b -> a+++" | "+++b) $ map (ppMinimal False) fs
where wrap | p = parens | otherwise = id
- instancesBit = ppInstances links instances (Just sigs) nm splice unicode qual
+ instSpec = Just $ InstSpec { ispecSigs = sigs, ispecTyVars = ltyvars }
+ instancesBit = ppInstances links instances instSpec nm splice unicode qual
ppClassDecl _ _ _ _ _ _ _ _ _ _ _ = error "declaration type not supported by ppShortClassDecl"
ppInstances :: LinksInfo
- -> [DocInstance DocName] -> Maybe [Sig DocName] -> DocName
+ -> [DocInstance DocName] -> Maybe (InstSpec DocName) -> DocName
-> Splice -> Unicode -> Qualification
-> Html
-ppInstances links instances msigs baseName splice unicode qual
+ppInstances links instances mspec baseName splice unicode qual
= subInstances qual instName links True (zipWith instDecl [1..] instances)
-- force Splice = True to use line URLs
where
instName = getOccString $ getName baseName
instDecl :: Int -> DocInstance DocName -> (SubDecl,Located DocName)
instDecl iid (inst, maybeDoc,l) =
- ((ppInstHead links splice unicode qual msigs iid inst, maybeDoc, []),l)
+ ((ppInstHead links splice unicode qual iid mspec inst, maybeDoc, []),l)
ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
- -> Maybe [Sig DocName] -> Int -> InstHead DocName
+ -> Int -> Maybe (InstSpec DocName) -> InstHead DocName
-> Html
-ppInstHead links splice unicode qual msigs iid (InstHead {..}) =
+ppInstHead links splice unicode qual iid mspec ihead@(InstHead {..}) =
case ihdInstType of
- ClassInst cs | Just sigs <- msigs ->
- subClsInstance (nameStr ++ "-" ++ show iid) hdr (mets sigs)
+ ClassInst cs | Just spec <- mspec ->
+ subClsInstance (nameStr ++ "-" ++ show iid) hdr (mets spec ihead)
where
hdr = ppContextNoLocs cs unicode qual <+> typ
mets = ppInstanceSigs links splice unicode qual
@@ -533,12 +535,14 @@ ppInstHead links splice unicode qual msigs iid (InstHead {..}) =
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
- -> [Sig DocName]
+ -> InstSpec DocName -> InstHead DocName
-> [Html]
-ppInstanceSigs links splice unicode qual sigs = do
- TypeSig lnames (L sspan typ) _ <- sigs
+ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
+ TypeSig lnames (L sspan typ) _ <- ispecSigs
let names = map unLoc lnames
- return $ ppFunSig False links sspan noDocForDecl names typ [] splice unicode qual
+ let typ' = specializeTyVarBndrs ispecTyVars ihdTypes typ
+ return $ ppFunSig False links sspan noDocForDecl names typ' []
+ splice unicode qual
lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index 5a03af66..76164b5e 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -328,6 +328,12 @@ data InstHead name = InstHead
, ihdInstType :: InstType name
}
+-- | Instance details used for printing specialized method signatures.
+data InstSpec name = InstSpec
+ { ispecTyVars :: LHsTyVarBndrs name
+ , ispecSigs :: [Sig name]
+ }
+
-----------------------------------------------------------------------------
-- * Documentation comments
-----------------------------------------------------------------------------
--
cgit v1.2.3
From 1680145961545a3f2c2e184c2a5a661fb748d5a1 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 18:42:17 +0200
Subject: Create stub functions for sugaring specialized types.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 2 +-
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 2a820531..f54b7c22 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -540,7 +540,7 @@ ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
TypeSig lnames (L sspan typ) _ <- ispecSigs
let names = map unLoc lnames
- let typ' = specializeTyVarBndrs ispecTyVars ihdTypes typ
+ let typ' = sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
return $ ppFunSig False links sspan noDocForDecl names typ' []
splice unicode qual
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index fa5ba536..c10c7e6e 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -4,6 +4,7 @@
module Haddock.Backends.Xhtml.Specialize
( specialize, specialize'
, specializeTyVarBndrs
+ , sugar
) where
@@ -40,3 +41,15 @@ specializeTyVarBndrs bndrs typs =
bndrs' = map (bname . unLoc) . hsq_tvs $ bndrs
bname (UserTyVar name) = name
bname (KindedTyVar (L _ name) _) = name
+
+
+sugar :: HsType name -> HsType name
+sugar = sugarTuples . sugarLists
+
+
+sugarLists :: HsType name -> HsType name
+sugarLists = id
+
+
+sugarTuples :: HsType name -> HsType name
+sugarTuples = id
--
cgit v1.2.3
From 3f31e24656d42bc6c50f441e055b7ecc4fdec8d0 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 19:31:15 +0200
Subject: Implement list syntax sugaring logic for specialized types.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 19 ++++++++++++++++---
1 file changed, 16 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index c10c7e6e..30501a13 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -1,4 +1,6 @@
{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE Rank2Types #-}
+{-# LANGUAGE ScopedTypeVariables #-}
module Haddock.Backends.Xhtml.Specialize
@@ -11,6 +13,7 @@ module Haddock.Backends.Xhtml.Specialize
import Haddock.Syb
import GHC
+import Name
import Data.Data
@@ -43,12 +46,22 @@ specializeTyVarBndrs bndrs typs =
bname (KindedTyVar (L _ name) _) = name
-sugar :: HsType name -> HsType name
+sugar :: (NamedThing name, DataId name) => HsType name -> HsType name
sugar = sugarTuples . sugarLists
-sugarLists :: HsType name -> HsType name
-sugarLists = id
+sugarLists :: forall name. (NamedThing name, DataId name)
+ => HsType name -> HsType name
+sugarLists = everywhere $ mkT (sugarListsStep :: HsType name -> HsType name)
+
+
+sugarListsStep :: NamedThing name => HsType name -> HsType name
+sugarListsStep (HsAppTy (L _ (HsTyVar name)) ltyp)
+ | isBuiltInSyntax name' && strName == "[]" = HsListTy ltyp
+ where
+ name' = getName name
+ strName = occNameString . nameOccName $ name'
+sugarListsStep typ = typ
sugarTuples :: HsType name -> HsType name
--
cgit v1.2.3
From 060b986c641cd496395b2d13dc316fc84462a7a4 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 20:25:41 +0200
Subject: Implement tuple syntax sugaring logic for specialized types.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 36 ++++++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 30501a13..a2cb8799 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -15,6 +15,7 @@ import Haddock.Syb
import GHC
import Name
+import Control.Monad
import Data.Data
@@ -64,5 +65,36 @@ sugarListsStep (HsAppTy (L _ (HsTyVar name)) ltyp)
sugarListsStep typ = typ
-sugarTuples :: HsType name -> HsType name
-sugarTuples = id
+sugarTuples :: forall name. (NamedThing name, DataId name)
+ => HsType name -> HsType name
+sugarTuples = everywhere $
+ mkT (sugarTuplesStep :: HsType name -> HsType name)
+
+
+sugarTuplesStep :: NamedThing name => HsType name -> HsType name
+sugarTuplesStep typ =
+ aux [] typ
+ where
+ aux apps (HsAppTy (L _ ftyp) atyp) = aux (atyp:apps) ftyp
+ aux apps (HsParTy (L _ typ')) = aux apps typ'
+ aux apps (HsTyVar name)
+ | isBuiltInSyntax name' && suitable = HsTupleTy HsBoxedTuple apps
+ where
+ name' = getName name
+ strName = occNameString . nameOccName $ name'
+ suitable = case parseTupleArity strName of
+ Just arity -> arity == length apps
+ Nothing -> False
+ aux _ _ = typ
+
+
+parseTupleArity :: String -> Maybe Int
+parseTupleArity ('(':commas) = do
+ n <- parseCommas commas
+ guard $ n /= 0
+ return $ n + 1
+ where
+ parseCommas (',':rest) = (+ 1) <$> parseCommas rest
+ parseCommas ")" = Just 0
+ parseCommas _ = Nothing
+parseTupleArity _ = Nothing
--
cgit v1.2.3
From 6be6ca76e9163569ad3bed1441cb9fcfa7df0cec Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 15 Jul 2015 20:31:48 +0200
Subject: Get rid of code duplication in type specialization module.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 30 +++++++++-------------
1 file changed, 12 insertions(+), 18 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index a2cb8799..50cce3d5 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -47,32 +47,26 @@ specializeTyVarBndrs bndrs typs =
bname (KindedTyVar (L _ name) _) = name
-sugar :: (NamedThing name, DataId name) => HsType name -> HsType name
-sugar = sugarTuples . sugarLists
-
-
-sugarLists :: forall name. (NamedThing name, DataId name)
- => HsType name -> HsType name
-sugarLists = everywhere $ mkT (sugarListsStep :: HsType name -> HsType name)
+sugar :: forall name. (NamedThing name, DataId name)
+ => HsType name -> HsType name
+sugar =
+ everywhere $ mkT step
+ where
+ step :: HsType name -> HsType name
+ step = sugarTuples . sugarLists
-sugarListsStep :: NamedThing name => HsType name -> HsType name
-sugarListsStep (HsAppTy (L _ (HsTyVar name)) ltyp)
+sugarLists :: NamedThing name => HsType name -> HsType name
+sugarLists (HsAppTy (L _ (HsTyVar name)) ltyp)
| isBuiltInSyntax name' && strName == "[]" = HsListTy ltyp
where
name' = getName name
strName = occNameString . nameOccName $ name'
-sugarListsStep typ = typ
-
-
-sugarTuples :: forall name. (NamedThing name, DataId name)
- => HsType name -> HsType name
-sugarTuples = everywhere $
- mkT (sugarTuplesStep :: HsType name -> HsType name)
+sugarLists typ = typ
-sugarTuplesStep :: NamedThing name => HsType name -> HsType name
-sugarTuplesStep typ =
+sugarTuples :: NamedThing name => HsType name -> HsType name
+sugarTuples typ =
aux [] typ
where
aux apps (HsAppTy (L _ ftyp) atyp) = aux (atyp:apps) ftyp
--
cgit v1.2.3
From 9edfaa67b6e4c80df43497f24133530d9a822dc6 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 17 Jul 2015 13:57:28 +0200
Subject: Create scaffolding of a framework for renaming specialized types.
---
haddock-api/haddock-api.cabal | 1 +
.../src/Haddock/Backends/Xhtml/Specialize.hs | 97 +++++++++++++++++++++-
2 files changed, 97 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/haddock-api.cabal b/haddock-api/haddock-api.cabal
index abe5adbe..2090c53e 100644
--- a/haddock-api/haddock-api.cabal
+++ b/haddock-api/haddock-api.cabal
@@ -43,6 +43,7 @@ library
, filepath
, directory
, containers
+ , transformers
, deepseq
, array
, xhtml >= 3000.2 && < 3000.3
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 50cce3d5..1a8446ee 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -6,7 +6,7 @@
module Haddock.Backends.Xhtml.Specialize
( specialize, specialize'
, specializeTyVarBndrs
- , sugar
+ , sugar, rename
) where
@@ -16,7 +16,13 @@ import GHC
import Name
import Control.Monad
+import Control.Monad.Trans.RWS
+
import Data.Data
+import Data.Map (Map)
+import qualified Data.Map as Map
+import Data.Set (Set)
+import qualified Data.Set as Set
specialize :: (Eq name, Typeable name)
@@ -92,3 +98,92 @@ parseTupleArity ('(':commas) = do
parseCommas ")" = Just 0
parseCommas _ = Nothing
parseTupleArity _ = Nothing
+
+
+rename :: Ord name => HsType name -> HsType name
+rename = fst . evalRWS undefined Map.empty . renameType -- TODO.
+
+
+type Rename name a = RWS (Set name) () (Map name name) a
+
+
+renameType :: Ord name => HsType name -> Rename name (HsType name)
+renameType (HsForAllTy ex mspan lbndrs lctx lt) = do
+ lbndrs' <- renameLTyVarBndrs lbndrs
+ HsForAllTy
+ <$> pure ex
+ <*> pure mspan
+ <*> pure lbndrs'
+ <*> located renameContext lctx
+ <*> renameLType lt
+renameType (HsTyVar name) = HsTyVar <$> renameName name
+renameType (HsAppTy lf la) = HsAppTy <$> renameLType lf <*> renameLType la
+renameType (HsFunTy la lr) = HsFunTy <$> renameLType la <*> renameLType lr
+renameType (HsListTy lt) = HsListTy <$> renameLType lt
+renameType (HsPArrTy lt) = HsPArrTy <$> renameLType lt
+renameType (HsTupleTy srt lt) = HsTupleTy srt <$> mapM renameLType lt
+renameType (HsOpTy la lop lb) = HsOpTy
+ <$> renameLType la
+ <*> pure lop -- TODO.
+ <*> renameLType lb
+renameType (HsParTy lt) = HsParTy <$> renameLType lt
+renameType (HsIParamTy ip lt) = HsIParamTy ip <$> renameLType lt
+renameType (HsEqTy la lb) = HsEqTy <$> renameLType la <*> renameLType lb
+renameType (HsKindSig lt lk) = HsKindSig <$> renameLType lt <*> pure lk
+renameType t@(HsQuasiQuoteTy _) = pure t -- TODO.
+renameType t@(HsSpliceTy _ _) = pure t -- TODO.
+renameType t@(HsDocTy _ _) = pure t -- TODO.
+renameType (HsBangTy bang lt) = HsBangTy bang <$> renameLType lt
+renameType t@(HsRecTy _) = pure t -- TODO.
+renameType t@(HsCoreTy _) = pure t
+renameType t@(HsExplicitListTy _ _) = pure t -- TODO.
+renameType t@(HsExplicitTupleTy _ _) = pure t -- TODO.
+renameType t@(HsTyLit _) = pure t
+renameType (HsWrapTy wrap t) = HsWrapTy wrap <$> renameType t
+renameType HsWildcardTy = pure HsWildcardTy
+renameType t@(HsNamedWildcardTy _) = pure t -- TODO.
+
+
+renameLType :: Ord name => LHsType name -> Rename name (LHsType name)
+renameLType = located renameType
+
+
+renameLTyVarBndrs :: Ord name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
+renameLTyVarBndrs lbndrs = do
+ tys' <- mapM (located renameTyVarBndr) $ hsq_tvs lbndrs
+ pure $ lbndrs { hsq_tvs = tys' }
+
+
+renameContext :: Ord name => HsContext name -> Rename name (HsContext name)
+renameContext = mapM $ located renameType
+
+
+renameTyVarBndr :: Ord name => HsTyVarBndr name -> Rename name (HsTyVarBndr name)
+renameTyVarBndr (UserTyVar name) =
+ UserTyVar <$> renameNameBndr name
+renameTyVarBndr (KindedTyVar name kinds) =
+ KindedTyVar <$> located renameNameBndr name <*> pure kinds
+
+
+renameNameBndr :: Ord name => name -> Rename name name
+renameNameBndr name = do
+ fv <- ask
+ when (name `Set.member` fv) $
+ freshName name
+ renameName name
+
+
+renameName :: Ord name => name -> Rename name name
+renameName name = do
+ rnmap <- get
+ pure $ case Map.lookup name rnmap of
+ Just name' -> name'
+ Nothing -> name
+
+
+freshName :: Ord name => name -> Rename name ()
+freshName _ = pure () -- TODO.
+
+
+located :: Functor f => (a -> f b) -> Located a -> f (Located b)
+located f (L loc e) = L loc <$> f e
--
cgit v1.2.3
From 51e145b013380965db7fe2a9983a3064cde57eb9 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 17 Jul 2015 18:47:03 +0200
Subject: Fill in missing cases in specialized type renaming function.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 26 +++++++++++++---------
1 file changed, 15 insertions(+), 11 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 1a8446ee..c59dd2c8 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -122,26 +122,26 @@ renameType (HsFunTy la lr) = HsFunTy <$> renameLType la <*> renameLType lr
renameType (HsListTy lt) = HsListTy <$> renameLType lt
renameType (HsPArrTy lt) = HsPArrTy <$> renameLType lt
renameType (HsTupleTy srt lt) = HsTupleTy srt <$> mapM renameLType lt
-renameType (HsOpTy la lop lb) = HsOpTy
- <$> renameLType la
- <*> pure lop -- TODO.
- <*> renameLType lb
+renameType (HsOpTy la lop lb) =
+ HsOpTy <$> renameLType la <*> renameLTyOp lop <*> renameLType lb
renameType (HsParTy lt) = HsParTy <$> renameLType lt
renameType (HsIParamTy ip lt) = HsIParamTy ip <$> renameLType lt
renameType (HsEqTy la lb) = HsEqTy <$> renameLType la <*> renameLType lb
renameType (HsKindSig lt lk) = HsKindSig <$> renameLType lt <*> pure lk
-renameType t@(HsQuasiQuoteTy _) = pure t -- TODO.
-renameType t@(HsSpliceTy _ _) = pure t -- TODO.
-renameType t@(HsDocTy _ _) = pure t -- TODO.
+renameType t@(HsQuasiQuoteTy _) = pure t
+renameType t@(HsSpliceTy _ _) = pure t
+renameType (HsDocTy lt doc) = HsDocTy <$> renameLType lt <*> pure doc
renameType (HsBangTy bang lt) = HsBangTy bang <$> renameLType lt
-renameType t@(HsRecTy _) = pure t -- TODO.
+renameType t@(HsRecTy _) = pure t
renameType t@(HsCoreTy _) = pure t
-renameType t@(HsExplicitListTy _ _) = pure t -- TODO.
-renameType t@(HsExplicitTupleTy _ _) = pure t -- TODO.
+renameType (HsExplicitListTy ph ltys) =
+ HsExplicitListTy ph <$> mapM renameLType ltys
+renameType (HsExplicitTupleTy phs ltys) =
+ HsExplicitTupleTy phs <$> mapM renameLType ltys
renameType t@(HsTyLit _) = pure t
renameType (HsWrapTy wrap t) = HsWrapTy wrap <$> renameType t
renameType HsWildcardTy = pure HsWildcardTy
-renameType t@(HsNamedWildcardTy _) = pure t -- TODO.
+renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
renameLType :: Ord name => LHsType name -> Rename name (LHsType name)
@@ -165,6 +165,10 @@ renameTyVarBndr (KindedTyVar name kinds) =
KindedTyVar <$> located renameNameBndr name <*> pure kinds
+renameLTyOp :: Ord name => LHsTyOp name -> Rename name (LHsTyOp name)
+renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
+
+
renameNameBndr :: Ord name => name -> Rename name name
renameNameBndr name = do
fv <- ask
--
cgit v1.2.3
From 1633853280a22aa1ddf682465eedaa1cf3b6905a Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 17 Jul 2015 18:51:04 +0200
Subject: Remove code duplication in specialized type renamer.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index c59dd2c8..a7b7a561 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -135,9 +135,9 @@ renameType (HsBangTy bang lt) = HsBangTy bang <$> renameLType lt
renameType t@(HsRecTy _) = pure t
renameType t@(HsCoreTy _) = pure t
renameType (HsExplicitListTy ph ltys) =
- HsExplicitListTy ph <$> mapM renameLType ltys
+ HsExplicitListTy ph <$> renameLTypes ltys
renameType (HsExplicitTupleTy phs ltys) =
- HsExplicitTupleTy phs <$> mapM renameLType ltys
+ HsExplicitTupleTy phs <$> renameLTypes ltys
renameType t@(HsTyLit _) = pure t
renameType (HsWrapTy wrap t) = HsWrapTy wrap <$> renameType t
renameType HsWildcardTy = pure HsWildcardTy
@@ -148,16 +148,20 @@ renameLType :: Ord name => LHsType name -> Rename name (LHsType name)
renameLType = located renameType
+renameLTypes :: Ord name => [LHsType name] -> Rename name [LHsType name]
+renameLTypes = mapM renameLType
+
+
+renameContext :: Ord name => HsContext name -> Rename name (HsContext name)
+renameContext = renameLTypes
+
+
renameLTyVarBndrs :: Ord name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
renameLTyVarBndrs lbndrs = do
tys' <- mapM (located renameTyVarBndr) $ hsq_tvs lbndrs
pure $ lbndrs { hsq_tvs = tys' }
-renameContext :: Ord name => HsContext name -> Rename name (HsContext name)
-renameContext = mapM $ located renameType
-
-
renameTyVarBndr :: Ord name => HsTyVarBndr name -> Rename name (HsTyVarBndr name)
renameTyVarBndr (UserTyVar name) =
UserTyVar <$> renameNameBndr name
--
cgit v1.2.3
From 4b352b94f07436d45dfcce8070c2f8301218b9ac Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sat, 18 Jul 2015 19:05:46 +0200
Subject: Change state of the type renaming monad.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 30 ++++++++++++----------
1 file changed, 16 insertions(+), 14 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index a7b7a561..d2a51fac 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -100,14 +100,14 @@ parseTupleArity ('(':commas) = do
parseTupleArity _ = Nothing
-rename :: Ord name => HsType name -> HsType name
+rename :: NamedThing name => HsType name -> HsType name
rename = fst . evalRWS undefined Map.empty . renameType -- TODO.
-type Rename name a = RWS (Set name) () (Map name name) a
+type Rename name a = RWS (Set OccName) () (Map Name name) a
-renameType :: Ord name => HsType name -> Rename name (HsType name)
+renameType :: NamedThing name => HsType name -> Rename name (HsType name)
renameType (HsForAllTy ex mspan lbndrs lctx lt) = do
lbndrs' <- renameLTyVarBndrs lbndrs
HsForAllTy
@@ -144,52 +144,54 @@ renameType HsWildcardTy = pure HsWildcardTy
renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
-renameLType :: Ord name => LHsType name -> Rename name (LHsType name)
+renameLType :: NamedThing name => LHsType name -> Rename name (LHsType name)
renameLType = located renameType
-renameLTypes :: Ord name => [LHsType name] -> Rename name [LHsType name]
+renameLTypes :: NamedThing name => [LHsType name] -> Rename name [LHsType name]
renameLTypes = mapM renameLType
-renameContext :: Ord name => HsContext name -> Rename name (HsContext name)
+renameContext :: NamedThing name => HsContext name
+ -> Rename name (HsContext name)
renameContext = renameLTypes
-renameLTyVarBndrs :: Ord name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
+renameLTyVarBndrs :: NamedThing name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
renameLTyVarBndrs lbndrs = do
tys' <- mapM (located renameTyVarBndr) $ hsq_tvs lbndrs
pure $ lbndrs { hsq_tvs = tys' }
-renameTyVarBndr :: Ord name => HsTyVarBndr name -> Rename name (HsTyVarBndr name)
+renameTyVarBndr :: NamedThing name => HsTyVarBndr name
+ -> Rename name (HsTyVarBndr name)
renameTyVarBndr (UserTyVar name) =
UserTyVar <$> renameNameBndr name
renameTyVarBndr (KindedTyVar name kinds) =
KindedTyVar <$> located renameNameBndr name <*> pure kinds
-renameLTyOp :: Ord name => LHsTyOp name -> Rename name (LHsTyOp name)
+renameLTyOp :: NamedThing name => LHsTyOp name -> Rename name (LHsTyOp name)
renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
-renameNameBndr :: Ord name => name -> Rename name name
+renameNameBndr :: NamedThing name => name -> Rename name name
renameNameBndr name = do
fv <- ask
- when (name `Set.member` fv) $
+ when (getOccName name `Set.member` fv) $
freshName name
renameName name
-renameName :: Ord name => name -> Rename name name
+renameName :: NamedThing name => name -> Rename name name
renameName name = do
rnmap <- get
- pure $ case Map.lookup name rnmap of
+ pure $ case Map.lookup (getName name) rnmap of
Just name' -> name'
Nothing -> name
-freshName :: Ord name => name -> Rename name ()
+freshName :: NamedThing name => name -> Rename name ()
freshName _ = pure () -- TODO.
--
cgit v1.2.3
From d508387d8bca5cdc7e29127ac859f170b66f81df Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sat, 18 Jul 2015 20:48:48 +0200
Subject: Implement simple mechanism for generating new type names.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 29 +++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index d2a51fac..2c4c8498 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -19,6 +19,8 @@ import Control.Monad
import Control.Monad.Trans.RWS
import Data.Data
+import qualified Data.List as List
+import Data.Maybe
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
@@ -185,14 +187,35 @@ renameNameBndr name = do
renameName :: NamedThing name => name -> Rename name name
renameName name = do
- rnmap <- get
- pure $ case Map.lookup (getName name) rnmap of
+ env <- get
+ pure $ case Map.lookup (getName name) env of
Just name' -> name'
Nothing -> name
freshName :: NamedThing name => name -> Rename name ()
-freshName _ = pure () -- TODO.
+freshName name = do
+ fv <- ask
+ env <- get
+ let taken = Set.union fv (Set.fromList . map getOccName . Map.keys $ env)
+ let name' = undefined $ findFreshName taken occ
+ put $ Map.insert (getName name) name' env
+ where
+ occ = getOccName name
+
+
+findFreshName :: Set OccName -> OccName -> OccName
+findFreshName taken =
+ fromJust . List.find isFresh . alternativeNames
+ where
+ isFresh = not . flip Set.member taken
+
+
+alternativeNames :: OccName -> [OccName]
+alternativeNames name =
+ [ mkVarOcc $ str ++ show i | i :: Int <- [0..] ]
+ where
+ str = occNameString name
located :: Functor f => (a -> f b) -> Located a -> f (Located b)
--
cgit v1.2.3
From 0495d71981573cf95f28468c7167b96ffd48ac11 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sat, 18 Jul 2015 21:02:07 +0200
Subject: Fill in stub behaviour with actual environment renaming.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 42 ++++++++++++++--------
1 file changed, 28 insertions(+), 14 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 2c4c8498..a9c46463 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -102,14 +102,27 @@ parseTupleArity ('(':commas) = do
parseTupleArity _ = Nothing
-rename :: NamedThing name => HsType name -> HsType name
+class NamedThing name => SetName name where
+
+ setName :: Name -> name -> name
+
+
+setInternalOccName :: SetName name => OccName -> name -> name
+setInternalOccName occ name =
+ setName nname' name
+ where
+ nname = getName name
+ nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
+
+
+rename :: SetName name => HsType name -> HsType name
rename = fst . evalRWS undefined Map.empty . renameType -- TODO.
type Rename name a = RWS (Set OccName) () (Map Name name) a
-renameType :: NamedThing name => HsType name -> Rename name (HsType name)
+renameType :: SetName name => HsType name -> Rename name (HsType name)
renameType (HsForAllTy ex mspan lbndrs lctx lt) = do
lbndrs' <- renameLTyVarBndrs lbndrs
HsForAllTy
@@ -146,26 +159,26 @@ renameType HsWildcardTy = pure HsWildcardTy
renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
-renameLType :: NamedThing name => LHsType name -> Rename name (LHsType name)
+renameLType :: SetName name => LHsType name -> Rename name (LHsType name)
renameLType = located renameType
-renameLTypes :: NamedThing name => [LHsType name] -> Rename name [LHsType name]
+renameLTypes :: SetName name => [LHsType name] -> Rename name [LHsType name]
renameLTypes = mapM renameLType
-renameContext :: NamedThing name => HsContext name
+renameContext :: SetName name => HsContext name
-> Rename name (HsContext name)
renameContext = renameLTypes
-renameLTyVarBndrs :: NamedThing name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
+renameLTyVarBndrs :: SetName name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
renameLTyVarBndrs lbndrs = do
tys' <- mapM (located renameTyVarBndr) $ hsq_tvs lbndrs
pure $ lbndrs { hsq_tvs = tys' }
-renameTyVarBndr :: NamedThing name => HsTyVarBndr name
+renameTyVarBndr :: SetName name => HsTyVarBndr name
-> Rename name (HsTyVarBndr name)
renameTyVarBndr (UserTyVar name) =
UserTyVar <$> renameNameBndr name
@@ -173,11 +186,11 @@ renameTyVarBndr (KindedTyVar name kinds) =
KindedTyVar <$> located renameNameBndr name <*> pure kinds
-renameLTyOp :: NamedThing name => LHsTyOp name -> Rename name (LHsTyOp name)
+renameLTyOp :: SetName name => LHsTyOp name -> Rename name (LHsTyOp name)
renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
-renameNameBndr :: NamedThing name => name -> Rename name name
+renameNameBndr :: SetName name => name -> Rename name name
renameNameBndr name = do
fv <- ask
when (getOccName name `Set.member` fv) $
@@ -185,7 +198,7 @@ renameNameBndr name = do
renameName name
-renameName :: NamedThing name => name -> Rename name name
+renameName :: SetName name => name -> Rename name name
renameName name = do
env <- get
pure $ case Map.lookup (getName name) env of
@@ -193,15 +206,16 @@ renameName name = do
Nothing -> name
-freshName :: NamedThing name => name -> Rename name ()
+freshName :: SetName name => name -> Rename name ()
freshName name = do
fv <- ask
env <- get
let taken = Set.union fv (Set.fromList . map getOccName . Map.keys $ env)
- let name' = undefined $ findFreshName taken occ
- put $ Map.insert (getName name) name' env
+ let name' = setInternalOccName (findFreshName taken occ) name
+ put $ Map.insert nname name' env
where
- occ = getOccName name
+ nname = getName name
+ occ = nameOccName nname
findFreshName :: Set OccName -> OccName -> OccName
--
cgit v1.2.3
From 2c94f5a7804ecf84f818a64ba41ca5829621323c Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sun, 19 Jul 2015 15:57:10 +0200
Subject: Fix logic behind binder type renaming.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index a9c46463..6a149719 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -193,9 +193,11 @@ renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
renameNameBndr :: SetName name => name -> Rename name name
renameNameBndr name = do
fv <- ask
- when (getOccName name `Set.member` fv) $
- freshName name
- renameName name
+ env <- get
+ case Map.lookup (getName name) env of
+ Just name' -> pure name'
+ Nothing | getOccName name `Set.member` fv -> freshName name
+ Nothing -> pure name
renameName :: SetName name => name -> Rename name name
@@ -206,13 +208,14 @@ renameName name = do
Nothing -> name
-freshName :: SetName name => name -> Rename name ()
+freshName :: SetName name => name -> Rename name name
freshName name = do
fv <- ask
env <- get
let taken = Set.union fv (Set.fromList . map getOccName . Map.keys $ env)
let name' = setInternalOccName (findFreshName taken occ) name
put $ Map.insert nname name' env
+ return name'
where
nname = getName name
occ = nameOccName nname
--
cgit v1.2.3
From 77b628b79bd179e7dde83d44bbea5acb4c0cb043 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Sun, 19 Jul 2015 20:30:06 +0200
Subject: Create function for retrieving free variables from given type.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 6a149719..7e568933 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -159,6 +159,21 @@ renameType HsWildcardTy = pure HsWildcardTy
renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
+freeVariables :: forall name. (NamedThing name, Data (HsType name))
+ => HsType name -> Set OccName
+freeVariables =
+ everythingWithState Set.empty Set.union query
+ where
+ query term ctx = case cast term :: Maybe (HsType name) of
+ Just (HsForAllTy _ _ bndrs _ _) ->
+ (Set.empty, Set.union ctx (bndrsNames bndrs))
+ Just (HsTyVar name)
+ | getName name `Set.member` ctx -> (Set.empty, ctx)
+ | otherwise -> (Set.singleton $ getOccName name, ctx)
+ _ -> (Set.empty, ctx)
+ bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
+
+
renameLType :: SetName name => LHsType name -> Rename name (LHsType name)
renameLType = located renameType
@@ -237,3 +252,8 @@ alternativeNames name =
located :: Functor f => (a -> f b) -> Located a -> f (Located b)
located f (L loc e) = L loc <$> f e
+
+
+tyVarName :: HsTyVarBndr name -> name
+tyVarName (UserTyVar name) = name
+tyVarName (KindedTyVar (L _ name) _) = name
--
cgit v1.2.3
From 532478bcee19cf465a6a0ad24e05c6a571213c05 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Mon, 20 Jul 2015 12:50:31 +0200
Subject: Fix compilation error caused by incorrect type signature.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 7e568933..37632ee7 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -159,7 +159,7 @@ renameType HsWildcardTy = pure HsWildcardTy
renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
-freeVariables :: forall name. (NamedThing name, Data (HsType name))
+freeVariables :: forall name. (NamedThing name, DataId name)
=> HsType name -> Set OccName
freeVariables =
everythingWithState Set.empty Set.union query
--
cgit v1.2.3
From 8135ce02b26385a610587abb26775055c5323b31 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Mon, 20 Jul 2015 13:52:21 +0200
Subject: Move `SetName` class definition to types module.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 6 +-----
haddock-api/src/Haddock/Types.hs | 17 +++++++++++++++++
2 files changed, 18 insertions(+), 5 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 37632ee7..4e68cb7b 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -11,6 +11,7 @@ module Haddock.Backends.Xhtml.Specialize
import Haddock.Syb
+import Haddock.Types
import GHC
import Name
@@ -102,11 +103,6 @@ parseTupleArity ('(':commas) = do
parseTupleArity _ = Nothing
-class NamedThing name => SetName name where
-
- setName :: Name -> name -> name
-
-
setInternalOccName :: SetName name => OccName -> name -> name
setInternalOccName occ name =
setName nname' name
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index 76164b5e..c5ca31c0 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -301,6 +301,23 @@ instance NamedThing DocName where
getName (Undocumented name) = name
+class NamedThing name => SetName name where
+
+ setName :: Name -> name -> name
+
+
+instance SetName Name where
+
+ setName name' _ = name'
+
+
+instance SetName DocName where
+
+ setName name' (Documented _ mdl) = Documented name' mdl
+ setName name' (Undocumented _) = Undocumented name'
+
+
+
-----------------------------------------------------------------------------
-- * Instances
-----------------------------------------------------------------------------
--
cgit v1.2.3
From 70ed9912b5400b1b2afd60cd8bd3585e3d355a5a Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Mon, 20 Jul 2015 13:59:13 +0200
Subject: Hook type renamer with instance method HTML pretty-printer.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 6 +++++-
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 5 +++--
2 files changed, 8 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index f54b7c22..176180ad 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -31,6 +31,7 @@ import Haddock.Doc (combineDocumentation)
import Data.List ( intersperse, sort )
import qualified Data.Map as Map
+import qualified Data.Set as Set
import Data.Maybe
import Text.XHtml hiding ( name, title, p, quote )
@@ -540,9 +541,12 @@ ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
TypeSig lnames (L sspan typ) _ <- ispecSigs
let names = map unLoc lnames
- let typ' = sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
+ let typ' = rename' . sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
return $ ppFunSig False links sspan noDocForDecl names typ' []
splice unicode qual
+ where
+ fv = foldr Set.union Set.empty . map freeVariables $ ihdTypes
+ rename' = rename fv
lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 4e68cb7b..3b3d95b9 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -7,6 +7,7 @@ module Haddock.Backends.Xhtml.Specialize
( specialize, specialize'
, specializeTyVarBndrs
, sugar, rename
+ , freeVariables
) where
@@ -111,8 +112,8 @@ setInternalOccName occ name =
nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
-rename :: SetName name => HsType name -> HsType name
-rename = fst . evalRWS undefined Map.empty . renameType -- TODO.
+rename :: SetName name => Set OccName -> HsType name -> HsType name
+rename fv typ = fst $ evalRWS (renameType typ) fv Map.empty
type Rename name a = RWS (Set OccName) () (Map Name name) a
--
cgit v1.2.3
From b4a82b390e3b6d7d5f1c10c42c4e36d5d7cf667b Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 21 Jul 2015 13:43:48 +0200
Subject: Make specialized signatures refer to original signature declaration.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 11 +++++++++--
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 2 +-
2 files changed, 10 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 176180ad..b3e1db81 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -223,6 +223,14 @@ ppTyName :: Name -> Html
ppTyName = ppName Prefix
+ppSimpleSig :: Unicode -> Qualification -> [DocName] -> HsType DocName -> Html
+ppSimpleSig unicode qual names typ =
+ ppTypeSig True occNames ppTyp unicode
+ where
+ ppTyp = ppType unicode qual typ
+ occNames = map getOccName names
+
+
--------------------------------------------------------------------------------
-- * Type families
--------------------------------------------------------------------------------
@@ -542,8 +550,7 @@ ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
TypeSig lnames (L sspan typ) _ <- ispecSigs
let names = map unLoc lnames
let typ' = rename' . sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
- return $ ppFunSig False links sspan noDocForDecl names typ' []
- splice unicode qual
+ return $ ppSimpleSig unicode qual names typ'
where
fv = foldr Set.union Set.empty . map freeVariables $ ihdTypes
rename' = rename fv
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 188b4243..d971b0e5 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -211,7 +211,7 @@ subClsInstance sid hdr mets =
where
anchorId = makeAnchorId $ "i:" ++ sid
hdrDiv = thediv ! collapseControl anchorId False "instance" << hdr
- methodDiv = thediv ! collapseSection anchorId False [] << subMethods mets
+ methodDiv = thediv ! collapseSection anchorId False [] << subBlock mets
subMethods :: [Html] -> Html
--
cgit v1.2.3
From 59d809746c08e3e3e506da5eeaaaed9d04407743 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 21 Jul 2015 14:36:37 +0200
Subject: Make specialized methods be nicely formatted again.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index b3e1db81..4862945a 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -223,10 +223,14 @@ ppTyName :: Name -> Html
ppTyName = ppName Prefix
-ppSimpleSig :: Unicode -> Qualification -> [DocName] -> HsType DocName -> Html
-ppSimpleSig unicode qual names typ =
- ppTypeSig True occNames ppTyp unicode
+ppSimpleSig :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> [DocName] -> HsType DocName
+ -> Html
+ppSimpleSig links splice unicode qual names typ =
+ topDeclElem' names $ ppTypeSig True occNames ppTyp unicode
where
+ -- TODO: Use *helpful* source span.
+ topDeclElem' = topDeclElem links (UnhelpfulSpan undefined) splice
ppTyp = ppType unicode qual typ
occNames = map getOccName names
@@ -550,7 +554,7 @@ ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
TypeSig lnames (L sspan typ) _ <- ispecSigs
let names = map unLoc lnames
let typ' = rename' . sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
- return $ ppSimpleSig unicode qual names typ'
+ return $ ppSimpleSig links splice unicode qual names typ'
where
fv = foldr Set.union Set.empty . map freeVariables $ ihdTypes
rename' = rename fv
--
cgit v1.2.3
From 42d49d550642aa58696af91bb250487ac42e9095 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 21 Jul 2015 14:44:22 +0200
Subject: Attach source locations to the specialized class methods.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 11 +++++------
1 file changed, 5 insertions(+), 6 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 4862945a..67405915 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -223,14 +223,13 @@ ppTyName :: Name -> Html
ppTyName = ppName Prefix
-ppSimpleSig :: LinksInfo -> Splice -> Unicode -> Qualification
+ppSimpleSig :: LinksInfo -> Splice -> Unicode -> Qualification -> SrcSpan
-> [DocName] -> HsType DocName
-> Html
-ppSimpleSig links splice unicode qual names typ =
+ppSimpleSig links splice unicode qual loc names typ =
topDeclElem' names $ ppTypeSig True occNames ppTyp unicode
where
- -- TODO: Use *helpful* source span.
- topDeclElem' = topDeclElem links (UnhelpfulSpan undefined) splice
+ topDeclElem' = topDeclElem links loc splice
ppTyp = ppType unicode qual typ
occNames = map getOccName names
@@ -551,10 +550,10 @@ ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
-> InstSpec DocName -> InstHead DocName
-> [Html]
ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
- TypeSig lnames (L sspan typ) _ <- ispecSigs
+ TypeSig lnames (L loc typ) _ <- ispecSigs
let names = map unLoc lnames
let typ' = rename' . sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
- return $ ppSimpleSig links splice unicode qual names typ'
+ return $ ppSimpleSig links splice unicode qual loc names typ'
where
fv = foldr Set.union Set.empty . map freeVariables $ ihdTypes
rename' = rename fv
--
cgit v1.2.3
From f0edea1969cdc06d0299c606debf533d7ece77f0 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 21 Jul 2015 19:22:30 +0200
Subject: Improve placement of instance methods expander button.
---
haddock-api/resources/html/Ocean.std-theme/ocean.css | 14 ++++++++++----
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 6 +++---
2 files changed, 13 insertions(+), 7 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/resources/html/Ocean.std-theme/ocean.css b/haddock-api/resources/html/Ocean.std-theme/ocean.css
index 9ad9f9d2..428040bc 100644
--- a/haddock-api/resources/html/Ocean.std-theme/ocean.css
+++ b/haddock-api/resources/html/Ocean.std-theme/ocean.css
@@ -146,15 +146,21 @@ ul.links li a {
background-image: url(plus.gif);
background-repeat: no-repeat;
}
-p.caption.collapser,
-p.caption.expander {
- background-position: 0 0.4em;
-}
.collapser, .expander {
padding-left: 14px;
margin-left: -14px;
cursor: pointer;
}
+p.caption.collapser,
+p.caption.expander {
+ background-position: 0 0.4em;
+}
+
+.instance.collapser, .instance.expander {
+ margin-left: 0px;
+ background-position: left center;
+}
+
pre {
padding: 0.25em;
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index d971b0e5..460cc6d7 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -207,11 +207,11 @@ subClsInstance :: String -- ^ Section unique id
-> [Html] -- ^ Method contents (pretty-printed signatures)
-> Html
subClsInstance sid hdr mets =
- hdrDiv <+> methodDiv
+ (hdrDiv << hdr) <+> (methodDiv << subBlock mets)
where
anchorId = makeAnchorId $ "i:" ++ sid
- hdrDiv = thediv ! collapseControl anchorId False "instance" << hdr
- methodDiv = thediv ! collapseSection anchorId False [] << subBlock mets
+ hdrDiv = thediv ! collapseControl anchorId False "instance"
+ methodDiv = thediv ! collapseSection anchorId False "methods"
subMethods :: [Html] -> Html
--
cgit v1.2.3
From aec90be7b95b2116e3c436b91b7c35aec026f6cc Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 21 Jul 2015 19:44:33 +0200
Subject: Make type renamer first try single-letter names as alternatives.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 3b3d95b9..c127ebbd 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -241,7 +241,15 @@ findFreshName taken =
alternativeNames :: OccName -> [OccName]
-alternativeNames name =
+alternativeNames name
+ | [_] <- occNameString name = letterNames ++ alternativeNames' name
+ where
+ letterNames = map (mkVarOcc . pure) ['a'..'z']
+alternativeNames name = alternativeNames' name
+
+
+alternativeNames' :: OccName -> [OccName]
+alternativeNames' name =
[ mkVarOcc $ str ++ show i | i :: Int <- [0..] ]
where
str = occNameString name
--
cgit v1.2.3
From 08e592eb55c4f4f86fe824e572517be7e44b2cb8 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 22 Jul 2015 00:09:10 +0200
Subject: Fix type renamer bug with incorrect names being generated.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 45 ++++++++++++++--------
1 file changed, 30 insertions(+), 15 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index c127ebbd..fccdaa95 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -16,6 +16,7 @@ import Haddock.Types
import GHC
import Name
+import FastString
import Control.Monad
import Control.Monad.Trans.RWS
@@ -104,6 +105,20 @@ parseTupleArity ('(':commas) = do
parseTupleArity _ = Nothing
+type NameRep = FastString
+
+getNameRep :: NamedThing name => name -> NameRep
+getNameRep = occNameFS . getOccName
+
+nameRepString :: NameRep -> String
+nameRepString = unpackFS
+
+stringNameRep :: String -> NameRep
+stringNameRep = mkFastString
+
+setInternalNameRep :: SetName name => NameRep -> name -> name
+setInternalNameRep = setInternalOccName . mkVarOccFS
+
setInternalOccName :: SetName name => OccName -> name -> name
setInternalOccName occ name =
setName nname' name
@@ -112,11 +127,11 @@ setInternalOccName occ name =
nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
-rename :: SetName name => Set OccName -> HsType name -> HsType name
+rename :: SetName name => Set NameRep -> HsType name -> HsType name
rename fv typ = fst $ evalRWS (renameType typ) fv Map.empty
-type Rename name a = RWS (Set OccName) () (Map Name name) a
+type Rename name a = RWS (Set NameRep) () (Map Name name) a
renameType :: SetName name => HsType name -> Rename name (HsType name)
@@ -157,7 +172,7 @@ renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
freeVariables :: forall name. (NamedThing name, DataId name)
- => HsType name -> Set OccName
+ => HsType name -> Set NameRep
freeVariables =
everythingWithState Set.empty Set.union query
where
@@ -166,7 +181,7 @@ freeVariables =
(Set.empty, Set.union ctx (bndrsNames bndrs))
Just (HsTyVar name)
| getName name `Set.member` ctx -> (Set.empty, ctx)
- | otherwise -> (Set.singleton $ getOccName name, ctx)
+ | otherwise -> (Set.singleton $ getNameRep name, ctx)
_ -> (Set.empty, ctx)
bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
@@ -208,7 +223,7 @@ renameNameBndr name = do
env <- get
case Map.lookup (getName name) env of
Just name' -> pure name'
- Nothing | getOccName name `Set.member` fv -> freshName name
+ Nothing | getNameRep name `Set.member` fv -> freshName name
Nothing -> pure name
@@ -224,35 +239,35 @@ freshName :: SetName name => name -> Rename name name
freshName name = do
fv <- ask
env <- get
- let taken = Set.union fv (Set.fromList . map getOccName . Map.keys $ env)
- let name' = setInternalOccName (findFreshName taken occ) name
+ let taken = Set.union fv (Set.fromList . map getNameRep . Map.keys $ env)
+ let name' = setInternalNameRep (findFreshName taken occ) name
put $ Map.insert nname name' env
return name'
where
nname = getName name
- occ = nameOccName nname
+ occ = getNameRep nname
-findFreshName :: Set OccName -> OccName -> OccName
+findFreshName :: Set NameRep -> NameRep -> NameRep
findFreshName taken =
fromJust . List.find isFresh . alternativeNames
where
isFresh = not . flip Set.member taken
-alternativeNames :: OccName -> [OccName]
+alternativeNames :: NameRep -> [NameRep]
alternativeNames name
- | [_] <- occNameString name = letterNames ++ alternativeNames' name
+ | [_] <- nameRepString name = letterNames ++ alternativeNames' name
where
- letterNames = map (mkVarOcc . pure) ['a'..'z']
+ letterNames = map (stringNameRep . pure) ['a'..'z']
alternativeNames name = alternativeNames' name
-alternativeNames' :: OccName -> [OccName]
+alternativeNames' :: NameRep -> [NameRep]
alternativeNames' name =
- [ mkVarOcc $ str ++ show i | i :: Int <- [0..] ]
+ [ stringNameRep $ str ++ show i | i :: Int <- [0..] ]
where
- str = occNameString name
+ str = nameRepString name
located :: Functor f => (a -> f b) -> Located a -> f (Located b)
--
cgit v1.2.3
From 0e3a90b9a1935a69b48dfb5906c346dd12c43ec1 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 22 Jul 2015 15:55:59 +0200
Subject: Add some documentation and refactor type specialization module.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 95 +++++++++++++++++-----
1 file changed, 74 insertions(+), 21 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index fccdaa95..1da089d9 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -30,23 +30,31 @@ import Data.Set (Set)
import qualified Data.Set as Set
+-- | Instantiate all occurrences of given name with particular type.
specialize :: (Eq name, Typeable name)
=> Data a
=> name -> HsType name -> a -> a
-specialize name details = everywhere (mkT $ specializeStep name details)
+specialize name details =
+ everywhere $ mkT step
+ where
+ step (HsTyVar name') | name == name' = details
+ step typ = typ
+-- | Instantiate all occurrences of given names with corresponding types.
+--
+-- It is just a convenience function wrapping 'specialize' that supports more
+-- that one specialization.
specialize' :: (Eq name, Typeable name)
=> Data a
=> [(name, HsType name)] -> a -> a
specialize' = flip $ foldr (uncurry specialize)
-specializeStep :: Eq name => name -> HsType name -> HsType name -> HsType name
-specializeStep name details (HsTyVar name') | name == name' = details
-specializeStep _ _ typ = typ
-
-
+-- | Instantiate given binders with corresponding types.
+--
+-- Again, it is just a convenience function around 'specialize'. Note that
+-- length of type list should be the same as the number of binders.
specializeTyVarBndrs :: (Eq name, Typeable name, DataId name)
=> LHsTyVarBndrs name -> [HsType name]
-> HsType name -> HsType name
@@ -58,6 +66,12 @@ specializeTyVarBndrs bndrs typs =
bname (KindedTyVar (L _ name) _) = name
+-- | Make given type use tuple and list literals where appropriate.
+--
+-- After applying 'specialize' function some terms may not use idiomatic list
+-- and tuple literals resulting in types like @[] a@ or @(,,) a b c@. This
+-- can be fixed using 'sugar' function, that will turn such types into @[a]@
+-- and @(a, b, c)@.
sugar :: forall name. (NamedThing name, DataId name)
=> HsType name -> HsType name
sugar =
@@ -93,6 +107,19 @@ sugarTuples typ =
aux _ _ = typ
+-- | Compute arity of given tuple operator.
+--
+-- >>> parseTupleArity "(,,)"
+-- Just 3
+--
+-- >>> parseTupleArity "(,,,,)"
+-- Just 5
+--
+-- >>> parseTupleArity "abc"
+-- Nothing
+--
+-- >>> parseTupleArity "()"
+-- Nothing
parseTupleArity :: String -> Maybe Int
parseTupleArity ('(':commas) = do
n <- parseCommas commas
@@ -105,6 +132,17 @@ parseTupleArity ('(':commas) = do
parseTupleArity _ = Nothing
+-- | Haskell AST type representation.
+--
+-- This type is used for renaming (more below), essentially the ambiguous (!)
+-- version of 'Name'. So, why is this 'FastString' instead of 'OccName'? Well,
+-- it was 'OccName' before, but turned out that 'OccName' sometimes also
+-- contains namespace information, differentiating visually same types.
+--
+-- And 'FastString' is used because it is /visual/ part of 'OccName' - it is
+-- not converted to 'String' or alike to avoid new allocations. Additionally,
+-- since it is stored mostly in 'Set', fast comparison of 'FastString' is also
+-- quite nice.
type NameRep = FastString
getNameRep :: NamedThing name => name -> NameRep
@@ -127,10 +165,39 @@ setInternalOccName occ name =
nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
+-- | Compute set of free variables of given type.
+freeVariables :: forall name. (NamedThing name, DataId name)
+ => HsType name -> Set NameRep
+freeVariables =
+ everythingWithState Set.empty Set.union query
+ where
+ query term ctx = case cast term :: Maybe (HsType name) of
+ Just (HsForAllTy _ _ bndrs _ _) ->
+ (Set.empty, Set.union ctx (bndrsNames bndrs))
+ Just (HsTyVar name)
+ | getName name `Set.member` ctx -> (Set.empty, ctx)
+ | otherwise -> (Set.singleton $ getNameRep name, ctx)
+ _ -> (Set.empty, ctx)
+ bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
+
+
+-- | Make given type visually unambiguous.
+--
+-- After applying 'specialize' method, some free type variables may become
+-- visually ambiguous - for example, having @a -> b@ and specializing @a@ to
+-- @(a -> b)@ we get @(a -> b) -> b@ where first occurrence of @b@ refers to
+-- different type variable than latter one. Applying 'rename' function
+-- will fix that type to be visually unambiguous again (making it something
+-- like @(a -> c) -> b@).
rename :: SetName name => Set NameRep -> HsType name -> HsType name
rename fv typ = fst $ evalRWS (renameType typ) fv Map.empty
+-- | Renaming monad.
+--
+-- This is just a simple RWS instance, where /reader/ part consists of names
+-- that are initially taken and cannot change, /state/ part is just context
+-- with name bindings and /writer/ part is not used.
type Rename name a = RWS (Set NameRep) () (Map Name name) a
@@ -171,21 +238,6 @@ renameType HsWildcardTy = pure HsWildcardTy
renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
-freeVariables :: forall name. (NamedThing name, DataId name)
- => HsType name -> Set NameRep
-freeVariables =
- everythingWithState Set.empty Set.union query
- where
- query term ctx = case cast term :: Maybe (HsType name) of
- Just (HsForAllTy _ _ bndrs _ _) ->
- (Set.empty, Set.union ctx (bndrsNames bndrs))
- Just (HsTyVar name)
- | getName name `Set.member` ctx -> (Set.empty, ctx)
- | otherwise -> (Set.singleton $ getNameRep name, ctx)
- _ -> (Set.empty, ctx)
- bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
-
-
renameLType :: SetName name => LHsType name -> Rename name (LHsType name)
renameLType = located renameType
@@ -235,6 +287,7 @@ renameName name = do
Nothing -> name
+-- | Generate fresh occurrence name, put it into context and return.
freshName :: SetName name => name -> Rename name name
freshName name = do
fv <- ask
--
cgit v1.2.3
From dfb697b2d41319a6c14134edcd89da2fa49ecd99 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 22 Jul 2015 17:20:28 +0200
Subject: Fix another bug where type renamer was generating incorrect names.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 1da089d9..47a96b34 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -292,7 +292,7 @@ freshName :: SetName name => name -> Rename name name
freshName name = do
fv <- ask
env <- get
- let taken = Set.union fv (Set.fromList . map getNameRep . Map.keys $ env)
+ let taken = Set.union fv (Set.fromList . map getNameRep . Map.elems $ env)
let name' = setInternalNameRep (findFreshName taken occ) name
put $ Map.insert nname name' env
return name'
--
cgit v1.2.3
From 3f404ba3c1b36212ae7507874aefb1e8cc107dd8 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 22 Jul 2015 18:46:10 +0200
Subject: Refactor type renamer to rebinding and pure renaming phases.
---
.../src/Haddock/Backends/Xhtml/Specialize.hs | 105 ++++++++++++---------
1 file changed, 60 insertions(+), 45 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 47a96b34..69cd939b 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -1,6 +1,7 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE RecordWildCards #-}
module Haddock.Backends.Xhtml.Specialize
@@ -19,7 +20,8 @@ import Name
import FastString
import Control.Monad
-import Control.Monad.Trans.RWS
+import Control.Monad.Trans.Reader
+import Control.Monad.Trans.State
import Data.Data
import qualified Data.List as List
@@ -190,20 +192,26 @@ freeVariables =
-- will fix that type to be visually unambiguous again (making it something
-- like @(a -> c) -> b@).
rename :: SetName name => Set NameRep -> HsType name -> HsType name
-rename fv typ = fst $ evalRWS (renameType typ) fv Map.empty
+rename fv typ = runReader (renameType typ) $ RenameEnv
+ { rneFV = fv
+ , rneCtx = Map.empty
+ }
-- | Renaming monad.
---
--- This is just a simple RWS instance, where /reader/ part consists of names
--- that are initially taken and cannot change, /state/ part is just context
--- with name bindings and /writer/ part is not used.
-type Rename name a = RWS (Set NameRep) () (Map Name name) a
+type Rename name = Reader (RenameEnv name)
+
+-- | Binding generation monad.
+type Rebind name = State (RenameEnv name)
+
+data RenameEnv name = RenameEnv
+ { rneFV :: Set NameRep
+ , rneCtx :: Map Name name
+ }
renameType :: SetName name => HsType name -> Rename name (HsType name)
-renameType (HsForAllTy ex mspan lbndrs lctx lt) = do
- lbndrs' <- renameLTyVarBndrs lbndrs
+renameType (HsForAllTy ex mspan lbndrs lctx lt) = rebind lbndrs $ \lbndrs' ->
HsForAllTy
<$> pure ex
<*> pure mspan
@@ -246,66 +254,73 @@ renameLTypes :: SetName name => [LHsType name] -> Rename name [LHsType name]
renameLTypes = mapM renameLType
-renameContext :: SetName name => HsContext name
- -> Rename name (HsContext name)
+renameContext :: SetName name => HsContext name -> Rename name (HsContext name)
renameContext = renameLTypes
-renameLTyVarBndrs :: SetName name => LHsTyVarBndrs name -> Rename name (LHsTyVarBndrs name)
-renameLTyVarBndrs lbndrs = do
- tys' <- mapM (located renameTyVarBndr) $ hsq_tvs lbndrs
- pure $ lbndrs { hsq_tvs = tys' }
-
-
-renameTyVarBndr :: SetName name => HsTyVarBndr name
- -> Rename name (HsTyVarBndr name)
-renameTyVarBndr (UserTyVar name) =
- UserTyVar <$> renameNameBndr name
-renameTyVarBndr (KindedTyVar name kinds) =
- KindedTyVar <$> located renameNameBndr name <*> pure kinds
-
-
renameLTyOp :: SetName name => LHsTyOp name -> Rename name (LHsTyOp name)
renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
-renameNameBndr :: SetName name => name -> Rename name name
-renameNameBndr name = do
- fv <- ask
- env <- get
- case Map.lookup (getName name) env of
- Just name' -> pure name'
- Nothing | getNameRep name `Set.member` fv -> freshName name
- Nothing -> pure name
-
-
renameName :: SetName name => name -> Rename name name
renameName name = do
- env <- get
- pure $ case Map.lookup (getName name) env of
+ RenameEnv { rneCtx = ctx } <- ask
+ pure $ case Map.lookup (getName name) ctx of
Just name' -> name'
Nothing -> name
+rebind :: SetName name
+ => LHsTyVarBndrs name -> (LHsTyVarBndrs name -> Rename name a)
+ -> Rename name a
+rebind lbndrs action = do
+ (lbndrs', env') <- runState (rebindLTyVarBndrs lbndrs) <$> ask
+ local (const env') (action lbndrs')
+
+
+rebindLTyVarBndrs :: SetName name
+ => LHsTyVarBndrs name -> Rebind name (LHsTyVarBndrs name)
+rebindLTyVarBndrs lbndrs = do
+ tys' <- mapM (located rebindTyVarBndr) $ hsq_tvs lbndrs
+ pure $ lbndrs { hsq_tvs = tys' }
+
+
+rebindTyVarBndr :: SetName name
+ => HsTyVarBndr name -> Rebind name (HsTyVarBndr name)
+rebindTyVarBndr (UserTyVar name) =
+ UserTyVar <$> rebindName name
+rebindTyVarBndr (KindedTyVar name kinds) =
+ KindedTyVar <$> located rebindName name <*> pure kinds
+
+
+rebindName :: SetName name => name -> Rebind name name
+rebindName name = do
+ RenameEnv { .. } <- get
+ case Map.lookup (getName name) rneCtx of
+ Just name' -> pure name'
+ Nothing | getNameRep name `Set.member` rneFV -> freshName name
+ Nothing -> pure name
+
+
-- | Generate fresh occurrence name, put it into context and return.
-freshName :: SetName name => name -> Rename name name
+freshName :: SetName name => name -> Rebind name name
freshName name = do
- fv <- ask
- env <- get
- let taken = Set.union fv (Set.fromList . map getNameRep . Map.elems $ env)
- let name' = setInternalNameRep (findFreshName taken occ) name
- put $ Map.insert nname name' env
+ env@RenameEnv { .. } <- get
+ let taken = Set.union rneFV (elems' rneCtx)
+ let name' = setInternalNameRep (findFreshName taken rep) name
+ put $ env { rneCtx = Map.insert nname name' rneCtx }
return name'
where
+ elems' = Set.fromList . map getNameRep . Map.elems
nname = getName name
- occ = getNameRep nname
+ rep = getNameRep nname
findFreshName :: Set NameRep -> NameRep -> NameRep
findFreshName taken =
fromJust . List.find isFresh . alternativeNames
where
- isFresh = not . flip Set.member taken
+ isFresh = not . Set.member taken
alternativeNames :: NameRep -> [NameRep]
--
cgit v1.2.3
From bea9253315b8b5beb6f66896eaceda92800ab32f Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 22 Jul 2015 19:21:27 +0200
Subject: Fix unwitting compilation bug.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 69cd939b..78af98b8 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -320,7 +320,7 @@ findFreshName :: Set NameRep -> NameRep -> NameRep
findFreshName taken =
fromJust . List.find isFresh . alternativeNames
where
- isFresh = not . Set.member taken
+ isFresh = not . flip Set.member taken
alternativeNames :: NameRep -> [NameRep]
--
cgit v1.2.3
From 85dab3d6aacf867a381c8810deaf585a43d42d43 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Thu, 23 Jul 2015 19:15:13 +0200
Subject: Integrate instance specification type into class instance definition.
---
haddock-api/src/Haddock/Backends/LaTeX.hs | 2 +-
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 4 ++--
haddock-api/src/Haddock/Convert.hs | 8 ++++++--
haddock-api/src/Haddock/Interface/Rename.hs | 5 ++++-
haddock-api/src/Haddock/Types.hs | 13 ++++++++++---
5 files changed, 23 insertions(+), 9 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/LaTeX.hs b/haddock-api/src/Haddock/Backends/LaTeX.hs
index 59e5af3e..47087911 100644
--- a/haddock-api/src/Haddock/Backends/LaTeX.hs
+++ b/haddock-api/src/Haddock/Backends/LaTeX.hs
@@ -562,7 +562,7 @@ ppInstDecl unicode instHead = keyword "instance" <+> ppInstHead unicode instHead
ppInstHead :: Bool -> InstHead DocName -> LaTeX
ppInstHead unicode (InstHead {..}) = case ihdInstType of
- ClassInst ctx -> ppContextNoLocs ctx unicode <+> typ
+ ClassInst ctx _ _ -> ppContextNoLocs ctx unicode <+> typ
TypeInst rhs -> keyword "type" <+> typ <+> tibody rhs
DataInst _ -> error "data instances not supported by --latex yet"
where
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 67405915..a894972e 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -531,13 +531,13 @@ ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
-> Html
ppInstHead links splice unicode qual iid mspec ihead@(InstHead {..}) =
case ihdInstType of
- ClassInst cs | Just spec <- mspec ->
+ ClassInst cs _ _ | Just spec <- mspec ->
subClsInstance (nameStr ++ "-" ++ show iid) hdr (mets spec ihead)
where
hdr = ppContextNoLocs cs unicode qual <+> typ
mets = ppInstanceSigs links splice unicode qual
nameStr = occNameString . nameOccName $ getName ihdClsName
- ClassInst cs -> ppContextNoLocs cs unicode qual <+> typ
+ ClassInst cs _ _ -> ppContextNoLocs cs unicode qual <+> typ
TypeInst rhs -> keyword "type" <+> typ
<+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
DataInst dd -> keyword "data" <+> typ
diff --git a/haddock-api/src/Haddock/Convert.hs b/haddock-api/src/Haddock/Convert.hs
index e51d9df7..3479780a 100644
--- a/haddock-api/src/Haddock/Convert.hs
+++ b/haddock-api/src/Haddock/Convert.hs
@@ -390,11 +390,15 @@ synifyKindSig :: Kind -> LHsKind Name
synifyKindSig k = synifyType WithinType k
synifyInstHead :: ([TyVar], [PredType], Class, [Type]) -> InstHead Name
-synifyInstHead (_, preds, cls, types) = InstHead
+synifyInstHead (tyvars, preds, cls, types) = InstHead
{ ihdClsName = getName cls
, ihdKinds = map (unLoc . synifyType WithinType) ks
, ihdTypes = map (unLoc . synifyType WithinType) ts
- , ihdInstType = ClassInst $ map (unLoc . synifyType WithinType) preds
+ , ihdInstType = ClassInst
+ { clsiCtx = map (unLoc . synifyType WithinType) preds
+ , clsiTyVars = synifyTyVars tyvars
+ , clsiSigs = map (synifyIdSig WithinType) $ classMethods cls
+ }
}
where (ks,ts) = break (not . isKind) types
diff --git a/haddock-api/src/Haddock/Interface/Rename.hs b/haddock-api/src/Haddock/Interface/Rename.hs
index 44635318..4e4d3ed9 100644
--- a/haddock-api/src/Haddock/Interface/Rename.hs
+++ b/haddock-api/src/Haddock/Interface/Rename.hs
@@ -264,7 +264,10 @@ renameInstHead InstHead {..} = do
kinds <- mapM renameType ihdKinds
types <- mapM renameType ihdTypes
itype <- case ihdInstType of
- ClassInst cs -> ClassInst <$> mapM renameType cs
+ ClassInst ctx bndrs sigs -> ClassInst
+ <$> mapM renameType ctx
+ <*> renameLTyVarBndrs bndrs
+ <*> mapM renameSig sigs
TypeInst ts -> TypeInst <$> traverse renameType ts
DataInst dd -> DataInst <$> renameTyClD dd
return InstHead
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index c5ca31c0..0c130cb1 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveFoldable, DeriveTraversable, StandaloneDeriving, TypeFamilies #-}
+{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveFoldable, DeriveTraversable, StandaloneDeriving, TypeFamilies, RecordWildCards #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-----------------------------------------------------------------------------
-- |
@@ -324,12 +324,19 @@ instance SetName DocName where
-- | The three types of instances
data InstType name
- = ClassInst [HsType name] -- ^ Context
+ = ClassInst
+ { clsiCtx :: [HsType name]
+ , clsiTyVars :: LHsTyVarBndrs name
+ , clsiSigs :: [Sig name]
+ }
| TypeInst (Maybe (HsType name)) -- ^ Body (right-hand side)
| DataInst (TyClDecl name) -- ^ Data constructors
instance OutputableBndr a => Outputable (InstType a) where
- ppr (ClassInst a) = text "ClassInst" <+> ppr a
+ ppr (ClassInst { .. }) = text "ClassInst"
+ <+> ppr clsiCtx
+ <+> ppr clsiTyVars
+ <+> ppr clsiSigs
ppr (TypeInst a) = text "TypeInst" <+> ppr a
ppr (DataInst a) = text "DataInst" <+> ppr a
--
cgit v1.2.3
From dc62b95d18c61fc0bf7c8d4c49d23a240af32568 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Thu, 23 Jul 2015 19:27:24 +0200
Subject: Get rid of no longer neccessary instance specification type.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 35 +++++++++++++-------------
haddock-api/src/Haddock/Types.hs | 6 -----
2 files changed, 17 insertions(+), 24 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index a894972e..34da4baa 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -282,7 +282,7 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
= subEquations qual $ map (ppTyFamEqn . unLoc) eqns
| otherwise
- = ppInstances links instances Nothing docname splice unicode qual
+ = ppInstances links instances docname splice unicode qual
-- Individual equation of a closed type family
ppTyFamEqn TyFamEqn { tfe_tycon = n, tfe_rhs = rhs
@@ -506,38 +506,37 @@ ppClassDecl summary links instances fixities loc d subdocs
ppMinimal p (Or fs) = wrap $ foldr1 (\a b -> a+++" | "+++b) $ map (ppMinimal False) fs
where wrap | p = parens | otherwise = id
- instSpec = Just $ InstSpec { ispecSigs = sigs, ispecTyVars = ltyvars }
- instancesBit = ppInstances links instances instSpec nm splice unicode qual
+ instancesBit = ppInstances links instances nm splice unicode qual
ppClassDecl _ _ _ _ _ _ _ _ _ _ _ = error "declaration type not supported by ppShortClassDecl"
ppInstances :: LinksInfo
- -> [DocInstance DocName] -> Maybe (InstSpec DocName) -> DocName
+ -> [DocInstance DocName] -> DocName
-> Splice -> Unicode -> Qualification
-> Html
-ppInstances links instances mspec baseName splice unicode qual
+ppInstances links instances baseName splice unicode qual
= subInstances qual instName links True (zipWith instDecl [1..] instances)
-- force Splice = True to use line URLs
where
instName = getOccString $ getName baseName
instDecl :: Int -> DocInstance DocName -> (SubDecl,Located DocName)
instDecl iid (inst, maybeDoc,l) =
- ((ppInstHead links splice unicode qual iid mspec inst, maybeDoc, []),l)
+ ((ppInstHead links splice unicode qual iid inst, maybeDoc, []),l)
ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
- -> Int -> Maybe (InstSpec DocName) -> InstHead DocName
+ -> Int -> InstHead DocName
-> Html
-ppInstHead links splice unicode qual iid mspec ihead@(InstHead {..}) =
+ppInstHead links splice unicode qual iid (InstHead {..}) =
case ihdInstType of
- ClassInst cs _ _ | Just spec <- mspec ->
- subClsInstance (nameStr ++ "-" ++ show iid) hdr (mets spec ihead)
+ ClassInst { .. } ->
+ subClsInstance (nameStr ++ "-" ++ show iid) hdr mets
where
- hdr = ppContextNoLocs cs unicode qual <+> typ
+ hdr = ppContextNoLocs clsiCtx unicode qual <+> typ
mets = ppInstanceSigs links splice unicode qual
+ clsiTyVars ihdTypes clsiSigs
nameStr = occNameString . nameOccName $ getName ihdClsName
- ClassInst cs _ _ -> ppContextNoLocs cs unicode qual <+> typ
TypeInst rhs -> keyword "type" <+> typ
<+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
DataInst dd -> keyword "data" <+> typ
@@ -547,15 +546,15 @@ ppInstHead links splice unicode qual iid mspec ihead@(InstHead {..}) =
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
- -> InstSpec DocName -> InstHead DocName
+ -> LHsTyVarBndrs DocName -> [HsType DocName] -> [Sig DocName]
-> [Html]
-ppInstanceSigs links splice unicode qual (InstSpec {..}) (InstHead {..}) = do
- TypeSig lnames (L loc typ) _ <- ispecSigs
+ppInstanceSigs links splice unicode qual bndrs tys sigs = do
+ TypeSig lnames (L loc typ) _ <- sigs
let names = map unLoc lnames
- let typ' = rename' . sugar $ specializeTyVarBndrs ispecTyVars ihdTypes typ
+ let typ' = rename' . sugar $ specializeTyVarBndrs bndrs tys typ
return $ ppSimpleSig links splice unicode qual loc names typ'
where
- fv = foldr Set.union Set.empty . map freeVariables $ ihdTypes
+ fv = foldr Set.union Set.empty . map freeVariables $ tys
rename' = rename fv
@@ -628,7 +627,7 @@ ppDataDecl summary links instances fixities subdocs loc doc dataDecl
(map unLoc (con_names (unLoc c)))) fixities
]
- instancesBit = ppInstances links instances Nothing docname
+ instancesBit = ppInstances links instances docname
splice unicode qual
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index 0c130cb1..b4a41020 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -352,12 +352,6 @@ data InstHead name = InstHead
, ihdInstType :: InstType name
}
--- | Instance details used for printing specialized method signatures.
-data InstSpec name = InstSpec
- { ispecTyVars :: LHsTyVarBndrs name
- , ispecSigs :: [Sig name]
- }
-
-----------------------------------------------------------------------------
-- * Documentation comments
-----------------------------------------------------------------------------
--
cgit v1.2.3
From 131e5835425e1e411ceb5cb73f9dee855b702053 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 24 Jul 2015 15:40:32 +0200
Subject: Fix bug where instance expander was opening wrong section.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 35 ++++++++++++++++++--------
1 file changed, 24 insertions(+), 11 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 34da4baa..a9f38c14 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -282,7 +282,7 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
= subEquations qual $ map (ppTyFamEqn . unLoc) eqns
| otherwise
- = ppInstances links instances docname splice unicode qual
+ = ppInstances links OriginFamily instances docname splice unicode qual
-- Individual equation of a closed type family
ppTyFamEqn TyFamEqn { tfe_tycon = n, tfe_rhs = rhs
@@ -506,37 +506,41 @@ ppClassDecl summary links instances fixities loc d subdocs
ppMinimal p (Or fs) = wrap $ foldr1 (\a b -> a+++" | "+++b) $ map (ppMinimal False) fs
where wrap | p = parens | otherwise = id
- instancesBit = ppInstances links instances nm splice unicode qual
+ instancesBit = ppInstances links OriginClass instances nm
+ splice unicode qual
ppClassDecl _ _ _ _ _ _ _ _ _ _ _ = error "declaration type not supported by ppShortClassDecl"
+data InstOrigin = OriginClass | OriginData | OriginFamily
+
+
ppInstances :: LinksInfo
- -> [DocInstance DocName] -> DocName
+ -> InstOrigin -> [DocInstance DocName] -> DocName
-> Splice -> Unicode -> Qualification
-> Html
-ppInstances links instances baseName splice unicode qual
+ppInstances links origin instances baseName splice unicode qual
= subInstances qual instName links True (zipWith instDecl [1..] instances)
-- force Splice = True to use line URLs
where
instName = getOccString $ getName baseName
instDecl :: Int -> DocInstance DocName -> (SubDecl,Located DocName)
- instDecl iid (inst, maybeDoc,l) =
- ((ppInstHead links splice unicode qual iid inst, maybeDoc, []),l)
+ instDecl no (inst, maybeDoc,l) =
+ ((ppInstHead links splice unicode qual origin no inst, maybeDoc, []),l)
ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
- -> Int -> InstHead DocName
+ -> InstOrigin -> Int -> InstHead DocName
-> Html
-ppInstHead links splice unicode qual iid (InstHead {..}) =
+ppInstHead links splice unicode qual origin no (InstHead {..}) =
case ihdInstType of
ClassInst { .. } ->
- subClsInstance (nameStr ++ "-" ++ show iid) hdr mets
+ subClsInstance iid hdr mets
where
hdr = ppContextNoLocs clsiCtx unicode qual <+> typ
mets = ppInstanceSigs links splice unicode qual
clsiTyVars ihdTypes clsiSigs
- nameStr = occNameString . nameOccName $ getName ihdClsName
+ iid = instanceId origin no ihdClsName
TypeInst rhs -> keyword "type" <+> typ
<+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
DataInst dd -> keyword "data" <+> typ
@@ -562,6 +566,15 @@ lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
lookupAnySubdoc n = fromMaybe noDocForDecl . lookup n
+instanceId :: InstOrigin -> Int -> DocName -> String
+instanceId orgin no name =
+ qual orgin ++ ":" ++ (occNameString . getOccName) name ++ "-" ++ show no
+ where
+ qual OriginClass = "ic"
+ qual OriginData = "id"
+ qual OriginFamily = "if"
+
+
-------------------------------------------------------------------------------
-- * Data & newtype declarations
-------------------------------------------------------------------------------
@@ -627,7 +640,7 @@ ppDataDecl summary links instances fixities subdocs loc doc dataDecl
(map unLoc (con_names (unLoc c)))) fixities
]
- instancesBit = ppInstances links instances docname
+ instancesBit = ppInstances links OriginData instances docname
splice unicode qual
--
cgit v1.2.3
From a66852d67fa3a035eaa255880fb82f6f499d7e39 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 24 Jul 2015 18:40:48 +0200
Subject: Fix another type renamer bug where not all names were rebound.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 78af98b8..38ec7d44 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -299,7 +299,7 @@ rebindName name = do
case Map.lookup (getName name) rneCtx of
Just name' -> pure name'
Nothing | getNameRep name `Set.member` rneFV -> freshName name
- Nothing -> pure name
+ Nothing -> reuseName name
-- | Generate fresh occurrence name, put it into context and return.
@@ -316,6 +316,13 @@ freshName name = do
rep = getNameRep nname
+reuseName :: SetName name => name -> Rebind name name
+reuseName name = do
+ env@RenameEnv { .. } <- get
+ put $ env { rneCtx = Map.insert (getName name) name rneCtx }
+ return name
+
+
findFreshName :: Set NameRep -> NameRep -> NameRep
findFreshName taken =
fromJust . List.find isFresh . alternativeNames
--
cgit v1.2.3
From fb62902d37e9467364bbbafc9e06128be89a7277 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 24 Jul 2015 19:32:22 +0200
Subject: Fix yet another renamer bug where some names were not unique.
---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 38ec7d44..a8a4e8ec 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -296,9 +296,10 @@ rebindTyVarBndr (KindedTyVar name kinds) =
rebindName :: SetName name => name -> Rebind name name
rebindName name = do
RenameEnv { .. } <- get
+ taken <- takenNames
case Map.lookup (getName name) rneCtx of
Just name' -> pure name'
- Nothing | getNameRep name `Set.member` rneFV -> freshName name
+ Nothing | getNameRep name `Set.member` taken -> freshName name
Nothing -> reuseName name
@@ -306,12 +307,11 @@ rebindName name = do
freshName :: SetName name => name -> Rebind name name
freshName name = do
env@RenameEnv { .. } <- get
- let taken = Set.union rneFV (elems' rneCtx)
+ taken <- takenNames
let name' = setInternalNameRep (findFreshName taken rep) name
put $ env { rneCtx = Map.insert nname name' rneCtx }
return name'
where
- elems' = Set.fromList . map getNameRep . Map.elems
nname = getName name
rep = getNameRep nname
@@ -323,6 +323,14 @@ reuseName name = do
return name
+takenNames :: NamedThing name => Rebind name (Set NameRep)
+takenNames = do
+ RenameEnv { .. } <- get
+ return $ Set.union rneFV (ctxElems rneCtx)
+ where
+ ctxElems = Set.fromList . map getNameRep . Map.elems
+
+
findFreshName :: Set NameRep -> NameRep -> NameRep
findFreshName taken =
fromJust . List.find isFresh . alternativeNames
--
cgit v1.2.3
From f0a5efd72072b37a2150efd281eb158033e67585 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 28 Jul 2015 23:34:03 +0200
Subject: Split instance subsection layout method to top-level declarations.
---
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 27 +++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 460cc6d7..da03985e 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -206,12 +206,29 @@ subClsInstance :: String -- ^ Section unique id
-> Html -- ^ Header contents (instance name and type)
-> [Html] -- ^ Method contents (pretty-printed signatures)
-> Html
-subClsInstance sid hdr mets =
- (hdrDiv << hdr) <+> (methodDiv << subBlock mets)
+subClsInstance iid hdr mets = subInstHead iid hdr <+> subInstMethods iid mets
+
+
+subInstHead :: String -- ^ Instance unique id (for anchor generation)
+ -> Html -- ^ Header content (instance name and type)
+ -> Html
+subInstHead iid hdr =
+ expander << hdr
where
- anchorId = makeAnchorId $ "i:" ++ sid
- hdrDiv = thediv ! collapseControl anchorId False "instance"
- methodDiv = thediv ! collapseSection anchorId False "methods"
+ expander = thediv ! collapseControl (instAnchorId iid) False "instance"
+
+
+subInstMethods :: String -- ^ Instance unique id (for anchor generation)
+ -> [Html] -- ^ Method contents (pretty-printed signatures)
+ -> Html
+subInstMethods iid mets =
+ section << subBlock mets
+ where
+ section = thediv ! collapseSection (instAnchorId iid) False "methods"
+
+
+instAnchorId :: String -> String
+instAnchorId iid = makeAnchorId $ "i:" ++ iid
subMethods :: [Html] -> Html
--
cgit v1.2.3
From 88df578a8573908d665d4597c4c619c29055a277 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Tue, 28 Jul 2015 23:58:13 +0200
Subject: Rearrange layout of instance methods in generated documentation.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 39 ++++++++++++++++--------
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 2 +-
2 files changed, 27 insertions(+), 14 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index a9f38c14..abcf3eaf 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -525,26 +525,39 @@ ppInstances links origin instances baseName splice unicode qual
where
instName = getOccString $ getName baseName
instDecl :: Int -> DocInstance DocName -> (SubDecl,Located DocName)
- instDecl no (inst, maybeDoc,l) =
- ((ppInstHead links splice unicode qual origin no inst, maybeDoc, []),l)
+ instDecl no (inst, mdoc, loc) =
+ ((ppInstHead links splice unicode qual mdoc origin no inst), loc)
ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
- -> InstOrigin -> Int -> InstHead DocName
- -> Html
-ppInstHead links splice unicode qual origin no (InstHead {..}) =
+ -> Maybe (MDoc DocName) -> InstOrigin -> Int -> InstHead DocName
+ -> SubDecl
+ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
case ihdInstType of
ClassInst { .. } ->
- subClsInstance iid hdr mets
+ ( subInstHead iid $ ppContextNoLocs clsiCtx unicode qual <+> typ
+ , mdoc
+ , [subInstMethods iid sigs]
+ )
where
- hdr = ppContextNoLocs clsiCtx unicode qual <+> typ
- mets = ppInstanceSigs links splice unicode qual
- clsiTyVars ihdTypes clsiSigs
iid = instanceId origin no ihdClsName
- TypeInst rhs -> keyword "type" <+> typ
- <+> maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
- DataInst dd -> keyword "data" <+> typ
- <+> ppShortDataDecl False True dd unicode qual
+ sigs = ppInstanceSigs links splice unicode qual
+ clsiTyVars ihdTypes clsiSigs
+ TypeInst rhs ->
+ (ptype, mdoc, [])
+ where
+ ptype = mconcat
+ [ keyword "type"
+ , typ
+ , maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
+ ]
+ DataInst dd ->
+ (pdata, mdoc, [])
+ where
+ pdata = mconcat
+ [ keyword "data" <+> typ
+ , ppShortDataDecl False True dd unicode qual
+ ]
where
typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode qual
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index da03985e..0b09e220 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -31,7 +31,7 @@ module Haddock.Backends.Xhtml.Layout (
subConstructors,
subEquations,
subFields,
- subInstances, subClsInstance,
+ subInstances, subClsInstance, subInstHead, subInstMethods,
subMethods,
subMinimal,
--
cgit v1.2.3
From 17e6df0835457035c7837e6d3f149bb3d448e5d7 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 00:01:56 +0200
Subject: Get rid of no longer used layout method.
---
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 12 ++----------
1 file changed, 2 insertions(+), 10 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 0b09e220..a1c54c99 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -31,7 +31,7 @@ module Haddock.Backends.Xhtml.Layout (
subConstructors,
subEquations,
subFields,
- subInstances, subClsInstance, subInstHead, subInstMethods,
+ subInstances, subInstHead, subInstMethods,
subMethods,
subMinimal,
@@ -200,15 +200,7 @@ subInstances qual nm lnks splice = maybe noHtml wrap . instTable
subCaption = paragraph ! collapseControl id_ True "caption" << "Instances"
id_ = makeAnchorId $ "i:" ++ nm
-
--- | Generate class instance div with specialized methods.
-subClsInstance :: String -- ^ Section unique id
- -> Html -- ^ Header contents (instance name and type)
- -> [Html] -- ^ Method contents (pretty-printed signatures)
- -> Html
-subClsInstance iid hdr mets = subInstHead iid hdr <+> subInstMethods iid mets
-
-
+
subInstHead :: String -- ^ Instance unique id (for anchor generation)
-> Html -- ^ Header content (instance name and type)
-> Html
--
cgit v1.2.3
From 467b07ecf9b22e3492b014dbae64f2f7d9b73f02 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 00:23:36 +0200
Subject: Attach section title to the instance methods block.
---
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index a1c54c99..117f8fc8 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -214,7 +214,7 @@ subInstMethods :: String -- ^ Instance unique id (for anchor generation)
-> [Html] -- ^ Method contents (pretty-printed signatures)
-> Html
subInstMethods iid mets =
- section << subBlock mets
+ section << subMethods mets
where
section = thediv ! collapseSection (instAnchorId iid) False "methods"
--
cgit v1.2.3
From 0c34ec0ae515d88437e04a49ca0131205be096e5 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 12:07:13 +0200
Subject: Attach associated types information to instance header.
---
haddock-api/src/Haddock/Backends/LaTeX.hs | 2 +-
haddock-api/src/Haddock/Convert.hs | 3 +++
haddock-api/src/Haddock/Interface/Rename.hs | 9 +++++----
haddock-api/src/Haddock/Types.hs | 1 +
4 files changed, 10 insertions(+), 5 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/LaTeX.hs b/haddock-api/src/Haddock/Backends/LaTeX.hs
index 47087911..24779a94 100644
--- a/haddock-api/src/Haddock/Backends/LaTeX.hs
+++ b/haddock-api/src/Haddock/Backends/LaTeX.hs
@@ -562,7 +562,7 @@ ppInstDecl unicode instHead = keyword "instance" <+> ppInstHead unicode instHead
ppInstHead :: Bool -> InstHead DocName -> LaTeX
ppInstHead unicode (InstHead {..}) = case ihdInstType of
- ClassInst ctx _ _ -> ppContextNoLocs ctx unicode <+> typ
+ ClassInst ctx _ _ _ -> ppContextNoLocs ctx unicode <+> typ
TypeInst rhs -> keyword "type" <+> typ <+> tibody rhs
DataInst _ -> error "data instances not supported by --latex yet"
where
diff --git a/haddock-api/src/Haddock/Convert.hs b/haddock-api/src/Haddock/Convert.hs
index 00a90e97..a2716d92 100644
--- a/haddock-api/src/Haddock/Convert.hs
+++ b/haddock-api/src/Haddock/Convert.hs
@@ -398,6 +398,9 @@ synifyInstHead (_, preds, cls, types) = InstHead
{ clsiCtx = map (unLoc . synifyType WithinType) preds
, clsiTyVars = synifyTyVars $ classTyVars cls
, clsiSigs = map synifyClsIdSig $ classMethods cls
+ , clsiAssocTys = do
+ (Right (FamDecl fam)) <- map (synifyTyCon Nothing) $ classATs cls
+ pure fam
}
}
where
diff --git a/haddock-api/src/Haddock/Interface/Rename.hs b/haddock-api/src/Haddock/Interface/Rename.hs
index 4e4d3ed9..82d14a2c 100644
--- a/haddock-api/src/Haddock/Interface/Rename.hs
+++ b/haddock-api/src/Haddock/Interface/Rename.hs
@@ -264,10 +264,11 @@ renameInstHead InstHead {..} = do
kinds <- mapM renameType ihdKinds
types <- mapM renameType ihdTypes
itype <- case ihdInstType of
- ClassInst ctx bndrs sigs -> ClassInst
- <$> mapM renameType ctx
- <*> renameLTyVarBndrs bndrs
- <*> mapM renameSig sigs
+ ClassInst { .. } -> ClassInst
+ <$> mapM renameType clsiCtx
+ <*> renameLTyVarBndrs clsiTyVars
+ <*> mapM renameSig clsiSigs
+ <*> mapM renameFamilyDecl clsiAssocTys
TypeInst ts -> TypeInst <$> traverse renameType ts
DataInst dd -> DataInst <$> renameTyClD dd
return InstHead
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index b4a41020..ac073036 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -328,6 +328,7 @@ data InstType name
{ clsiCtx :: [HsType name]
, clsiTyVars :: LHsTyVarBndrs name
, clsiSigs :: [Sig name]
+ , clsiAssocTys :: [FamilyDecl name]
}
| TypeInst (Maybe (HsType name)) -- ^ Body (right-hand side)
| DataInst (TyClDecl name) -- ^ Data constructors
--
cgit v1.2.3
From 73f4a18d0b29dd209a5f1172c8ed662be11d5690 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 12:25:54 +0200
Subject: Make instance details section contain associated types information.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 13 ++++++++++++-
haddock-api/src/Haddock/Backends/Xhtml/Layout.hs | 11 ++++++-----
2 files changed, 18 insertions(+), 6 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index abcf3eaf..6fb36e29 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -537,12 +537,14 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
ClassInst { .. } ->
( subInstHead iid $ ppContextNoLocs clsiCtx unicode qual <+> typ
, mdoc
- , [subInstMethods iid sigs]
+ , [subInstDetails iid ats sigs]
)
where
iid = instanceId origin no ihdClsName
sigs = ppInstanceSigs links splice unicode qual
clsiTyVars ihdTypes clsiSigs
+ ats = ppInstanceAssocTys links splice unicode qual
+ clsiAssocTys
TypeInst rhs ->
(ptype, mdoc, [])
where
@@ -562,6 +564,15 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode qual
+ppInstanceAssocTys :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> [FamilyDecl DocName]
+ -> [Html]
+ppInstanceAssocTys links splice unicode qual =
+ map ppTyFam'
+ where
+ ppTyFam' fam = ppTyFamHeader False True fam unicode qual
+
+
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
-> LHsTyVarBndrs DocName -> [HsType DocName] -> [Sig DocName]
-> [Html]
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
index 117f8fc8..074b6801 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Layout.hs
@@ -31,7 +31,7 @@ module Haddock.Backends.Xhtml.Layout (
subConstructors,
subEquations,
subFields,
- subInstances, subInstHead, subInstMethods,
+ subInstances, subInstHead, subInstDetails,
subMethods,
subMinimal,
@@ -210,13 +210,14 @@ subInstHead iid hdr =
expander = thediv ! collapseControl (instAnchorId iid) False "instance"
-subInstMethods :: String -- ^ Instance unique id (for anchor generation)
+subInstDetails :: String -- ^ Instance unique id (for anchor generation)
+ -> [Html] -- ^ Associated type contents
-> [Html] -- ^ Method contents (pretty-printed signatures)
-> Html
-subInstMethods iid mets =
- section << subMethods mets
+subInstDetails iid ats mets =
+ section << (subAssociatedTypes ats <+> subMethods mets)
where
- section = thediv ! collapseSection (instAnchorId iid) False "methods"
+ section = thediv ! collapseSection (instAnchorId iid) False "inst-details"
instAnchorId :: String -> String
--
cgit v1.2.3
From 3fb4ec56a9e7fc167c8fd970bc15b554ab85a1c9 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 12:50:51 +0200
Subject: Improve look of rendered associated families in instance details.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 6fb36e29..4b28e4ff 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -302,6 +302,15 @@ ppAssocType summ links doc (L loc decl) fixities splice unicode qual =
ppTyFam summ True links [] fixities loc (fst doc) decl splice unicode qual
+ppSimpleAssocTy :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> FamilyDecl DocName
+ -> Html
+ppSimpleAssocTy links splice unicode qual decl =
+ ppAssocType False links noDocForDecl ldecl [] splice unicode qual
+ where
+ ldecl = L (getLoc $ fdLName decl) decl
+
+
--------------------------------------------------------------------------------
-- * TyClDecl helpers
--------------------------------------------------------------------------------
@@ -568,9 +577,9 @@ ppInstanceAssocTys :: LinksInfo -> Splice -> Unicode -> Qualification
-> [FamilyDecl DocName]
-> [Html]
ppInstanceAssocTys links splice unicode qual =
- map ppTyFam'
+ map ppSimpleAssocTy'
where
- ppTyFam' fam = ppTyFamHeader False True fam unicode qual
+ ppSimpleAssocTy' = ppSimpleAssocTy links splice unicode qual
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
--
cgit v1.2.3
From 3073526a26d013e8751068fbd526974dcfb8259f Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 15:37:48 +0200
Subject: Make instance details record use new type for family declarations.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 23 +++++++++++------------
haddock-api/src/Haddock/Convert.hs | 2 +-
haddock-api/src/Haddock/Interface/Rename.hs | 12 +++++++++++-
haddock-api/src/Haddock/Types.hs | 2 +-
4 files changed, 24 insertions(+), 15 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 4b28e4ff..eb4524c2 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -291,6 +291,14 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
<+> equals <+> ppType unicode qual (unLoc rhs)
, Nothing, [] )
+
+
+ppPseudoFamilyDecl :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> PseudoFamilyDecl DocName
+ -> Html
+ppPseudoFamilyDecl = undefined
+
+
--------------------------------------------------------------------------------
-- * Associated Types
--------------------------------------------------------------------------------
@@ -302,15 +310,6 @@ ppAssocType summ links doc (L loc decl) fixities splice unicode qual =
ppTyFam summ True links [] fixities loc (fst doc) decl splice unicode qual
-ppSimpleAssocTy :: LinksInfo -> Splice -> Unicode -> Qualification
- -> FamilyDecl DocName
- -> Html
-ppSimpleAssocTy links splice unicode qual decl =
- ppAssocType False links noDocForDecl ldecl [] splice unicode qual
- where
- ldecl = L (getLoc $ fdLName decl) decl
-
-
--------------------------------------------------------------------------------
-- * TyClDecl helpers
--------------------------------------------------------------------------------
@@ -574,12 +573,12 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
ppInstanceAssocTys :: LinksInfo -> Splice -> Unicode -> Qualification
- -> [FamilyDecl DocName]
+ -> [PseudoFamilyDecl DocName]
-> [Html]
ppInstanceAssocTys links splice unicode qual =
- map ppSimpleAssocTy'
+ map ppFamilyDecl'
where
- ppSimpleAssocTy' = ppSimpleAssocTy links splice unicode qual
+ ppFamilyDecl' = ppPseudoFamilyDecl links splice unicode qual
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
diff --git a/haddock-api/src/Haddock/Convert.hs b/haddock-api/src/Haddock/Convert.hs
index a2716d92..095bd9e0 100644
--- a/haddock-api/src/Haddock/Convert.hs
+++ b/haddock-api/src/Haddock/Convert.hs
@@ -400,7 +400,7 @@ synifyInstHead (_, preds, cls, types) = InstHead
, clsiSigs = map synifyClsIdSig $ classMethods cls
, clsiAssocTys = do
(Right (FamDecl fam)) <- map (synifyTyCon Nothing) $ classATs cls
- pure fam
+ pure $ mkPseudoFamilyDecl fam
}
}
where
diff --git a/haddock-api/src/Haddock/Interface/Rename.hs b/haddock-api/src/Haddock/Interface/Rename.hs
index 82d14a2c..146a7c0b 100644
--- a/haddock-api/src/Haddock/Interface/Rename.hs
+++ b/haddock-api/src/Haddock/Interface/Rename.hs
@@ -268,7 +268,7 @@ renameInstHead InstHead {..} = do
<$> mapM renameType clsiCtx
<*> renameLTyVarBndrs clsiTyVars
<*> mapM renameSig clsiSigs
- <*> mapM renameFamilyDecl clsiAssocTys
+ <*> mapM renamePseudoFamilyDecl clsiAssocTys
TypeInst ts -> TypeInst <$> traverse renameType ts
DataInst dd -> DataInst <$> renameTyClD dd
return InstHead
@@ -352,6 +352,16 @@ renameFamilyDecl (FamilyDecl { fdInfo = info, fdLName = lname
return (FamilyDecl { fdInfo = info', fdLName = lname'
, fdTyVars = ltyvars', fdKindSig = tckind' })
+
+renamePseudoFamilyDecl :: PseudoFamilyDecl Name
+ -> RnM (PseudoFamilyDecl DocName)
+renamePseudoFamilyDecl (PseudoFamilyDecl { .. }) = PseudoFamilyDecl
+ <$> renameFamilyInfo pfdInfo
+ <*> renameL pfdLName
+ <*> mapM renameLType pfdTyVars
+ <*> renameMaybeLKind pfdKindSig
+
+
renameFamilyInfo :: FamilyInfo Name -> RnM (FamilyInfo DocName)
renameFamilyInfo DataFamily = return DataFamily
renameFamilyInfo OpenTypeFamily = return OpenTypeFamily
diff --git a/haddock-api/src/Haddock/Types.hs b/haddock-api/src/Haddock/Types.hs
index 90672c9d..1f074ac3 100644
--- a/haddock-api/src/Haddock/Types.hs
+++ b/haddock-api/src/Haddock/Types.hs
@@ -328,7 +328,7 @@ data InstType name
{ clsiCtx :: [HsType name]
, clsiTyVars :: LHsTyVarBndrs name
, clsiSigs :: [Sig name]
- , clsiAssocTys :: [FamilyDecl name]
+ , clsiAssocTys :: [PseudoFamilyDecl name]
}
| TypeInst (Maybe (HsType name)) -- ^ Body (right-hand side)
| DataInst (TyClDecl name) -- ^ Data constructors
--
cgit v1.2.3
From 5980cfb8eb5bf86e420c2d1b82d6d3b92c0c1fda Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 16:51:22 +0200
Subject: Split printer of type family header to separate functions.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 37 ++++++++++++++------------
1 file changed, 20 insertions(+), 17 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index eb4524c2..e248dc25 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -239,28 +239,31 @@ ppSimpleSig links splice unicode qual loc names typ =
--------------------------------------------------------------------------------
+ppFamilyInfo :: Bool -> FamilyInfo DocName -> Html
+ppFamilyInfo assoc OpenTypeFamily
+ | assoc = keyword "type"
+ | otherwise = keyword "type family"
+ppFamilyInfo assoc DataFamily
+ | assoc = keyword "data"
+ | otherwise = keyword "data family"
+ppFamilyInfo _ (ClosedTypeFamily _) = keyword "type family"
+
+
+ppFamilyKind :: Unicode -> Qualification -> Maybe (LHsKind DocName) -> Html
+ppFamilyKind unicode qual (Just kind) =
+ dcolon unicode <+> ppLKind unicode qual kind
+ppFamilyKind _ _ Nothing = noHtml
+
+
ppTyFamHeader :: Bool -> Bool -> FamilyDecl DocName
-> Unicode -> Qualification -> Html
ppTyFamHeader summary associated d@(FamilyDecl { fdInfo = info
, fdKindSig = mkind })
unicode qual =
- (case info of
- OpenTypeFamily
- | associated -> keyword "type"
- | otherwise -> keyword "type family"
- DataFamily
- | associated -> keyword "data"
- | otherwise -> keyword "data family"
- ClosedTypeFamily _
- -> keyword "type family"
- ) <+>
-
- ppFamDeclBinderWithVars summary d <+>
-
- (case mkind of
- Just kind -> dcolon unicode <+> ppLKind unicode qual kind
- Nothing -> noHtml
- )
+ ppFamilyInfo associated info <+>
+ ppFamDeclBinderWithVars summary d <+>
+ ppFamilyKind unicode qual mkind
+
ppTyFam :: Bool -> Bool -> LinksInfo -> [DocInstance DocName] ->
[(DocName, Fixity)] -> SrcSpan -> Documentation DocName ->
--
cgit v1.2.3
From 709ce61f4cf18b2d6a24411670713d7480b8218c Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 17:45:00 +0200
Subject: Implement HTML renderer for pseudo-family declarations.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index e248dc25..e6869916 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -265,6 +265,14 @@ ppTyFamHeader summary associated d@(FamilyDecl { fdInfo = info
ppFamilyKind unicode qual mkind
+ppPseudoFamilyHeader :: Unicode -> Qualification -> PseudoFamilyDecl DocName
+ -> Html
+ppPseudoFamilyHeader unicode qual (PseudoFamilyDecl { .. }) =
+ ppFamilyInfo True pfdInfo <+>
+ ppAppNameTypes (unLoc pfdLName) [] (map unLoc pfdTyVars) unicode qual <+>
+ ppFamilyKind unicode qual pfdKindSig
+
+
ppTyFam :: Bool -> Bool -> LinksInfo -> [DocInstance DocName] ->
[(DocName, Fixity)] -> SrcSpan -> Documentation DocName ->
FamilyDecl DocName -> Splice -> Unicode -> Qualification -> Html
@@ -299,7 +307,11 @@ ppTyFam summary associated links instances fixities loc doc decl splice unicode
ppPseudoFamilyDecl :: LinksInfo -> Splice -> Unicode -> Qualification
-> PseudoFamilyDecl DocName
-> Html
-ppPseudoFamilyDecl = undefined
+ppPseudoFamilyDecl links splice unicode qual
+ decl@(PseudoFamilyDecl { pfdLName = L loc name, .. }) =
+ wrapper $ ppPseudoFamilyHeader unicode qual decl
+ where
+ wrapper = topDeclElem links loc splice [name]
--------------------------------------------------------------------------------
--
cgit v1.2.3
From 00571a39acaa5aaa292b5a4bd5c17f122951f7ae Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 18:21:04 +0200
Subject: Apply type specializer to associated type family declarations.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 7 ++++---
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 14 +++++++++++++-
2 files changed, 17 insertions(+), 4 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index e6869916..294af864 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -567,7 +567,7 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
sigs = ppInstanceSigs links splice unicode qual
clsiTyVars ihdTypes clsiSigs
ats = ppInstanceAssocTys links splice unicode qual
- clsiAssocTys
+ clsiTyVars ihdTypes clsiAssocTys
TypeInst rhs ->
(ptype, mdoc, [])
where
@@ -588,10 +588,11 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
ppInstanceAssocTys :: LinksInfo -> Splice -> Unicode -> Qualification
+ -> LHsTyVarBndrs DocName -> [HsType DocName]
-> [PseudoFamilyDecl DocName]
-> [Html]
-ppInstanceAssocTys links splice unicode qual =
- map ppFamilyDecl'
+ppInstanceAssocTys links splice unicode qual bndrs tys =
+ map ppFamilyDecl' . map (specializePseudoFamilyDecl bndrs tys)
where
ppFamilyDecl' = ppPseudoFamilyDecl links splice unicode qual
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index a8a4e8ec..109788fd 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -7,6 +7,7 @@
module Haddock.Backends.Xhtml.Specialize
( specialize, specialize'
, specializeTyVarBndrs
+ , specializePseudoFamilyDecl
, sugar, rename
, freeVariables
) where
@@ -58,8 +59,9 @@ specialize' = flip $ foldr (uncurry specialize)
-- Again, it is just a convenience function around 'specialize'. Note that
-- length of type list should be the same as the number of binders.
specializeTyVarBndrs :: (Eq name, Typeable name, DataId name)
+ => Data a
=> LHsTyVarBndrs name -> [HsType name]
- -> HsType name -> HsType name
+ -> a -> a
specializeTyVarBndrs bndrs typs =
specialize' $ zip bndrs' typs
where
@@ -68,6 +70,16 @@ specializeTyVarBndrs bndrs typs =
bname (KindedTyVar (L _ name) _) = name
+specializePseudoFamilyDecl :: (Eq name, Typeable name, DataId name)
+ => LHsTyVarBndrs name -> [HsType name]
+ -> PseudoFamilyDecl name
+ -> PseudoFamilyDecl name
+specializePseudoFamilyDecl bndrs typs decl =
+ decl { pfdTyVars = map specializeTyVars (pfdTyVars decl) }
+ where
+ specializeTyVars = specializeTyVarBndrs bndrs typs
+
+
-- | Make given type use tuple and list literals where appropriate.
--
-- After applying 'specialize' function some terms may not use idiomatic list
--
cgit v1.2.3
From 730d8b0e76c5e637f2cdd7d980865f6208729366 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 18:43:39 +0200
Subject: Create helper method for specializing type signatures.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 9 ++-------
haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs | 18 +++++++++++++-----
2 files changed, 15 insertions(+), 12 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 294af864..7255bf42 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -31,7 +31,6 @@ import Haddock.Doc (combineDocumentation)
import Data.List ( intersperse, sort )
import qualified Data.Map as Map
-import qualified Data.Set as Set
import Data.Maybe
import Text.XHtml hiding ( name, title, p, quote )
@@ -601,13 +600,9 @@ ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
-> LHsTyVarBndrs DocName -> [HsType DocName] -> [Sig DocName]
-> [Html]
ppInstanceSigs links splice unicode qual bndrs tys sigs = do
- TypeSig lnames (L loc typ) _ <- sigs
+ TypeSig lnames (L loc typ) _ <- map (specializeSig bndrs tys) sigs
let names = map unLoc lnames
- let typ' = rename' . sugar $ specializeTyVarBndrs bndrs tys typ
- return $ ppSimpleSig links splice unicode qual loc names typ'
- where
- fv = foldr Set.union Set.empty . map freeVariables $ tys
- rename' = rename fv
+ return $ ppSimpleSig links splice unicode qual loc names typ
lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
index 109788fd..2295605b 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
@@ -5,11 +5,7 @@
module Haddock.Backends.Xhtml.Specialize
- ( specialize, specialize'
- , specializeTyVarBndrs
- , specializePseudoFamilyDecl
- , sugar, rename
- , freeVariables
+ ( specializePseudoFamilyDecl, specializeSig
) where
@@ -80,6 +76,18 @@ specializePseudoFamilyDecl bndrs typs decl =
specializeTyVars = specializeTyVarBndrs bndrs typs
+specializeSig :: (Eq name, Typeable name, DataId name, SetName name)
+ => LHsTyVarBndrs name -> [HsType name]
+ -> Sig name
+ -> Sig name
+specializeSig bndrs typs (TypeSig lnames (L loc typ) prn) =
+ TypeSig lnames (L loc typ') prn
+ where
+ typ' = rename fv . sugar $ specializeTyVarBndrs bndrs typs typ
+ fv = foldr Set.union Set.empty . map freeVariables $ typs
+specializeSig _ _ sig = sig
+
+
-- | Make given type use tuple and list literals where appropriate.
--
-- After applying 'specialize' function some terms may not use idiomatic list
--
cgit v1.2.3
From f0222eaf888dafb9fdb6dbbac0527fc28223588d Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Wed, 29 Jul 2015 19:32:32 +0200
Subject: Refactor specializer module to be independent from XHTML backend.
---
haddock-api/haddock-api.cabal | 2 +-
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 18 +-
.../src/Haddock/Backends/Xhtml/Specialize.hs | 382 --------------------
haddock-api/src/Haddock/Convert.hs | 6 +-
haddock-api/src/Haddock/Interface/Specialize.hs | 396 +++++++++++++++++++++
haddock.cabal | 2 +-
6 files changed, 409 insertions(+), 397 deletions(-)
delete mode 100644 haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
create mode 100644 haddock-api/src/Haddock/Interface/Specialize.hs
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/haddock-api.cabal b/haddock-api/haddock-api.cabal
index 2090c53e..b4ceb1a0 100644
--- a/haddock-api/haddock-api.cabal
+++ b/haddock-api/haddock-api.cabal
@@ -69,6 +69,7 @@ library
Haddock.Interface.AttachInstances
Haddock.Interface.LexParseRn
Haddock.Interface.ParseModuleHeader
+ Haddock.Interface.Specialize
Haddock.Parser
Haddock.Utils
Haddock.Backends.Xhtml
@@ -76,7 +77,6 @@ library
Haddock.Backends.Xhtml.DocMarkup
Haddock.Backends.Xhtml.Layout
Haddock.Backends.Xhtml.Names
- Haddock.Backends.Xhtml.Specialize
Haddock.Backends.Xhtml.Themes
Haddock.Backends.Xhtml.Types
Haddock.Backends.Xhtml.Utils
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 7255bf42..7da1f08e 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -22,7 +22,6 @@ module Haddock.Backends.Xhtml.Decl (
import Haddock.Backends.Xhtml.DocMarkup
import Haddock.Backends.Xhtml.Layout
import Haddock.Backends.Xhtml.Names
-import Haddock.Backends.Xhtml.Specialize
import Haddock.Backends.Xhtml.Types
import Haddock.Backends.Xhtml.Utils
import Haddock.GhcUtils
@@ -563,10 +562,8 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
)
where
iid = instanceId origin no ihdClsName
- sigs = ppInstanceSigs links splice unicode qual
- clsiTyVars ihdTypes clsiSigs
- ats = ppInstanceAssocTys links splice unicode qual
- clsiTyVars ihdTypes clsiAssocTys
+ sigs = ppInstanceSigs links splice unicode qual clsiSigs
+ ats = ppInstanceAssocTys links splice unicode qual clsiAssocTys
TypeInst rhs ->
(ptype, mdoc, [])
where
@@ -587,20 +584,19 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
ppInstanceAssocTys :: LinksInfo -> Splice -> Unicode -> Qualification
- -> LHsTyVarBndrs DocName -> [HsType DocName]
-> [PseudoFamilyDecl DocName]
-> [Html]
-ppInstanceAssocTys links splice unicode qual bndrs tys =
- map ppFamilyDecl' . map (specializePseudoFamilyDecl bndrs tys)
+ppInstanceAssocTys links splice unicode qual =
+ map ppFamilyDecl'
where
ppFamilyDecl' = ppPseudoFamilyDecl links splice unicode qual
ppInstanceSigs :: LinksInfo -> Splice -> Unicode -> Qualification
- -> LHsTyVarBndrs DocName -> [HsType DocName] -> [Sig DocName]
+ -> [Sig DocName]
-> [Html]
-ppInstanceSigs links splice unicode qual bndrs tys sigs = do
- TypeSig lnames (L loc typ) _ <- map (specializeSig bndrs tys) sigs
+ppInstanceSigs links splice unicode qual sigs = do
+ TypeSig lnames (L loc typ) _ <- sigs
let names = map unLoc lnames
return $ ppSimpleSig links splice unicode qual loc names typ
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs b/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
deleted file mode 100644
index 2295605b..00000000
--- a/haddock-api/src/Haddock/Backends/Xhtml/Specialize.hs
+++ /dev/null
@@ -1,382 +0,0 @@
-{-# LANGUAGE FlexibleContexts #-}
-{-# LANGUAGE Rank2Types #-}
-{-# LANGUAGE ScopedTypeVariables #-}
-{-# LANGUAGE RecordWildCards #-}
-
-
-module Haddock.Backends.Xhtml.Specialize
- ( specializePseudoFamilyDecl, specializeSig
- ) where
-
-
-import Haddock.Syb
-import Haddock.Types
-
-import GHC
-import Name
-import FastString
-
-import Control.Monad
-import Control.Monad.Trans.Reader
-import Control.Monad.Trans.State
-
-import Data.Data
-import qualified Data.List as List
-import Data.Maybe
-import Data.Map (Map)
-import qualified Data.Map as Map
-import Data.Set (Set)
-import qualified Data.Set as Set
-
-
--- | Instantiate all occurrences of given name with particular type.
-specialize :: (Eq name, Typeable name)
- => Data a
- => name -> HsType name -> a -> a
-specialize name details =
- everywhere $ mkT step
- where
- step (HsTyVar name') | name == name' = details
- step typ = typ
-
-
--- | Instantiate all occurrences of given names with corresponding types.
---
--- It is just a convenience function wrapping 'specialize' that supports more
--- that one specialization.
-specialize' :: (Eq name, Typeable name)
- => Data a
- => [(name, HsType name)] -> a -> a
-specialize' = flip $ foldr (uncurry specialize)
-
-
--- | Instantiate given binders with corresponding types.
---
--- Again, it is just a convenience function around 'specialize'. Note that
--- length of type list should be the same as the number of binders.
-specializeTyVarBndrs :: (Eq name, Typeable name, DataId name)
- => Data a
- => LHsTyVarBndrs name -> [HsType name]
- -> a -> a
-specializeTyVarBndrs bndrs typs =
- specialize' $ zip bndrs' typs
- where
- bndrs' = map (bname . unLoc) . hsq_tvs $ bndrs
- bname (UserTyVar name) = name
- bname (KindedTyVar (L _ name) _) = name
-
-
-specializePseudoFamilyDecl :: (Eq name, Typeable name, DataId name)
- => LHsTyVarBndrs name -> [HsType name]
- -> PseudoFamilyDecl name
- -> PseudoFamilyDecl name
-specializePseudoFamilyDecl bndrs typs decl =
- decl { pfdTyVars = map specializeTyVars (pfdTyVars decl) }
- where
- specializeTyVars = specializeTyVarBndrs bndrs typs
-
-
-specializeSig :: (Eq name, Typeable name, DataId name, SetName name)
- => LHsTyVarBndrs name -> [HsType name]
- -> Sig name
- -> Sig name
-specializeSig bndrs typs (TypeSig lnames (L loc typ) prn) =
- TypeSig lnames (L loc typ') prn
- where
- typ' = rename fv . sugar $ specializeTyVarBndrs bndrs typs typ
- fv = foldr Set.union Set.empty . map freeVariables $ typs
-specializeSig _ _ sig = sig
-
-
--- | Make given type use tuple and list literals where appropriate.
---
--- After applying 'specialize' function some terms may not use idiomatic list
--- and tuple literals resulting in types like @[] a@ or @(,,) a b c@. This
--- can be fixed using 'sugar' function, that will turn such types into @[a]@
--- and @(a, b, c)@.
-sugar :: forall name. (NamedThing name, DataId name)
- => HsType name -> HsType name
-sugar =
- everywhere $ mkT step
- where
- step :: HsType name -> HsType name
- step = sugarTuples . sugarLists
-
-
-sugarLists :: NamedThing name => HsType name -> HsType name
-sugarLists (HsAppTy (L _ (HsTyVar name)) ltyp)
- | isBuiltInSyntax name' && strName == "[]" = HsListTy ltyp
- where
- name' = getName name
- strName = occNameString . nameOccName $ name'
-sugarLists typ = typ
-
-
-sugarTuples :: NamedThing name => HsType name -> HsType name
-sugarTuples typ =
- aux [] typ
- where
- aux apps (HsAppTy (L _ ftyp) atyp) = aux (atyp:apps) ftyp
- aux apps (HsParTy (L _ typ')) = aux apps typ'
- aux apps (HsTyVar name)
- | isBuiltInSyntax name' && suitable = HsTupleTy HsBoxedTuple apps
- where
- name' = getName name
- strName = occNameString . nameOccName $ name'
- suitable = case parseTupleArity strName of
- Just arity -> arity == length apps
- Nothing -> False
- aux _ _ = typ
-
-
--- | Compute arity of given tuple operator.
---
--- >>> parseTupleArity "(,,)"
--- Just 3
---
--- >>> parseTupleArity "(,,,,)"
--- Just 5
---
--- >>> parseTupleArity "abc"
--- Nothing
---
--- >>> parseTupleArity "()"
--- Nothing
-parseTupleArity :: String -> Maybe Int
-parseTupleArity ('(':commas) = do
- n <- parseCommas commas
- guard $ n /= 0
- return $ n + 1
- where
- parseCommas (',':rest) = (+ 1) <$> parseCommas rest
- parseCommas ")" = Just 0
- parseCommas _ = Nothing
-parseTupleArity _ = Nothing
-
-
--- | Haskell AST type representation.
---
--- This type is used for renaming (more below), essentially the ambiguous (!)
--- version of 'Name'. So, why is this 'FastString' instead of 'OccName'? Well,
--- it was 'OccName' before, but turned out that 'OccName' sometimes also
--- contains namespace information, differentiating visually same types.
---
--- And 'FastString' is used because it is /visual/ part of 'OccName' - it is
--- not converted to 'String' or alike to avoid new allocations. Additionally,
--- since it is stored mostly in 'Set', fast comparison of 'FastString' is also
--- quite nice.
-type NameRep = FastString
-
-getNameRep :: NamedThing name => name -> NameRep
-getNameRep = occNameFS . getOccName
-
-nameRepString :: NameRep -> String
-nameRepString = unpackFS
-
-stringNameRep :: String -> NameRep
-stringNameRep = mkFastString
-
-setInternalNameRep :: SetName name => NameRep -> name -> name
-setInternalNameRep = setInternalOccName . mkVarOccFS
-
-setInternalOccName :: SetName name => OccName -> name -> name
-setInternalOccName occ name =
- setName nname' name
- where
- nname = getName name
- nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
-
-
--- | Compute set of free variables of given type.
-freeVariables :: forall name. (NamedThing name, DataId name)
- => HsType name -> Set NameRep
-freeVariables =
- everythingWithState Set.empty Set.union query
- where
- query term ctx = case cast term :: Maybe (HsType name) of
- Just (HsForAllTy _ _ bndrs _ _) ->
- (Set.empty, Set.union ctx (bndrsNames bndrs))
- Just (HsTyVar name)
- | getName name `Set.member` ctx -> (Set.empty, ctx)
- | otherwise -> (Set.singleton $ getNameRep name, ctx)
- _ -> (Set.empty, ctx)
- bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
-
-
--- | Make given type visually unambiguous.
---
--- After applying 'specialize' method, some free type variables may become
--- visually ambiguous - for example, having @a -> b@ and specializing @a@ to
--- @(a -> b)@ we get @(a -> b) -> b@ where first occurrence of @b@ refers to
--- different type variable than latter one. Applying 'rename' function
--- will fix that type to be visually unambiguous again (making it something
--- like @(a -> c) -> b@).
-rename :: SetName name => Set NameRep -> HsType name -> HsType name
-rename fv typ = runReader (renameType typ) $ RenameEnv
- { rneFV = fv
- , rneCtx = Map.empty
- }
-
-
--- | Renaming monad.
-type Rename name = Reader (RenameEnv name)
-
--- | Binding generation monad.
-type Rebind name = State (RenameEnv name)
-
-data RenameEnv name = RenameEnv
- { rneFV :: Set NameRep
- , rneCtx :: Map Name name
- }
-
-
-renameType :: SetName name => HsType name -> Rename name (HsType name)
-renameType (HsForAllTy ex mspan lbndrs lctx lt) = rebind lbndrs $ \lbndrs' ->
- HsForAllTy
- <$> pure ex
- <*> pure mspan
- <*> pure lbndrs'
- <*> located renameContext lctx
- <*> renameLType lt
-renameType (HsTyVar name) = HsTyVar <$> renameName name
-renameType (HsAppTy lf la) = HsAppTy <$> renameLType lf <*> renameLType la
-renameType (HsFunTy la lr) = HsFunTy <$> renameLType la <*> renameLType lr
-renameType (HsListTy lt) = HsListTy <$> renameLType lt
-renameType (HsPArrTy lt) = HsPArrTy <$> renameLType lt
-renameType (HsTupleTy srt lt) = HsTupleTy srt <$> mapM renameLType lt
-renameType (HsOpTy la lop lb) =
- HsOpTy <$> renameLType la <*> renameLTyOp lop <*> renameLType lb
-renameType (HsParTy lt) = HsParTy <$> renameLType lt
-renameType (HsIParamTy ip lt) = HsIParamTy ip <$> renameLType lt
-renameType (HsEqTy la lb) = HsEqTy <$> renameLType la <*> renameLType lb
-renameType (HsKindSig lt lk) = HsKindSig <$> renameLType lt <*> pure lk
-renameType t@(HsQuasiQuoteTy _) = pure t
-renameType t@(HsSpliceTy _ _) = pure t
-renameType (HsDocTy lt doc) = HsDocTy <$> renameLType lt <*> pure doc
-renameType (HsBangTy bang lt) = HsBangTy bang <$> renameLType lt
-renameType t@(HsRecTy _) = pure t
-renameType t@(HsCoreTy _) = pure t
-renameType (HsExplicitListTy ph ltys) =
- HsExplicitListTy ph <$> renameLTypes ltys
-renameType (HsExplicitTupleTy phs ltys) =
- HsExplicitTupleTy phs <$> renameLTypes ltys
-renameType t@(HsTyLit _) = pure t
-renameType (HsWrapTy wrap t) = HsWrapTy wrap <$> renameType t
-renameType HsWildcardTy = pure HsWildcardTy
-renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
-
-
-renameLType :: SetName name => LHsType name -> Rename name (LHsType name)
-renameLType = located renameType
-
-
-renameLTypes :: SetName name => [LHsType name] -> Rename name [LHsType name]
-renameLTypes = mapM renameLType
-
-
-renameContext :: SetName name => HsContext name -> Rename name (HsContext name)
-renameContext = renameLTypes
-
-
-renameLTyOp :: SetName name => LHsTyOp name -> Rename name (LHsTyOp name)
-renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
-
-
-renameName :: SetName name => name -> Rename name name
-renameName name = do
- RenameEnv { rneCtx = ctx } <- ask
- pure $ case Map.lookup (getName name) ctx of
- Just name' -> name'
- Nothing -> name
-
-
-rebind :: SetName name
- => LHsTyVarBndrs name -> (LHsTyVarBndrs name -> Rename name a)
- -> Rename name a
-rebind lbndrs action = do
- (lbndrs', env') <- runState (rebindLTyVarBndrs lbndrs) <$> ask
- local (const env') (action lbndrs')
-
-
-rebindLTyVarBndrs :: SetName name
- => LHsTyVarBndrs name -> Rebind name (LHsTyVarBndrs name)
-rebindLTyVarBndrs lbndrs = do
- tys' <- mapM (located rebindTyVarBndr) $ hsq_tvs lbndrs
- pure $ lbndrs { hsq_tvs = tys' }
-
-
-rebindTyVarBndr :: SetName name
- => HsTyVarBndr name -> Rebind name (HsTyVarBndr name)
-rebindTyVarBndr (UserTyVar name) =
- UserTyVar <$> rebindName name
-rebindTyVarBndr (KindedTyVar name kinds) =
- KindedTyVar <$> located rebindName name <*> pure kinds
-
-
-rebindName :: SetName name => name -> Rebind name name
-rebindName name = do
- RenameEnv { .. } <- get
- taken <- takenNames
- case Map.lookup (getName name) rneCtx of
- Just name' -> pure name'
- Nothing | getNameRep name `Set.member` taken -> freshName name
- Nothing -> reuseName name
-
-
--- | Generate fresh occurrence name, put it into context and return.
-freshName :: SetName name => name -> Rebind name name
-freshName name = do
- env@RenameEnv { .. } <- get
- taken <- takenNames
- let name' = setInternalNameRep (findFreshName taken rep) name
- put $ env { rneCtx = Map.insert nname name' rneCtx }
- return name'
- where
- nname = getName name
- rep = getNameRep nname
-
-
-reuseName :: SetName name => name -> Rebind name name
-reuseName name = do
- env@RenameEnv { .. } <- get
- put $ env { rneCtx = Map.insert (getName name) name rneCtx }
- return name
-
-
-takenNames :: NamedThing name => Rebind name (Set NameRep)
-takenNames = do
- RenameEnv { .. } <- get
- return $ Set.union rneFV (ctxElems rneCtx)
- where
- ctxElems = Set.fromList . map getNameRep . Map.elems
-
-
-findFreshName :: Set NameRep -> NameRep -> NameRep
-findFreshName taken =
- fromJust . List.find isFresh . alternativeNames
- where
- isFresh = not . flip Set.member taken
-
-
-alternativeNames :: NameRep -> [NameRep]
-alternativeNames name
- | [_] <- nameRepString name = letterNames ++ alternativeNames' name
- where
- letterNames = map (stringNameRep . pure) ['a'..'z']
-alternativeNames name = alternativeNames' name
-
-
-alternativeNames' :: NameRep -> [NameRep]
-alternativeNames' name =
- [ stringNameRep $ str ++ show i | i :: Int <- [0..] ]
- where
- str = nameRepString name
-
-
-located :: Functor f => (a -> f b) -> Located a -> f (Located b)
-located f (L loc e) = L loc <$> f e
-
-
-tyVarName :: HsTyVarBndr name -> name
-tyVarName (UserTyVar name) = name
-tyVarName (KindedTyVar (L _ name) _) = name
diff --git a/haddock-api/src/Haddock/Convert.hs b/haddock-api/src/Haddock/Convert.hs
index 095bd9e0..c9664652 100644
--- a/haddock-api/src/Haddock/Convert.hs
+++ b/haddock-api/src/Haddock/Convert.hs
@@ -25,7 +25,6 @@ import Data.Either (lefts, rights)
import Data.List( partition )
import DataCon
import FamInstEnv
-import Haddock.Types
import HsSyn
import Kind ( splitKindFunTys, synTyConResKind, isKind )
import Name
@@ -41,6 +40,9 @@ import TysWiredIn ( listTyConName, eqTyCon )
import Unique ( getUnique )
import Var
+import Haddock.Types
+import Haddock.Interface.Specialize
+
-- the main function here! yay!
@@ -390,7 +392,7 @@ synifyKindSig :: Kind -> LHsKind Name
synifyKindSig k = synifyType WithinType k
synifyInstHead :: ([TyVar], [PredType], Class, [Type]) -> InstHead Name
-synifyInstHead (_, preds, cls, types) = InstHead
+synifyInstHead (_, preds, cls, types) = specializeInstHead $ InstHead
{ ihdClsName = getName cls
, ihdKinds = map (unLoc . synifyType WithinType) ks
, ihdTypes = map (unLoc . synifyType WithinType) ts
diff --git a/haddock-api/src/Haddock/Interface/Specialize.hs b/haddock-api/src/Haddock/Interface/Specialize.hs
new file mode 100644
index 00000000..df7f63bc
--- /dev/null
+++ b/haddock-api/src/Haddock/Interface/Specialize.hs
@@ -0,0 +1,396 @@
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE Rank2Types #-}
+{-# LANGUAGE ScopedTypeVariables #-}
+{-# LANGUAGE RecordWildCards #-}
+
+
+module Haddock.Interface.Specialize
+ ( specializeInstHead
+ ) where
+
+
+import Haddock.Syb
+import Haddock.Types
+
+import GHC
+import Name
+import FastString
+
+import Control.Monad
+import Control.Monad.Trans.Reader
+import Control.Monad.Trans.State
+
+import Data.Data
+import qualified Data.List as List
+import Data.Maybe
+import Data.Map (Map)
+import qualified Data.Map as Map
+import Data.Set (Set)
+import qualified Data.Set as Set
+
+
+-- | Instantiate all occurrences of given name with particular type.
+specialize :: (Eq name, Typeable name)
+ => Data a
+ => name -> HsType name -> a -> a
+specialize name details =
+ everywhere $ mkT step
+ where
+ step (HsTyVar name') | name == name' = details
+ step typ = typ
+
+
+-- | Instantiate all occurrences of given names with corresponding types.
+--
+-- It is just a convenience function wrapping 'specialize' that supports more
+-- that one specialization.
+specialize' :: (Eq name, Typeable name)
+ => Data a
+ => [(name, HsType name)] -> a -> a
+specialize' = flip $ foldr (uncurry specialize)
+
+
+-- | Instantiate given binders with corresponding types.
+--
+-- Again, it is just a convenience function around 'specialize'. Note that
+-- length of type list should be the same as the number of binders.
+specializeTyVarBndrs :: (Eq name, Typeable name, DataId name)
+ => Data a
+ => LHsTyVarBndrs name -> [HsType name]
+ -> a -> a
+specializeTyVarBndrs bndrs typs =
+ specialize' $ zip bndrs' typs
+ where
+ bndrs' = map (bname . unLoc) . hsq_tvs $ bndrs
+ bname (UserTyVar name) = name
+ bname (KindedTyVar (L _ name) _) = name
+
+
+specializePseudoFamilyDecl :: (Eq name, Typeable name, DataId name)
+ => LHsTyVarBndrs name -> [HsType name]
+ -> PseudoFamilyDecl name
+ -> PseudoFamilyDecl name
+specializePseudoFamilyDecl bndrs typs decl =
+ decl { pfdTyVars = map specializeTyVars (pfdTyVars decl) }
+ where
+ specializeTyVars = specializeTyVarBndrs bndrs typs
+
+
+specializeSig :: (Eq name, Typeable name, DataId name, SetName name)
+ => LHsTyVarBndrs name -> [HsType name]
+ -> Sig name
+ -> Sig name
+specializeSig bndrs typs (TypeSig lnames (L loc typ) prn) =
+ TypeSig lnames (L loc typ') prn
+ where
+ typ' = rename fv . sugar $ specializeTyVarBndrs bndrs typs typ
+ fv = foldr Set.union Set.empty . map freeVariables $ typs
+specializeSig _ _ sig = sig
+
+
+specializeInstHead :: (Eq name, Typeable name, DataId name, SetName name)
+ => InstHead name -> InstHead name
+specializeInstHead ihd@InstHead { ihdInstType = clsi@ClassInst { .. }, .. } =
+ ihd { ihdInstType = instType' }
+ where
+ instType' = clsi
+ { clsiSigs = map specializeSig' clsiSigs
+ , clsiAssocTys = map specializeFamilyDecl' clsiAssocTys
+ }
+ specializeSig' = specializeSig clsiTyVars ihdTypes
+ specializeFamilyDecl' = specializePseudoFamilyDecl clsiTyVars ihdTypes
+specializeInstHead ihd = ihd
+
+
+-- | Make given type use tuple and list literals where appropriate.
+--
+-- After applying 'specialize' function some terms may not use idiomatic list
+-- and tuple literals resulting in types like @[] a@ or @(,,) a b c@. This
+-- can be fixed using 'sugar' function, that will turn such types into @[a]@
+-- and @(a, b, c)@.
+sugar :: forall name. (NamedThing name, DataId name)
+ => HsType name -> HsType name
+sugar =
+ everywhere $ mkT step
+ where
+ step :: HsType name -> HsType name
+ step = sugarTuples . sugarLists
+
+
+sugarLists :: NamedThing name => HsType name -> HsType name
+sugarLists (HsAppTy (L _ (HsTyVar name)) ltyp)
+ | isBuiltInSyntax name' && strName == "[]" = HsListTy ltyp
+ where
+ name' = getName name
+ strName = occNameString . nameOccName $ name'
+sugarLists typ = typ
+
+
+sugarTuples :: NamedThing name => HsType name -> HsType name
+sugarTuples typ =
+ aux [] typ
+ where
+ aux apps (HsAppTy (L _ ftyp) atyp) = aux (atyp:apps) ftyp
+ aux apps (HsParTy (L _ typ')) = aux apps typ'
+ aux apps (HsTyVar name)
+ | isBuiltInSyntax name' && suitable = HsTupleTy HsBoxedTuple apps
+ where
+ name' = getName name
+ strName = occNameString . nameOccName $ name'
+ suitable = case parseTupleArity strName of
+ Just arity -> arity == length apps
+ Nothing -> False
+ aux _ _ = typ
+
+
+-- | Compute arity of given tuple operator.
+--
+-- >>> parseTupleArity "(,,)"
+-- Just 3
+--
+-- >>> parseTupleArity "(,,,,)"
+-- Just 5
+--
+-- >>> parseTupleArity "abc"
+-- Nothing
+--
+-- >>> parseTupleArity "()"
+-- Nothing
+parseTupleArity :: String -> Maybe Int
+parseTupleArity ('(':commas) = do
+ n <- parseCommas commas
+ guard $ n /= 0
+ return $ n + 1
+ where
+ parseCommas (',':rest) = (+ 1) <$> parseCommas rest
+ parseCommas ")" = Just 0
+ parseCommas _ = Nothing
+parseTupleArity _ = Nothing
+
+
+-- | Haskell AST type representation.
+--
+-- This type is used for renaming (more below), essentially the ambiguous (!)
+-- version of 'Name'. So, why is this 'FastString' instead of 'OccName'? Well,
+-- it was 'OccName' before, but turned out that 'OccName' sometimes also
+-- contains namespace information, differentiating visually same types.
+--
+-- And 'FastString' is used because it is /visual/ part of 'OccName' - it is
+-- not converted to 'String' or alike to avoid new allocations. Additionally,
+-- since it is stored mostly in 'Set', fast comparison of 'FastString' is also
+-- quite nice.
+type NameRep = FastString
+
+getNameRep :: NamedThing name => name -> NameRep
+getNameRep = occNameFS . getOccName
+
+nameRepString :: NameRep -> String
+nameRepString = unpackFS
+
+stringNameRep :: String -> NameRep
+stringNameRep = mkFastString
+
+setInternalNameRep :: SetName name => NameRep -> name -> name
+setInternalNameRep = setInternalOccName . mkVarOccFS
+
+setInternalOccName :: SetName name => OccName -> name -> name
+setInternalOccName occ name =
+ setName nname' name
+ where
+ nname = getName name
+ nname' = mkInternalName (nameUnique nname) occ (nameSrcSpan nname)
+
+
+-- | Compute set of free variables of given type.
+freeVariables :: forall name. (NamedThing name, DataId name)
+ => HsType name -> Set NameRep
+freeVariables =
+ everythingWithState Set.empty Set.union query
+ where
+ query term ctx = case cast term :: Maybe (HsType name) of
+ Just (HsForAllTy _ _ bndrs _ _) ->
+ (Set.empty, Set.union ctx (bndrsNames bndrs))
+ Just (HsTyVar name)
+ | getName name `Set.member` ctx -> (Set.empty, ctx)
+ | otherwise -> (Set.singleton $ getNameRep name, ctx)
+ _ -> (Set.empty, ctx)
+ bndrsNames = Set.fromList . map (getName . tyVarName . unLoc) . hsq_tvs
+
+
+-- | Make given type visually unambiguous.
+--
+-- After applying 'specialize' method, some free type variables may become
+-- visually ambiguous - for example, having @a -> b@ and specializing @a@ to
+-- @(a -> b)@ we get @(a -> b) -> b@ where first occurrence of @b@ refers to
+-- different type variable than latter one. Applying 'rename' function
+-- will fix that type to be visually unambiguous again (making it something
+-- like @(a -> c) -> b@).
+rename :: SetName name => Set NameRep -> HsType name -> HsType name
+rename fv typ = runReader (renameType typ) $ RenameEnv
+ { rneFV = fv
+ , rneCtx = Map.empty
+ }
+
+
+-- | Renaming monad.
+type Rename name = Reader (RenameEnv name)
+
+-- | Binding generation monad.
+type Rebind name = State (RenameEnv name)
+
+data RenameEnv name = RenameEnv
+ { rneFV :: Set NameRep
+ , rneCtx :: Map Name name
+ }
+
+
+renameType :: SetName name => HsType name -> Rename name (HsType name)
+renameType (HsForAllTy ex mspan lbndrs lctx lt) = rebind lbndrs $ \lbndrs' ->
+ HsForAllTy
+ <$> pure ex
+ <*> pure mspan
+ <*> pure lbndrs'
+ <*> located renameContext lctx
+ <*> renameLType lt
+renameType (HsTyVar name) = HsTyVar <$> renameName name
+renameType (HsAppTy lf la) = HsAppTy <$> renameLType lf <*> renameLType la
+renameType (HsFunTy la lr) = HsFunTy <$> renameLType la <*> renameLType lr
+renameType (HsListTy lt) = HsListTy <$> renameLType lt
+renameType (HsPArrTy lt) = HsPArrTy <$> renameLType lt
+renameType (HsTupleTy srt lt) = HsTupleTy srt <$> mapM renameLType lt
+renameType (HsOpTy la lop lb) =
+ HsOpTy <$> renameLType la <*> renameLTyOp lop <*> renameLType lb
+renameType (HsParTy lt) = HsParTy <$> renameLType lt
+renameType (HsIParamTy ip lt) = HsIParamTy ip <$> renameLType lt
+renameType (HsEqTy la lb) = HsEqTy <$> renameLType la <*> renameLType lb
+renameType (HsKindSig lt lk) = HsKindSig <$> renameLType lt <*> pure lk
+renameType t@(HsQuasiQuoteTy _) = pure t
+renameType t@(HsSpliceTy _ _) = pure t
+renameType (HsDocTy lt doc) = HsDocTy <$> renameLType lt <*> pure doc
+renameType (HsBangTy bang lt) = HsBangTy bang <$> renameLType lt
+renameType t@(HsRecTy _) = pure t
+renameType t@(HsCoreTy _) = pure t
+renameType (HsExplicitListTy ph ltys) =
+ HsExplicitListTy ph <$> renameLTypes ltys
+renameType (HsExplicitTupleTy phs ltys) =
+ HsExplicitTupleTy phs <$> renameLTypes ltys
+renameType t@(HsTyLit _) = pure t
+renameType (HsWrapTy wrap t) = HsWrapTy wrap <$> renameType t
+renameType HsWildcardTy = pure HsWildcardTy
+renameType (HsNamedWildcardTy name) = HsNamedWildcardTy <$> renameName name
+
+
+renameLType :: SetName name => LHsType name -> Rename name (LHsType name)
+renameLType = located renameType
+
+
+renameLTypes :: SetName name => [LHsType name] -> Rename name [LHsType name]
+renameLTypes = mapM renameLType
+
+
+renameContext :: SetName name => HsContext name -> Rename name (HsContext name)
+renameContext = renameLTypes
+
+
+renameLTyOp :: SetName name => LHsTyOp name -> Rename name (LHsTyOp name)
+renameLTyOp (wrap, lname) = (,) wrap <$> located renameName lname
+
+
+renameName :: SetName name => name -> Rename name name
+renameName name = do
+ RenameEnv { rneCtx = ctx } <- ask
+ pure $ case Map.lookup (getName name) ctx of
+ Just name' -> name'
+ Nothing -> name
+
+
+rebind :: SetName name
+ => LHsTyVarBndrs name -> (LHsTyVarBndrs name -> Rename name a)
+ -> Rename name a
+rebind lbndrs action = do
+ (lbndrs', env') <- runState (rebindLTyVarBndrs lbndrs) <$> ask
+ local (const env') (action lbndrs')
+
+
+rebindLTyVarBndrs :: SetName name
+ => LHsTyVarBndrs name -> Rebind name (LHsTyVarBndrs name)
+rebindLTyVarBndrs lbndrs = do
+ tys' <- mapM (located rebindTyVarBndr) $ hsq_tvs lbndrs
+ pure $ lbndrs { hsq_tvs = tys' }
+
+
+rebindTyVarBndr :: SetName name
+ => HsTyVarBndr name -> Rebind name (HsTyVarBndr name)
+rebindTyVarBndr (UserTyVar name) =
+ UserTyVar <$> rebindName name
+rebindTyVarBndr (KindedTyVar name kinds) =
+ KindedTyVar <$> located rebindName name <*> pure kinds
+
+
+rebindName :: SetName name => name -> Rebind name name
+rebindName name = do
+ RenameEnv { .. } <- get
+ taken <- takenNames
+ case Map.lookup (getName name) rneCtx of
+ Just name' -> pure name'
+ Nothing | getNameRep name `Set.member` taken -> freshName name
+ Nothing -> reuseName name
+
+
+-- | Generate fresh occurrence name, put it into context and return.
+freshName :: SetName name => name -> Rebind name name
+freshName name = do
+ env@RenameEnv { .. } <- get
+ taken <- takenNames
+ let name' = setInternalNameRep (findFreshName taken rep) name
+ put $ env { rneCtx = Map.insert nname name' rneCtx }
+ return name'
+ where
+ nname = getName name
+ rep = getNameRep nname
+
+
+reuseName :: SetName name => name -> Rebind name name
+reuseName name = do
+ env@RenameEnv { .. } <- get
+ put $ env { rneCtx = Map.insert (getName name) name rneCtx }
+ return name
+
+
+takenNames :: NamedThing name => Rebind name (Set NameRep)
+takenNames = do
+ RenameEnv { .. } <- get
+ return $ Set.union rneFV (ctxElems rneCtx)
+ where
+ ctxElems = Set.fromList . map getNameRep . Map.elems
+
+
+findFreshName :: Set NameRep -> NameRep -> NameRep
+findFreshName taken =
+ fromJust . List.find isFresh . alternativeNames
+ where
+ isFresh = not . flip Set.member taken
+
+
+alternativeNames :: NameRep -> [NameRep]
+alternativeNames name
+ | [_] <- nameRepString name = letterNames ++ alternativeNames' name
+ where
+ letterNames = map (stringNameRep . pure) ['a'..'z']
+alternativeNames name = alternativeNames' name
+
+
+alternativeNames' :: NameRep -> [NameRep]
+alternativeNames' name =
+ [ stringNameRep $ str ++ show i | i :: Int <- [0..] ]
+ where
+ str = nameRepString name
+
+
+located :: Functor f => (a -> f b) -> Located a -> f (Located b)
+located f (L loc e) = L loc <$> f e
+
+
+tyVarName :: HsTyVarBndr name -> name
+tyVarName (UserTyVar name) = name
+tyVarName (KindedTyVar (L _ name) _) = name
diff --git a/haddock.cabal b/haddock.cabal
index 4ea2a82a..71b78347 100644
--- a/haddock.cabal
+++ b/haddock.cabal
@@ -90,6 +90,7 @@ executable haddock
Haddock.Interface.AttachInstances
Haddock.Interface.LexParseRn
Haddock.Interface.ParseModuleHeader
+ Haddock.Interface.Specialize
Haddock.Parser
Haddock.Utils
Haddock.Backends.Xhtml
@@ -97,7 +98,6 @@ executable haddock
Haddock.Backends.Xhtml.DocMarkup
Haddock.Backends.Xhtml.Layout
Haddock.Backends.Xhtml.Names
- Haddock.Backends.Xhtml.Specialize
Haddock.Backends.Xhtml.Themes
Haddock.Backends.Xhtml.Types
Haddock.Backends.Xhtml.Utils
--
cgit v1.2.3
From ca667e192d0867c9c2a3025918414147f50b7c19 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 31 Jul 2015 12:38:59 +0200
Subject: Fix bug with missing space in documentation for associated types.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 7da1f08e..af946f9f 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -567,18 +567,13 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
TypeInst rhs ->
(ptype, mdoc, [])
where
- ptype = mconcat
- [ keyword "type"
- , typ
- , maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
- ]
+ ptype = keyword "type" <+> typ <+> prhs
+ prhs = maybe noHtml (\t -> equals <+> ppType unicode qual t) rhs
DataInst dd ->
(pdata, mdoc, [])
where
- pdata = mconcat
- [ keyword "data" <+> typ
- , ppShortDataDecl False True dd unicode qual
- ]
+ pdata = keyword "data" <+> typ <+> pdecl
+ pdecl = ppShortDataDecl False True dd unicode qual
where
typ = ppAppNameTypes ihdClsName ihdKinds ihdTypes unicode qual
--
cgit v1.2.3
From a49a3f0840f2880814c35f58d89805b3cd3039d3 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 31 Jul 2015 16:24:36 +0200
Subject: Fix issue with incorrect instance details sections being expanded.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 39 ++++++++++++++++++++++----
1 file changed, 34 insertions(+), 5 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index af946f9f..35e5c5f6 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE TransformListComp #-}
{-# LANGUAGE RecordWildCards #-}
+{-# LANGUAGE Rank2Types #-}
-----------------------------------------------------------------------------
-- |
-- Module : Haddock.Backends.Html.Decl
@@ -26,8 +27,11 @@ import Haddock.Backends.Xhtml.Types
import Haddock.Backends.Xhtml.Utils
import Haddock.GhcUtils
import Haddock.Types
+import Haddock.Syb
import Haddock.Doc (combineDocumentation)
+import Data.Bits
+import Data.Data (Data, cast)
import Data.List ( intersperse, sort )
import qualified Data.Map as Map
import Data.Maybe
@@ -35,6 +39,7 @@ import Text.XHtml hiding ( name, title, p, quote )
import GHC
import GHC.Exts
+import Unique
import Name
import BooleanFormula
@@ -553,7 +558,7 @@ ppInstances links origin instances baseName splice unicode qual
ppInstHead :: LinksInfo -> Splice -> Unicode -> Qualification
-> Maybe (MDoc DocName) -> InstOrigin -> Int -> InstHead DocName
-> SubDecl
-ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
+ppInstHead links splice unicode qual mdoc origin no ihd@(InstHead {..}) =
case ihdInstType of
ClassInst { .. } ->
( subInstHead iid $ ppContextNoLocs clsiCtx unicode qual <+> typ
@@ -561,7 +566,7 @@ ppInstHead links splice unicode qual mdoc origin no (InstHead {..}) =
, [subInstDetails iid ats sigs]
)
where
- iid = instanceId origin no ihdClsName
+ iid = instanceId origin no ihd
sigs = ppInstanceSigs links splice unicode qual clsiSigs
ats = ppInstanceAssocTys links splice unicode qual clsiAssocTys
TypeInst rhs ->
@@ -600,15 +605,39 @@ lookupAnySubdoc :: Eq id1 => id1 -> [(id1, DocForDecl id2)] -> DocForDecl id2
lookupAnySubdoc n = fromMaybe noDocForDecl . lookup n
-instanceId :: InstOrigin -> Int -> DocName -> String
-instanceId orgin no name =
- qual orgin ++ ":" ++ (occNameString . getOccName) name ++ "-" ++ show no
+instanceId :: InstOrigin -> Int -> InstHead DocName -> String
+instanceId orgin no ihd = concat
+ [ qual orgin
+ , ":" ++ (occNameString . getOccName . ihdClsName) ihd
+ , "-" ++ show (instHeadId ihd)
+ , "-" ++ show no
+ ]
where
qual OriginClass = "ic"
qual OriginData = "id"
qual OriginFamily = "if"
+-- | Compute unique identifier for given instance.
+--
+-- This is rather poor way of doing it. Ideally, we would like to have
+-- everything wrapped in a stateful monad that allows us to generate unique
+-- identifiers as needed. Since introducing such monad would require major
+-- refactoring, for now we just generate naive hash for given instance.
+--
+-- Hashing is very, very trivial and turns a list of 'DocName' to 'Int'. Idea
+-- for such simple hash function is stolen from
+-- .
+instHeadId :: InstHead DocName -> Int
+instHeadId (InstHead { .. }) =
+ djb2 . map key $ [ihdClsName] ++ names ihdTypes ++ names ihdKinds
+ where
+ names = everything (++) $
+ maybeToList . (cast :: forall a. Data a => a -> Maybe DocName)
+ djb2 = foldl (\h c -> h * 33 `xor` c) 5381
+ key = getKey . nameUnique . getName
+
+
-------------------------------------------------------------------------------
-- * Data & newtype declarations
-------------------------------------------------------------------------------
--
cgit v1.2.3
From 96a118be9d02cc433f0982ca728e5c80a2c4c8af Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 31 Jul 2015 18:24:40 +0200
Subject: Make section identifier of instance details more GHC-independent.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index 35e5c5f6..c30d0e62 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -39,7 +39,6 @@ import Text.XHtml hiding ( name, title, p, quote )
import GHC
import GHC.Exts
-import Unique
import Name
import BooleanFormula
@@ -609,8 +608,8 @@ instanceId :: InstOrigin -> Int -> InstHead DocName -> String
instanceId orgin no ihd = concat
[ qual orgin
, ":" ++ (occNameString . getOccName . ihdClsName) ihd
- , "-" ++ show (instHeadId ihd)
- , "-" ++ show no
+ , ":" ++ show (instHeadId ihd)
+ , ":" ++ show no
]
where
qual OriginClass = "ic"
@@ -626,16 +625,27 @@ instanceId orgin no ihd = concat
-- refactoring, for now we just generate naive hash for given instance.
--
-- Hashing is very, very trivial and turns a list of 'DocName' to 'Int'. Idea
--- for such simple hash function is stolen from
+-- for such simple hash function (djb2) is stolen from
-- .
+--
+-- Hashing is performed on string representation of `Name`. Why string instead
+-- of 'Unique' of that 'Name'? That would be much faster and nicer, yes.
+-- However, 'Unique' is not very deterministic, so running it on different
+-- configurations would yield different HTML documents. This is not very bad,
+-- as nobody cares about these identifiers but it would require us to strip
+-- section anchors in testing framework and that is not only inconvenient but
+-- also makes testing less viable. And it is only temporary solution so we can
+-- live with it.
instHeadId :: InstHead DocName -> Int
instHeadId (InstHead { .. }) =
djb2 . map key $ [ihdClsName] ++ names ihdTypes ++ names ihdKinds
where
names = everything (++) $
maybeToList . (cast :: forall a. Data a => a -> Maybe DocName)
- djb2 = foldl (\h c -> h * 33 `xor` c) 5381
- key = getKey . nameUnique . getName
+ key = djb2 . occNameString . nameOccName . getName
+
+ djb2 :: Enum a => [a] -> Int
+ djb2 = foldl (\h c -> h * 33 `xor` fromEnum c) 5381
-------------------------------------------------------------------------------
--
cgit v1.2.3
From 472440c233fccf662ff41193db66c62e7bc6f6d1 Mon Sep 17 00:00:00 2001
From: Łukasz Hanuszczak
Date: Fri, 31 Jul 2015 20:16:33 +0200
Subject: Make identifier generation also architecture-independent.
---
haddock-api/src/Haddock/Backends/Xhtml/Decl.hs | 12 ++++---
html-test/ref/Bug7.html | 8 ++---
html-test/ref/Hash.html | 8 ++---
html-test/ref/HiddenInstances.html | 16 +++++-----
html-test/ref/HiddenInstancesB.html | 8 ++---
html-test/ref/QuasiExpr.html | 4 +--
html-test/ref/SpuriousSuperclassConstraints.html | 8 ++---
html-test/ref/Test.html | 4 +--
html-test/ref/TypeFamilies.html | 40 ++++++++++++------------
9 files changed, 55 insertions(+), 53 deletions(-)
(limited to 'haddock-api/src/Haddock/Backends')
diff --git a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
index c30d0e62..f1203210 100644
--- a/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
+++ b/haddock-api/src/Haddock/Backends/Xhtml/Decl.hs
@@ -31,10 +31,12 @@ import Haddock.Syb
import Haddock.Doc (combineDocumentation)
import Data.Bits
+import Data.Char
import Data.Data (Data, cast)
import Data.List ( intersperse, sort )
import qualified Data.Map as Map
import Data.Maybe
+import Data.Word
import Text.XHtml hiding ( name, title, p, quote )
import GHC
@@ -636,16 +638,16 @@ instanceId orgin no ihd = concat
-- section anchors in testing framework and that is not only inconvenient but
-- also makes testing less viable. And it is only temporary solution so we can
-- live with it.
-instHeadId :: InstHead DocName -> Int
+instHeadId :: InstHead DocName -> Word64
instHeadId (InstHead { .. }) =
- djb2 . map key $ [ihdClsName] ++ names ihdTypes ++ names ihdKinds
+ djb2 id . map key $ [ihdClsName] ++ names ihdTypes ++ names ihdKinds
where
names = everything (++) $
maybeToList . (cast :: forall a. Data a => a -> Maybe DocName)
- key = djb2 . occNameString . nameOccName . getName
+ key = djb2 (fromIntegral . ord) . occNameString . nameOccName . getName
- djb2 :: Enum a => [a] -> Int
- djb2 = foldl (\h c -> h * 33 `xor` fromEnum c) 5381
+ djb2 :: (a -> Word64) -> [a] -> Word64
+ djb2 conv = foldl (\h c -> h * 33 `xor` conv c) 5381
-------------------------------------------------------------------------------
diff --git a/html-test/ref/Bug7.html b/html-test/ref/Bug7.html
index f0fbc44d..4c0ba83b 100644
--- a/html-test/ref/Bug7.html
+++ b/html-test/ref/Bug7.html
@@ -106,7 +106,7 @@ window.onload = function () {pageLoad();setSynopsis("mini_Bug7.html");};
>