Skip to content

Commit e6465ca

Browse files
authored
Indent cramped record field body if indent_size is low. (#2802)
* 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.
1 parent a8725d9 commit e6465ca

File tree

7 files changed

+334
-23
lines changed

7 files changed

+334
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [6.0.0-alpha-008] - 2023-03-27
4+
5+
### Fixed
6+
* Nested multiline record with indent_size = 2. [#2801](https://github.com/fsprojects/fantomas/issues/2801)
7+
* Idempotency problem when comment after opening brace in inherit record. [#2803](https://github.com/fsprojects/fantomas/issues/2803)
8+
39
## [6.0.0-alpha-007] - 2023-03-27
410

511
### Changed

src/Fantomas.Core.Tests/AlignedMultilineBracketStyleTests.fs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,3 +1593,42 @@ struct // 1
15931593
// 5
15941594
|} // 6
15951595
"""
1596+
1597+
[<Test>]
1598+
let ``multiline field body expression where indent_size = 2`` () =
1599+
formatSourceString
1600+
false
1601+
"""
1602+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
1603+
let range =
1604+
{ Start =
1605+
{ Line = range.StartLine - 1
1606+
Character = range.StartColumn }
1607+
End =
1608+
{ Line = range.EndLine - 1
1609+
Character = range.EndColumn } }
1610+
1611+
[| { Range = range; NewText = formatted } |]
1612+
"""
1613+
{ config with IndentSize = 2 }
1614+
|> prepend newline
1615+
|> should
1616+
equal
1617+
"""
1618+
let handlerFormattedRangeDoc (lines : NamedText, formatted : string, range : FormatSelectionRange) =
1619+
let range =
1620+
{
1621+
Start =
1622+
{
1623+
Line = range.StartLine - 1
1624+
Character = range.StartColumn
1625+
}
1626+
End =
1627+
{
1628+
Line = range.EndLine - 1
1629+
Character = range.EndColumn
1630+
}
1631+
}
1632+
1633+
[| { Range = range ; NewText = formatted } |]
1634+
"""

src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs

Lines changed: 175 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,8 +1742,8 @@ let ``record with comments above field, indent 2`` () =
17421742
equal
17431743
"""
17441744
{ Foo =
1745-
// bar
1746-
someValue }
1745+
// bar
1746+
someValue }
17471747
"""
17481748

17491749
[<Test>]
@@ -1799,8 +1799,8 @@ let ``anonymous record with multiline field, indent 2`` () =
17991799
equal
18001800
"""
18011801
{| Foo =
1802-
// meh
1803-
someValue |}
1802+
// meh
1803+
someValue |}
18041804
"""
18051805

18061806
[<Test>]
@@ -2280,3 +2280,174 @@ let a =
22802280
// test2
22812281
|}
22822282
"""
2283+
2284+
[<Test>]
2285+
let ``multiline field body expression where indent_size = 2, 2801`` () =
2286+
formatSourceString
2287+
false
2288+
"""
2289+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2290+
let range =
2291+
{ Start =
2292+
{ Line = range.StartLine - 1
2293+
Character = range.StartColumn }
2294+
End =
2295+
{ Line = range.EndLine - 1
2296+
Character = range.EndColumn } }
2297+
2298+
[| { Range = range; NewText = formatted } |]
2299+
"""
2300+
{ config with IndentSize = 2 }
2301+
|> prepend newline
2302+
|> should
2303+
equal
2304+
"""
2305+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2306+
let range =
2307+
{ Start =
2308+
{ Line = range.StartLine - 1
2309+
Character = range.StartColumn }
2310+
End =
2311+
{ Line = range.EndLine - 1
2312+
Character = range.EndColumn } }
2313+
2314+
[| { Range = range; NewText = formatted } |]
2315+
"""
2316+
2317+
[<Test>]
2318+
let ``multiline field body expression where indent_size = 2, anonymous record`` () =
2319+
formatSourceString
2320+
false
2321+
"""
2322+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2323+
let range =
2324+
{| Start =
2325+
{ Line = range.StartLine - 1
2326+
Character = range.StartColumn }
2327+
End =
2328+
{ Line = range.EndLine - 1
2329+
Character = range.EndColumn } |}
2330+
2331+
[| { Range = range; NewText = formatted } |]
2332+
"""
2333+
{ config with IndentSize = 2 }
2334+
|> prepend newline
2335+
|> should
2336+
equal
2337+
"""
2338+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2339+
let range =
2340+
{| Start =
2341+
{ Line = range.StartLine - 1
2342+
Character = range.StartColumn }
2343+
End =
2344+
{ Line = range.EndLine - 1
2345+
Character = range.EndColumn } |}
2346+
2347+
[| { Range = range; NewText = formatted } |]
2348+
"""
2349+
2350+
[<Test>]
2351+
let ``multiline field body expression where indent_size = 2, update record`` () =
2352+
formatSourceString
2353+
false
2354+
"""
2355+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2356+
let range =
2357+
{ x with
2358+
Start =
2359+
{ Line = range.StartLine - 1
2360+
Character = range.StartColumn }
2361+
End =
2362+
{ Line = range.EndLine - 1
2363+
Character = range.EndColumn } }
2364+
2365+
[| { Range = range; NewText = formatted } |]
2366+
"""
2367+
{ config with IndentSize = 2 }
2368+
|> prepend newline
2369+
|> should
2370+
equal
2371+
"""
2372+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2373+
let range =
2374+
{ x with
2375+
Start =
2376+
{ Line = range.StartLine - 1
2377+
Character = range.StartColumn }
2378+
End =
2379+
{ Line = range.EndLine - 1
2380+
Character = range.EndColumn } }
2381+
2382+
[| { Range = range; NewText = formatted } |]
2383+
"""
2384+
2385+
let ``multiline field body expression where indent_size = 2, inherit record`` () =
2386+
formatSourceString
2387+
false
2388+
"""
2389+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2390+
let range =
2391+
{ inherit Foo()
2392+
Start =
2393+
{ Line = range.StartLine - 1
2394+
Character = range.StartColumn }
2395+
End =
2396+
{ Line = range.EndLine - 1
2397+
Character = range.EndColumn } }
2398+
[| { Range = range; NewText = formatted } |]
2399+
"""
2400+
{ config with IndentSize = 2 }
2401+
|> prepend newline
2402+
|> should
2403+
equal
2404+
"""
2405+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
2406+
let range =
2407+
{ inherit Foo()
2408+
Start =
2409+
{ Line = range.StartLine - 1
2410+
Character = range.StartColumn }
2411+
End =
2412+
{ Line = range.EndLine - 1
2413+
Character = range.EndColumn } }
2414+
2415+
[| { Range = range; NewText = formatted } |]
2416+
"""
2417+
2418+
[<Test>]
2419+
let ``trivia after opening brace in inherit expression, 2803`` () =
2420+
formatSourceString
2421+
false
2422+
"""
2423+
let range =
2424+
{ // foo
2425+
// bar
2426+
inherit // reason
2427+
Foo()
2428+
X = y
2429+
Z =
2430+
someReallyLongExpressionThatIsLongerThanTheLineLength
2431+
aLongArgument
2432+
//
2433+
anotherLongArgument
2434+
fooBar }
2435+
"""
2436+
config
2437+
|> prepend newline
2438+
|> should
2439+
equal
2440+
"""
2441+
let range =
2442+
{ // foo
2443+
// bar
2444+
inherit // reason
2445+
Foo()
2446+
X = y
2447+
Z =
2448+
someReallyLongExpressionThatIsLongerThanTheLineLength
2449+
aLongArgument
2450+
//
2451+
anotherLongArgument
2452+
fooBar }
2453+
"""

src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<Compile Include="Stroustrup\FunctionApplicationDualListTests.fs" />
111111
<Compile Include="Stroustrup\SynExprAnonRecdStructTests.fs" />
112112
<Compile Include="Stroustrup\ExperimentalElmishTests.fs" />
113+
<Compile Include="Stroustrup\RecordInstanceTests.fs" />
113114
<Compile Include="IdentTests.fs" />
114115
<Compile Include="RecordDeclarationsWithXMLDocTests.fs" />
115116
<Compile Include="MaxIfThenShortWidthTests.fs" />
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Fantomas.Core.Tests.Stroustrup.RecordInstanceTests
2+
3+
open NUnit.Framework
4+
open FsUnit
5+
open Fantomas.Core.Tests.TestHelper
6+
open Fantomas.Core
7+
8+
let config =
9+
{ config with
10+
MultilineBracketStyle = Stroustrup }
11+
12+
[<Test>]
13+
let ``multiline field body expression where indent_size = 2`` () =
14+
formatSourceString
15+
false
16+
"""
17+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
18+
let range =
19+
{ Start =
20+
{ Line = range.StartLine - 1
21+
Character = range.StartColumn }
22+
End =
23+
{ Line = range.EndLine - 1
24+
Character = range.EndColumn } }
25+
26+
[| { Range = range; NewText = formatted } |]
27+
"""
28+
{ config with IndentSize = 2 }
29+
|> prepend newline
30+
|> should
31+
equal
32+
"""
33+
let handlerFormattedRangeDoc (lines: NamedText, formatted: string, range: FormatSelectionRange) =
34+
let range = {
35+
Start = {
36+
Line = range.StartLine - 1
37+
Character = range.StartColumn
38+
}
39+
End = {
40+
Line = range.EndLine - 1
41+
Character = range.EndColumn
42+
}
43+
}
44+
45+
[| { Range = range; NewText = formatted } |]
46+
"""

src/Fantomas.Core/ASTTransformer.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ let mkOpenAndCloseForArrayOrList isArray range =
222222

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

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

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

968969
ExprInheritRecordNode(stn "{" mOpen, inheritCtor, fieldNodes, stn "}" mClose, exprRange)

0 commit comments

Comments
 (0)