Skip to content

Commit

Permalink
Indent cramped record field body if indent_size is low. (#2802)
Browse files Browse the repository at this point in the history
* Indent cramped record field body if indent_size is low.

* Correct range of inherit keyword and inherit expression.

* Correctly indent multiline fields in inherit expression.

* Add changelog entry.

* Add new version.
  • Loading branch information
nojaf authored Mar 27, 2023
1 parent a8725d9 commit e6465ca
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

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

### 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

0 comments on commit e6465ca

Please sign in to comment.