diff --git a/src/Fantomas.Tests/InterpolatedStringTests.fs b/src/Fantomas.Tests/InterpolatedStringTests.fs index 1e734f3846..275f34bc63 100644 --- a/src/Fantomas.Tests/InterpolatedStringTests.fs +++ b/src/Fantomas.Tests/InterpolatedStringTests.fs @@ -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 ]}\"\"\" " [] -let ``indentation in interpolation`` () = +let ``keep indentation in interpolation`` () = formatSourceString false """ @@ -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" """ [] @@ -259,3 +253,98 @@ let main _ = printfn $@\"Migrate notes of file \"\"{oldId}\"\" to new file \"\"{newId}\"\".\" 0 " + +[] +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}" +""" + +[] +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 ()} +" +""" + +[] +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()}" +""" + +[] +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`` + )} +" +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 6ae7d753e3..10421ce481 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -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