Skip to content

Commit

Permalink
Only keep collection line comments if they are not separate by a new …
Browse files Browse the repository at this point in the history
…line. Fixes fsprojects#920.
  • Loading branch information
nojaf committed Jun 17, 2020
1 parent f502c77 commit cc6219a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
44 changes: 43 additions & 1 deletion src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
"""

[<Test>]
let ``newline between comments should lead to individual comments, 920`` () =
formatSourceString false """
[<AllowNullLiteral>]
type IExports =
abstract DataSet: DataSetStatic
abstract DataView: DataViewStatic
abstract Graph2d: Graph2dStatic
abstract Timeline: TimelineStatic
// abstract Timeline: TimelineStaticStatic
abstract Network: NetworkStatic

// type [<AllowNullLiteral>] MomentConstructor1 =
// [<Emit "$0($1...)">] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?strict: bool -> Moment

// type [<AllowNullLiteral>] MomentConstructor2 =
// [<Emit "$0($1...)">] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?language: string * ?strict: bool -> Moment

// type MomentConstructor =
// U2<MomentConstructor1, MomentConstructor2>
""" config
|> prepend newline
|> should equal """
[<AllowNullLiteral>]
type IExports =
abstract DataSet: DataSetStatic
abstract DataView: DataViewStatic
abstract Graph2d: Graph2dStatic
abstract Timeline: TimelineStatic
// abstract Timeline: TimelineStaticStatic
abstract Network: NetworkStatic

// type [<AllowNullLiteral>] MomentConstructor1 =
// [<Emit "$0($1...)">] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?strict: bool -> Moment

// type [<AllowNullLiteral>] MomentConstructor2 =
// [<Emit "$0($1...)">] abstract Invoke: ?inp: MomentInput * ?format: MomentFormatSpecification * ?language: string * ?strict: bool -> Moment

// type MomentConstructor =
// U2<MomentConstructor1, MomentConstructor2>
"""
7 changes: 6 additions & 1 deletion src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,13 @@ let rec private getTriviaFromTokensThemSelves (allTokens: Token list) (tokens: T
match tokens with
| headToken::rest when (headToken.TokenInfo.TokenName = "LINE_COMMENT") ->
let lineCommentTokens =
let mutable currentLineNumber = headToken.LineNumber

rest
|> List.takeWhile (fun t -> t.TokenInfo.TokenName = "LINE_COMMENT") // && t.LineNumber = headToken.LineNumber)
|> List.takeWhile (fun t ->
let take = t.TokenInfo.TokenName = "LINE_COMMENT" && t.LineNumber <= (currentLineNumber + 1)
currentLineNumber <- t.LineNumber
take)

let comment =
headToken
Expand Down

0 comments on commit cc6219a

Please sign in to comment.