From dcd5ff81d492b5c1b5292502b732b7e1c1550491 Mon Sep 17 00:00:00 2001 From: Retheesh Erathu Date: Tue, 19 Oct 2021 06:32:52 +0200 Subject: [PATCH] Respect column info for multi-line comments Fixes https://github.com/fsprojects/fantomas/issues/1223 Co-authored-by: ijanus --- src/Fantomas.Tests/CommentTests.fs | 77 ++++++++++++++++++++++ src/Fantomas.Tests/LambdaTests.fs | 2 +- src/Fantomas.Tests/PatternMatchingTests.fs | 2 +- src/Fantomas.Tests/TokenParserTests.fs | 4 +- src/Fantomas.Tests/TriviaTests.fs | 18 ++--- src/Fantomas/Context.fs | 21 ++++-- src/Fantomas/TokenParser.fs | 2 +- src/Fantomas/Trivia.fs | 29 +++++--- src/Fantomas/TriviaTypes.fs | 2 +- 9 files changed, 128 insertions(+), 29 deletions(-) diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index d90e92363b..361621d142 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -1707,6 +1707,83 @@ let foo a = // bar """ +[] +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 = + 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 = + 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 + } +""" + +[] +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 +""" + +[] +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 +""" + [] let ``comment after bracket in record should not be duplicated in computation expression, 1912`` () = formatSourceString diff --git a/src/Fantomas.Tests/LambdaTests.fs b/src/Fantomas.Tests/LambdaTests.fs index 5f84eeac89..4e6c79f8e9 100644 --- a/src/Fantomas.Tests/LambdaTests.fs +++ b/src/Fantomas.Tests/LambdaTests.fs @@ -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 -> diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 00e098b26a..de1bd7e401 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -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 diff --git a/src/Fantomas.Tests/TokenParserTests.fs b/src/Fantomas.Tests/TokenParserTests.fs index fba3ae107d..278a93d80e 100644 --- a/src/Fantomas.Tests/TokenParserTests.fs +++ b/src/Fantomas.Tests/TokenParserTests.fs @@ -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" [] @@ -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 diff --git a/src/Fantomas.Tests/TriviaTests.fs b/src/Fantomas.Tests/TriviaTests.fs index dc4a29f756..a25e45ba2f 100644 --- a/src/Fantomas.Tests/TriviaTests.fs +++ b/src/Fantomas.Tests/TriviaTests.fs @@ -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 *)" + | [ { ContentBefore = [ Comment (BlockComment (comment, _, _, _)) ] } ] -> comment == "(* meh *)" | _ -> failwith "Expected block comment" [] @@ -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" [] @@ -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" [] @@ -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" [] @@ -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" @@ -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" @@ -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" @@ -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) @@ -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" [] diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index 932a264253..49c8e6c1fb 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -1172,11 +1172,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 diff --git a/src/Fantomas/TokenParser.fs b/src/Fantomas/TokenParser.fs index dcbb240bc5..7b04e24e44 100644 --- a/src/Fantomas/TokenParser.fs +++ b/src/Fantomas/TokenParser.fs @@ -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 diff --git a/src/Fantomas/Trivia.fs b/src/Fantomas/Trivia.fs index 4b07adefaf..aebb545b66 100644 --- a/src/Fantomas/Trivia.fs +++ b/src/Fantomas/Trivia.fs @@ -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 @@ -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 } -> diff --git a/src/Fantomas/TriviaTypes.fs b/src/Fantomas/TriviaTypes.fs index 1b721dbf86..156f98aa1c 100644 --- a/src/Fantomas/TriviaTypes.fs +++ b/src/Fantomas/TriviaTypes.fs @@ -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