Skip to content

Commit

Permalink
Fix 776, add IdentBetweenTicks trivia for SynPat.LongIdent node, dont…
Browse files Browse the repository at this point in the history
… use backticks when not needed (#777)

* #776 test

* add IdentBetweenTicks trivia for SynPat.LongIdent node

* dont use backticks when not needed
  • Loading branch information
jindraivanek committed Apr 24, 2020
1 parent a0eadfd commit 757ad17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ let ``should keep identifiers with + in double backticks``() =
|> should equal """let ``Foo+Bar`` () = x
"""

[<Test>]
let ``double backticks with non-alphanum character, 776``() =
formatSourceString false """let ``!foo hoo`` () = ()
let ``@foo hoo`` () = ()
let ``$foo hoo`` () = ()
let ``%foo hoo`` () = ()
let ``^foo hoo`` () = ()
let ``&foo hoo`` () = ()
let ``*foo hoo`` () = ()
let ``<foo hoo`` () = ()
let ``>foo hoo`` () = ()
let ``=foo hoo`` () = ()
let ``-foo hoo`` () = ()
""" config
|> should equal """let ``!foo hoo`` () = ()
let ``@foo hoo`` () = ()
let ``$foo hoo`` () = ()
let ``%foo hoo`` () = ()
let ``^foo hoo`` () = ()
let ``&foo hoo`` () = ()
let ``*foo hoo`` () = ()
let ``<foo hoo`` () = ()
let ``>foo hoo`` () = ()
let ``=foo hoo`` () = ()
let ``-foo hoo`` () = ()
"""

[<Test>]
let ``let bindings with return types``() =
formatSourceString false """
Expand Down
5 changes: 4 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2753,13 +2753,16 @@ and genTrivia (range: range) f =
enterNode range +> f +> leaveNode range

and infixOperatorFromTrivia range fallback (ctx: Context) =
// by specs, section 3.4 https://fsharp.org/specs/language-spec/4.1/FSharpSpec-4.1-latest.pdf#page=24&zoom=auto,-137,312
let validIdentRegex = """^(_|\p{L}|\p{Nl})([_'0-9]|\p{L}|\p{Nl}\p{Pc}|\p{Mn}|\p{Mc}|\p{Cf})*$"""
let isValidIdent x = Regex.Match(x, validIdentRegex).Success
ctx.Trivia
|> List.choose(fun t ->
match t.Range = range with
| true ->
match t.ContentItself with
| Some(IdentOperatorAsWord(iiw)) -> Some iiw
| Some(IdentBetweenTicks(iiw)) -> Some iiw // Used when value between ``...``
| Some(IdentBetweenTicks(iiw)) when not(isValidIdent fallback) -> Some iiw // Used when value between ``...``
| _ -> None
| _ -> None)
|> List.tryHead
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ let private addTriviaToTriviaNode (startOfSourceCode:int) (triviaNodes: TriviaNo
match t.Type with
| MainNode("SynExpr.Ident")
| MainNode("SynPat.Named")
| MainNode("SynPat.LongIdent")
| MainNode("Ident") -> true
| _ -> false
isIdent && (t.Range.StartColumn = range.StartColumn || t.Range.StartColumn = range.StartColumn + 1) && t.Range.StartLine = range.StartLine
Expand Down

0 comments on commit 757ad17

Please sign in to comment.