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

Indent cramped record field body if indent_size is low. #2802

Merged
merged 6 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [Unreleased]

### Fixed
* Nested multiline record with indent_size = 2. [#2801](https://github.com/fsprojects/fantomas/issues/2801)
* Idempotency problem when comment after opening brace in inherit record. [#2803](https://github.com/fsprojects/fantomas/issues/2803)

## [6.0.0-alpha-007] - 2023-03-27

### Changed
Expand Down
39 changes: 39 additions & 0 deletions src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,3 +1593,42 @@ struct // 1
// 5
|} // 6
"""

[<Test>]
let ``multiline field body expression where indent_size = 2`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines : NamedText, formatted : string, range : FormatSelectionRange) =
let range =
{
Start =
{
Line = range.StartLine - 1
Character = range.StartColumn
}
End =
{
Line = range.EndLine - 1
Character = range.EndColumn
}
}

[| { Range = range ; NewText = formatted } |]
"""
179 changes: 175 additions & 4 deletions src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,8 +1742,8 @@ let ``record with comments above field, indent 2`` () =
equal
"""
{ Foo =
// bar
someValue }
// bar
someValue }
"""

[<Test>]
Expand Down Expand Up @@ -1799,8 +1799,8 @@ let ``anonymous record with multiline field, indent 2`` () =
equal
"""
{| Foo =
// meh
someValue |}
// meh
someValue |}
"""

[<Test>]
Expand Down Expand Up @@ -2280,3 +2280,174 @@ let a =
// test2
|}
"""

[<Test>]
let ``multiline field body expression where indent_size = 2, 2801`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""

[<Test>]
let ``multiline field body expression where indent_size = 2, anonymous record`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{| Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } |}

[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{| Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } |}

[| { Range = range; NewText = formatted } |]
"""

[<Test>]
let ``multiline field body expression where indent_size = 2, update record`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ x with
Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ x with
Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""

let ``multiline field body expression where indent_size = 2, inherit record`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ inherit Foo()
Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }
[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ inherit Foo()
Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""

[<Test>]
let ``trivia after opening brace in inherit expression, 2803`` () =
formatSourceString
false
"""
let range =
{ // foo
// bar
inherit // reason
Foo()
X = y
Z =
someReallyLongExpressionThatIsLongerThanTheLineLength
aLongArgument
//
anotherLongArgument
fooBar }
"""
config
|> prepend newline
|> should
equal
"""
let range =
{ // foo
// bar
inherit // reason
Foo()
X = y
Z =
someReallyLongExpressionThatIsLongerThanTheLineLength
aLongArgument
//
anotherLongArgument
fooBar }
"""
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 @@ -110,6 +110,7 @@
<Compile Include="Stroustrup\FunctionApplicationDualListTests.fs" />
<Compile Include="Stroustrup\SynExprAnonRecdStructTests.fs" />
<Compile Include="Stroustrup\ExperimentalElmishTests.fs" />
<Compile Include="Stroustrup\RecordInstanceTests.fs" />
<Compile Include="IdentTests.fs" />
<Compile Include="RecordDeclarationsWithXMLDocTests.fs" />
<Compile Include="MaxIfThenShortWidthTests.fs" />
Expand Down
46 changes: 46 additions & 0 deletions src/Fantomas.Core.Tests/Stroustrup/RecordInstanceTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Fantomas.Core.Tests.Stroustrup.RecordInstanceTests

open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelper
open Fantomas.Core

let config =
{ config with
MultilineBracketStyle = Stroustrup }

[<Test>]
let ``multiline field body expression where indent_size = 2`` () =
formatSourceString
false
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range =
{ Start =
{ Line = range.StartLine - 1
Character = range.StartColumn }
End =
{ Line = range.EndLine - 1
Character = range.EndColumn } }

[| { Range = range; NewText = formatted } |]
"""
{ config with IndentSize = 2 }
|> prepend newline
|> should
equal
"""
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
let range = {
Start = {
Line = range.StartLine - 1
Character = range.StartColumn
}
End = {
Line = range.EndLine - 1
Character = range.EndColumn
}
}

[| { Range = range; NewText = formatted } |]
"""
3 changes: 2 additions & 1 deletion src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ let mkOpenAndCloseForArrayOrList isArray range =

let mkInheritConstructor (creationAide: CreationAide) (t: SynType) (e: SynExpr) (mInherit: range) (m: range) =
let inheritNode = stn "inherit" mInherit
let m = unionRanges mInherit m

match e with
| SynExpr.Const(constant = SynConst.Unit; range = StartEndRange 1 (mOpen, unitRange, mClose)) ->
Expand Down Expand Up @@ -962,7 +963,7 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =

match baseInfo, copyInfo with
| Some _, Some _ -> failwith "Unexpected that both baseInfo and copyInfo are present in SynExpr.Record"
| Some(t, e, mInherit, _, m), None ->
| Some(t, e, m, _, mInherit), None ->
let inheritCtor = mkInheritConstructor creationAide t e mInherit m

ExprInheritRecordNode(stn "{" mOpen, inheritCtor, fieldNodes, stn "}" mClose, exprRange)
Expand Down
Loading