@@ -22,6 +22,7 @@ import Data.Function (on)
2222import Data.List
2323import Data.Maybe (fromMaybe)
2424import Data.Version (showVersion)
25+import Data.Foldable (traverse_)
2526
2627import Options.Applicative
2728
@@ -30,13 +31,11 @@ import qualified Paths_purescript as Paths
3031import System.Exit (exitSuccess, exitFailure)
3132import System.IO (hPutStrLn, stderr)
3233
33−
3434data PSCDocsOptions = PSCDocsOptions
3535 { pscdIncludeHeir :: Bool
3636 , pscdInputFiles :: [FilePath]
3737 }
3838
39−
4039docgen :: PSCDocsOptions -> IO ()
4140docgen (PSCDocsOptions showHierarchy input) = do
4241 e <- P.parseModulesFromFiles (fromMaybe "") <$> mapM (fmap (first Just) . parseFile) (nub input)
@@ -62,10 +61,16 @@ spacer = tell [""]
6261headerLevel :: Int -> String -> Docs
6362headerLevel level hdr = tell [replicate level '#' ++ ' ' : hdr]
6463
64+withIndent :: Int -> Docs -> Docs
65+withIndent indent = censor (map (replicate indent ' ' ++ ))
66+
6567atIndent :: Int -> String -> Docs
6668atIndent indent text =
6769 let ls = lines text in
68− forM_ ls $ \l -> tell [replicate indent ' ' ++ l]
70+ withIndent indent (tell ls)
71+
72+ticks :: String -> String
73+ticks = ("`" ++) . (++ "`")
6974
7075renderModules :: Bool -> [P.Module] -> Docs
7176renderModules showHierarchy ms = do
@@ -109,7 +114,9 @@ renderModule showHierarchy mdl@(P.Module moduleName _ exps) =
109114
110115renderTopLevel :: Maybe [P.DeclarationRef] -> [P.Declaration] -> Docs
111116renderTopLevel exps decls = forM_ (sortBy (compare `on` getName) decls) $ \decl -> do
112− renderDeclaration 4 exps decl
117+ traverse_ (headerLevel 4) (ticks `fmap` getDeclarationTitle decl)
118+ spacer
119+ renderDeclaration exps decl
113120 spacer
114121
115122renderTypeclassImage :: P.ModuleName -> Docs
@@ -117,53 +124,81 @@ renderTypeclassImage name =
117124 let name' = P.runModuleName name
118125 in tell [""]
119126
120−renderDeclaration :: Int -> Maybe [P.DeclarationRef] -> P.Declaration -> Docs
121−renderDeclaration n _ (P.TypeDeclaration ident ty) =
122− atIndent n $ show ident ++ " :: " ++ prettyPrintType' ty
123−renderDeclaration n _ (P.ExternDeclaration _ ident _ ty) =
124− atIndent n $ show ident ++ " :: " ++ prettyPrintType' ty
125−renderDeclaration n exps (P.DataDeclaration dtype name args ctors) = do
127+getDeclarationTitle :: P.Declaration -> Maybe String
128+getDeclarationTitle (P.TypeDeclaration name _) = Just (show name)
129+getDeclarationTitle (P.ExternDeclaration _ name _ _) = Just (show name)
130+getDeclarationTitle (P.DataDeclaration _ name _ _) = Just (show name)
131+getDeclarationTitle (P.ExternDataDeclaration name _) = Just (show name)
132+getDeclarationTitle (P.TypeSynonymDeclaration name _ _) = Just (show name)
133+getDeclarationTitle (P.TypeClassDeclaration name _ _ _) = Just (show name)
134+getDeclarationTitle (P.TypeInstanceDeclaration name _ _ _ _) = Just (show name)
135+getDeclarationTitle (P.PositionedDeclaration _ _ d) = getDeclarationTitle d
136+getDeclarationTitle _ = Nothing
137+
138+renderDeclaration :: Maybe [P.DeclarationRef] -> P.Declaration -> Docs
139+renderDeclaration _ (P.TypeDeclaration ident ty) =
140+ atIndent 4 $ show ident ++ " :: " ++ prettyPrintType' ty
141+renderDeclaration _ (P.ExternDeclaration _ ident _ ty) =
142+ atIndent 4 $ show ident ++ " :: " ++ prettyPrintType' ty
143+renderDeclaration exps (P.DataDeclaration dtype name args ctors) = do
126144 let
127145 typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
128146 typeName = prettyPrintType' typeApp
129147 exported = filter (P.isDctorExported name exps . fst) ctors
130− atIndent n $ show dtype ++ " " ++ typeName ++ (if null exported then "" else " where")
131− forM_ exported $ \(ctor, tys) ->
132− let ctorTy = foldr P.function typeApp tys
133− in atIndent (n + 2) $ P.runProperName ctor ++ " :: " ++ prettyPrintType' ctorTy
134−renderDeclaration n _ (P.ExternDataDeclaration name kind) =
135− atIndent n $ "data " ++ P.runProperName name ++ " :: " ++ P.prettyPrintKind kind
136−renderDeclaration n _ (P.TypeSynonymDeclaration name args ty) = do
148+ atIndent 4 $ show dtype ++ " " ++ typeName
149+ zipWithM_ (\isFirst (ctor, tys) ->
150+ atIndent 6 $ (if isFirst then "= " else "| ") ++ P.runProperName ctor ++ " " ++ unwords (map P.prettyPrintTypeAtom tys))
151+ (True : repeat False) exported
152+renderDeclaration _ (P.ExternDataDeclaration name kind) =
153+ atIndent 4 $ "data " ++ P.runProperName name ++ " :: " ++ P.prettyPrintKind kind
154+renderDeclaration _ (P.TypeSynonymDeclaration name args ty) = do
137155 let
138156 typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
139157 typeName = prettyPrintType' typeApp
140− atIndent n $ "type " ++ typeName ++ " = " ++ prettyPrintType' ty
141−renderDeclaration n exps (P.TypeClassDeclaration name args implies ds) = do
158+ atIndent 4 $ "type " ++ typeName ++ " = " ++ prettyPrintType' ty
159+renderDeclaration _ (P.TypeClassDeclaration name args implies ds) = do
142160 let impliesText = case implies of
143161 [] -> ""
144162 is -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map P.prettyPrintTypeAtom tys')) is) ++ ") <= "
145163 classApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
146164 className = prettyPrintType' classApp
147− atIndent n $ "class " ++ impliesText ++ className ++ " where"
148− mapM_ (renderDeclaration (n + 2) exps) ds
149−renderDeclaration n _ (P.TypeInstanceDeclaration name constraints className tys _) = do
165+ atIndent 4 $ "class " ++ impliesText ++ className ++ " where"
166+ mapM_ renderClassMember ds
167+ where
168+ renderClassMember (P.PositionedDeclaration _ _ d) = renderClassMember d
169+ renderClassMember (P.TypeDeclaration ident ty) = atIndent 6 $ show ident ++ " :: " ++ prettyPrintType' ty
170+ renderClassMember _ = error "Invalid argument to renderClassMember."
171+renderDeclaration _ (P.TypeInstanceDeclaration name constraints className tys _) = do
150172 let constraintsText = case constraints of
151173 [] -> ""
152174 cs -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map P.prettyPrintTypeAtom tys')) cs) ++ ") => "
153− atIndent n $ "instance " ++ show name ++ " :: " ++ constraintsText ++ show className ++ " " ++ unwords (map P.prettyPrintTypeAtom tys)
154−renderDeclaration n exps (P.PositionedDeclaration _ com d) = do
155− renderComments n com
156− spacer
157− renderDeclaration n exps d
158−renderDeclaration _ _ _ = return ()
159−
160−renderComments :: Int -> [P.Comment] -> Docs
161−renderComments n cs = mapM_ (atIndent n) ls
175+ atIndent 4 $ "instance " ++ show name ++ " :: " ++ constraintsText ++ show className ++ " " ++ unwords (map P.prettyPrintTypeAtom tys)
176+renderDeclaration exps (P.PositionedDeclaration _ com d) = do
177+ renderComments com
178+ renderDeclaration exps d
179+renderDeclaration _ _ = return ()
180+
181+renderComments :: [P.Comment] -> Docs
182+renderComments cs = do
183+ let raw = concatMap toLines cs
184+
185+ if all hasPipe raw
186+ then mapM_ (atIndent 0) (map stripPipes raw)
187+ else mapM_ (atIndent 4) raw
188+
189+ unless (null raw) spacer
162190 where
163− ls = concatMap toLines cs
164191
165192 toLines (P.LineComment s) = [s]
166193 toLines (P.BlockComment s) = lines s
194+
195+ hasPipe s = case dropWhile (== ' ') s of { ('|':_) -> True; _ -> False }
196+
197+ stripPipes = dropPipe . dropWhile (== ' ')
198+
199+ dropPipe ('|':' ':s) = s
200+ dropPipe ('|':s) = s
201+ dropPipe s = s
167202
168203toTypeVar :: (String, Maybe P.Kind) -> P.Type
169204toTypeVar (s, Nothing) = P.TypeVar s