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

Revisit if/then/else #2334

Merged
merged 5 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* Restore the CodeFormatter.MakeRange public API. [#2306](https://github.com/fsprojects/fantomas/pull/2306)
* Update FCS to 'Add arrow to SynType.Fun trivia.', commit 5a5a5f6cd07aa4a8326baa07d4f7af1305ced6f4
* Update FCS to 'Fix setter first', commit 267d0a57f217df756d9ac33c6aa4ffbfe3b53097
* Update style of long if/match expressions (See [fslang-design#646](https://github.com/fsharp/fslang-design/issues/646)). [#2334](https://github.com/fsprojects/fantomas/pull/2334)

### Added
* Add setting `fsharp_max_if_then_short_width`. [#2299](https://github.com/fsprojects/fantomas/issues/2299)

## [5.0.0-alpha-010] - 2022-06-27

Expand Down
21 changes: 21 additions & 0 deletions docs-old/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fsharp_space_after_comma=true
fsharp_space_before_semicolon=false
fsharp_space_after_semicolon=true
fsharp_space_around_delimiter=true
fsharp_max_if_then_short_width=0
fsharp_max_if_then_else_short_width=40
fsharp_max_infix_operator_expression=50
fsharp_max_record_width=40
Expand Down Expand Up @@ -442,6 +443,26 @@ let a = [1;2;3]
let b = [|4;5;6|]
```

### fsharp_max_if_then_short_width
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yisusalanpng keep in mind that I added this documentation bit to the old docs.
Please move it as well in #2336


Control the maximum length for which if/then expression without an else expression can be on one line.
The [Microsoft F# style guide](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#formatting-if-expressions) recommends to never write such an expression in one line.
> If the else expression is absent, it is recommended to never to write the entire expression in one line.
Default = 0.

`defaultConfig`

```fsharp
if a then
()
```

`{ defaultConfig with MaxIfThenShortWidth = 15 }`

```fsharp
if a then ()
```

### fsharp_max_if_then_else_short_width

Fantomas by default follows the if/then/else conventions listed in the [Microsoft F# style guide](https://docs.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#formatting-if-expressions).
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/ActivePatternTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let (|ParseRegex|_|) regex str =
{ config with
MaxValueBindingWidth = 30
MaxFunctionBindingWidth = 30
MaxIfThenElseShortWidth = 70 }
MaxIfThenElseShortWidth = 75 }
|> prepend newline
|> should
equal
Expand Down
38 changes: 18 additions & 20 deletions src/Fantomas.Core.Tests/AppTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -486,35 +486,33 @@ let ``parenthesis around composed function expression, 1341`` () =

[<Test>]
let ``parenthesis around short composed function expression, tuple, 1700`` () =
formatSourceString false """((=) (ownerName, username))""" config
|> should
equal
"""((=) (ownerName, username))
"""
formatSourceString false "((=) (ownerName, username))" { config with InsertFinalNewline = false }
|> should equal "((=) (ownerName, username))"

[<Test>]
let ``parenthesis around short composed function expression, tuple in if, 1700`` () =
formatSourceString false """if ((=) (ownerName, username)) then 6""" config
|> should
equal
"""if ((=) (ownerName, username)) then 6
"""
formatSourceString
false
"if ((=) (ownerName, username)) then 6"
{ config with
MaxIfThenShortWidth = 40
InsertFinalNewline = false }
|> should equal "if ((=) (ownerName, username)) then 6"

[<Test>]
let ``parenthesis around short composed function expression, no tuple, 1700`` () =
formatSourceString false """((=) ownerName)""" config
|> should
equal
"""((=) ownerName)
"""
formatSourceString false """((=) ownerName)""" { config with InsertFinalNewline = false }
|> should equal """((=) ownerName)"""

[<Test>]
let ``parenthesis around short composed function expression, no tuple in if, 1700, part 2`` () =
formatSourceString false """if ((=) ownerName) then 6""" config
|> should
equal
"""if ((=) ownerName) then 6
"""
formatSourceString
false
"if ((=) ownerName) then 6"
{ config with
MaxIfThenShortWidth = 25
InsertFinalNewline = false }
|> should equal "if ((=) ownerName) then 6"

[<Test>]
let ``parenthesis around simple function expression`` () =
Expand Down
7 changes: 5 additions & 2 deletions src/Fantomas.Core.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,9 @@ if a then
()
else
// Comment 1
if b then
if
b
then
()
// Comment 2
else
Expand Down Expand Up @@ -993,7 +995,8 @@ else 0"""
equal
"""
if //comment
true then
true
then
1
else
0
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ module Dbg =
let print _ = ()
#endif
"""
config
{ config with MaxIfThenShortWidth = 30 }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -1704,7 +1704,7 @@ module Dbg =
let print _ = ()
#endif
"""
config
{ config with MaxIfThenShortWidth = 30 }
|> prepend newline
|> should
equal
Expand Down Expand Up @@ -2504,7 +2504,7 @@ let ``comment after compiler define`` () =
let tcrefObjTy, enclosingDeclaredTypars, renaming, objTy = FreshenTyconRef m rigid tcref declaredTyconTypars
#endif
"""
config
{ config with MaxIfThenElseShortWidth = 20 }
|> prepend newline
|> should
equal
Expand Down
4 changes: 3 additions & 1 deletion src/Fantomas.Core.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,9 @@ let create: Highlighter =
|> List.ofSeq
|> FormattedText.fromList
"""
{ config with MaxIfThenElseShortWidth = 80 }
{ config with
MaxIfThenElseShortWidth = 80
MaxIfThenShortWidth = 80 }
|> prepend newline
|> should
equal
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ let ``if/elif without else`` () =
if true then ()
elif true then ()
"""
config
{ config with MaxIfThenShortWidth = 20 }
|> prepend newline
|> should
equal
Expand Down
18 changes: 11 additions & 7 deletions src/Fantomas.Core.Tests/DotGetTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,19 @@ let PublishValueDefn cenv env declKind (vspec: Val) =
equal
"""
let PublishValueDefn cenv env declKind (vspec: Val) =
if (declKind = ModuleOrMemberBinding)
&& ((GetCurrAccumulatedModuleOrNamespaceType env)
.ModuleOrNamespaceKind = Namespace)
&& (Option.isNone vspec.MemberInfo) then
if
(declKind = ModuleOrMemberBinding)
&& ((GetCurrAccumulatedModuleOrNamespaceType env)
.ModuleOrNamespaceKind = Namespace)
&& (Option.isNone vspec.MemberInfo)
then
errorR (Error(FSComp.SR.tcNamespaceCannotContainValues (), vspec.Range))

if (declKind = ExtrinsicExtensionBinding)
&& ((GetCurrAccumulatedModuleOrNamespaceType env)
.ModuleOrNamespaceKind = Namespace) then
if
(declKind = ExtrinsicExtensionBinding)
&& ((GetCurrAccumulatedModuleOrNamespaceType env)
.ModuleOrNamespaceKind = Namespace)
then
errorR (Error(FSComp.SR.tcNamespaceCannotContainExtensionMembers (), vspec.Range))

()
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/DynamicOperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let ``keep () when dynamic operator inside boolean expr, #476`` () =
NoColor
|> Input.Color
"""
config
{ config with MaxIfThenElseShortWidth = 5 }
|> prepend newline
|> should
equal
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<Compile Include="IdentTests.fs" />
<Compile Include="RecordDeclarationsWithXMLDocTests.fs" />
<Compile Include="AstExtensionsTests.fs" />
<Compile Include="MaxIfThenShortWidthTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Extras\Fantomas.Extras.fsproj" />
Expand Down
Loading