Skip to content

Commit

Permalink
Fix #491
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Sep 27, 2019
1 parent bd69fed commit 0619568
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ let ``should keep whitespace after function call``() =
[<Test>]
let ``should keep type annotations``() =
formatSourceString false """let empty<'T> : LazyList<'T> = EmptyValue<'T>.Value""" config
|> should equal """let empty<'T>: LazyList<'T> = EmptyValue<'T>.Value
|> should equal """let empty<'T> : LazyList<'T> = EmptyValue<'T>.Value
"""

[<Test>]
Expand Down
9 changes: 8 additions & 1 deletion src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,11 @@ let someFun someReallyLoooooooooooooooongValue =
someOtherFun 1 3
someOtherOtherFun 2 4
"""
"""

[<Test>]
let ``should keep space before :`` () =
formatSourceString false "let refl<'a> : Teq<'a, 'a> = Teq(id, id)" config
|> fun formatted -> formatSourceString false formatted config
|> should equal "let refl<'a> : Teq<'a, 'a> = Teq(id, id)
"
16 changes: 14 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,19 @@ and genExprSepEqPrependType astContext prefix (pat:SynPat) e ctx =
| MatchLambda _ -> false
| _ -> futureNlnCheck (genExpr astContext e) ctx
match e with
| TypedExpr(Typed, e, t) -> (prefix +> sepColon +> genType astContext false t +> sepEq
+> breakNlnOrAddSpace astContext (multilineCheck || checkPreserveBreakForExpr e ctx) e) ctx
| TypedExpr(Typed, e, t) ->
let addExtraSpaceBeforeGenericType (ctx: Context) =
match pat with
| SynPat.LongIdent(_, _, Some(SynValTyparDecls(_)), _, _, _) when (not ctx.Config.SpaceBeforeColon) ->
let dump = (dump ctx).ToCharArray()
match Array.tryLast dump with
| Some('>') -> sepSpace
| _ -> sepNone
| _ -> sepNone
<| ctx

(prefix +> addExtraSpaceBeforeGenericType +> sepColon +> genType astContext false t +> sepEq
+> breakNlnOrAddSpace astContext (multilineCheck || checkPreserveBreakForExpr e ctx) e) ctx
| e ->
let hasCommentAfterEqual =
ctx.Trivia
Expand Down Expand Up @@ -481,6 +492,7 @@ and genLetBinding astContext pref b =
+> opt sepSpace ao genAccess
+> ifElse isMutable (!- "mutable ") sepNone +> ifElse isInline (!- "inline ") sepNone
+> genPat astContext p
+> dumpAndContinue

genExprSepEqPrependType astContext prefix p e

Expand Down
2 changes: 0 additions & 2 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,6 @@ let (|DoBinding|LetBinding|MemberBinding|PropertyBinding|ExplicitCtor|) = functi
DoBinding(ats, px, expr)
| SynBinding.Binding(ao, _, isInline, isMutable, attrs, px, _, pat, _, expr, _, _) ->
LetBinding(attrs, px, ao, isInline, isMutable, pat, expr)

let (|BindingReturnInfo|) (SynBindingReturnInfo (t, _, ats)) = (ats, t)

// Expressions (55 cases, lacking to handle 11 cases)

Expand Down
3 changes: 1 addition & 2 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,4 @@ let collectTrivia config tokens lineCount (ast: ParsedInput) =
| [] -> []
| _ ->
List.fold addTriviaToTriviaNode triviaNodes trivias
|> List.filter (triviaNodeIsNotEmpty) // only keep nodes where something special needs to happen.
|> Dbg.teePrint
|> List.filter (triviaNodeIsNotEmpty) // only keep nodes where something special needs to happen.

0 comments on commit 0619568

Please sign in to comment.