Skip to content

Commit

Permalink
Respect column info for multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
su8898 authored and knocte committed Oct 22, 2021
1 parent 33a7687 commit de5f840
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 27 deletions.
79 changes: 78 additions & 1 deletion src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1647,4 +1647,81 @@ type TorDirectory =
// comment
}
}
"""
"""

[<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
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,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 @@ -1669,7 +1669,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 @@ -175,7 +175,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 @@ -197,7 +197,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 @@ -149,7 +149,7 @@ let ``block comment added to trivia`` () =
let triviaNodes = toTrivia source |> List.head

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

[<Test>]
Expand All @@ -162,7 +162,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 @@ -174,7 +174,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 @@ -184,7 +184,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 @@ -197,7 +197,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 @@ -211,7 +211,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 @@ -231,7 +231,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 @@ -247,7 +247,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 @@ -278,7 +278,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
41 changes: 36 additions & 5 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,11 +1170,42 @@ let internal printTriviaContent (c: TriviaContent) (ctx: Context) =
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 printAtZeroIndent (f: Context -> Context) ctx =
let writerModel = ctx.WriterModel
let oldIndent = writerModel.Indent
let oldColumn = writerModel.AtColumn

(writerEvent (SetAtColumn 0)
>> writerEvent (SetIndent 0)
>> f
>> writerEvent (RestoreAtColumn oldColumn)
>> writerEvent (RestoreIndent oldIndent))
ctx

let printComment =
if before && addNewline then
(String.replicate (commentRange.StartColumn) " ")
else
String.empty

let printNewLn =
if before && addNewline then
"\n"
else
String.empty

let comment =
sprintf "%s%s%s" printNewLn printComment blockComment

ifElse
(before && addNewline)
(printAtZeroIndent (writerEvent (Write comment)))
(printAtZeroIndent (
sepSpace
+> writerEvent (Write comment)
+> sepSpace
))
+> ifElse after sepNlnForTrivia sepNone
| Comment (LineCommentOnSingleLine (s, commentRange)) ->
(ifElse addNewline sepNlnForTrivia sepNone)
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,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
25 changes: 19 additions & 6 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ let private addTriviaToTriviaNode
(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 @@ -311,25 +311,38 @@ let private addTriviaToTriviaNode
&& 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
|> updateTriviaNode
(fun tn -> tn.ContentAfter.Add(Comment(BlockComment(comment, true, false, range))))
triviaNodes
| None, None -> triviaNodes

| { Item = Comment (LineCommentAfterSourceCode _ as comment)
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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 de5f840

Please sign in to comment.