Skip to content

Commit

Permalink
Don't indent multiline interpolated string expressions. Fixes fsproje…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed May 1, 2021
1 parent 83c5566 commit 9b0e09d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 17 deletions.
117 changes: 103 additions & 14 deletions src/Fantomas.Tests/InterpolatedStringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ let str =
"
let str =
$\"\"\"
{
let square x = x * x
let isOdd x = x % 2 <> 0
let oddSquares = List.filter isOdd >> List.map square
oddSquares [ 1 .. 0 ]
}\"\"\"
{let square x = x * x
let isOdd x = x % 2 <> 0
let oddSquares = List.filter isOdd >> List.map square
oddSquares [ 1 .. 0 ]}\"\"\"
"

[<Test>]
let ``indentation in interpolation`` () =
let ``keep indentation in interpolation`` () =
formatSourceString
false
"""
Expand All @@ -123,13 +121,9 @@ let ``indentation in interpolation`` () =
|> should
equal
"""
$"abc {
let x = 3
x + x
} def {
let x = 4
x + x
} xyz"
$"abc {let x = 3
x + x} def {let x = 4
x + x} xyz"
"""

[<Test>]
Expand Down Expand Up @@ -259,3 +253,98 @@ let main _ =
printfn $@\"Migrate notes of file \"\"{oldId}\"\" to new file \"\"{newId}\"\".\"
0
"

[<Test>]
let ``multiline expression should not receive any extra indentation, 1511`` () =
formatSourceString
false
"""
let storageConnection = $"DefaultEndpointsProtocol=https;AccountName=%s{storageAccount.Name};AccountKey=%s{storageAccountKey.Value}"
let serviceStorageConnection = $"DefaultEndpointsProtocol=https;AccountName=%s{serviceStorageAccount.Name};AccountKey=%s{serviceStorageAccountKey.Value}"
"""
config
|> prepend newline
|> should
equal
"""
let storageConnection =
$"DefaultEndpointsProtocol=https;AccountName=%s{storageAccount.Name};AccountKey=%s{storageAccountKey.Value}"
let serviceStorageConnection =
$"DefaultEndpointsProtocol=https;AccountName=%s{serviceStorageAccount.Name};AccountKey=%s{serviceStorageAccountKey.Value}"
"""

[<Test>]
let ``indentation inside try with is correct`` () =
formatSourceString
false
"""
$"
{
try
let a = 0
let b = y
let c = 9
foo ()
with ex -> interpolationFailed ()
}
"
"""
config
|> prepend newline
|> should
equal
"""
$"
{try
let a = 0
let b = y
let c = 9
foo ()
with ex -> interpolationFailed ()}
"
"""

[<Test>]
let ``construct url with Fable`` () =
formatSourceString
false
"""
let newUrl =
$"{window.location.protocol}//{window.location.host}{ window.location.pathname}{newHash}?{``params``.ToString()}"
"""
{ config with MaxLineLength = 80 }
|> prepend newline
|> should
equal
"""
let newUrl =
$"{window.location.protocol}//{window.location.host}{window.location.pathname}{newHash}?{``params``.ToString()}"
"""

[<Test>]
let ``multiline function application inside interpolated expression is printed as multiline`` () =
formatSourceString
false
"""
let foo = $"
longLeadingStringPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaart{bar.ToString(window.location.protocol,window.location.host,window.location.pathname,newHash,``params``)}
"
"""
{ config with MaxLineLength = 80 }
|> prepend newline
|> should
equal
"""
let foo =
$"
longLeadingStringPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaart{bar.ToString(
window.location.protocol,
window.location.host,
window.location.pathname,
newHash,
``params``
)}
"
"""
16 changes: 13 additions & 3 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2533,9 +2533,19 @@ and genExpr astContext synExpr ctx =
|> Option.map (fun sc -> range, sc))

let genInterpolatedFillExpr expr =
leadingExpressionIsMultiline
(atCurrentColumn (autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext expr)))
(fun isMultiline -> onlyIf isMultiline sepNln)
fun ctx ->
genExpr
astContext
expr
{ ctx with
Config =
{ ctx.Config with
// override the max line length for the interpolated expression.
// this is to avoid scenarios where the long / multiline format of the expresion will be used
// where the construct is this short
// see unit test ``construct url with Fable``
MaxLineLength = ctx.WriterModel.Column + ctx.Config.MaxLineLength } }
|> atCurrentColumnIndent

let expr =
if List.length stringRanges = List.length stringsFromTrivia then
Expand Down

0 comments on commit 9b0e09d

Please sign in to comment.