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

Print in keyword after correct SynBinding #1180

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,3 +1061,18 @@ let internal sepSpace =
else
(!- " ") ctx
"""

[<Test>]
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
()
"""
2 changes: 1 addition & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a better thing to do here would be looking inside the range between the last binding end and the body expression start.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thought of that but we capture nested LetOrUse expressions so that information is lost in CodePrinter.
See

let rec (|LetOrUses|_|) =

|> List.tryHead

let isInSameLine ctx =
Expand Down
8 changes: 8 additions & 0 deletions src/Fantomas/TriviaHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it need to handle cases where in is on another line?

do
    let _ = ()
      in
     () // note the different indent is allowed here due to `in` use

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it should since we are doing this.
Thanks for pointing that out.

&& range.StartColumn < t.Range.StartColumn) -> Some(tok, t)
| _ -> None)

let ``has line comment after`` triviaNode =
triviaNode.ContentAfter
|> List.filter (fun tn ->
Expand Down