diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index 035392a2a8..48a6f26acc 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -231,8 +231,8 @@ let f() = |> should equal """ -/// XML COMMENT A -// Other comment + /// XML COMMENT A + // Other comment let f () = // COMMENT A let y = 1 @@ -398,9 +398,9 @@ let ``should keep comments on else if`` () = """ if a then () else - // Comment 1 +// Comment 1 if b then () - // Comment 2 +// Comment 2 else () """ config @@ -1250,7 +1250,7 @@ let a = 8 equal """ let a = 8 -// foobar + // foobar """ [] @@ -1559,6 +1559,55 @@ Host // """ +[] +let ``should not move the starting point of a single-line comment, 1233`` () = + formatSourceString + false + """ +type CustomCancelSource() = + interface IDisposable with + member self.Dispose() = + try + self.Cancel() + with + | :? ObjectDisposedException -> + () + // TODO: cleanup also subscribed handlers? see https://stackoverflow.com/q/58912910/544947 +""" + config + |> prepend newline + |> should + equal + """ +type CustomCancelSource() = + interface IDisposable with + member self.Dispose() = + try + self.Cancel() + with + | :? ObjectDisposedException -> () + // TODO: cleanup also subscribed handlers? see https://stackoverflow.com/q/58912910/544947 +""" + +[] +let ``should not move the starting point of a single-line comment (2), 1233`` () = + formatSourceString + false + """ +let foo a = + someLongFunctionCall parameterOne parameterTwo parameterThree + // bar +""" + config + |> prepend newline + |> should + equal + """ +let foo a = + someLongFunctionCall parameterOne parameterTwo parameterThree + // bar +""" + [] let ``comment after bracket in record should not be duplicated in computation expression, 1912`` () = formatSourceString @@ -1598,4 +1647,4 @@ type TorDirectory = // comment } } -""" +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/ComputationExpressionTests.fs b/src/Fantomas.Tests/ComputationExpressionTests.fs index 3c30d0e3a6..3c0a63ac31 100644 --- a/src/Fantomas.Tests/ComputationExpressionTests.fs +++ b/src/Fantomas.Tests/ComputationExpressionTests.fs @@ -2309,7 +2309,7 @@ let ``keep new line before match bang, 1313`` () = |> should equal """ -/// a codefix that generates union cases for an incomplete match expression + /// a codefix that generates union cases for an incomplete match expression let generateUnionCases = ifDiagnosticByMessage (fun diagnostic codeActionParams -> diff --git a/src/Fantomas.Tests/ControlStructureTests.fs b/src/Fantomas.Tests/ControlStructureTests.fs index 0585090a0c..f42b69bb3a 100644 --- a/src/Fantomas.Tests/ControlStructureTests.fs +++ b/src/Fantomas.Tests/ControlStructureTests.fs @@ -724,7 +724,7 @@ let ``keep new line before for loop, 1317`` () = |> should equal """ -/// Fold over the array passing the index and element at that index to a folding function + /// Fold over the array passing the index and element at that index to a folding function let foldi (folder: 'State -> int -> 'T -> 'State) (state: 'State) (array: 'T []) = checkNonNull "array" array diff --git a/src/Fantomas.Tests/ElmishTests.fs b/src/Fantomas.Tests/ElmishTests.fs index adfa4cb62f..c74ab69699 100644 --- a/src/Fantomas.Tests/ElmishTests.fs +++ b/src/Fantomas.Tests/ElmishTests.fs @@ -1456,6 +1456,6 @@ a [] [ equal """ a [] [ -// def + // def ] """ diff --git a/src/Fantomas.Tests/ModuleTests.fs b/src/Fantomas.Tests/ModuleTests.fs index 8de8b6faa4..4c65f88778 100644 --- a/src/Fantomas.Tests/ModuleTests.fs +++ b/src/Fantomas.Tests/ModuleTests.fs @@ -39,9 +39,9 @@ let ``open modules`` () = |> should equal """ -// comment1 + // comment1 open System.IO -// comment2 + // comment2 open System """ diff --git a/src/Fantomas.Tests/OperatorTests.fs b/src/Fantomas.Tests/OperatorTests.fs index 6b74ba4d87..c5e2153aac 100644 --- a/src/Fantomas.Tests/OperatorTests.fs +++ b/src/Fantomas.Tests/OperatorTests.fs @@ -952,7 +952,7 @@ let ``add in keyword when let binding is part of single infix expression`` () = |> should equal """ -// Check for the [] attribute on an argument position + // Check for the [] attribute on an argument position let isCustomOperationProjectionParameter i (nm: Ident) = match tryGetArgInfosForCustomOperator nm with | None -> false diff --git a/src/Fantomas.Tests/TokenParserTests.fs b/src/Fantomas.Tests/TokenParserTests.fs index ace1ddf74d..e73791940c 100644 --- a/src/Fantomas.Tests/TokenParserTests.fs +++ b/src/Fantomas.Tests/TokenParserTests.fs @@ -220,7 +220,7 @@ let a = 9 // foo""" match triviaNodes with - | { Item = Comment (LineCommentOnSingleLine l1) } :: _ -> String.normalizeNewLine l1 == expectedComment + | { Item = Comment (LineCommentOnSingleLine (l1, _)) } :: _ -> String.normalizeNewLine l1 == expectedComment | _ -> failwith "Expected two line comments" [] diff --git a/src/Fantomas.Tests/TriviaTests.fs b/src/Fantomas.Tests/TriviaTests.fs index c31d46dcef..eded38aeb9 100644 --- a/src/Fantomas.Tests/TriviaTests.fs +++ b/src/Fantomas.Tests/TriviaTests.fs @@ -46,7 +46,7 @@ let a = 9 let triviaNodes = toTrivia source |> List.head match triviaNodes with - | [ { ContentBefore = [ Comment (LineCommentOnSingleLine lineComment) ] } ] -> lineComment == "// meh" + | [ { ContentBefore = [ Comment (LineCommentOnSingleLine (lineComment, _)) ] } ] -> lineComment == "// meh" | _ -> failwith "Expected line comment" [] @@ -59,7 +59,7 @@ let a = 'c' let triviaNodes = toTrivia source |> List.head match triviaNodes with - | [ { ContentBefore = [ Comment (LineCommentOnSingleLine lineComment) ] } ] -> lineComment == "// foo" + | [ { ContentBefore = [ Comment (LineCommentOnSingleLine (lineComment, _)) ] } ] -> lineComment == "// foo" | _ -> failwith "Expected line comment" [] @@ -101,7 +101,7 @@ let a = 7 // bar""" match triviaNodes with - | [ { ContentBefore = [ Comment (LineCommentOnSingleLine comments) ] } ] -> + | [ { ContentBefore = [ Comment (LineCommentOnSingleLine (comments, _)) ] } ] -> String.normalizeNewLine comments == expectedComment | _ -> fail () @@ -133,7 +133,7 @@ let ``comment after all source code`` () = match triviaNodes with | [ { Type = MainNode mn - ContentAfter = [ Comment (LineCommentOnSingleLine lineComment) ] } ] -> + ContentAfter = [ Comment (LineCommentOnSingleLine (lineComment, _)) ] } ] -> mn == SynModuleDecl_Types lineComment @@ -261,7 +261,7 @@ let a = b + c let triviaNodes = toTrivia source |> List.head match triviaNodes with - | [ { ContentBefore = [ Comment (LineCommentOnSingleLine comment) ] } ] -> comment == "// (* meh *)" + | [ { ContentBefore = [ Comment (LineCommentOnSingleLine (comment, _)) ] } ] -> comment == "// (* meh *)" | _ -> failwith "Expected line comment" [] @@ -506,7 +506,7 @@ type LongIdentWithDots = /// LongIdent can be empty list - it is used to denote that name of some AST element is absent (i.e. empty type name in inherit)""" match trivia with - | [ { ContentBefore = [ Comment (LineCommentOnSingleLine comment) ] } ] -> + | [ { ContentBefore = [ Comment (LineCommentOnSingleLine (comment, _)) ] } ] -> String.normalizeNewLine comment == expectedComment | _ -> fail () diff --git a/src/Fantomas.Tests/TupleTests.fs b/src/Fantomas.Tests/TupleTests.fs index 12b807d5e7..ab683413a0 100644 --- a/src/Fantomas.Tests/TupleTests.fs +++ b/src/Fantomas.Tests/TupleTests.fs @@ -323,6 +323,6 @@ func ( 0, // def // ghi 1 // jkl -// mno + // mno ) """ diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index d32e6295e5..83cc4f952a 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -1176,6 +1176,11 @@ let internal printTriviaContent (c: TriviaContent) (ctx: Context) = -- s +> sepSpace +> ifElse after sepNlnForTrivia sepNone + | Comment (LineCommentOnSingleLine (s, commentRange)) -> + (ifElse addNewline sepNlnForTrivia sepNone) + +> ifElse (ctx.WriterModel.Indent = 0) (rep commentRange.StartColumn !- " ") sepNone + +> !-s + +> sepNlnForTrivia | Newline -> (ifElse addNewline (sepNlnForTrivia +> sepNlnForTrivia) sepNlnForTrivia) | Keyword _ | Number _ @@ -1185,8 +1190,7 @@ let internal printTriviaContent (c: TriviaContent) (ctx: Context) = | CharContent _ | EmbeddedIL _ | KeywordString _ -> sepNone // don't print here but somewhere in CodePrinter - | Directive s - | Comment (LineCommentOnSingleLine s) -> + | Directive s -> (ifElse addNewline sepNlnForTrivia sepNone) +> !-s +> sepNlnForTrivia diff --git a/src/Fantomas/TokenParser.fs b/src/Fantomas/TokenParser.fs index 660c42688e..f861b16161 100644 --- a/src/Fantomas/TokenParser.fs +++ b/src/Fantomas/TokenParser.fs @@ -885,16 +885,16 @@ let rec private getTriviaFromTokensThemSelves commentTokens |> List.skip afterSourceTokens.Length - let triviaContent = - collectComment commentTokens - |> LineCommentOnSingleLine - |> Comment - let range = let headToken = List.head commentTokens let lastToken = List.tryLast commentTokens getRangeBetween mkRange headToken (Option.defaultValue headToken lastToken) + let triviaContent = + (collectComment commentTokens, range) + |> LineCommentOnSingleLine + |> Comment + Trivia.Create triviaContent range |> Some else None @@ -906,16 +906,16 @@ let rec private getTriviaFromTokensThemSelves // We should not hit this branch foundTrivia else - let triviaContent = - collectComment commentTokens - |> LineCommentOnSingleLine - |> Comment - let range = let headToken = List.head commentTokens let lastToken = List.tryLast commentTokens getRangeBetween mkRange headToken (Option.defaultValue headToken lastToken) + let triviaContent = + (collectComment commentTokens, range) + |> LineCommentOnSingleLine + |> Comment + (Trivia.Create triviaContent range :: foundTrivia) getTriviaFromTokensThemSelves mkRange lastButOneNonWhiteSpaceToken lastNonWhiteSpaceToken rest info diff --git a/src/Fantomas/Trivia.fs b/src/Fantomas/Trivia.fs index f99c36133c..73a7cf7c71 100644 --- a/src/Fantomas/Trivia.fs +++ b/src/Fantomas/Trivia.fs @@ -272,10 +272,14 @@ let private addTriviaToTriviaNode trivia = match trivia with - | { Item = Comment (LineCommentOnSingleLine _ as comment) + | { Item = Comment (LineCommentOnSingleLine (comment, _)) Range = range } -> match triviaBetweenAttributeAndParentBinding triviaNodes range.StartLine with - | Some _ as node -> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Comment(comment))) triviaNodes node + | Some _ as node -> + updateTriviaNode + (fun tn -> tn.ContentAfter.Add(Comment(LineCommentOnSingleLine(comment, range)))) + triviaNodes + node | None -> let nodeAfterLine = findFirstNodeAfterLine triviaNodes range.StartLine @@ -283,11 +287,15 @@ let private addTriviaToTriviaNode match nodeAfterLine with | Some _ -> nodeAfterLine - |> updateTriviaNode (fun tn -> tn.ContentBefore.Add(Comment(comment))) triviaNodes + |> updateTriviaNode + (fun tn -> tn.ContentBefore.Add(Comment(LineCommentOnSingleLine(comment, range)))) + triviaNodes | None -> // try and find a node above findNodeBeforeLineFromStart triviaNodes range.StartLine - |> updateTriviaNode (fun tn -> tn.ContentAfter.Add(Comment(comment))) triviaNodes + |> updateTriviaNode + (fun tn -> tn.ContentAfter.Add(Comment(LineCommentOnSingleLine(comment, range)))) + triviaNodes | { Item = Comment (BlockComment (comment, _, _)) Range = range } -> diff --git a/src/Fantomas/TriviaTypes.fs b/src/Fantomas/TriviaTypes.fs index 6040323322..f3793f1863 100644 --- a/src/Fantomas/TriviaTypes.fs +++ b/src/Fantomas/TriviaTypes.fs @@ -61,7 +61,7 @@ type Token = type Comment = | LineCommentAfterSourceCode of comment: string - | LineCommentOnSingleLine of comment: string + | LineCommentOnSingleLine of comment: string * range | BlockComment of string * newlineBefore: bool * newlineAfter: bool (* LineComment Examples