From d7f4ad0680444ff05e3279dc0f2a888132f93010 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 2 Oct 2020 11:07:54 +0200 Subject: [PATCH] Print in keyword after correct SynBinding. Fixes #1143. --- src/Fantomas.Tests/LetBindingTests.fs | 15 +++++++++++++++ src/Fantomas/CodePrinter.fs | 2 +- src/Fantomas/TriviaHelpers.fs | 8 ++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 1319307f4f..86ef33ff08 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -1061,3 +1061,18 @@ let internal sepSpace = else (!- " ") ctx """ + +[] +let ``in keyword in LetOrUse with and keyword, 1176`` () = + formatSourceString false """ +do + let rec f = () + and g = () in + () +""" config + |> prepend newline + |> should equal """ +do let rec f = () + and g = () in + () +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 94a8ce879e..8717664aef 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -2000,7 +2000,7 @@ and genExpr astContext synExpr = mkRange "IN" binding.RangeOfBindingAndRhs.End e.Range.Start Map.tryFindOrEmptyList IN ctx.TriviaTokenNodes - |> TriviaHelpers.``keyword token inside range`` inRange + |> TriviaHelpers.``keyword token after start column and on same line`` inRange |> List.tryHead let isInSameLine ctx = diff --git a/src/Fantomas/TriviaHelpers.fs b/src/Fantomas/TriviaHelpers.fs index 330f60da23..e3b9b8d2a5 100644 --- a/src/Fantomas/TriviaHelpers.fs +++ b/src/Fantomas/TriviaHelpers.fs @@ -39,6 +39,14 @@ module internal TriviaHelpers = | TriviaNodeType.Token (_, tok) when (RangeHelpers.``range contains`` range t.Range) -> Some(tok, t) | _ -> None) + let ``keyword token after start column and on same line`` (range: range) (trivia: TriviaNode list) = + trivia + |> List.choose (fun t -> + match t.Type with + | TriviaNodeType.Token (_, tok) when (range.StartLine = t.Range.StartLine + && range.StartColumn < t.Range.StartColumn) -> Some(tok, t) + | _ -> None) + let ``has line comment after`` triviaNode = triviaNode.ContentAfter |> List.filter (fun tn ->