Skip to content

Commit

Permalink
Respect column info for multi-line comments
Browse files Browse the repository at this point in the history
Fixes fsprojects#1223

Co-authored-by: ijanus <[email protected]>
  • Loading branch information
2 people authored and knocte committed Apr 7, 2022
1 parent 51b778e commit a8fa573
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 29 deletions.
77 changes: 77 additions & 0 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,83 @@ let foo a =
// bar
"""

[<Test>]
let ``should not move the starting point of a multi-line comment, 1223`` () =
formatSourceString
false
"""
module Foo =
let private GetConfirmedEtherBalanceInternal (web3: Web3) (publicAddress: string): Async<HexBigInteger> =
async {
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(*
if (Config.DebugLog) then
Infrastructure.LogError (SPrintF2 "Last block number and last confirmed block number: %s: %s"
(latestBlock.Value.ToString()) (blockForConfirmationReference.BlockNumber.Value.ToString()))
*)
return blockForConfirmationReference
}
"""
config
|> prepend newline
|> should
equal
"""
module Foo =
let private GetConfirmedEtherBalanceInternal (web3: Web3) (publicAddress: string) : Async<HexBigInteger> =
async {
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(*
if (Config.DebugLog) then
Infrastructure.LogError (SPrintF2 "Last block number and last confirmed block number: %s: %s"
(latestBlock.Value.ToString()) (blockForConfirmationReference.BlockNumber.Value.ToString()))
*)
return blockForConfirmationReference
}
"""

[<Test>]
let ``should not move the starting point of a multi-line comment (2), 1223`` () =
formatSourceString
false
"""
module Foo =
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(* test *)
return blockForConfirmationReference
"""
config
|> prepend newline
|> should
equal
"""
module Foo =
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(* test *)
return blockForConfirmationReference
"""

[<Test>]
let ``should not move the starting point of a multi-line comment (3), 1223`` () =
formatSourceString
false
"""
module Foo =
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(* test *)
return blockForConfirmationReference
"""
config
|> prepend newline
|> should
equal
"""
module Foo =
let! blockForConfirmationReference = GetBlockToCheckForConfirmedBalance web3
(* test *)
return blockForConfirmationReference
"""

[<Test>]
let ``comment after bracket in record should not be duplicated in computation expression, 1912`` () =
formatSourceString
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ let ``comment between opening parenthesis and lambda, 1190`` () =
equal
"""
(
(* comment before gets swallowed *)
(* comment before gets swallowed *)
fun x -> x * 42)
(fun x ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/PatternMatchingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,7 +1662,7 @@ let GenApp (cenv: cenv) cgbuf eenv (f, fty, tyargs, curriedArgs, m) sequel =
arityInfo.Length = curriedArgs.Length)
&&
(* no tailcall out of exception handler, etc. *)
(* no tailcall out of exception handler, etc. *)
(match sequelIgnoringEndScopesAndDiscard sequel with
| Return
| ReturnVoid -> true
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas.Tests/TokenParserTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ let ``single line block comment should be found in tokens`` () =
let triviaNodes = getTriviaFromTokens tokens

match List.tryLast triviaNodes with
| Some { Item = Comment (BlockComment (blockComment, _, _)) } -> blockComment == "(* not fonz *)"
| Some { Item = Comment (BlockComment (blockComment, _, _, _)) } -> blockComment == "(* not fonz *)"
| _ -> failwith "expected block comment"

[<Test>]
Expand All @@ -193,7 +193,7 @@ let ``multi line block comment should be found in tokens`` () =
|> String.normalizeNewLine

match triviaNodes with
| [ { Item = Comment (BlockComment (blockComment, _, _))
| [ { Item = Comment (BlockComment (blockComment, _, _, _))
Range = range } ] ->
blockComment == expectedComment
range.StartLine == 2
Expand Down
18 changes: 9 additions & 9 deletions src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ let ``block comment added to trivia`` () =
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentAfter = [ Comment (BlockComment (comment, _, _)) ] } ] -> comment == "(* meh *)"
| [ { ContentAfter = [ Comment (BlockComment (comment, _, _, _)) ] } ] -> comment == "(* meh *)"
| _ -> failwith "Expected block comment"

[<Test>]
Expand All @@ -156,7 +156,7 @@ let a = b
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true)) ] } ] -> comment == "(* meh *)"
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true, _)) ] } ] -> comment == "(* meh *)"
| _ -> failwith "Expected block comment"

[<Test>]
Expand All @@ -168,7 +168,7 @@ let ``block comment on newline EOF added to trivia`` () =
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentAfter = [ Comment (BlockComment (comment, true, _)) ] } ] -> comment == "(* meh *)"
| [ { ContentAfter = [ Comment (BlockComment (comment, true, _, _)) ] } ] -> comment == "(* meh *)"
| _ -> failwith "Expected block comment"

[<Test>]
Expand All @@ -178,7 +178,7 @@ let ``block comment on EOF added to trivia`` () =
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentAfter = [ Comment (BlockComment (comment, _, _)) ] } ] -> comment == "(* meh *)"
| [ { ContentAfter = [ Comment (BlockComment (comment, _, _, _)) ] } ] -> comment == "(* meh *)"
| _ -> failwith "Expected block comment"

[<Test>]
Expand All @@ -191,7 +191,7 @@ let a = c + d
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true)) ] } ] -> comment == "(* (* meh *) *)"
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true, _)) ] } ] -> comment == "(* (* meh *) *)"
| _ -> failwith "Expected block comment"


Expand All @@ -205,7 +205,7 @@ let a = 9
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true)) ] } ] -> comment == "(* // meh *)"
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true, _)) ] } ] -> comment == "(* // meh *)"
| _ -> failwith "Expected block comment"


Expand All @@ -225,7 +225,7 @@ bla *)"""
|> String.normalizeNewLine

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true)) ] } ] -> comment == expectedComment
| [ { ContentBefore = [ Comment (BlockComment (comment, _, true, _)) ] } ] -> comment == expectedComment
| _ -> failwith "Expected block comment"


Expand All @@ -241,7 +241,7 @@ x
let triviaNodes = toTrivia source |> List.head

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

Expand Down Expand Up @@ -272,7 +272,7 @@ MEH
let triviaNodes = toTrivia source |> List.head

match triviaNodes with
| [ { ContentBefore = [ Comment (BlockComment (c, _, true)) ] } ] -> c == (String.normalizeNewLine comment)
| [ { ContentBefore = [ Comment (BlockComment (c, _, true, _)) ] } ] -> c == (String.normalizeNewLine comment)
| _ -> failwith "Expected block comment"

[<Test>]
Expand Down
21 changes: 16 additions & 5 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,22 @@ let internal printTriviaContent (c: TriviaContent) (ctx: Context) =
let comment = sprintf "%s%s" (if addSpace then " " else String.empty) s

writerEvent (WriteBeforeNewline comment)
| Comment (BlockComment (s, before, after)) ->
ifElse (before && addNewline) sepNlnForTrivia sepNone
+> sepSpace
-- s
+> sepSpace
| Comment (BlockComment (blockComment, before, after, commentRange)) ->
let writerModel = ctx.WriterModel
let oldIndent = writerModel.Indent
let oldColumn = writerModel.AtColumn

ifElse
(before && addNewline)
(writerEvent (SetAtColumn 0)
+> writerEvent (SetIndent commentRange.StartColumn)
+> sepNlnForTrivia
+> writerEvent (RestoreAtColumn oldColumn)
+> writerEvent (RestoreIndent oldIndent)
+> writerEvent (Write blockComment))
(sepSpace
+> writerEvent (Write blockComment)
+> sepSpace)
+> ifElse after sepNlnForTrivia sepNone
| Comment (LineCommentOnSingleLine (s, commentRange)) ->
let writerModel = ctx.WriterModel
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ let rec private getTriviaFromTokensThemSelves
getRangeBetween mkRange headToken (Option.defaultValue headToken lastToken)

let info =
Trivia.Create (Comment(BlockComment(comment, false, false))) range
Trivia.Create (Comment(BlockComment(comment, false, false, range))) range
|> List.prependItem foundTrivia

getTriviaFromTokensThemSelves mkRange lastButOne lastToken rest info
Expand Down
29 changes: 20 additions & 9 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ let private addTriviaToTriviaNode (startOfSourceCode: int) (triviaNodes: TriviaN
(fun tn -> tn.ContentAfter.Add(Comment(LineCommentOnSingleLine(comment, range))))
triviaNodes

| { Item = Comment (BlockComment (comment, _, _))
| { Item = Comment (BlockComment (comment, _, _, _))
Range = range } ->
let nodeAfter =
findNodeAfterLineAndColumn triviaNodes range.StartLine range.StartColumn
Expand All @@ -186,28 +186,39 @@ let private addTriviaToTriviaNode (startOfSourceCode: int) (triviaNodes: TriviaN
&& na.Range.StartLine > range.EndLine)
->
Some na
|> updateTriviaNode (fun tn -> tn.ContentBefore.Add(Comment(BlockComment(comment, true, true)))) triviaNodes
|> updateTriviaNode
(fun tn -> tn.ContentBefore.Add(Comment(BlockComment(comment, true, true, range))))
triviaNodes
| Some n, _ when n.Range.EndLine = range.StartLine ->
Some n
|> updateTriviaNode
(fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, false, false))))
(fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, false, false, range))))
triviaNodes
| Some nb, Some n when (nb.Range.EndLine > range.StartLine) ->
Some n
|> updateTriviaNode
(fun tn ->
let newline = tn.Range.StartLine > range.EndLine
tn.ContentBefore.Add(Comment(BlockComment(comment, true, newline, range))))
triviaNodes
| _, Some n ->
Some n
|> updateTriviaNode
(fun tn ->
let newline = tn.Range.StartLine > range.EndLine
tn.ContentBefore.Add(Comment(BlockComment(comment, false, newline))))
tn.ContentBefore.Add(Comment(BlockComment(comment, false, newline, range))))
triviaNodes
| Some _, _ when (commentIsAfterLastTriviaNode triviaNodes range) ->
findLastNode triviaNodes
|> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, false)))) triviaNodes
|> updateTriviaNode
(fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, false, range))))
triviaNodes
| Some n, _ ->
Some n
|> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, false)))) triviaNodes
| None, None ->
findNodeBeforeLineFromStart triviaNodes range.StartLine
|> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, true)))) triviaNodes
|> updateTriviaNode
(fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, false, range))))
triviaNodes
| None, None -> triviaNodes

| { Item = Comment (LineCommentAfterSourceCode _ as comment)
Range = range } ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Token =
type Comment =
| LineCommentAfterSourceCode of comment: string
| LineCommentOnSingleLine of comment: string * range
| BlockComment of string * newlineBefore: bool * newlineAfter: bool
| BlockComment of string * newlineBefore: bool * newlineAfter: bool * range

(* LineComment Examples
Expand Down

0 comments on commit a8fa573

Please sign in to comment.