Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting power operator in code quotation pattern match fails #1945

Closed
2 of 3 tasks
cgravill opened this issue Nov 2, 2021 · 3 comments
Closed
2 of 3 tasks

Formatting power operator in code quotation pattern match fails #1945

cgravill opened this issue Nov 2, 2021 · 3 comments

Comments

@cgravill
Copy link
Member

cgravill commented Nov 2, 2021

Issue created from fantomas-online

Code

open System
open FSharp.Quotations
open FSharp.Quotations.DerivedPatterns

let expr : Expr<float> = <@ 1.4 / 4.4 @>

match expr with
| SpecificCall <@@ ( / ) @@> (_, _, [s1; s2]) ->    
    printfn "Div"
| SpecificCall <@@ ( ** ) @@> (_, _, [s1; s2]) ->
    printfn "Pow"
| _ ->
    printfn "No match"

[<EntryPoint>]
let main argv =
    printfn "Hello world"
    0 // return an integer exit code

Result

open System
open FSharp.Quotations
open FSharp.Quotations.DerivedPatterns

let expr: Expr<float> = <@ 1.4 / 4.4 @>

match expr with
| SpecificCall <@@ (/) @@> (_, _, [ s1; s2 ]) -> printfn "Div"
| SpecificCall <@@ (**) @@> (_, _, [ s1; s2 ]) -> printfn "Pow"
| _ -> printfn "No match"

[<EntryPoint>]
let main argv =
    printfn "Hello world"
    0 // return an integer exit code

Problem description

Taken from a larger codebase where we're doing code transformations. The power operator ** is getting formatted such that it breaks the code - it looks like it's become a code comment which may explain the error.

Extra information

  • The formatted result breaks by code.
  • The formatted result gives compiler warnings.
  • I or my company would be willing to help fix this.

Options

Fantomas 4.6 branch at 11/01/2021 19:30:14 - c02b8d1

Default Fantomas configuration

Did you know that you can ignore files when formatting from fantomas-tool or the FAKE targets by using a .fantomasignore file?

@nojaf
Copy link
Contributor

nojaf commented Nov 2, 2021

Hey Colin! Thanks for reporting this issue.

I believe the problem might be centred around

let (|OpNameFullInPattern|) (x: Identifier) =
let r = x.Ranges
let s = x.Text
let s' = DecompileOpName s
(if IsActivePatternName s
|| IsInfixOperator s
|| IsPrefixOperator s
|| IsTernaryOperator s
|| s = "op_Dynamic" then
/// Use two spaces for symmetry
if String.startsWithOrdinal "*" s' && s' <> "*" then
sprintf "( %s )" s'
else
sprintf "(%s)" s'
else
match x with
| Id (Ident s)
| LongId (LongIdent s) -> DecompileOpName s)
|> fun s -> (s, r)

Are you interested in submitting a PR?

@cgravill
Copy link
Member Author

cgravill commented Nov 2, 2021

I'd be happy to have a go. It looks like adding a special case then leaving extra space would work.

@nojaf
Copy link
Contributor

nojaf commented Nov 2, 2021

Yup, I was thinking along those lines as well.

nojaf added a commit that referenced this issue Nov 6, 2021
* #1945 regression test

* Adapt (|OpNameFullInPattern|) special handling of
star operators. When the space is removed it becomes
a nested comment breaking code

* Use standard template

* Remove blank line and put source on next line.

Co-authored-by: nojaf <florian.verdonck@outlook.com>
@nojaf nojaf closed this as completed Nov 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants