Skip to content

Commit

Permalink
Add indent when line comment precedes type info, fsprojects#565 (fspr…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf authored Jan 3, 2020
1 parent ad6a81f commit 28bf163
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
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

0 comments on commit 28bf163

Please sign in to comment.