Skip to content

Commit

Permalink
Consider comment after single semicolon as LineCommentOnSingleLine. F…
Browse files Browse the repository at this point in the history
…ixes #1643. (#1653)
  • Loading branch information
nojaf committed Apr 15, 2021
1 parent f971f5c commit bbaa6d2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,70 @@ let meh = 7
// Dollar
let meh = 7
"""

[<Test>]
let ``comment after semi colon in record definition, 1643`` () =
formatSourceString
false
"""
type T =
{ id : int
; value : RT.Dval
; retries : int
; canvasID : CanvasID
; canvasName : string
; module_ : string
; name : string
; modifier : string
; // Delay in ms since it entered the queue
delay : float }
"""
config
|> prepend newline
|> should
equal
"""
type T =
{ id: int
value: RT.Dval
retries: int
canvasID: CanvasID
canvasName: string
module_: string
name: string
modifier: string
// Delay in ms since it entered the queue
delay: float }
"""

[<Test>]
let ``comment after semicolon`` () =
formatSourceString
false
"""
let a = 8 ; // foobar
"""
config
|> prepend newline
|> should
equal
"""
let a = 8 // foobar
"""

[<Test>]
let ``comment after semicolon on next line`` () =
formatSourceString
false
"""
let a = 8
; // foobar
"""
config
|> prepend newline
|> should
equal
"""
let a = 8
// foobar
"""
20 changes: 20 additions & 0 deletions src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,12 @@ let private (|WhiteSpaceToken|_|) (token: Token) =
else
None

let private (|SemicolonToken|_|) (token: Token) =
if token.TokenInfo.Tag = 83 then
Some token
else
None

let private (|LineComments|_|) (tokens: Token list) =
let rec collect (tokens: Token list) (lastLineNumber: int) (commentTokens: Token list) =
match tokens with
Expand Down Expand Up @@ -730,6 +736,20 @@ let rec private getTriviaFromTokensThemSelves

let isAfterSourceCode =
match prevButOneToken, prevToken with
| Some (SemicolonToken sc), Some (WhiteSpaceToken _) when sc.LineNumber = headLineNumber ->
let tokensOfSameLine =
let rec collect (index: int) (acc: Token list) : Token list =
match List.tryItem index allTokens with
| Some t when (t.LineNumber = headLineNumber) -> collect (index - 1) (t :: acc)
| _ -> acc

collect (headIndex - 3) []

match tokensOfSameLine with
| []
// WHITESPACE SEMICOLON WHITESPACE LINE_COMMENT, see https://github.com/fsprojects/fantomas/issues/1643
| [ WhiteSpaceToken _ ] -> false
| _ -> true
| Some { LineNumber = ncln }, Some (WhiteSpaceToken _) ->
// IDENT WHITESPACE LINE_COMMENT
ncln = headLineNumber
Expand Down

0 comments on commit bbaa6d2

Please sign in to comment.