Skip to content

Commit

Permalink
Improve module exposing line so that it is informed by Elm.group.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdgriffith committed Oct 6, 2024
1 parent 7db0752 commit 3cea2dd
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/Internal/Compiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Internal.Compiler exposing
, mergeAliases
, mergeInferences
, noImports
, nodeAtLine
, nodify
, nodifyAll
, parens
Expand Down Expand Up @@ -950,6 +951,15 @@ nodify exp =
Node Range.emptyRange exp


nodeAtLine : Int -> a -> Node a
nodeAtLine line exp =
Node
{ start = { column = 0, row = line }
, end = { column = 0, row = line }
}
exp


nodifyAll : List a -> List (Node a)
nodifyAll =
List.map nodify
Expand Down
106 changes: 86 additions & 20 deletions src/Internal/Render.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Elm.Syntax.Declaration
import Elm.Syntax.Documentation
import Elm.Syntax.Exposing as Expose
import Elm.Syntax.Module
import Elm.Syntax.Node as Node
import Elm.Syntax.Range as Range
import Internal.Comments
import Internal.Compiler as Compiler
Expand Down Expand Up @@ -66,14 +67,16 @@ renderDecls :
->
{ declarations : List Compiler.RenderedDeclaration
, imports : List Compiler.Module
, exposed : List Expose.TopLevelExpose
, exposed : List ExposeGroup
, exposePath : List Int
, hasPorts : Bool
, warnings : List Compiler.Warning
}
->
{ declarations : List Compiler.RenderedDeclaration
, imports : List Compiler.Module
, exposed : List Expose.TopLevelExpose
, exposed : List ExposeGroup
, exposePath : List Int
, hasPorts : Bool
, warnings : List Compiler.Warning
}
Expand Down Expand Up @@ -102,8 +105,9 @@ renderDecls fileDetails decl gathered =
Compiler.RenderedDecl (addDocs decDetails.docs result.declaration) :: gathered.declarations
, imports =
result.additionalImports ++ decDetails.imports ++ gathered.imports
, exposePath = gathered.exposePath
, exposed =
addExposed decDetails.exposed result.declaration gathered.exposed
addExposed gathered.exposePath decDetails.exposed result.declaration gathered.exposed
, hasPorts =
if gathered.hasPorts then
gathered.hasPorts
Expand All @@ -125,9 +129,22 @@ renderDecls fileDetails decl gathered =
}

Compiler.Group groupDecls ->
let
incrementExposePath g =
{ g
| exposePath =
case g.exposePath of
[] ->
[]

top :: remain ->
top + 1 :: remain
}
in
List.foldl (renderDecls fileDetails)
gathered
{ gathered | exposePath = 0 :: gathered.exposePath }
groupDecls
|> incrementExposePath


type ExposedGroup
Expand All @@ -146,7 +163,8 @@ render initialDocs fileDetails =
rendered :
{ declarations : List Compiler.RenderedDeclaration
, imports : List Compiler.Module
, exposed : List Expose.TopLevelExpose
, exposePath : List Int
, exposed : List ExposeGroup
, hasPorts : Bool
, warnings : List Compiler.Warning
}
Expand All @@ -155,6 +173,7 @@ render initialDocs fileDetails =
(renderDecls fileDetails)
{ imports = []
, hasPorts = False
, exposePath = [ 0 ]
, exposed = []
, declarations = []
, warnings = []
Expand All @@ -181,7 +200,9 @@ render initialDocs fileDetails =
_ ->
Compiler.nodify
(Expose.Explicit
(Compiler.nodifyAll rendered.exposed)
(List.indexedMap groupExposedItems rendered.exposed
|> List.concat
)
)
}
, aliases = fileDetails.aliases
Expand Down Expand Up @@ -233,7 +254,8 @@ render initialDocs fileDetails =

type RenderingMode
= Normal
| RenderingDocsLine
-- Rendering Docs line and the count of how many elements have been rendered
| RenderingDocsLine Int


type DocMode
Expand All @@ -249,7 +271,7 @@ exposedGroupToMarkdown docMode groups mode rendered =
Normal ->
rendered

RenderingDocsLine ->
RenderingDocsLine _ ->
rendered

(ModuleDocs docs) :: rest ->
Expand All @@ -265,7 +287,7 @@ exposedGroupToMarkdown docMode groups mode rendered =
in
exposedGroupToMarkdown docMode rest mode (rendered ++ separator ++ docs)

RenderingDocsLine ->
RenderingDocsLine _ ->
exposedGroupToMarkdown docMode rest mode (rendered ++ "\n\n" ++ docs)

(Exposed exposedName) :: rest ->
Expand All @@ -274,13 +296,17 @@ exposedGroupToMarkdown docMode groups mode rendered =
case mode of
Normal ->
if String.isEmpty rendered then
exposedGroupToMarkdown docMode rest RenderingDocsLine ("@docs " ++ exposedName)
exposedGroupToMarkdown docMode rest (RenderingDocsLine 1) ("@docs " ++ exposedName)

else
exposedGroupToMarkdown docMode rest RenderingDocsLine (rendered ++ "\n\n@docs " ++ exposedName)
exposedGroupToMarkdown docMode rest (RenderingDocsLine 1) (rendered ++ "\n\n@docs " ++ exposedName)

RenderingDocsLine docsItemCount ->
if docsItemCount > 5 then
exposedGroupToMarkdown docMode rest (RenderingDocsLine 1) (rendered ++ "\n@docs " ++ exposedName)

RenderingDocsLine ->
exposedGroupToMarkdown docMode rest mode (rendered ++ ", " ++ exposedName)
else
exposedGroupToMarkdown docMode rest (RenderingDocsLine (docsItemCount + 1)) (rendered ++ ", " ++ exposedName)

OnlyGroups ->
exposedGroupToMarkdown docMode rest mode rendered
Expand Down Expand Up @@ -372,13 +398,53 @@ addDocs maybeDoc decl =
decl


addExposed : Compiler.Expose -> Elm.Syntax.Declaration.Declaration -> List Expose.TopLevelExpose -> List Expose.TopLevelExpose
addExposed exposed declaration otherExposes =
groupExposedItems : Int -> ExposeGroup -> List (Node.Node Expose.TopLevelExpose)
groupExposedItems line group =
List.map
(Compiler.nodeAtLine line)
group.exposed


type alias ExposeGroup =
{ id : List Int
, exposed : List Expose.TopLevelExpose
}


addExposed :
List Int
-> Compiler.Expose
-> Elm.Syntax.Declaration.Declaration
-> List ExposeGroup
-> List ExposeGroup
addExposed exposePath exposed declaration otherExposes =
case exposed of
Compiler.NotExposed ->
otherExposes

Compiler.Exposed details ->
let
addToExposedCollection new =
case otherExposes of
[] ->
[ { id = exposePath
, exposed = [ new ]
}
]

top :: rest ->
if top.id == exposePath then
{ id = top.id
, exposed = new :: top.exposed
}
:: rest

else
{ id = exposePath
, exposed = [ new ]
}
:: otherExposes
in
case declaration of
Elm.Syntax.Declaration.FunctionDeclaration fn ->
let
Expand All @@ -387,7 +453,7 @@ addExposed exposed declaration otherExposes =
Compiler.denode (.name (Compiler.denode fn.declaration))
in
Expose.FunctionExpose fnName
:: otherExposes
|> addToExposedCollection

Elm.Syntax.Declaration.AliasDeclaration synonym ->
let
Expand All @@ -396,7 +462,7 @@ addExposed exposed declaration otherExposes =
Compiler.denode synonym.name
in
Expose.TypeOrAliasExpose aliasName
:: otherExposes
|> addToExposedCollection

Elm.Syntax.Declaration.CustomTypeDeclaration myType ->
let
Expand All @@ -409,11 +475,11 @@ addExposed exposed declaration otherExposes =
{ name = typeName
, open = Just Range.emptyRange
}
:: otherExposes
|> addToExposedCollection

else
Expose.TypeOrAliasExpose typeName
:: otherExposes
|> addToExposedCollection

Elm.Syntax.Declaration.PortDeclaration myPort ->
let
Expand All @@ -422,7 +488,7 @@ addExposed exposed declaration otherExposes =
Compiler.denode myPort.name
in
Expose.FunctionExpose typeName
:: otherExposes
|> addToExposedCollection

Elm.Syntax.Declaration.InfixDeclaration _ ->
otherExposes
Expand Down
Loading

0 comments on commit 3cea2dd

Please sign in to comment.