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

Only keep collecting line comments if they are not separate by a new line #923

Merged
merged 3 commits into from
Jun 19, 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
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