Skip to content

Commit

Permalink
Move tuple function transformation to Upgrade_0_19
Browse files Browse the repository at this point in the history
  • Loading branch information
avh4 committed May 16, 2018
1 parent 935c8f9 commit 19a048c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 78 deletions.
79 changes: 6 additions & 73 deletions src/ElmFormat/Render/Box.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1063,25 +1063,11 @@ formatExpression' elmVersion context aexpr =
formatExpression elmVersion context left

AST.Expression.App left args multiline ->
case (elmVersion, RA.drop left) of
(Elm_0_19_Upgrade, AST.Expression.TupleFunction n) ->
let
forceMultiline =
case (multiline, args) of
(_, []) -> False -- no items; don't split
(AST.FASplitFirst, _) -> True -- all are split
(AST.FAJoinFirst _, [_]) -> False -- only one and it's joined
(AST.FAJoinFirst AST.JoinAll, _) -> False -- all are joined
(AST.FAJoinFirst AST.SplitAll, _:_:_) -> True -- first is joined, but rest are split
in
formatExpression elmVersion context (formatTupleLambda_0_19 n args forceMultiline)

_ ->
ElmStructure.application
multiline
(formatExpression elmVersion InfixSeparated left)
(fmap (formatPreCommentedExpression elmVersion SpaceSeparated) args)
|> expressionParens SpaceSeparated context
ElmStructure.application
multiline
(formatExpression elmVersion InfixSeparated left)
(fmap (formatPreCommentedExpression elmVersion SpaceSeparated) args)
|> expressionParens SpaceSeparated context

AST.Expression.If if' elseifs (elsComments, els) ->
let
Expand Down Expand Up @@ -1233,12 +1219,7 @@ formatExpression' elmVersion context aexpr =
ElmStructure.group True "(" "," ")" multiline $ map (formatCommentedExpression elmVersion SyntaxSeparated) exprs

AST.Expression.TupleFunction n ->
case elmVersion of
Elm_0_19_Upgrade ->
formatExpression elmVersion context (formatTupleLambda_0_19 n [] False)

_ ->
line $ keyword $ "(" ++ (List.replicate (n-1) ',') ++ ")"
line $ keyword $ "(" ++ (List.replicate (n-1) ',') ++ ")"

AST.Expression.Access expr field ->
formatExpression elmVersion SpaceSeparated expr -- TODO: does this need a different context than SpaceSeparated?
Expand Down Expand Up @@ -1300,54 +1281,6 @@ formatPreCommentedExpression elmVersion context (pre, e) =
formatCommented' pre' (formatExpression elmVersion context) e'


formatTupleLambda_0_19 :: Int -> [(AST.Comments, AST.Expression.Expr)] -> Bool -> AST.Expression.Expr
formatTupleLambda_0_19 n args forceMultiline =
let
vars =
if n <= 26
then fmap (\c -> [c]) (drop (length appliedArgs) $ take n ['a'..'z'])
else error (pleaseReport'' "UNEXPECTED TUPLE" "more than 26 elements")

appliedArgs =
take n args

extraArgs =
drop n args

makeArg varName =
([], noRegion $ AST.Pattern.VarPattern $ AST.LowercaseIdentifier varName)

makeTupleItem varName =
AST.Commented [] (noRegion $ AST.Expression.VarExpr $ AST.Variable.VarRef [] $ AST.LowercaseIdentifier varName) []

makeInlinedValue (comments, expr) =
AST.Commented comments expr []

tuple =
(noRegion $ AST.Expression.Tuple (fmap makeInlinedValue appliedArgs ++ fmap makeTupleItem vars) forceMultiline)

replacement =
case vars of
[] ->
tuple

_ : _ ->
(noRegion $ AST.Expression.Lambda (fmap makeArg vars) [] tuple False)
in
case extraArgs of
[] ->
replacement

_ : _ ->
let
multiline =
if forceMultiline
then AST.FASplitFirst
else AST.FAJoinFirst AST.JoinAll
in
noRegion $ AST.Expression.App replacement extraArgs multiline


formatRecordLike ::
(base -> Box) -> (key -> Line) -> String -> (value -> Box)
-> Maybe (AST.Commented base) -> AST.Sequence (AST.Pair key value)-> AST.Comments -> AST.ForceMultiline
Expand Down
55 changes: 50 additions & 5 deletions src/ElmFormat/Upgrade_0_19.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import Reporting.Annotation (Located(A))

import qualified Data.Map.Strict as Dict
import qualified Data.Maybe as Maybe
import qualified ElmFormat.Version
import qualified Reporting.Annotation as RA
import qualified Reporting.Region as Region
import qualified ReversedList


transform :: Expr -> Expr
Expand Down Expand Up @@ -90,6 +92,18 @@ transform expr =

_ -> Nothing

makeTuple n =
let
vars =
if n <= 26
then fmap (\c -> [c]) (take n ['a'..'z'])
else error (pleaseReport'' "UNEXPECTED TUPLE" "more than 26 elements")
in
Lambda
(fmap makeArg vars)
[]
(noRegion $ AST.Expression.Tuple (fmap (\v -> Commented [] (makeVarRef v) []) vars) False)
False
in
case RA.drop expr of
VarExpr var ->
Expand All @@ -98,6 +112,12 @@ transform expr =
App (A _ (VarExpr var)) args multiline ->
Maybe.fromMaybe expr $ fmap (\new -> applyLambda (noRegion new) args multiline) $ replace var

TupleFunction n ->
noRegion $ makeTuple n

App (A _ (TupleFunction n)) args multiline ->
applyLambda (noRegion $ makeTuple n) args multiline

_ ->
expr

Expand All @@ -107,6 +127,12 @@ transform expr =
--


pleaseReport'' :: String -> String -> String
pleaseReport'' what details =
"<elm-format-" ++ ElmFormat.Version.asString ++ ": "++ what ++ ": " ++ details ++ " -- please report this at https://github.com/avh4/elm-format/issues >"



nowhere :: Region.Position
nowhere =
Region.Position 0 0
Expand Down Expand Up @@ -209,11 +235,30 @@ instance MapExpr LetDeclaration where
_ -> d


inlineVar :: LowercaseIdentifier -> Expr' -> Expr' -> Expr'
inlineVar name value expr =
inlineVar :: LowercaseIdentifier -> Bool -> Expr' -> Expr' -> Expr'
inlineVar name insertMultiline value expr =
Maybe.fromMaybe expr $ inlineVar' name insertMultiline value expr


inlineVar' :: LowercaseIdentifier -> Bool -> Expr' -> Expr' -> Maybe Expr'
inlineVar' name insertMultiline value expr =
case expr of
VarExpr (VarRef [] n) | n == name -> value
_ -> mapExpr (inlineVar name value) expr
VarExpr (VarRef [] n) | n == name -> Just value

AST.Expression.Tuple terms' multiline ->
let
step (acc, expand) t@(Commented pre (A _ term) post) =
case inlineVar' name insertMultiline value term of
Nothing -> (ReversedList.push t acc, expand)
Just term' -> (ReversedList.push (Commented pre (noRegion term') post) acc, insertMultiline || expand)

(terms'', multiline'') = foldl step (ReversedList.empty, multiline) terms'
in
Just $ AST.Expression.Tuple (ReversedList.toList terms'') multiline''

-- TODO: handle expanding multiline in contexts other than tuples

_ -> Just $ mapExpr (inlineVar name insertMultiline value) expr


applyLambda :: Expr -> [PreCommented Expr] -> FunctionApplicationMultiline -> Expr
Expand Down Expand Up @@ -247,7 +292,7 @@ applyLambda lambda args appMultiline =

Just mappings ->
let
newBody = foldl (\e (name, value) -> mapExpr (inlineVar name value) e) body mappings
newBody = foldl (\e (name, value) -> mapExpr (inlineVar name (appMultiline == FASplitFirst) value) e) body mappings
newMultiline =
case appMultiline of
FASplitFirst -> FASplitFirst
Expand Down

0 comments on commit 19a048c

Please sign in to comment.