Skip to content

Commit

Permalink
Generate documentation for local modules in the html backend (#2326)
Browse files Browse the repository at this point in the history
![image](https://github.com/anoma/juvix/assets/5511599/bffc47d1-660e-484a-8bca-e61fc7e15cd9)

Local modules are shown as regular statements, with their definitions
displayed nested
  • Loading branch information
janmasrovira authored Aug 31, 2023
1 parent 3c5304f commit f463aee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
21 changes: 17 additions & 4 deletions src/Juvix/Compiler/Backend/Html/Translation/FromTyped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ goTopModule cs m = do
content :: Sem s Html
content = do
preface' <- docPreface
interface' <- interface
interface' <- moduleInterface
return
$ Html.div
! Attr.id "content"
Expand Down Expand Up @@ -368,8 +368,8 @@ goTopModule cs m = do
! Attr.id "module-header"
$ (p ! Attr.class_ "caption" $ toHtml (prettyText tmp))

interface :: Sem s Html
interface = do
moduleInterface :: Sem s Html
moduleInterface = do
sigs' <- mconcatMapM goStatement (m ^. moduleBody)
return
$ Html.div
Expand Down Expand Up @@ -429,7 +429,7 @@ goStatement = \case
StatementFunctionDef t -> goFunctionDef t
StatementSyntax s -> goSyntax s
StatementImport {} -> mempty
StatementModule {} -> mempty -- TODO handle local modules
StatementModule m -> goLocalModule m
StatementProjectionDef {} -> mempty
where
goSyntax :: SyntaxDef 'Scoped -> Sem r Html
Expand Down Expand Up @@ -486,6 +486,19 @@ goAlias def = do
sig' <- ppCodeHtml defaultOptions def
defHeader (def ^. aliasDefName) sig' Nothing

-- | local modules generated by inductive types should not show up.
goLocalModule :: forall r. (Members '[Reader HtmlOptions, Reader NormalizedTable] r) => Module 'Scoped 'ModuleLocal -> Sem r Html
goLocalModule def = fmap (fromMaybe mempty) . runFail $ do
failWhen (def ^. moduleInductive)
sig' <- ppHelper (ppModuleHeader def)
header' <- defHeader (def ^. modulePath) sig' (def ^. moduleDoc)
body' <-
( Html.div
! Attr.class_ "subs"
)
<$> mconcatMap goStatement (def ^. moduleBody)
return (header' <> body')

goOpen :: forall r. (Members '[Reader HtmlOptions] r) => OpenModule 'Scoped -> Sem r Html
goOpen op
| Public <- op ^. openPublic = noDefHeader <$> ppCodeHtml defaultOptions op
Expand Down
21 changes: 13 additions & 8 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,19 @@ withNameIdSuffix nid a = do
instance PrettyPrint S.NameId where
ppCode (S.NameId k) = noLoc (pretty k)

ppModuleHeader :: (SingI t, SingI s) => PrettyPrinting (Module s t)
ppModuleHeader Module {..} = do
let modulePath' = ppModulePathType _modulePath
ppCode _moduleKw
<+> modulePath'

instance (SingI t, SingI s) => PrettyPrint (Module s t) where
ppCode :: forall r. (Members '[ExactPrint, Reader Options] r) => Module s t -> Sem r ()
ppCode Module {..} = do
ppCode m@Module {..} = do
let moduleBody' = localIndent (ppStatements _moduleBody)
modulePath' = ppModulePathType _modulePath
moduleDoc' = whenJust _moduleDoc ppCode
modulePragmas' = whenJust _modulePragmas ppCode
header' = ppModuleHeader m
body'
| null _moduleBody = ensureEmptyLine
| otherwise =
Expand All @@ -351,12 +357,11 @@ instance (SingI t, SingI s) => PrettyPrint (Module s t) where
<> line
moduleDoc'
<> modulePragmas'
<> ppCode _moduleKw
<+> modulePath'
<> ppCode Kw.delimSemicolon
<> line
<> body'
<> ending
<> header'
<> ppCode Kw.delimSemicolon
<> line
<> body'
<> ending
where
topSpace :: Sem r ()
topSpace = case sing :: SModuleIsTop t of
Expand Down

0 comments on commit f463aee

Please sign in to comment.