From cb09c45f8e16d6c69ce02edd88ff80c0274c22c2 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Fri, 19 Jun 2020 10:46:44 +0200 Subject: [PATCH] Only keep collecting line comments if they are not separate by a new line (#923) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Only keep collection line comments if they are not separate by a new line. Fixes #920. * Removed mutable variable 🙌 --- src/Fantomas.Tests/CommentTests.fs | 44 +++++++++++++++++++++++++++++- src/Fantomas/TokenParser.fs | 8 ++++-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index 90d18c43e3..d35de6e808 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -860,4 +860,46 @@ type LongIdentWithDots = /// more freedom about typechecking these expressions. /// LongIdent can be empty list - it is used to denote that name of some AST element is absent (i.e. empty type name in inherit) type LongIdentWithDots = LongIdentWithDots of id: LongIdent * dotms: range list -""" \ No newline at end of file +""" + +[] +let ``newline between comments should lead to individual comments, 920`` () = + formatSourceString false """ +[] +type IExports = + abstract DataSet: DataSetStatic + abstract DataView: DataViewStatic + abstract Graph2d: Graph2dStatic + abstract Timeline: TimelineStatic + // abstract Timeline: TimelineStaticStatic + abstract Network: NetworkStatic + +// type [] MomentConstructor1 = +// [] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?strict: bool -> Moment + +// type [] MomentConstructor2 = +// [] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?language: string * ?strict: bool -> Moment + +// type MomentConstructor = +// U2 +""" config + |> prepend newline + |> should equal """ +[] +type IExports = + abstract DataSet: DataSetStatic + abstract DataView: DataViewStatic + abstract Graph2d: Graph2dStatic + abstract Timeline: TimelineStatic + // abstract Timeline: TimelineStaticStatic + abstract Network: NetworkStatic + +// type [] MomentConstructor1 = +// [] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?strict: bool -> Moment + +// type [] MomentConstructor2 = +// [] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?language: string * ?strict: bool -> Moment + +// type MomentConstructor = +// U2 +""" diff --git a/src/Fantomas/TokenParser.fs b/src/Fantomas/TokenParser.fs index f1077c113e..cdd0ad2967 100644 --- a/src/Fantomas/TokenParser.fs +++ b/src/Fantomas/TokenParser.fs @@ -262,9 +262,11 @@ let rec private getTriviaFromTokensThemSelves (allTokens: Token list) (tokens: T match tokens with | headToken::rest when (headToken.TokenInfo.TokenName = "LINE_COMMENT") -> let lineCommentTokens = - rest - |> List.takeWhile (fun t -> t.TokenInfo.TokenName = "LINE_COMMENT") // && t.LineNumber = headToken.LineNumber) - + Seq.zip rest (headToken::rest |> List.map (fun x -> x.LineNumber)) + |> Seq.takeWhile (fun (t, currentLineNumber) -> t.TokenInfo.TokenName = "LINE_COMMENT" && t.LineNumber <= (currentLineNumber + 1)) + |> Seq.map fst + |> Seq.toList + let comment = headToken |> List.prependItem lineCommentTokens