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

Add indent when line comment precedes type info, #565 #617

Merged
merged 3 commits into from
Jan 3, 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
22 changes: 22 additions & 0 deletions src/Fantomas.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,28 @@ let x =
if true then 1 else 0
"""

[<Test>]
let ``line comment before return type info should indent before colon, 565`` () =
formatSourceString false """module Bar =
let f a
// foo
: int
=
0
""" ({ config with
SpaceAfterComma = false
SpaceAfterSemicolon = false
SpaceAroundDelimiter = false
SpaceBeforeArgument = false })
|> prepend newline
|> should equal """
module Bar =
let f a
// foo
: int =
0
"""

[<Test>]
let ``has symbol in signature requires paren, 564`` () =
formatSourceString false """module Bar =
Expand Down
8 changes: 7 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,13 @@ and genExprSepEqPrependType astContext prefix (pat:SynPat) e ctx =
addSpaceAfterGenericConstructBeforeColon
| _ -> sepNone

(prefix +> addExtraSpaceBeforeGenericType +> sepColon +> genType astContext false t +> sepEq
let genCommentBeforeColon ctx =
let hasLineComment = TriviaHelpers.``has line comment before`` t.Range ctx.Trivia
(ifElse hasLineComment indent sepNone +> enterNode t.Range) ctx

(prefix +> addExtraSpaceBeforeGenericType
+> genCommentBeforeColon
+> sepColon +> genType astContext false t +> sepEq
+> breakNlnOrAddSpace astContext (hasTriviaContentAfterEqual || multilineCheck || checkPreserveBreakForExpr e ctx) e) ctx
| e ->

Expand Down
8 changes: 7 additions & 1 deletion src/Fantomas/TriviaHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ module TriviaHelpers =
| Comment(LineCommentAfterSourceCode(_)) -> true
| _ -> false
)
|> (List.isEmpty >> not)
|> (List.isEmpty >> not)

let internal ``has line comment before`` range triviaNodes =
triviaNodes
|> List.tryFind (fun tv -> tv.Range = range)
|> Option.map (fun tv -> tv.ContentBefore |> List.exists (function | Comment(LineCommentOnSingleLine(_)) -> true | _ -> false))
|> Option.defaultValue false