Skip to content

Commit

Permalink
Implement MaxBindingWidth for members
Browse files Browse the repository at this point in the history
Implements the MaxBindingWidth setting for member values and
member functions and adds test cases.
  • Loading branch information
Bobface committed May 26, 2020
1 parent 596b9c9 commit 8d6907d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/AttributeTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ type Funcs =
|> should equal """[<Extension>]
type Funcs =
[<Extension>]
static member ToFunc(f: Action<_, _, _>) = Func<_, _, _, _>(fun a b c -> f.Invoke(a, b, c))
static member ToFunc(f: Action<_, _, _>) =
Func<_, _, _, _>(fun a b c -> f.Invoke(a, b, c))
"""

[<Test>]
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="MultilineBlockBracketsOnSameColumnArrayOrListTests.fs" />
<Compile Include="NewlineBetweenTypeDefinitionAndMembersTests.fs" />
<Compile Include="KeepIfThenInSameLineTests.fs" />
<Compile Include="MaxBindingWidthTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
Expand Down
83 changes: 83 additions & 0 deletions src/Fantomas.Tests/MaxBindingWidthTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
module Fantomas.Tests.MaxBindingWidthTests

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

let config = { config with MaxBindingWidth = 20 }

[<Test>]
let ``should apply to value definition``() =
formatSourceString false """let a = bbbbbbbbbbbbbbbbbbbbbbbbbb""" config
|> should equal """let a =
bbbbbbbbbbbbbbbbbbbbbbbbbb
"""

[<Test>]
let ``should not apply to short value definition``() =
formatSourceString false """let a = b""" config
|> should equal """let a = b
"""

[<Test>]
let ``should apply to function definition``() =
formatSourceString false """let a bbbbbbbbbbbbbbbbbbbbbbbbbb = bbbbbbbbbbbbbbbbbbbbbbbbbb + 1""" config
|> should equal """let a bbbbbbbbbbbbbbbbbbbbbbbbbb =
bbbbbbbbbbbbbbbbbbbbbbbbbb + 1
"""

[<Test>]
let ``should not apply to short function definition``() =
formatSourceString false """let a b = b + 1""" config
|> should equal """let a b = b + 1
"""

[<Test>]
let ``should apply to member value definition``() =
formatSourceString false """type T =
let aaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbb + 1
member this.ccccccccccccccccccccccccccccccc = dddddddddddddddddddddddddddd + 2
""" config
|> should equal """type T =
let aaaaaaaaaaaaaaaaaaaa =
bbbbbbbbbbbbbbbbbbb + 1
member this.ccccccccccccccccccccccccccccccc =
dddddddddddddddddddddddddddd + 2
"""

[<Test>]
let ``should not apply to short member value definition``() =
formatSourceString false """type T =
let a = b + 1
member this.c = d + 2
""" config
|> should equal """type T =
let a = b + 1
member this.c = d + 2
"""

[<Test>]
let ``should apply to member function definition``() =
formatSourceString false """type T =
let aaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbb = bbbbbbbbbbbbbbbbbbb + 1
member this.cccccccccccccc dddddddddddddd = dddddddddddddd + 2
""" config
|> should equal """type T =
let aaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbb =
bbbbbbbbbbbbbbbbbbb + 1
member this.cccccccccccccc dddddddddddddd =
dddddddddddddd + 2
"""

[<Test>]
let ``should not apply to short member function definition``() =
formatSourceString false """type T =
let a b = b + 1
member this.c d = d + 2
""" config
|> should equal """type T =
let a b = b + 1
member this.c d = d + 2
"""
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,8 @@ type TestExtensions =
static member SomeExtension(x) = ""
[<Extension>]
static member SomeOtherExtension(x) = ""
static member SomeOtherExtension(x) =
""
"""

[<Test>]
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,15 @@ and genMemberBinding astContext b =
ifElse
prefixIsLong
(indent +> sepNln +> sepColon +> genType astContext false t +> sepNln +> !- "=" +> sepNln +> genExpr astContext e +> unindent)
(sepColon +> genType astContext false t +> sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)))
(sepColon +> genType astContext false t +> sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsMaxBindingWidthIgnoreShort (genExpr astContext e)))
| e ->
genAttributesAndXmlDoc
+> leadingExpressionIsMultiline prefix
(fun prefixIsLong ->
ifElse
prefixIsLong
(indent +> sepNln +> !- "=" +> sepNln +> genExpr astContext e +> unindent)
(sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth (genExpr astContext e)))
(sepEq +> sepSpaceOrIndentAndNlnIfExpressionExceedsMaxBindingWidthIgnoreShort (genExpr astContext e)))

| ExplicitCtor(ats, px, ao, p, e, so) ->
let prefix =
Expand Down
32 changes: 23 additions & 9 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,21 @@ let internal leadingExpressionIsMultiline leadingExpression continuationExpressi
let hasWriteLineEventsAfterExpression = contextAfterLeading.WriterEvents |>Seq.skip eventCountBeforeExpression |> Seq.exists (function | WriteLine _ -> true | _ -> false)
continuationExpression hasWriteLineEventsAfterExpression contextAfterLeading

let private expressionExceedsPageWidth beforeShort afterShort beforeLong afterLong expr (ctx: Context) =
let private expressionExceedsWidth width ignoreShort beforeShort afterShort beforeLong afterLong expr (ctx: Context) =
// if the context is already inside a ShortExpression mode, we should try the shortExpression in this case.
match ctx.WriterModel.Mode with
| ShortExpression _ ->
| ShortExpression _ when not ignoreShort ->
// if the context is already inside a ShortExpression mode, we should try the shortExpression in this case.
(beforeShort +> expr +> afterShort) ctx
| _ ->
let shortExpressionContext = ctx.WithShortExpression(ctx.Config.PageWidth, 0)
let shortExpressionContext = ctx.WithShortExpression(width, 0)
let resultContext = (beforeShort +> expr +> afterShort) shortExpressionContext
let fallbackExpression = beforeLong +> expr +> afterLong

match resultContext.WriterModel.Mode with
| ShortExpression info ->
// verify the expression is not longer than allowed
if info.ConfirmedMultiline || info.IsTooLong ctx.Config.PageWidth resultContext.Column
if info.ConfirmedMultiline || info.IsTooLong width resultContext.Column
then fallbackExpression ctx
else
{ resultContext with WriterModel = { resultContext.WriterModel with Mode = ctx.WriterModel.Mode } }
Expand All @@ -592,19 +592,32 @@ let private expressionExceedsPageWidth beforeShort afterShort beforeLong afterLo
/// try and write the expression on the remainder of the current line
/// add an indent and newline if the expression is longer
let internal autoIndentAndNlnIfExpressionExceedsPageWidth expr (ctx:Context) =
expressionExceedsPageWidth
expressionExceedsWidth
ctx.Config.PageWidth false
sepNone sepNone // before and after for short expressions
(indent +> sepNln) unindent // before and after for long expressions
expr ctx

let internal sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth expr (ctx:Context) =
expressionExceedsPageWidth
let internal sepSpaceOrIndentAndNlnIfExpressionExceedsWidth width expr (ctx:Context) =
expressionExceedsWidth
width false
sepSpace sepNone // before and after for short expressions
(indent +> sepNln) unindent // before and after for long expressions
expr ctx

let internal sepSpaceOrIndentAndNlnIfExpressionExceedsMaxBindingWidthIgnoreShort expr (ctx:Context) =
expressionExceedsWidth
ctx.Config.MaxBindingWidth true
sepSpace sepNone // before and after for short expressions
(indent +> sepNln) unindent // before and after for long expressions
expr ctx

let internal sepSpaceOrIndentAndNlnIfExpressionExceedsPageWidth expr (ctx:Context) =
sepSpaceOrIndentAndNlnIfExpressionExceedsWidth ctx.Config.PageWidth expr ctx

let internal autoNlnIfExpressionExceedsPageWidth expr (ctx: Context) =
expressionExceedsPageWidth
expressionExceedsWidth
ctx.Config.PageWidth false
sepNone sepNone // before and after for short expressions
sepNln sepNone // before and after for long expressions
expr ctx
Expand Down Expand Up @@ -936,7 +949,8 @@ let internal sepNlnTypeAndMembers (firstMemberRange: range option) ctx =
ctx

let internal autoNlnConsideringTriviaIfExpressionExceedsPageWidth expr range (ctx: Context) =
expressionExceedsPageWidth
expressionExceedsWidth
ctx.Config.PageWidth false
sepNone sepNone // before and after for short expressions
(sepNlnConsideringTriviaContentBefore range) sepNone // before and after for long expressions
expr ctx
Expand Down

0 comments on commit 8d6907d

Please sign in to comment.