Skip to content

Commit

Permalink
Only keep collecting line comments if they are not separate by a new …
Browse files Browse the repository at this point in the history
…line (#923)

* Only keep collection line comments if they are not separate by a new line. Fixes #920.

* Removed mutable variable 🙌
  • Loading branch information
nojaf committed Jun 19, 2020
1 parent 1d551ca commit cb09c45
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 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>
"""
8 changes: 5 additions & 3 deletions src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb09c45

Please sign in to comment.