Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string interpolation part crossing max line length introduces new line and lots of whitespace #1511

Closed
1 of 3 tasks
johlrich opened this issue Mar 7, 2021 · 2 comments · Fixed by #1694
Closed
1 of 3 tasks

Comments

@johlrich
Copy link
Contributor

johlrich commented Mar 7, 2021

Visual issue only as the new line and whitespace is all inside of the expression part, the string literal is unaffected.

Issue created from fantomas-online

Code

let storageConnection = $"DefaultEndpointsProtocol=https;AccountName=%s{storageAccount.Name};AccountKey=%s{storageAccountKey.Value}"

let serviceStorageConnection = $"DefaultEndpointsProtocol=https;AccountName=%s{serviceStorageAccount.Name};AccountKey=%s{serviceStorageAccountKey.Value}"

Result

let storageConnection =
    $"DefaultEndpointsProtocol=https;AccountName=%s{storageAccount.Name};AccountKey=%s{storageAccountKey.Value}"

let serviceStorageConnection =
    $"DefaultEndpointsProtocol=https;AccountName=%s{serviceStorageAccount.Name};AccountKey=%s{
                                                                                                  serviceStorageAccountKey.Value
    }"

Problem description

Please describe here the Fantomas problem you encountered.
Check out our Contribution Guidelines.

Extra information

  • The formatted result breaks by code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas Master at 03/06/2021 17:13:59 - f828c65

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

@johlrich
Copy link
Contributor Author

johlrich commented Mar 7, 2021

Ideally if the expression gets formatted and is still 1 line, we could allow it to be kept in place.

If it needs to be multiline, perhaps they should be at start of line and indent based on its own context only.

Thoughts? Happy to try and help again if we come up with a desirable solution

@nojaf
Copy link
Contributor

nojaf commented Mar 8, 2021

Hey Jonathan, thank you for reporting this issue.
Interpolated string expressions now add the indent and newline when the expression is multiline or the max_line_length was crossed.
See

(atCurrentColumn (autoIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext expr)))

In your case, the latter happened. You could easily change this and remove the autoIndentAndNlnIfExpressionExceedsPageWidth function call.
That will of course lead to different results for the existing tests:

[<Test>]
let ``multiline expression in multiline string`` () =
formatSourceString
false
"
let str =
$\"\"\"
{
let square x = x * x
let isOdd x = x % 2 <> 0
let oddSquares =
List.filter isOdd >> List.map square
oddSquares [ 1 .. 0 ]
}\"\"\"
"
config
|> prepend newline
|> should
equal
"
let str =
$\"\"\"
{
let square x = x * x
let isOdd x = x % 2 <> 0
let oddSquares = List.filter isOdd >> List.map square
oddSquares [ 1 .. 0 ]
}\"\"\"
"

So it is not that easy to give here a good answer.

When I originally went with the implementation of interpolated strings I kinda winged it as the style guides did not have any guidance at the time. Could you perhaps raise in https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting what needs to happen if the construct is actually multiline?

Once we know that, we can act upon this. If the guide does not dictate any extra newlines and indents than the fix is quite easy as described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants