Skip to content

Commit

Permalink
Remove inconsistent multiline string after equals sign. Fixes #1556. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Mar 31, 2021
1 parent e4c78be commit 44926de
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 70 deletions.
18 changes: 12 additions & 6 deletions src/Fantomas.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1765,11 +1765,13 @@ let b = "
|> should
equal
"""
let a = "
let a =
"
#if FOO
"
let b = "
let b =
"
#endif
"
"""
Expand All @@ -1793,13 +1795,15 @@ let b = \"\"\"
|> should
equal
"
let a = \"\"\"
let a =
\"\"\"
\"
#if FOO
\"
\"\"\"
let b = \"\"\"
let b =
\"\"\"
#endif
\"\"\"
"
Expand All @@ -1820,11 +1824,13 @@ let b = "
|> should
equal
"""
let a = "
let a =
"
#if FOO
\""
let b = "
let b =
"
#endif
"
"""
Expand Down
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ let r =
a
&& // && b
c
Bar = \"\"\"
Bar =
\"\"\"
Fooey
\"\"\" |}
"
Expand Down
15 changes: 10 additions & 5 deletions src/Fantomas.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ let r =
a
&& // && b
c
Bar = \"\"\"
Bar =
\"\"\"
Fooey
\"\"\" |}
"
Expand Down Expand Up @@ -535,7 +536,8 @@ type Database =
[| { Id = 0
AuthorId = 1
Title = \"What is the average wing speed of an unladen swallow?\"
Description = \"\"\"
Description =
\"\"\"
Hello, yesterday I saw a flight of swallows and was wondering what their **average wing speed** is?
If you know the answer please share it.
Expand All @@ -545,7 +547,8 @@ If you know the answer please share it.
CreatedAt = DateTime.Parse \"2017-09-14T19:57:33.103Z\"
AuthorId = 0
Score = 2
Content = \"\"\"
Content =
\"\"\"
> What do you mean, an African or European Swallow?
>
> Monty Python’s: The Holy Grail
Expand All @@ -558,7 +561,8 @@ I thought you were asking it seriously, well done.
CreatedAt = DateTime.Parse \"2017-09-14T20:07:27.103Z\"
AuthorId = 2
Score = 1
Content = \"\"\"
Content =
\"\"\"
Maxime,
I believe you found [this blog post](http://www.saratoga.com/how-should-i-know/2013/07/what-is-the-average-air-speed-velocity-of-a-laden-swallow/).
Expand All @@ -571,7 +575,8 @@ And so Robin, the conclusion of the post is:
{ Id = 1
AuthorId = 0
Title = \"Why did you create Fable?\"
Description = \"\"\"
Description =
\"\"\"
Hello Alfonso,
I wanted to know why you created Fable. Did you always plan to use F#? Or were you thinking in others languages?
Expand Down
18 changes: 12 additions & 6 deletions src/Fantomas.Tests/StringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ let alu =
|> should
equal
"""
let alu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
let alu =
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
Expand Down Expand Up @@ -153,10 +154,12 @@ let ``should preserve triple-quote strings`` () =
equal
"
type GetList() =
let switchvox_users_voicemail_getList_response = \"\"\"
let switchvox_users_voicemail_getList_response =
\"\"\"
</response>\"\"\"
let switchvox_users_voicemail_getList = \"\"\"
let switchvox_users_voicemail_getList =
\"\"\"
</request>\"\"\"
member self.X = switchvox_users_voicemail_getList_response
Expand Down Expand Up @@ -189,7 +192,8 @@ let main argv =
use fun1 =
R.eval (
R.parse (
text = \"\"\"
text =
\"\"\"
function(i) {
x <- rnorm(1000)
y <- rnorm(1000)
Expand Down Expand Up @@ -242,7 +246,8 @@ with empty lines\"\"\"
|> should
equal
"
let x = \"\"\"some
let x =
\"\"\"some
content
Expand Down Expand Up @@ -273,7 +278,8 @@ let ``newline in string`` () =
"""
[<Test>]
let ``newline in string`` () =
let source = "\"
let source =
"\"
\""
let triviaNodes =
Expand Down
94 changes: 92 additions & 2 deletions src/Fantomas.Tests/SynConstTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ let a = \\\"\\\\\\\"
"
[<Test>]
let ``defines inside string, escaped quote`` () =
let source = \"
let source =
\"
let a = \\\"\\\\\\\"
#if FOO
#if BAR
Expand Down Expand Up @@ -408,9 +409,98 @@ printfn bar\"\"\" // difference is the 4 spaces on line 188
|> should
equal
"
let source = \"\"\"printfn foo
let source =
\"\"\"printfn foo
printfn bar\"\"\" // difference is the 4 spaces on line 188
let x = 9
"

[<Test>]
let ``multiline string in let value binding, 1556`` () =
formatSourceString
false
"
let foo = \"\"\"moo,
long
triple quotes string thing
\"\"\"
"
config
|> prepend newline
|> should
equal
"
let foo =
\"\"\"moo,
long
triple quotes string thing
\"\"\"
"

[<Test>]
let ``multiline string in let value binding, return type`` () =
formatSourceString
false
"
let foo: string = \"\"\"moo,
long
triple quotes string thing
\"\"\"
"
config
|> prepend newline
|> should
equal
"
let foo : string =
\"\"\"moo,
long
triple quotes string thing
\"\"\"
"

[<Test>]
let ``multiline string in let function binding, no return type`` () =
formatSourceString
false
"
let foo () = \"\"\"moo,
long
triple quotes string thing
\"\"\"
"
config
|> prepend newline
|> should
equal
"
let foo () =
\"\"\"moo,
long
triple quotes string thing
\"\"\"
"

[<Test>]
let ``multiline string in let function binding, return type`` () =
formatSourceString
false
"
let foo () : string = \"\"\"moo,
long
triple quotes string thing
\"\"\"
"
config
|> prepend newline
|> should
equal
"
let foo () : string =
\"\"\"moo,
long
triple quotes string thing
\"\"\"
"
60 changes: 15 additions & 45 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -896,18 +896,14 @@ and genRecordFieldName astContext (RecordFieldName (s, eo) as node) =
eo
(fun e ->
let expr =
match e with
| MultilineString _ -> sepSpace +> genExpr astContext e
| _ -> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)

!-s +> sepEq +> expr)
|> genTriviaFor RecordField_ range

and genAnonRecordFieldName astContext (AnonRecordFieldName (s, e)) =
let expr =
match e with
| MultilineString _ -> sepSpace +> genExpr astContext e
| _ -> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)
sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)

!-s +> sepEq +> expr

Expand Down Expand Up @@ -955,26 +951,18 @@ and genNamedArgumentExpr (astContext: ASTContext) operatorExpr e1 e2 =
+> sepSpace
+> genExpr astContext e2

match e2 with
| MultilineString _ -> short
| _ ->
let long =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" operatorExpr
+> indent
+> sepNln
+> genExpr astContext e2
+> unindent
let long =
genExpr astContext e1
+> sepSpace
+> genInfixOperator "=" operatorExpr
+> indent
+> sepNln
+> genExpr astContext e2
+> unindent

expressionFitsOnRestOfLine short long
expressionFitsOnRestOfLine short long

and genExpr astContext synExpr ctx =
let appNlnFun e =
match e with
| MultilineString _ -> id
| _ -> autoNlnIfExpressionExceedsPageWidth

let kw tokenName f = tokN synExpr.Range tokenName f

let sepOpenTFor r = tokN r LPAREN sepOpenT
Expand Down Expand Up @@ -1665,10 +1653,6 @@ and genExpr astContext synExpr ctx =
expr ctx
| Paren (lpr, e, rpr, pr) ->
match e with
| MultilineString _ ->
sepOpenTFor lpr
+> atCurrentColumn (genExpr astContext e +> indentIfNeeded sepNone)
+> sepCloseTFor rpr pr
| LetOrUses _ ->
sepOpenTFor lpr
+> atCurrentColumn (genExpr astContext e)
Expand Down Expand Up @@ -1983,7 +1967,7 @@ and genExpr astContext synExpr ctx =
expressionFitsOnRestOfLine short long

// Always spacing in multiple arguments
| App (e, es) -> genApp appNlnFun astContext e es
| App (e, es) -> genApp astContext e es
| TypeApp (e, ts) ->
genExpr astContext e
+> genGenericTypeParameters astContext ts
Expand Down Expand Up @@ -3146,7 +3130,7 @@ and genIndexers astContext node =
+> genRest astContext es
| _ -> sepNone

and genApp appNlnFun astContext e es ctx =
and genApp astContext e es ctx =
let shortExpression =
let addFirstSpace =
ifElseCtx
Expand All @@ -3166,7 +3150,7 @@ and genApp appNlnFun astContext e es ctx =
+> sepOpenSFixed
+> sepSpace
+> indent
+> appNlnFun e (genExpr astContext e)
+> autoNlnIfExpressionExceedsPageWidth (genExpr astContext e)
+> unindent
else
genExpr astContext e
Expand Down Expand Up @@ -5168,21 +5152,7 @@ and genSynBindingValue
+> genExpr astContext e
+> unindent

let hasMultilineString =
match e with
| SynExpr.Const (SynConst.String (_, r), _) ->
ctx.TriviaMainNodes
|> Map.tryFindOrEmptyList SynConst_String
|> TriviaHelpers.hasMultilineString r
| SynExpr.Const (SynConst.Bytes (_, r), _) ->
ctx.TriviaMainNodes
|> Map.tryFindOrEmptyList SynConst_Bytes
|> TriviaHelpers.hasMultilineString r
| _ -> false

if hasMultilineString then
short ctx
elif isMultiline then
if isMultiline then
long ctx
else
isShortExpression ctx.Config.MaxValueBindingWidth short long ctx)
Expand Down
Loading

0 comments on commit 44926de

Please sign in to comment.