Skip to content

Commit

Permalink
Optimize comment after source code detection. Fixes fsprojects#1671.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Apr 22, 2021
1 parent c69dc06 commit b5c6d7f
Show file tree
Hide file tree
Showing 5 changed files with 344 additions and 136 deletions.
172 changes: 172 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,3 +1311,175 @@ let a = b
(* meh *)
let a = b
"""

[<Test>]
let ``comment right after first token`` () =
formatSourceString
false
"""
1//
// next line
"""
config
|> prepend newline
|> should
equal
"""
1 //
// next line
"""

[<Test>]
[<Ignore("line comment after block comment currently not supported")>]
let ``block comment followed by line comment`` () =
formatSourceString
false
"""
(* foo *)// bar
let a = 0
"""
config
|> prepend newline
|> should
equal
"""
(* foo *) // bar
let a = 0
"""

[<Test>]
let ``line comment after source code`` () =
formatSourceString
false
"""
__SOURCE_DIRECTORY__ // comment
"""
config
|> prepend newline
|> should
equal
"""
__SOURCE_DIRECTORY__ // comment
"""

[<Test>]
let ``line comment after hash define`` () =
formatSourceString
false
"""
#if FOO // MEH
#endif
"""
config
|> prepend newline
|> should
equal
"""
#if FOO // MEH
#endif
"""

[<Test>]
let ``line comment after interpolated string`` () =
formatSourceString
false
"""
$"{meh}.." // foo
"""
config
|> prepend newline
|> should
equal
"""
$"{meh}.." // foo
"""

[<Test>]
let ``line comment after negative constant`` () =
formatSourceString
false
"""
-1.0 // foo
"""
config
|> prepend newline
|> should
equal
"""
-1.0 // foo
"""

[<Test>]
let ``line comment after trivia number`` () =
formatSourceString
false
"""
1. // bar
"""
config
|> prepend newline
|> should
equal
"""
1. // bar
"""

[<Test>]
let ``line comment after infix operator in full words`` () =
formatSourceString
false
"""
op_LessThan // meh
"""
config
|> prepend newline
|> should
equal
"""
op_LessThan // meh
"""

[<Test>]
let ``line comment after ident between ticks`` () =
formatSourceString
false
"""
``foo oo`` // bar
"""
config
|> prepend newline
|> should
equal
"""
``foo oo`` // bar
"""

[<Test>]
let ``line comment after special char`` () =
formatSourceString
false
"""
'\u0000' // foo
"""
config
|> prepend newline
|> should
equal
"""
'\u0000' // foo
"""

[<Test>]
let ``line comment after embedded il`` () =
formatSourceString
false
"""
(# "" x : 'U #) // bar
"""
config
|> prepend newline
|> should
equal
"""
(# "" x : 'U #) // bar
"""
7 changes: 3 additions & 4 deletions src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,9 @@ x
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (fooComment, _, true)); Comment (BlockComment (barComment, _, true)) ] } ] ->
fooComment == "(* foo *)"
barComment == "(* bar *)"
| _ -> fail ()
| [ { ContentBefore = [ Comment (BlockComment (combinedComment, _, true)) ] } ] ->
combinedComment == "(* foo *)\n(* bar *)"
| _ -> Assert.Fail(sprintf "Unexpected trivia %A" triviaNodes)

[<Test>]
let ``block comment inside line comment parsed correctly`` () =
Expand Down
6 changes: 4 additions & 2 deletions src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ and genExpr astContext synExpr ctx =
+> sepSpace
+> genExpr astContext e

| Paren (_, ILEmbedded r, _, _) ->
| Paren (_, ILEmbedded r, rpr, _) ->
fun ctx ->
let expr =
Map.tryFindOrEmptyList SynExpr_LibraryOnlyILAssembly ctx.TriviaMainNodes
Expand All @@ -1618,7 +1618,9 @@ and genExpr astContext synExpr ctx =
|> Option.map (!-)
|> Option.defaultValue sepNone

expr ctx
(expr
+> optSingle (fun r -> leaveNodeTokenByName r RPAREN) rpr)
ctx
| Paren (lpr, e, rpr, pr) ->
match e with
| LetOrUses _ ->
Expand Down
Loading

0 comments on commit b5c6d7f

Please sign in to comment.