Skip to content

Commit

Permalink
Improve output of juvix dev import-tree print (#2976)
Browse files Browse the repository at this point in the history
Example:

Old:
```
Import Tree:
============

* Package at /home/jan/.config/juvix/0.6.5/package-base/
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/Bool.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/List.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/Maybe.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/Nat.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/String.juvix
Juvix/Builtin/V1.juvix imports Juvix/Builtin/V1/Trait/Natural.juvix
Juvix/Builtin/V1/List.juvix imports Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1/Nat.juvix imports Juvix/Builtin/V1/Nat/Base.juvix
Juvix/Builtin/V1/Nat.juvix imports Juvix/Builtin/V1/Trait/FromNatural.juvix
Juvix/Builtin/V1/Nat.juvix imports Juvix/Builtin/V1/Trait/Natural.juvix
Juvix/Builtin/V1/Nat/Base.juvix imports Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1/String.juvix imports Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1/Trait/FromNatural.juvix imports Juvix/Builtin/V1/Nat/Base.juvix
Juvix/Builtin/V1/Trait/Natural.juvix imports Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1/Trait/Natural.juvix imports Juvix/Builtin/V1/Nat/Base.juvix
Juvix/Builtin/V1/Trait/Natural.juvix imports Juvix/Builtin/V1/Trait/FromNatural.juvix
```

New:
```
Import Tree:
============

* Package at /home/jan/.config/juvix/0.6.5/package-base/
Nodes (10)
Juvix/Builtin/V1/Nat.juvix
Juvix/Builtin/V1/Nat/Base.juvix
Juvix/Builtin/V1/Fixity.juvix
Juvix/Builtin/V1/Trait/Natural.juvix
Juvix/Builtin/V1/Bool.juvix
Juvix/Builtin/V1.juvix
Juvix/Builtin/V1/List.juvix
Juvix/Builtin/V1/String.juvix
Juvix/Builtin/V1/Trait/FromNatural.juvix
Juvix/Builtin/V1/Maybe.juvix

Edges (17)
Juvix/Builtin/V1.juvix imports (7):
  • Juvix/Builtin/V1/Bool.juvix
  • Juvix/Builtin/V1/Fixity.juvix
  • Juvix/Builtin/V1/List.juvix
  • Juvix/Builtin/V1/Maybe.juvix
  • Juvix/Builtin/V1/Nat.juvix
  • Juvix/Builtin/V1/String.juvix
  • Juvix/Builtin/V1/Trait/Natural.juvix

Juvix/Builtin/V1/Bool.juvix imports (0):

Juvix/Builtin/V1/Fixity.juvix imports (0):

Juvix/Builtin/V1/List.juvix imports (1):
  • Juvix/Builtin/V1/Fixity.juvix

Juvix/Builtin/V1/Maybe.juvix imports (0):

Juvix/Builtin/V1/Nat.juvix imports (3):
  • Juvix/Builtin/V1/Nat/Base.juvix
  • Juvix/Builtin/V1/Trait/FromNatural.juvix
  • Juvix/Builtin/V1/Trait/Natural.juvix

Juvix/Builtin/V1/Nat/Base.juvix imports (1):
  • Juvix/Builtin/V1/Fixity.juvix

Juvix/Builtin/V1/String.juvix imports (1):
  • Juvix/Builtin/V1/Fixity.juvix

Juvix/Builtin/V1/Trait/FromNatural.juvix imports (1):
  • Juvix/Builtin/V1/Nat/Base.juvix

Juvix/Builtin/V1/Trait/Natural.juvix imports (3):
  • Juvix/Builtin/V1/Fixity.juvix
  • Juvix/Builtin/V1/Nat/Base.juvix
  • Juvix/Builtin/V1/Trait/FromNatural.juvix
```
  • Loading branch information
janmasrovira authored Aug 28, 2024
1 parent 7c8ac15 commit e0bbac2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,7 @@ instance PrettyPrint ImportTreeStats where
header "Import Tree Statistics:"
header "======================="
itemize
[ noLoc "Total number of modules:" <+> noLoc (pretty _importTreeStatsTotalModules),
[ noLoc "Total number of nodes:" <+> noLoc (pretty _importTreeStatsTotalModules),
noLoc "Total number of edges:" <+> noLoc (pretty _importTreeStatsTotalEdges),
noLoc "Height (longest chain of imports):" <+> noLoc (pretty _importTreeStatsHeight)
]
Expand All @@ -1268,33 +1268,41 @@ instance PrettyPrint ImportTree where
header "Import Tree:"
header "============"
hardline
forM_ (Map.toList importsTable) $ \(pkgRoot, tbl) -> do

forM_ (Map.toList importsTable) $ \(pkgRoot, tbl :: Map (Path Rel File) (Set ImportNode)) -> do
annotated AnnImportant (noLoc ("* Package at " <> pretty pkgRoot))
hardline
let pkgNodes :: HashSet ImportNode = fromJust (nodesByRoot ^. at pkgRoot)
header ("Nodes (" <> show (length pkgNodes) <> ")")
forM_ pkgNodes $ \node -> do
noLoc (pMod (node ^. importNodeFile))
hardline
hardline
let numEdges = sum (map length (toList tbl))
header ("Edges (" <> show numEdges <> ")")
forM_ (Map.toList tbl) $ \(fromFile, toFiles) -> do
forM_ toFiles $ \toFile -> do
let pMod :: Path x File -> Doc Ann
pMod = annotate (AnnKind KNameTopModule) . pretty
fromMod = pMod fromFile
toMod
noLoc (pMod fromFile P.<+> annotate AnnKeyword "imports" P.<+> "(" <> pretty (length toFiles) <> "):")
hardline
indent . itemize . (`map` (toList toFiles)) $ \toFile -> do
let toMod
| pkgRoot == toFile ^. importNodePackageRoot = pMod (toFile ^. importNodeFile)
| otherwise = pMod (toFile ^. importNodeAbsFile)
noLoc (fromMod P.<+> annotate AnnKeyword "imports" P.<+> toMod)
hardline
noLoc toMod
hardline
unless (null toFiles) hardline
hardline
where
pMod :: Path x File -> Doc Ann
pMod = annotate (AnnKind KNameTopModule) . pretty

allNodes :: [ImportNode]
allNodes = HashMap.keys (tree ^. importTree)

allRoots :: [Path Abs Dir]
allRoots = nubSort (map (^. importNodePackageRoot) allNodes)

nodesByRoot :: HashMap (Path Abs Dir) (HashSet ImportNode)
nodesByRoot =
foldl'
(HashMap.unionWith (<>))
mempty
[hashMap [(node ^. importNodePackageRoot, hashSet [node])] | node <- allNodes]
nodesByRoot = importTreeNodesByPackage tree

-- fromPackageRoot -> fromFile -> tofile
importsTable :: Map (Path Abs Dir) (Map (Path Rel File) (Set ImportNode))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Juvix.Compiler.Pipeline.Loader.PathResolver.ImportTree.Base
importTreeEdges,
importTreeNodes,
importTreeProjectNodes,
importTreeNodesByPackage,
ImportTreeBuilder,
runImportTreeBuilder,
ignoreImportTreeBuilder,
Expand Down Expand Up @@ -89,6 +90,12 @@ withImportNode fromNode m = do
(`interpret` m) $ \case
ImportTreeAddEdge importScan toNode -> internalRegisterEdge importScan fromNode toNode

importTreeNodesByPackage :: ImportTree -> HashMap (Path Abs Dir) (HashSet ImportNode)
importTreeNodesByPackage tree = run . execState mempty $
forM_ (tree ^. importTreeNodes) $ \node ->
modify @(HashMap (Path Abs Dir) (HashSet ImportNode))
(over (at (node ^. importNodePackageRoot)) (Just . maybe (HashSet.singleton node) (HashSet.insert node)))

importTree :: SimpleGetter ImportTree (HashMap ImportNode (HashSet ImportNode))
importTree = fimportTree

Expand Down

0 comments on commit e0bbac2

Please sign in to comment.