Skip to content

Commit

Permalink
Add feedback from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Oct 29, 2024
1 parent da19ee5 commit ad7e654
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
46 changes: 44 additions & 2 deletions src/Fantomas.Core.Tests/PrefixTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,17 @@ let operator_application_literal_values_with_sign =
"-4.41F"
"-4.14"
"-12456I"
"-0.7833M" ]
"-0.7833M"
"+46y"
"+46s"
"+46"
"+46l"
"+423n"
"+46L"
"+3.41F"
"+3.14"
"+32456I"
"+0.7833M" ]

[<TestCaseSource("operator_application_literal_values_with_sign")>]
let ``operators maintain spacing from literal values which start with + or -`` (literalValue: string) =
Expand Down Expand Up @@ -155,7 +165,9 @@ let operator_application_literal_values_without_sign =
"\"text\"B" ]

[<TestCaseSource("operator_application_literal_values_without_sign")>]
let ``operators maintain spacing from literal values which start without + or -`` (literalValue: string) =
let ``no space added between prefix operators and literal values that do not start with a symbol``
(literalValue: string)
=
formatSourceString
$"""
let subtractTwo = + %s{literalValue}
Expand All @@ -167,3 +179,33 @@ let subtractTwo = + %s{literalValue}
$"""
let subtractTwo = +%s{literalValue}
"""

[<Test>]
let ``add space between prefix and quotation`` () =
formatSourceString
"""
let _ = + <@ 1 @>
"""
config
|> prepend newline
|> should
equal
"""
let _ = + <@ 1 @>
"""

[<Test>]
let ``add space between prefix and measure`` () =
formatSourceString
"""
let _ = - +1<m>
let _ = - -1<m>
"""
config
|> prepend newline
|> should
equal
"""
let _ = - +1<m>
let _ = - -1<m>
"""
21 changes: 14 additions & 7 deletions src/Fantomas.Core/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -757,13 +757,20 @@ let genExpr (e: Expr) =
let genWithSpace = genSingleTextNode node.Operator +> sepSpace +> genExpr node.Expr

match node.Expr with
// E.g. !! @"foobar", because !!@ would be mistaken for an operator.
| Expr.Constant(Constant.FromText(stn)) ->
match stn.Text.[0] with
| '@'
| '-'
| '+' -> genWithSpace
| _ -> genWithoutSpace
// E.g. + <@ 1 @>
| Expr.Quote _ -> genWithSpace
| Expr.Constant(constant) ->
// E.g. !! @"foobar", because !!@ would be mistaken for an operator.
match constant with
| Constant.FromText(stn) ->
match stn.Text.[0] with
| '@'
| '-'
| '+' -> genWithSpace
| _ -> genWithoutSpace
// E.g. - +1<m>
| Constant.Measure _ -> genWithSpace
| Constant.Unit _ -> genWithoutSpace
// !- $"blah{s}"
| Expr.InterpolatedStringExpr _ -> genWithSpace
// We don't respect SpaceBeforeLowercaseInvocation here because it can alter the meaning of the code.
Expand Down

0 comments on commit ad7e654

Please sign in to comment.