Skip to content

Commit

Permalink
Respect column info for single line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
su8898 authored and knocte committed Nov 11, 2021
1 parent d94dfe3 commit 9a2b831
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 37 deletions.
61 changes: 55 additions & 6 deletions src/Fantomas.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1250,7 +1250,7 @@ let a = 8
equal
"""
let a = 8
// foobar
// foobar
"""

[<Test>]
Expand Down Expand Up @@ -1559,6 +1559,55 @@ Host
//
"""

[<Test>]
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
"""

[<Test>]
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
"""

[<Test>]
let ``comment after bracket in record should not be duplicated in computation expression, 1912`` () =
formatSourceString
Expand Down Expand Up @@ -1598,4 +1647,4 @@ type TorDirectory =
// comment
}
}
"""
"""
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/ComputationExpressionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/ElmishTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,6 @@ a [] [
equal
"""
a [] [
// def
// def
]
"""
4 changes: 2 additions & 2 deletions src/Fantomas.Tests/ModuleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ let ``open modules`` () =
|> should
equal
"""
// comment1
// comment1
open System.IO
// comment2
// comment2
open System
"""

Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ let ``add in keyword when let binding is part of single infix expression`` () =
|> should
equal
"""
// Check for the [<ProjectionParameter>] attribute on an argument position
// Check for the [<ProjectionParameter>] attribute on an argument position
let isCustomOperationProjectionParameter i (nm: Ident) =
match tryGetArgInfosForCustomOperator nm with
| None -> false
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/TokenParserTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[<Test>]
Expand Down
12 changes: 6 additions & 6 deletions src/Fantomas.Tests/TriviaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

[<Test>]
Expand All @@ -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"

[<Test>]
Expand Down Expand Up @@ -101,7 +101,7 @@ let a = 7
// bar"""

match triviaNodes with
| [ { ContentBefore = [ Comment (LineCommentOnSingleLine comments) ] } ] ->
| [ { ContentBefore = [ Comment (LineCommentOnSingleLine (comments, _)) ] } ] ->
String.normalizeNewLine comments
== expectedComment
| _ -> fail ()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"

[<Test>]
Expand Down Expand Up @@ -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 ()

Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/TupleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,6 @@ func (
0, // def
// ghi
1 // jkl
// mno
// mno
)
"""
8 changes: 6 additions & 2 deletions src/Fantomas/Context.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/Fantomas/Trivia.fs
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,30 @@ 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

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 } ->
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/TriviaTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9a2b831

Please sign in to comment.