diff --git a/src/Fantomas.Core.Tests/PrefixTests.fs b/src/Fantomas.Core.Tests/PrefixTests.fs index 61f67b45c..a596bb4da 100644 --- a/src/Fantomas.Core.Tests/PrefixTests.fs +++ b/src/Fantomas.Core.Tests/PrefixTests.fs @@ -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" ] [] let ``operators maintain spacing from literal values which start with + or -`` (literalValue: string) = @@ -155,7 +165,9 @@ let operator_application_literal_values_without_sign = "\"text\"B" ] [] -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} @@ -167,3 +179,33 @@ let subtractTwo = + %s{literalValue} $""" let subtractTwo = +%s{literalValue} """ + +[] +let ``add space between prefix and quotation`` () = + formatSourceString + """ +let _ = + <@ 1 @> +""" + config + |> prepend newline + |> should + equal + """ +let _ = + <@ 1 @> +""" + +[] +let ``add space between prefix and measure`` () = + formatSourceString + """ +let _ = - +1 +let _ = - -1 +""" + config + |> prepend newline + |> should + equal + """ +let _ = - +1 +let _ = - -1 +""" diff --git a/src/Fantomas.Core/CodePrinter.fs b/src/Fantomas.Core/CodePrinter.fs index 1cf3d21fe..bb9f0d473 100644 --- a/src/Fantomas.Core/CodePrinter.fs +++ b/src/Fantomas.Core/CodePrinter.fs @@ -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 + | 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.